User:MarkS/XEB/dev.js
/* Version: 3.1
* ============================================================
* en: ADD SOME EXTRA BUTTONS TO THE EDITPANEL
* de: FÜGE NEUE BUTTON IN DIE WERKZEUGLEISTE
* pt: ACRESCENTA ALGUNS BOTÕES EXTRAS AO PAINEL DE EDIÇÃO
* ============================================================
* Vorschläge für neue Buttons werden gerne entgegengenommen
* Die Reihenfolge und Anzahl der Buttons ist über die (alphabetische) Variable XEBOrder wählbar.
*
* ================================
* Control Variables
*
* window.rmEditButtons - Removes standard toolbar buttons
* window.XEBOrder - The order in which the buttons are displayed
* window.myButtons - Add user defined buttons
*
*/
mw.loader.load( '//en.wikipedia.org/w/index.php?title=User:MarkS/XEB/live.css&action=raw&ctype=text/css', 'text/css' );
if (typeof XEBPopups === 'undefined') {
XEBPopups = true;
}
if (typeof XEBHideDelay === 'undefined') {
// Time before the popup disappears after the mouse moves out
window.XEBHideDelay = 0.5;
}
if (typeof XEBExtendEditSummary === 'undefined') {
// Is the edit summary extended after a popup
window.XEBExtendEditSummary = true;
}
if (typeof usersignature === 'undefined') {
var usersignature = '-- ~~' + '~~';
}
var Isrc = '//upload.wikimedia.org/wikipedia/commons/';
var BDict = {
'A': ['e/e9/Button_headline2.png', 'Secondary headline', '\n===', '===', 'Secondary headline'],
'B': ['1/13/Button_enter.png', 'Line break', '
', , ],
'C': ['5/5f/Button_center.png', 'Center', '
'D': ['e/ea/Button_align_left.png', 'Left-Align', '
'D1': ['a/a5/Button_align_right.png', 'Right-Align', '
'E': ['0/04/Button_array.png', 'Table', '\n
class="wikitable" \n | |
\n| 1 | 2\n |
\n| 3 | 4', '\n |
'F': ['1/1e/Button_font_color.png', 'Insert coloured text', 'Coloured text<\/span>', 'ColourName'],
'FS': ['1/1b/Button_miss_signature.png', 'Unsigned post', '— Preceding unsigned comment added by ', ' (talk • contribs) date', 'user name or IP'],
'G': ['9/9e/Btn_toolbar_gallery.png', 'Picture gallery', "\n
'H': ['7/74/Button_comment.png', 'Comment', "", 'Comment'],
'I1': ['6/6a/Button_sup_letter.png', 'Superscript', '', '<\/sup>', 'Superscript text'],
'I2': ['a/aa/Button_sub_letter.png', 'Subscript', '', '<\/sub>', 'Subscript text'],
'J1': ['5/58/Button_small.png', 'Small', '', '<\/small>', 'Small Text'],
'J2': ['5/56/Button_big.png', 'Big text', '', '<\/big>', 'Big text'],
'K': ['b/b4/Button_category03.png', 'Category', "Category:", "", 'Category name'],
'L': ['8/8e/Button_shifting.png', 'Insert tab(s)', ':', '', ':'],
'M': ['f/fd/Button_blockquote.png', 'Insert block of quoted text', '
\n', '\n<\/blockquote>', 'Block quote'],'N': ['4/4b/Button_nbsp.png', 'nonbreaking space', ' ', , ],
'O': ['2/23/Button_code.png', 'Insert code', '
', '<\/code>', 'Code'],
'P': ['3/3c/Button_pre.png', 'Pre formatted Text', '
', '<\/pre>', 'Pre formatted text'],'P1': ['9/93/Button_sub_link.png', 'Insert link to sub-page', '', '/Sub_Page', 'Page'],
'Q': ['d/d3/Button_definition_list.png', 'Insert definition list', '\n; ', '\n: Item 1\n: Item 2', 'Definition'],
'R': ['7/79/Button_reflink.png', 'Insert a reference', '', '<\/ref>', 'Insert reference material'],
'R1': ['7/79/Button_reflink.png', 'Start a reference', '', '', 'Reference material'],
'R3': ['1/1a/Button_reflink_advanced_3.png', 'No reference material', , '"/>', ],
'R4': ['9/9a/Button_references.png', 'Reference footer', "\n==Notes==\n\n
\n", , ],\n 'S': ['c/c9/Button_strike.png', 'Strikeout', '
', '<\/s>', 'Struck out text'],'T': ['e/eb/Button_plantilla.png', 'Template', '{{', '}}', 'Template name'],
'TS': ['a/a4/TableStart.png', 'Start a table', '
', ],
', , ], 'TC': ['7/71/TableCell.png', 'Table cell', '|', , ],
'TE': ['0/06/TableEnd.png', 'End a table', , '
'TR': ['4/4c/TableRow.png', 'Start a table row', '|-', , ],
'T1': ['3/30/Tt_icon.png', 'Teletype text', '', '<\/tt>', 'Teletype Text'],
'TL': ['3/37/Button_tl_template.png', 'Template link', "{{subst:" + "tl|", '}}', 'Template name'],
'U': ['f/fd/Button_underline.png', 'Underlined', "", "<\/u>", 'Underlined text'],
'V': ['c/c8/Button_redirect.png', 'Redirect', "#REDIRECT ", "", 'Article Name'],
'W': ['8/88/Btn_toolbar_enum.png', 'Numbering', "\n# ", "\n# Element 2\n# Element 3", 'Element 1'],
'X': ['1/11/Btn_toolbar_liste.png', 'List', "\n* ", "\n* Element B\n* Element C", 'Element A'],
'Y1': ['c/ce/Button_no_include.png', 'No Include', "
", "<\/noinclude>", 'Text'], 'Y2': ['7/79/Button_include.png', 'Include only', "
", "<\/includeonly>", 'Text'], 'Z': ['3/35/Button_substitute.png', 'Substitute', "{{subst:", "}}", 'Template'],
'AI': ['1/1c/Button_advanced_image.png', 'Advanaced Image', "Image:", "", 'FileName.jpg'],
'GEO': ['b/b8/Button_Globe.png', 'Geo location', "", "", ""],
'TALK': ['4/49/Button_talk.png', 'Add talk template', "", "", ""]
};
var XEBOrder2 = [];
initButtons();
if (!mw.config.get( 'wgIsArticle' )) {
// only if edit
if (XEBPopups) {
$(extendButtons);
}
}
function initButtons() {
var bc, d, i, customTools = {};
if (typeof XEBOrder !== 'string') {
// can be modified
XEBOrder2 = 'A,D,C,D1,F,U,J1,E,G,Q,W,X,K,L,H,O,R,T'.split(',');
} else if (XEBOrder.toLowerCase() === 'all') {
$.each(BDict, function( b, value ){
XEBOrder2.push(b);
});
} else {
XEBOrder2 = XEBOrder.toUpperCase().split(',');
}
$.each(BDict, function( b, value ){
// Add the start of the URL (Isrc) to the XEB buttons
BDict[b][0] = Isrc + BDict[b][0];
});
// If the user has defined any buttons then add them into the available button lists
if (typeof myButtons === 'object') {
$.each(myButtons, function( b, value ){
// custom user buttons
BDict[b] = myButtons[b];
});
}
mw.loader.using( 'user.options', function () {
if ( mw.user.options.get('usebetatoolbar') ) {
// Use the enhanced edit toolbar
mw.loader.using( 'ext.wikiEditor', function () {
$(function(){
// Build the new buttons
for (i = 0; i < XEBOrder2.length; i++) {
bc = BDict[XEBOrder2[i]];
//Check if bc is an object
// - protects if user specified a non-existant buttons
// - IE causes a javascript error when viewing a page
if (typeof bc === 'object') {
customTools[ XEBOrder2[i] ] = {
label: bc[1],
type: 'button',
icon: bc[0],
action: {
type: 'encapsulate',
options: {
pre: bc[2],
peri: bc[4],
post: bc[3]
}
}
};
}
}
$('#wpTextbox1').wikiEditor( 'addToToolbar', {
'sections': {
'extra': {
'type': 'toolbar',
'label': 'Extras'
}
},
'section': 'extra',
'groups': {
'my-buttons': {
'label': 'My buttons',
'tools': customTools
}
}
} );
});
} );
} else {
// Use the old edit toolbar
// Add the MediaWiki standard buttons into the available buttons
$.each(mw.toolbar.buttons, function( b, value ){
// add standard buttons for full XEB order changing
// BDict[b]=[];
BDict[b] = [ value.imageFile, value.speedTip, value.tagOpen, value.tagClose, value.sampleText];
// for (d in value) {BDict[b].push(value}[d]);
});
// Build the new buttons
for (i = 0; i < XEBOrder2.length; i++) {
bc = BDict[XEBOrder2[i]];
//Check if bc is an object
// - protects if user specified a non-existant buttons
// - IE causes a javascript error when viewing a page
if (typeof bc === 'object') {
//Call addButton from module mediawiki.action.edit
mw.toolbar.addButton(bc[0], bc[1], bc[2], bc[3], bc[4] /* , imageId, selectText */ );
}
}
// Remove the default buttons (if requested by the user)
eraseButtons();
}
} );
}
/** en: Removes arbitrary standard buttons from the toolbar
* @author: :de:User:Olliminatore
* @version: 0.1 (01.10.2006) **/
function eraseButtons() {
var x, i, enExtraButtons;
//Remove the buttons the user doesn't want
if (typeof rmEditButtons !== 'object') {
return;
}
if (typeof rmEditButtons[0] === 'string' && rmEditButtons[0].toLowerCase() === 'all') {
// English Wikipedia creates 11 extra buttons which are stored in mwCustomEditButtons
// rather than mw.toolbar.buttons. However, there is no guarantee it will always be 11
// so we count them here.
enExtraButtons = window.mwCustomEditButtons.length;
mw.toolbar.buttons = [];
for (i = 0; i < enExtraButtons; i++) {
window.mwCustomEditButtons.shift();
}
}
//Sort the user's requests so we remove the button with the highest index first
//- This ensures we remove the buttons the user expects whatever order he requested the buttons in
window.rmEditButtons.sort(sortit);
//Remove individual buttons the user doesn't want
for (i = 0; i < rmEditButtons.length; i++) {
var n = rmEditButtons[i];
//Standard Wikimedia buttons
if (n >= 0 && n < mw.toolbar.buttons.length) {
if (n < mw.toolbar.buttons.length) {
x = -1;
while ((++x) < mw.toolbar.buttons.length) {
if (x >= n) {
mw.toolbar.buttons[x] = mw.toolbar.buttons[x + 1];
}
}
}
mw.toolbar.buttons.pop();
}
//Extra buttons in English Wikipedia
n = n - mw.toolbar.buttons.length;
if (n > 0 && n < window.mwCustomEditButtons.length) {
if (n < window.mwCustomEditButtons.length) {
x = -1;
while ((++x) < window.mwCustomEditButtons.length) {
if (x >= n) {
window.mwCustomEditButtons[x] = window.mwCustomEditButtons[x + 1];
}
}
}
window.mwCustomEditButtons.pop();
}
}
}
//Function:
// sortit
//Purpose:
// Used to sort the rmEditButtons array into descending order
function sortit(a, b) {
return (b - a);
}
//Function:
//Purpose:
// Adds extended onclick-function to some buttons
function extendButtons() {
var allEditButtons = document.getElementById('toolbar');
if ( !allEditButtons ) {
return false;
}
if (typeof editform !== 'undefined') {
if (!(window.editform = document.forms.editform)) {
return false;
}
}
// table
extendAButton(Isrc + '0/04/Button_array.png', XEBPopupTable);
extendAButton(Isrc + '7/79/Button_reflink.png', XEBPopupRef);
extendAButton(Isrc + 'b/b8/Button_Globe.png', XEBPopupGeoLink);
extendAButton(Isrc + '4/49/Button_talk.png', XEBPopupTalk);
extendAButton(Isrc + '1/1c/Button_advanced_image.png', XEBPopupImage);
//extendAButton(Isrc+'6/6a/Button_sup_letter.png',XEBPopupFormattedText);
// redirect -##IE doesn't like this line. Object doesn't support this property or method
//c=XEBOrder2.getIndex('V');
// if(c != -1)
// allEditButtons[bu_len+c].onclick=function(){
// var a='#REDIRECT \[\['+prompt("Which page do you want to redirect to\?")+'\]\]';
// document.editform.elements['wpTextbox1'].value=a;
// document.editform.elements['wpSummary'].value=a;
// document.editform.elements['wpWatchthis'].checked=false
// };
}
function extendAButton(url, newfunc) {
var i;
var allEditButtons = document.getElementById('toolbar');
if ( !allEditButtons ) {
return false;
}
if (typeof editform !== 'undefined') {
if (!(window.editform = document.forms.editform)) {
return false;
}
}
allEditButtons = allEditButtons.getElementsByTagName('img');
for (i = 0; i < allEditButtons.length; i++) {
if (allEditButtons[i].src === url) {
allEditButtons[i].onclick = newfunc;
}
}
}
//==========================================================================================================
// General purpose popup code
//==========================================================================================================
function getXEBPopupDiv(name) {
var XEBMainDiv = document.getElementById('XEB');
if (XEBMainDiv === null) {
XEBMainDiv = document.createElement('div');
document.body.appendChild(XEBMainDiv);
XEBMainDiv.id = 'XEB';
}
var me = document.getElementById('XEBPopup' && name);
if (me !== null) {
return me;
}
me = document.createElement('div');
XEBMainDiv.appendChild(me);
me.id = 'XEBPopup';
me.style.position = 'absolute';
me.display = 'none';
me.visibility = 'hidden';
me.onmouseout = CheckHideXEBPopup;
me.onmouseover = cancelHidePopup;
return me;
}
//Function:
// CheckHideXEBPopup
//Purpose:
// Looks at the cursor position and if it has moved outside the popup it will close the popup
//Called:
// When the onMouseEvent is fired on the popup
function CheckHideXEBPopup(e) {
var x, y, ph, s, pl, pt, pw;
var m = document.getElementById('XEBmnu');
if (is_gecko) {
ph = m.offsetHeight;
x = e.clientX + window.scrollX;
y = e.clientY + window.scrollY;
s = window.getComputedStyle(m, '');
ph = s.height;
ph = Number(ph.substring(0, ph.length - 2));
} else {
x = event.clientX + document.documentElement.scrollLeft + document.body.scrollLeft;
y = event.clientY + document.documentElement.scrollTop + document.body.scrollTop;
ph = m.offsetHeight;
}
pl = curPopup.x;
pt = curPopup.y;
pw = m.style.width;
pw = Number(pw.substring(0, pw.length - 2));
if (x > (pl + 2) && x < (pl + pw - 5) && y > (pt + 2) && y < (pt + ph - 5)) {
return;
}
curPopup.hideTimeout = setTimeout(hideXEBPopup, XEBHideDelay * 1000);
}
function cancelHidePopup() {
clearTimeout(curPopup.hideTimeout);
}
function hideXEBPopup() {
var XEBMainDiv = document.getElementById('XEB');
var m = document.getElementById('XEBPopup');
XEBMainDiv.removeChild(m);
}
function XEBstartDrag(e) {
var m = new GetPos(e || event);
curPopup.startDrag.mouse = m;
curPopup.startDrag.floatpopup.y = parseInt(curPopup.div.style.top, 10);
curPopup.startDrag.floatpopup.x = parseInt(curPopup.div.style.left, 10);
curPopup.dragging = true;
}
function XEBstopDrag(e) {
if (curPopup.dragging === false) {
return;
}
curPopup.dragging = false;
}
function XEBDrag(e) {
if (curPopup.dragging === false) {
return;
}
var m = new GetPos(e || event);
var x = parseInt(curPopup.startDrag.floatpopup.x + (m.x - curPopup.startDrag.mouse.x), 10);
var y = parseInt(curPopup.startDrag.floatpopup.y + (m.y - curPopup.startDrag.mouse.y), 10);
curPopup.div.style.top = y + 'px';
curPopup.div.style.left = x + 'px';
curPopup.x = x;
curPopup.y = y;
}
//=============================================================================
// Popup: Table
//=============================================================================
function XEBPopup(name, x, y) {
// Make sure the popup can appear on the screen
this.IESelectedRange = XEBgetIESelectedRange();
var winW = (is_gecko) ? window.innerWidth : document.body.offsetWidth;
if ((winW - this.width) < x) {
x = (winW - this.width);
}
this.div = getXEBPopupDiv(name);
this.div.style.zIndex = 2000;
this.div.display = 'inline';
this.div.visibility = 'visible';
this.div.style.top = y + 'px';
this.x = x;
this.y = y;
this.name = name;
this.startDrag = {};
this.startDrag.floatpopup = {};
}
function setInnerHTML(text) {
var i;
var winW = (is_gecko) ? window.innerWidth : document.body.offsetWidth;
if ((winW - this.width) < this.x) {
this.x = (winW - this.width);
}
this.div.style.left = this.x + 'px';
var mt = '
';';mt += '
Title';mt += text;
mt += '
this.div.innerHTML = mt;
//Turn off autocomplete. If the mouse moves over the autocomplete popup then x,y in CheckHidePopup is relative to the
// autocomplete popup and our popup is hidden
var InTexts = this.div.getElementsByTagName('input');
for (i = 0; i < InTexts.length; i++) {
var theInput = InTexts[i];
if (theInput.type === 'text') {
theInput.setAttribute('autocomplete', 'off');
}
}
//Add rollover features to menu items. Doing it here means we don't have to do it for each menu
var x = XEBgetElementsByClassName(this.div, 'XEBMnuItm', 'span');
for (i = 0; i < x.length; i++) {
var theItm = x[i];
theItm.onmouseout = XEBMenuMouseOut;
theItm.onmouseover = XEBMenuMouseOver;
}
this.div.style.borderWidth = 'thin';
this.div.style.borderStyle = 'solid';
this.div.style.backgroundColor = '#D0D0D0';
}
XEBPopup.prototype.width = 250;
XEBPopup.prototype.dragging = false;
XEBPopup.prototype.setInnerHTML = setInnerHTML;
var curPopup;
function GetPos(e) {
this.x = e.clientX - 10 + document.documentElement.scrollLeft + document.body.scrollLeft;
this.y = e.clientY - 10 + document.documentElement.scrollTop + document.body.scrollTop;
}
function XEBPopupTable(e) {
var m = new GetPos(e || event);
curPopup = new XEBPopup('table', m.x, m.y);
var mt = '
Enter the table parameters below: <\/p>' +
'