Aller au contenu

MediaWiki:Gadget-MonobookToolbarSortSelected.js

Une page de Wikipédia, l'encyclopédie libre.
Ceci est une version archivée de cette page, en date du 28 septembre 2014 à 17:41 et modifiée en dernier par Arkanosis (discuter | contributions) (Instanciation des boutons avec jQuery). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
if(typeof(MonobookToolbarSortSelected)==="undefined"){ // Test anti double inclusion

MonobookToolbarSortSelected = new Object();

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

MonobookToolbarSortSelected.AddButton = function(){
     if(!document.editform) return;
     var Toolbar = document.getElementById(MonobookToolbarSortSelected.toolbarId);
     if(!Toolbar){
          var Textarea = document.getElementById("wpTextbox1");
          if(!Textarea) return;
          Toolbar = document.createElement('div');
          Toolbar.id = MonobookToolbarSortSelected.toolbarId;
          Textarea.parentNode.insertBefore(Toolbar, Textarea);
     }
     var buttonU = $('<img src="/media/wikipedia/commons/6/6f/Button_arrow_up.PNG" heigth="23" width="23" alt="Tri croissant" />').get();
     buttonU.title = "Tri croissant";
     buttonU.onclick = function(){
          MonobookToolbarSortSelected.UpdateText(0);
          return false;
     };
     buttonU.className = "mw-toolbar-editbutton";
     Toolbar.appendChild(buttonU);

     var buttonD = $('<img src="/media/wikipedia/commons/2/2b/Button_arrow_down.PNG" heigth="23" width="23" alt="Tri décroissant" />').get();
     buttonD.title = "Tri décroissant";
     buttonD.onclick = function(){
          MonobookToolbarSortSelected.UpdateText(1);
          return false;
     };
     buttonD.className = "mw-toolbar-editbutton";
     Toolbar.appendChild(buttonD);
};

MonobookToolbarSortSelected.UpdateText = function(type){
    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;
        selText = MonobookToolbarSortSelected.SortText(selText, type);
        range.text = selText;
        //mark sample text as selected
        if (range.moveStart) {
            if (window.opera)
            range.moveStart('character',  - selText.length);
            range.moveEnd('character', 0);
        }
        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);
        selText = MonobookToolbarSortSelected.SortText(selText, type);
        txtarea.value = txtarea.value.substring(0, startPos)  + selText + txtarea.value.substring(endPos, txtarea.value.length);
        //set new selection
        txtarea.selectionStart = startPos;
        txtarea.selectionEnd = startPos + selText.length;
        //restore textarea scroll position
        txtarea.scrollTop = textScroll;
    }
};

MonobookToolbarSortSelected.SortText = function(Text, type){
     var TextLines = Text.split("\n");
     TextLines.sort();
     if(type==1) TextLines.reverse();
     return TextLines.join("\n");
};

addOnloadHook(MonobookToolbarSortSelected.AddButton);

} // Fin test anti double inclusion