User:Alex brollo/Bottoni.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
/* function show2 (lang) {
  if (lang==1) {
    if (wgCanonicalNamespace == "File") {getPage(wgPageName.replace("File:","Indice:"),"it.wikisource.org");}
    if (wgCanonicalNamespace == "Creator") {getPage(wgPageName.replace("Creator:",""),"it.wikipedia.org");}
    }
if (lang==2) {
    if (wgCanonicalNamespace == "File") {getPage(wgPageName.replace("File:","Index:"),"en.wikisource.org");}
    if (wgCanonicalNamespace == "Creator") {getPage(wgPageName.replace("Creator:",""),"en.wikipedia.org");}
    }
} */

function newButton(nome, funzione) {
  if (wgAction == "history") {
    return;
  }
  if (find_stringa(funzione, "(", ")", 1) == "") {
    funzione += "()";
  }

  if (nome.substring(0, 2) == "//") {
    var html = '<img src="' + nome + '" onclick="' + funzione + '" />';
    $("#newtattoo").append($(html));
  } else {

    $("#newtattoo").append($('<button type="button" onclick="' + funzione + '"><small>' + nome + '</small></button>'));
  }
}

newButton("codice wiki della pagina", "show()");
newButton("visualizzazione Book", "show(1)");
newButton("metadati it:w", "show2(1)");
newButton("metadati en:w", "show2(2)");
if (wgAction == "edit" || wgAction == "submit") newButton("creator", "creatorMake()");
if (wgAction == "edit" || wgAction == "submit") newButton("Tabella attiva", "loadActiveTab()");


/**
 * @description
 * Create a button with a given label or image or both
 *
 * @param {string} label The label the new button should carry
 * @param {string} image A string (URI) to image file or jQuery object of an image to add
 * @param {title} title The title attribute (tooltip) for the button
 * @param {Function} callback The callback function that should be invoked when the button is pressed. NOT a string but a "pointer" to a function
 *
 * @example
 // Create the button
 var $myButton = $createButton("My Button", "//example.myurl.it/myurl.png", "Starts the self destruction", function(e) {
    alert("This is inside an anonymous function (a function that does not have a name it could referenced to). Someone clicked the button!");
 });
 // Append the button to the DOM
 $myButton.prependTo("#content");

 * @example
 // Create a reference to the content-element for faster execution
 var $content = $("#content");
 // Creates two buttons and append/prepend it in an element
 var _aFunction = function(e) {
    alert("This is called from within a named function. You can use named functions to handle multiple events.");
 };
 $createButton("A Button", "", "Does this stuff ...", _aFunction).prependTo($content);
 $createButton("A second Button", "", "Does the same stuff ... but looks different", _aFunction).appendTo($content);
 *
 * 
 **/

function $createButton(label, image, title, callback) {
  // Create a button element, set its attributes, bind the event-handlers (on)
  // and assign the result to the variable $btn
  var $btn = $('<button>').attr({
    type: 'button',
    role: 'button',
    title: title
  }).on({
    click: callback
  }).text(label);
  if (image) {
    // If the image is a jQuery object, append it directly, otherwise
    // create an image object
    if ('string' === typeof image) {
      image = $('<img>').attr({
        src: image
      });
    }
    $btn.append(image);
  }
  return $btn;
}