User:V111P/js/addToolbarButtons.js

/*

* addToolbarButtons.js

* version 2019-05-09

*

* This function lets you add function-calling buttons

* to the toolbar above the textarea, regardless of whether the user is using

* the legacy/classic (2006) editing toolbar (now only available as a gadget),

* or the newer, 2010 wikitext editor. (The visual editor is not supported).

*

* Home: //en.wikipedia.org/wiki/User:V111P/js/addToolbarButtons

* You can use this code under the license CC0

*/

// add a single button or several buttons from the supplied array

// or else add all buttons specified in the array window.toolbarButtonsToAdd

mediaWiki.libs.addToolbarButtons = window.addToolbarButtons = function (props) {

"use strict";

if ($.inArray(mw.config.get( 'wgAction' ), ['edit', 'submit']) == -1)

return; // not source-editing a page

if (!props || props[0]) {

var arr = props || window.toolbarButtonsToAdd || [];

$.each(arr, function (i, val) {

if (typeof val == 'object' && !val[0])

mediaWiki.libs.addToolbarButtons(val);

});

arr.length = 0;

return;

}

var button = {

id: '',

tooltip: '',

section: 'main',

group: 'insert',

iconUrl: '//upload.wikimedia.org/wikipedia/commons/'

+ 'thumb/1/1a/White_pog.svg/22px-White_pog.svg.png'

}

$.extend(button, props || {});

function error(msg) {

if (window.console && console.error)

console.error('addToolbarButtons.js: ' + msg);

}

if (!button.id) {

error('No button id specified.');

return;

}

if ($('#' + button.id)[0]) {

error('An element with id ' + button.id + ' already exists on the page.');

return;

}

if (!props.iconUrl && !props.iconUrlClassic)

button.iconUrlClassic = '//upload.wikimedia.org/wikipedia/commons/e/ec/Button_base.png';

button.before = (typeof button.before == 'string' ? button.before : '');

button.between = (typeof button.between == 'string' ? button.between : '');

button.after = (typeof button.after == 'string' ? button.after : '');

button.inserts = (button.before + button.between + button.after).length > 0;

if (!button.callback && !button.inserts) {

error('Neither a callback function nor characters to insert specified.');

return;

}

// add button to the new, WikiEditor, toolbar

function customizeBetaToolbar() {

var tools = {};

tools[button.id] = {

label: button.tooltip,

type: 'button',

icon: button.iconUrl,

action: {

type: (button.inserts ? 'encapsulate' : 'callback'),

execute: (button.inserts ? void(0) : button.callback),

options: (button.inserts ? {

pre: button.before,

peri: button.between,

post: button.after

} : void(0))

}

};

$('#wpTextbox1').wikiEditor('addToToolbar', {

'section': button.section,

'group': button.group,

'tools': tools

});

var btn = $('.tool-button[rel="' + button.id + '"]').attr('id', button.id);

if (button.inserts && button.callback)

btn.click(button.callback);

}

mw.loader.using( 'user.options', function () {

if ( mw.user.options.get('usebetatoolbar') ) {

mw.loader.using( 'ext.wikiEditor', function () {

$( customizeBetaToolbar );

} );

}

else if (mw.toolbar && mw.toolbar.addButton) {

// add a button to the classic toolbar

var tempButtonId = button.id + (!button.inserts ? 'TempButton' : '');

mw.toolbar.addButton(

(button.iconUrlClassic || button.iconUrl),

button.tooltip,

button.before, button.after, button.between,

tempButtonId,

tempButtonId

);

if (button.inserts) {

button.callback && $('#' + button.id).click(button.callback);

}

else {

var $tempButton = $('#' + tempButtonId);

if ($tempButton[0]) {

// clone the button to remove added event handlers

// if not done the selection in the textarea is collapsed

// before the callback function is called

var newB = $tempButton[0].cloneNode();

newB.id = button.id;

$tempButton.after(newB).remove();

$(newB).click(button.callback);

}

}

}

});

};

mediaWiki.libs.addToolbarButtons.version = 1000;

try { // $() doesn't work after errors from other scripts, so try directly first:

mediaWiki.libs.addToolbarButtons();

}

catch (e) { // error - page still loading

$(mediaWiki.libs.addToolbarButtons);

}