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 5 mai 2013 à 19:52 et modifiée en dernier par Dr Brains (discuter | contributions) (Nouvelle page : if(typeof(MonobookToolbarSortSelected)==="undefined"){ // Test anti double inclusion MonobookToolbarSortSelected = new Object(); MonobookToolbarSortSelected.AddButton = fu...). Elle peut contenir des erreurs, des inexactitudes ou des contenus vandalisés non présents dans la version actuelle.
(diff) ← Version précédente | Voir la version actuelle (diff) | Version suivante → (diff)
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.AddButton = function(){
     if(!document.editform) return;
     var Toolbar = document.getElementById('toolbar');
     if(!Toolbar) Toolbar = document.getElementById('MonobookToolbar');
     if(!Toolbar){
          Toolbar = document.createElement('div');
          Toolbar.id = 'MonobookToolbar';
          document.editform.insertBefore(Toolbar, document.editform.firstChild);
     }
     var buttonU = '<img src="/media/wikipedia/commons/6/6f/Button_arrow_up.PNG" heigth="23" width="23" alt="Tri croissant" />';
 
     var NewButtonU = document.createElement('a')
     NewButtonU.title = "Tri croissant";
     NewButtonU.href = "#";
     NewButtonU.onclick = function(){ 
          MonobookToolbarSortSelected.UpdateText(0);
          return false;
     }
     NewButtonU.className = "mw-toolbar-editbutton";
     NewButtonU.innerHTML = button;
     Toolbar.appendChild(NewButtonU);

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

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