Aller au contenu

MediaWiki:Gadget-MonobookToolbar.js

Une page de Wikiquote, le recueil des citations libres.

Note : après avoir publié vos modifications, il se peut que vous deviez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

  • Firefox / Safari : maintenez la touche Maj (Shift) en cliquant sur le bouton Actualiser ou appuyez sur Ctrl + F5 ou Ctrl + R (⌘ + R sur un Mac).
  • Google Chrome : appuyez sur Ctrl + Maj + R (⌘ + Shift + R sur un Mac).
  •  Edge : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl + F5.
/* [[Catégorie:JavaScript de Wikiquote|MonobookToolbar]] */

if(typeof(MonobookToolbar)=="undefined"){  // Test anti-double inclusion

var MonobookToolbar = new Object();

MonobookToolbar.buttons = new Array();

MonobookToolbar.fonctions = new Object();

MonobookToolbar.toolbarId = mw.user.options.get('usebetatoolbar') === 1 ? 'monobooktoolbar' : 'toolbar';

MonobookToolbar.fonctions.Alert = function(text){
   if(mw.config.get('wgUserName') && mw.config.get('wgUserName') === "Dr Brains") alert(text);
};

MonobookToolbar.fonctions.Init = function($){
   var Textarea = document.getElementById("wpTextbox1");
   if(!Textarea) return;
   var UseBetaToolbar = (mw.user.options.get('usebetatoolbar') === 1 );
   var forceOldToolbar = (mw.user.options.get('gadget-ForceMonobookToolbar') === "1");
   //MonobookToolbar.fonctions.Alert("* UseBetaToolbar = "+UseBetaToolbar + "\n* forceOldToolbar = "+forceOldToolbar);
   if(UseBetaToolbar && !forceOldToolbar) return;

   //MonobookToolbar.fonctions.Alert("RUN");
   var toolbar = document.getElementById(MonobookToolbar.toolbarId);
   if(!toolbar){
      toolbar = document.createElement('div');
      toolbar.id = MonobookToolbar.toolbarId;
      Textarea.parentNode.insertBefore(toolbar, Textarea);
   }
   if(typeof(MonobookToolbar_UserCustom)==="function") MonobookToolbar_UserCustom();
   MonobookToolbar.fonctions.InitButtons();
};

MonobookToolbar.fonctions.InitButtons = function(){
    for(var a=0,l=MonobookToolbar.buttons.length;a<l;a++){
        MonobookToolbar.fonctions.InsertButton(a);
    }
    // FIXME: Use of "mwCustomEditButtons" is deprecated. Use mw.toolbar instead.
    if(typeof(mwCustomEditButtons)==="object"){
        for(var a=0,l=mwCustomEditButtons.length;a<l;a++){
            if(mwCustomEditButtons[a].inserted) continue;
            mwCustomEditButtons[a].inserted = true;
            MonobookToolbar.fonctions.InsertButton(a, true);
        }
    }
};

MonobookToolbar.fonctions.InsertButton = function(index, old){
    var parent = document.getElementById(MonobookToolbar.toolbarId);
    var item = MonobookToolbar.buttons[index];
    // FIXME: Use of "mwCustomEditButtons" is deprecated. Use mw.toolbar instead.
    if(old && mwCustomEditButtons) item = mwCustomEditButtons[index];
    if(!parent || !item) return false;
    if(item.imageId) {
      var oldImage = document.getElementById(item.imageId);
      if (oldImage) {
        oldImage.parentNode.removeChild(oldImage);
      }
    }
    var image = document.createElement("img");
    image.width = 23;
    image.height = 22;
    if (item.imageId) image.id = item.imageId;
    image.src = item.imageFile;
    image.border = 0;
    image.alt = item.speedTip;
    image.className = "mw-toolbar-editbutton";
    image.title = item.speedTip;
    image.onclick = function() {
        MonobookToolbar.fonctions.insertTags(item.tagOpen, item.tagClose, item.sampleText);
        return false;
    };
    parent.appendChild(image);
    return true;
};

MonobookToolbar.fonctions.insertTags = function(tagOpen, tagClose, sampleText){
    var txtarea = document.getElementById("wpTextbox1");
    if (!txtarea){
        // some alternate form? take the first one we can find
        var areas = document.getElementsByTagName('textarea');
        txtarea = areas[0];
    }
    var selText, isSample = false;
    if (document.selection && document.selection.createRange) { // IE/Opera
        //save window scroll position
        if (document.documentElement && document.documentElement.scrollTop)
        var winScroll = document.documentElement.scrollTop;
        else if (document.body)
        var winScroll = document.body.scrollTop;
        //get current selection
        txtarea.focus();
        var range = document.selection.createRange();
        selText = range.text;
        //insert tags
        checkSelectedText();
        range.text = tagOpen + selText + tagClose;
        //mark sample text as selected
        if (isSample && range.moveStart) {
            if (window.opera)
            tagClose = tagClose.replace(/\n/g,'');
            range.moveStart('character', - tagClose.length - selText.length);
            range.moveEnd('character', - tagClose.length);
        }
        range.select();
        //restore window scroll position
        if (document.documentElement && document.documentElement.scrollTop)
        document.documentElement.scrollTop = winScroll;
        else if (document.body)
        document.body.scrollTop = winScroll;
    } else if (txtarea.selectionStart || txtarea.selectionStart == '0') { // Mozilla
        //save textarea scroll position
        var textScroll = txtarea.scrollTop;
        //get current selection
        txtarea.focus();
        var startPos = txtarea.selectionStart;
        var endPos = txtarea.selectionEnd;
        selText = txtarea.value.substring(startPos, endPos);
        //insert tags
        checkSelectedText();
        txtarea.value = txtarea.value.substring(0, startPos) + tagOpen + selText + tagClose + txtarea.value.substring(endPos, txtarea.value.length);
        //set new selection
        if (isSample) {
            txtarea.selectionStart = startPos + tagOpen.length;
            txtarea.selectionEnd = startPos + tagOpen.length + selText.length;
        } else {
            txtarea.selectionStart = startPos + tagOpen.length + selText.length + tagClose.length;
            txtarea.selectionEnd = txtarea.selectionStart;
        }
        //restore textarea scroll position
        txtarea.scrollTop = textScroll;
    }
    function checkSelectedText(){
        if (!selText) {
            selText = sampleText;
            isSample = true;
        } else if (selText.charAt(selText.length - 1) == ' ') { //exclude ending space char
            selText = selText.substring(0, selText.length - 1);
            tagClose += ' ';
        }
    }
};

MonobookToolbar.fonctions.CreateButton = function(imageFile, speedTip, tagOpen, tagClose, sampleText, imageId){
    var NewIndex = MonobookToolbar.buttons.length;
    MonobookToolbar.buttons[NewIndex] = {
        "imageId": imageId,
        "imageFile": imageFile,
        "speedTip": speedTip,
        "tagOpen": tagOpen,
        "tagClose": tagClose,
        "sampleText": sampleText
    };
    if(document.getElementById(MonobookToolbar.toolbarId)){
        if(imageId){
            MonobookToolbar.fonctions.InsertButton(NewIndex);
        }
    }
};

$(MonobookToolbar.fonctions.Init);

}  // Fin test anti-double inclusion