Utente:L0ll0/personalButtons.js

Da Wikisource.

Nota: dopo aver pubblicato, potrebbe essere necessario pulire la cache del proprio browser per vedere i cambiamenti.

  • Firefox / Safari: tieni premuto il tasto delle maiuscole Shift e fai clic su Ricarica, oppure premi Ctrl-F5 o Ctrl-R (⌘-R su Mac)
  • Google Chrome: premi Ctrl-Shift-R (⌘-Shift-R su un Mac)
  • Internet Explorer / Edge: tieni premuto il tasto Ctrl e fai clic su Aggiorna, oppure premi Ctrl-F5
  • Opera: premi Ctrl-F5.
/**
 * Modulo: Pulsantiera
 *
 * Tentativo di generalizzaione per moduli del superscript BAT di Alex brollo
 * @link https://wikisource.org/wiki/User:Alex_brollo/common.js
 */
var userApp = window.userApp || {};

(function (app) {
	"use strict";
	/**
	 * Creazione della pulsantiera. Dopo l'esecuzione esiste #newtattoo.
	 */
	var creaPulsantiera = function () {
		var $Content;
		if (mw.user.options.get('skin') === 'modern') {
			$Content = $('#mw_content');
		} else {
			$Content = $('#content');
		}

		// Se esiste già sono a posto.
		if ($("#newtattoo").length > 0) {
			return;
		}

		var $Pulsantiera = $('<div>')
			.attr("id", "newtattoo")
			.attr("align", "right")
			// Inline CSS style
			// TODO: Una volta generalizzato sarà bene generalizzare anche il CSS
			.css("position", "fixed")
			.css("bottom", 0)
			.css("right", 0)
			.css("background-color", "white")
			.css("border", "1px solid")
			.css("border-color", "#F0F0F0")
			.css("z-index", 1500);

		$Content.append($Pulsantiera);
	};

	/**
	 * Recupera i setting dalla pagine Utente:Nome_utente/PersonalButtons
	 *
	 * @return array[]
	 */
	var leggiPersonalButtons = function () {
		var dfd = new $.Deferred();
		var sUrl = [
			mw.config.get("wgServer"),
			"/w/index.php?action=raw&title=User:",
			mw.config.get("wgUserName"),
			"/PersonalButtons"
		].join("");

		$.ajax({
			url: sUrl
		})
			.done(function (sResponse) {
				var aResults = [];
				var aElements = $.parseHTML($.trim(sResponse));

				// If there are HTMl elements...
				if (aElements) {
					var elPre;
					// ... search for <pre>
					$.each(aElements, function (nIdx, elElement) {
						if (elElement.tagName === "PRE") {
							elPre = elElement;
							return false;
						}
					});

					// If there is <pre>...
					if (elPre) {
						// ... split the rows
						var aText = $(elPre).text().split("\n");

						// Populating results
						$.each(aText, function (nIdx, sRow) {
							// Excluding empty rows and JS-like comments
							if (sRow && sRow.slice(0, 2) !== "//") {
								aResults.push(sRow.split(","));
							}
						});
					}
				}

				dfd.resolve(aResults);
			})
			.fail(function () {
				dfd.reject();
			});

		return dfd;
	};

	// primo passo per nuovo newButton
	/**
	 * Aggiunge pulsanti alla pulsantiera partendo da un array di
	 * configurazioni.
	 *
	 * @param {Array[]} aButtons
	 */
	app.appendButtons = function (aButtons) {
		$.each(aButtons, function (nIdx, aButton) {
			var sName = aButton[0];
			// var sAction = aButton[1]; TODO: Da implementare
			var sDescription = aButton[2];
			var sFunction = $.trim(aButton[3]);
			var sPre = aButton[4];
			var sPost = aButton[5];

			var $Button = $("<button>")
				.attr("class", "baseButton")
				.attr("type", "button")
				.attr("id", "button-" + nIdx)
				.attr("title", sDescription)
				.css("display", "inline")
				.append($("<small>")
					.text(sName));

			if (sFunction !== "incapsula") {
				$Button.on("click", function () {
					// Accetto solo funzioni dentro all'oggetto app
					app[sFunction]();
				});
			} else {
				$Button.on("click", function () {
					app.incapsula(sPre.replace(/\\n/g, '\n'), sPost.replace(/\\n/g, '\n'));
				});
			}

			// Controllo che sia caricato il modulo "shortcut"
			if (app.shortcut) {
				var sShortcut = null;
				// Cerca se c'è una shortcut dentro alla descrizione
				if (sDescription.match(/\*([^*]+)\*/)) {
					sShortcut = sDescription.match(/\*([^*]+)\*/)[1];
				}

				if (sShortcut) {
					app.shortcut.add(sShortcut, function () {
						$("#button-" + nIdx).click();
					});
				}
			}

			$("#newtattoo").append($Button);
		});
	};

	// Creazione della pulsantiera
	creaPulsantiera();

	// Popolamento della pulsantiera
	app.personalButtons = [];
	leggiPersonalButtons()
		.done(function (aButtons) {
			app.personalButtons = aButtons;
			app.appendButtons(app.personalButtons);
		});
}(userApp));