User:V111P/js/addToolbarButtons.js: Difference between revisions

Content deleted Content added
No edit summary
don't try to add to classic toolbar unless the newer one in not used and mw.toolbar exists
 
(6 intermediate revisions by 2 users not shown)
Line 1:
/*
* addToolbarButtons.js
* version 20132019-1005-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),
* regardless of whether the user is using the old toolbar,
* or the newer, 2010 wikitext editor. (The visual editor is not supported).
* or the "enhanced editing" toolbar.
* (The visual editor is not supported).
*
* Home: //en.wikipedia.org/wiki/User:V111P/js/addToolbarButtons
* You can use this code under the license CC0
*/
Line 15 ⟶ 14:
// 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 26 ⟶ 25:
$.each(arr, function (i, val) {
if (typeof val == 'object' && !val[0])
windowmediaWiki.libs.addToolbarButtons(val);
});
 
Line 43 ⟶ 42:
$.extend(button, props || {});
 
iffunction error(!button.idmsg) {
if (window.console && console.error)
console.error('addToolbarButtons.js: No' button+ id specified.'msg);
}
 
.attr if ('id', !button.id) {
error('No button id specified.');
return;
}
 
var oldButton =if ($('#' + button.id);[0]) {
error('An element with id ' + button.id + ' already exists on the page.');
return;
}
Line 57 ⟶ 65:
button.inserts = (button.before + button.between + button.after).length > 0;
if (!button.callback && !button.inserts) {
console.error('addToolbarButtons.js:Neither Noa callback function ornor characters to insert specified.');
if (window.console && console.error)
console.error('addToolbarButtons.js: No callback function or characters to insert specified.');
return;
}
 
var oldButton = $('#' + button.id);
if (oldButton[0]) {
oldButton.attr('id', button.id + '_ButtonToBeRemoved')
.replaceWith(
$(oldButton[0].cloneNode()) // clone to remove event handlers
.attr('id', button.id)
.click(button.callback)
);
return;
}
Line 102 ⟶ 98:
mw.loader.using( 'user.options', function () {
if ( mw.user.options.get('usebetatoolbar') ) {
mw.loader.using( 'ext.wikiEditor.toolbar', function () {
$( customizeBetaToolbar );
} );
}
else if (mw.toolbar && mw.toolbar.addButton) {
} );
// add a button to the classic toolbar
 
if var ($('#'tempButtonId += button.id).size + ()!button.inserts ==? 0)'TempButton' {: '');
// add a button to the classic toolbar
mw.loader.using('mediawiki.action.edit', function () {
oldButton.remove();
var tempButtonId = button.id; //+ (!button.inserts ? 'TempButton' : '');
mw.toolbar.addButton(
(button.iconUrlClassic || button.iconUrl),
Line 120 ⟶ 112:
tempButtonId
);
if (button.callbackinserts) {
button.callback && $('#' + button.id).click(button.callback);
}
}); else {
var $tempButton = $('#' + tempButtonId);
}
if ($tempButton[0]) {
$(oldButton[0].cloneNode()) // 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);
}
); }
}
} );
 
};
 
windowmediaWiki.libs.addToolbarButtons.version = 1000;
 
try { // $() doesn't work after errors from other scripts, so try directly first:
try {
windowmediaWiki.libs.addToolbarButtons();
}
catch (e) { // error - page still loading
$(windowmediaWiki.libs.addToolbarButtons); // doesn't work after errors from other scripts
}