MediaWiki:Gadget-UploadToolbar.js

/* _____________________________________________________________________________ * |                                                                             | * |                    === WARNING: GLOBAL GADGET FILE ===                      | * |                 Changes to this page affect many users. | * |          Please discuss changes on the talk page before editing. | * |_____________________________________________________________________________| * * Gadget definition for file upload wikitext editing toolbar */ (function (mw, $) { if(mw.config.get('wgCanonicalSpecialPageName') !== 'Upload')    return;  var toolbar, isReady, $toolbar, queue, slice, $currentFocused;  var toolarray = [    {      imageId: 'mw-editbutton-bold',      tagOpen: "",      tagClose: "",      sampleText: 'Bold text',      speedTip: 'Bold text',    },    {      imageId: 'mw-editbutton-italic',      tagOpen: "",      tagClose: "",      sampleText: 'Italic text',      speedTip: 'Italic text',    },    {      imageId: 'mw-editbutton-link',      tagOpen: ,      tagClose: ,      sampleText: 'Link title',      speedTip: 'Internal link',    },    {      imageId: 'mw-editbutton-extlink',      tagOpen: '[',      tagClose: ']',      sampleText: 'http://www.example.com link title',      speedTip: 'External link',    },    {      imageId: 'mw-editbutton-headline',      tagOpen: "\n== ",      tagClose: " ==\n",      sampleText: 'Headline text', speedTip: 'Level 2 headline', },   {      imageId: 'mw-editbutton-nowiki', tagOpen: " ", tagClose: " ", sampleText: 'Insert non-formatted text here', speedTip: 'Ignore wiki formatting', } ];

function insertButton(button, speedTip, tagOpen, tagClose, sampleText, imageId) { var $button;

// Backwards compatibility if(typeof button !== 'object') { button = { imageFile: button, speedTip: speedTip, tagOpen: tagOpen, tagClose: tagClose, sampleText: sampleText, imageId: imageId };   }

if(button.imageFile) { $button = $(' ').attr({       src: button.imageFile,        alt: button.speedTip,        title: button.speedTip,        id: button.imageId || undefined,        'class': 'mw-toolbar-editbutton'      }); } else { $button = $(' ').attr({       title: button.speedTip,        id: button.imageId || undefined,        'class': 'mw-toolbar-editbutton'      }); }

$button.click(function (e) {     if(button.onClick !== undefined) {        button.onClick(e);      } else {        toolbar.insertTags(button.tagOpen, button.tagClose, button.sampleText);      }

return false; });

$toolbar.append($button); }

isReady = false; $toolbar = false;

queue = []; slice = queue.slice;

toolbar = { addButton: function { if(isReady) { insertButton.apply(toolbar, arguments); } else { // Convert arguments list to array queue.push(slice.call(arguments)); }   },

addButtons: function (buttons) { if(!$.isArray(buttons)) { buttons = slice.call(arguments); }     if(isReady) { $.each(buttons, function {          insertButton(this);        }); } else { // Push each button into the queue queue.push.apply(queue, buttons); }   },

insertTags: function (tagOpen, tagClose, sampleText) { if($currentFocused && $currentFocused.length) { $currentFocused.textSelection(         'encapsulateSelection', {            pre: tagOpen,            peri: sampleText,            post: tagClose          }        ); }   }  };  toolbar.addButtons(toolarray);

// Expose API publicly mw.libs.uploadToolbar = toolbar;

$(function {    var i, button;

// Used to determine where to insert tags $currentFocused = $('#wpUploadDescription');

// Populate the selector cache for $toolbar if($('#toolbar').length === 0) { $('#mw-htmlform-description tbody tr').first.after('   '); }   $toolbar = $('#toolbar'); for(i = 0; i < queue.length; i++) { button = queue[i]; if($.isArray(button)) { // Forwarded arguments array from mw.toolbar.addButton insertButton.apply(toolbar, button); } else { // Raw object from mw.toolbar.addButtons insertButton(button); }   }

// Clear queue queue.length = 0;

// This causes further calls to addButton to go to insertion directly instead of to the queue. // It is important that this is after the one and only loop through the queue isReady = true;

// Apply to dynamically created textboxes as well as normal ones $(document).on('focus', 'textarea, input:text', function {      $currentFocused = $(this);    }); }); })(mediaWiki, jQuery);