Content deleted Content added
addToolbarButtons |
don't try to add to classic toolbar unless the newer one in not used and mw.toolbar exists |
||
(9 intermediate revisions by 2 users not shown) | |||
Line 1:
/*
* 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).▼
▲ * (The visual editor is not supported).
*
* You can use this code under the license CC0
▲ * Home and documentation: //en.wikipedia.org/User:V111P/js/addToolbarButtons
*/
// 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";
Line 20:
return; // not source-editing a page
if (!props ||
var arr = props || window.toolbarButtonsToAdd || [];
$.each(arr, function (i, val) {
if (typeof val == 'object' && !val[0])
});
Line 42:
$.extend(button, props || {});
if (window.console && console.error)
console.error('addToolbarButtons.js:
}▼
error('No button id specified.');
return;
}
error('An element with id ' + button.id + ' already exists on the page.');
return;
}
Line 56 ⟶ 65:
button.inserts = (button.before + button.between + button.after).length > 0;
if (!button.callback && !button.inserts) {
▲ console.error('addToolbarButtons.js: No callback function or characters to insert specified.');
return;
}
▲ $('#' + button.id).remove();
// add button to the new, WikiEditor, toolbar
Line 72 ⟶ 78:
action: {
type: (button.inserts ? 'encapsulate' : 'callback'),
execute: (button.inserts ? void(0) : button.callback),
options: (button.inserts ? {
pre: button.before,
Line 92 ⟶ 98:
mw.loader.using( 'user.options', function () {
if ( mw.user.options.get('usebetatoolbar') ) {
mw.loader.using( 'ext.wikiEditor
$( customizeBetaToolbar );
} );
}
else if (mw.toolbar && mw.toolbar.addButton) {
// add a button to the classic toolbar▼
▲ if ($('#' + button.id).size() == 0) {
▲ // add a button to the classic toolbar
var tempButtonId = button.id + (!button.inserts ? 'TempButton' : '');
mw.toolbar.addButton(
Line 116 ⟶ 119:
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;
Line 122 ⟶ 127:
}
}
}
}); ▲ }
};
try { // $() doesn't work after errors from other scripts, so try directly first:
}
catch (e) { // error - page still loading
$(
}
|