Usuari:Anskar/scripts/vextris.js
Aparença
El codi que afegiu en aquesta pàgina podria tenir contingut maliciós que comprometi el vostre compte. Si no esteu segur si el codi a afegir és segur, pregunteu abans en la Taverna. El codi s'executarà en mostrar una previsualització d'aquesta pàgina. |
Nota: Després de desar, heu de netejar la memòria cau del navegador per veure els canvis. En la majoria de navegadors amb Windows o Linux, premeu Ctrl+F5 o bé premeu Shift i cliqueu el botó "Actualitza" (Ctrl i "Actualitza" amb Internet Explorer). Vegeu més informació i instruccions per a cada navegador a Viquipèdia:Neteja de la memòria cau.
var c = "//upload.wikimedia.org/wikipedia/commons/thumb/";
var c2 = "//upload.wikimedia.org/wikipedia/commons/";
var idpref = "mw-customeditbutton-myspecialbutton-"
function customizeToolbar(){
$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
'sections': {
'vextris': {
'type': 'toolbar', // Can also be 'booklet'
'label': 'Vextris'
// or 'labelMsg': 'section-emoticons-label' for a localized label
}
}
} );
$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
'section': 'vextris',
'groups': {
'first': {
'label': ''
},
'format': {
'label': 'Format'
},
'users': {
'label': ''
}
}
} );
}
if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1) {
mw.loader.using( 'user.options', function(){
if ( mw.user.options.get('usebetatoolbar') ) {
mw.loader.using( 'ext.wikiEditor', function () {
$(document).ready( customizeToolbar );
} );
}
})
}
mw.hook( 'ext.lqt.textareaCreated' ).add( customizeToolbar );
(function ($, mw) {
"use strict";
var queue = [], $toolbar;
function insertButton(btnObj) {
$toolbar.wikiEditor('addToToolbar', btnObj);
}
function handleQueue() {
var i;
if (!$toolbar) {
return;
}
for (i = 0; i < queue.length; i += 1) {
insertButton(queue[i]);
}
queue = [];
}
function check() {
$toolbar = ($.wikiEditor && $.fn.wikiEditor && $('#wpTextbox1').length) ? $('#wpTextbox1') : false;
handleQueue();
}
// Only on editpage
if ($.inArray(mw.config.get('wgAction'), ['edit', 'submit']) !== -1) {
/**
* veInsertWikiEditorButton
*
* @param options {Object} An object with options:
* - section {String} (optional) The name of the section in the WikiEditor. Defaults to 'main'
* - group {String} (optional) The name of the group in the WikiEditor. Defaults to 'insert'
* - id {String} (required) Unique id (ie. 'my-button')
* - icon {String} (recommended) URL to the icon, should be square about 21 to 22px
* - label {String} (required) Tooltip displayed when hovering button
* - insertBefore {String} (optional) Wikitext to be inserted before the cursor on-click
* - sampleText {String} (optional) Text inserted in place of the cursor if no text was selected
* - insertAfter {String} (optional) Wikitext to be inserted after the cursor on-click
* - callback {Function} (optional) Called when the button is clicked
* - autoSummary {mixed} (optional) Null or an Object with the following properties:
* - summary {String} (required) Edit summary that should be used
* - position {String} (optional) 'append', 'prepend' or 'replace'
* - delimiter {String} (optional) delimiter between the (possibly) current summary and the to-be-inserted summary
*/
window.veInsertWikiEditorButton = function (options) {
// Defaults
options = $.extend({
'section': 'main',
'group': 'insert',
'id': null,
'icon': c + 'f/f0/Toolbaricon_bold_!.png/21px-Toolbaricon_bold_!.png',
'label': '',
'insertBefore': '',
'sampleText': '',
'insertAfter': '',
'callback': null,
'autoSummary': {
'summary': null,
'position': 'append',
'delimiter': '; '
}
}, options);
// Required
if (!options.label) {
return false;
}
if(!options.id) {
options.id = idpref + options.label
} else if (options.id.indexOf("-")==-1) {
options.id = idpref + options.id
}
console.log(options.id)
var btnObj = {
'section': options.section,
'group': options.group,
'tools': {}
};
btnObj.tools[options.id] = {
label: options.label,
type: 'button',
icon: options.icon,
action: {
type: 'callback',
execute: function () {
// encapsulateSelection
$toolbar.textSelection('encapsulateSelection', {
pre: options.insertBefore,
peri: options.sampleText,
post: options.insertAfter
});
// Auto summary
if (options.autoSummary && options.autoSummary.summary) {
var $summary = $('#wpSummary'), currentSum = $summary.val();
if (!$.trim(currentSum)) {
$summary.val(options.autoSummary.summary);
} else {
switch (options.autoSummary.position) {
case 'prepend':
$summary.val(
options.autoSummary.summary +
options.autoSummary.delimiter +
currentSum
);
break;
case 'replace':
$summary.val(options.autoSummary.summary);
break;
default: // 'append'
$summary.val(
currentSum +
options.autoSummary.delimiter +
options.autoSummary.summary
);
}
}
}
// Callback
if ($.isFunction(options.callback)) {
options.callback();
}
}
}
};
if ($toolbar) {
console.log("toolbar")
insertButton(btnObj);
} else {
console.log("queue")
queue[queue.length] = btnObj;
}
};
$(document).ready(check);
$(window).load(check);
} else {
// No-op function to avoid errors on other pages
window.veInsertWikiEditorButton = function () {};
}
}(jQuery, mediaWiki));