Difference between revisions of "MediaWiki:Common.js"

From YIPPIE
Jump to navigation Jump to search
(add toolbar buttons what the heck)
 
(try this one instead)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
+
mw.hook( 'wikipage.editform' ).add( function () {
/*
+
    mw.loader.using( 'mediawiki.toolbar' ).then( function () {
* addToolbarButtons.js
+
        mw.toolbar.addButton( {
* version 2019-05-09
+
            imageFile: '//upload.wikimedia.org/wikipedia/en/3/34/Button_hide_comment.png',
*
+
            speedTip: 'Comment visible only for editors',
* This function lets you add function-calling buttons
+
            tagOpen: '<!-- ',
* to the toolbar above the textarea, regardless of whether the user is using
+
            tagClose: ' -->',
* the legacy/classic (2006) editing toolbar (now only available as a gadget),
+
            sampleText: 'Insert comment here',
* or the newer, 2010 wikitext editor. (The visual editor is not supported).
+
            imageId: 'button-comment'
*
+
        } );
* 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);
 
}
 

Revision as of 05:13, 14 January 2021

mw.hook( 'wikipage.editform' ).add( function () {
    mw.loader.using( 'mediawiki.toolbar' ).then( function () {
        mw.toolbar.addButton( {
            imageFile: '//upload.wikimedia.org/wikipedia/en/3/34/Button_hide_comment.png',
            speedTip: 'Comment visible only for editors',
            tagOpen: '<!-- ',
            tagClose: ' -->',
            sampleText: 'Insert comment here',
            imageId: 'button-comment'
        } );
    } );
} );