MediaWiki:Gadget-MonobookToolbar.js

Матеріал з Вікіпедії — вільної енциклопедії.
Перейти до навігації Перейти до пошуку

Увага: Після публікування слід очистити кеш браузера, щоб побачити зміни.

  • Firefox / Safari: тримайте Shift, коли натискаєте Оновити, або натисніть Ctrl-F5 чи Ctrl-Shift-R (⌘-R на Apple Mac)
  • Google Chrome: натисніть Ctrl-Shift-R (⌘-Shift-R на Apple Mac)
  • Internet Explorer / Edge: тримайте Ctrl, коли натискаєте Оновити, або натисніть Ctrl-F5
  • Opera: натисніть Ctrl-F5
if (typeof MonobookToolbar === 'undefined') {  // Test anti-double inclusion

var MonobookToolbar = {};
window.MonobookToolbar = MonobookToolbar;

MonobookToolbar.buttons = [];

MonobookToolbar.fonctions = {};

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

MonobookToolbar.fonctions.Init = function ($) {
    var UseBetaToolbar = mw.user.options.get('usebetatoolbar');
    
    var forceOldToolbar = mw.user.options.get('gadget-ForceMonobookToolbar');
    if (UseBetaToolbar && !forceOldToolbar) return;

    var Textarea = document.getElementById('wpTextbox1');
    if (!Textarea) return;

    var toolbar = document.getElementById(MonobookToolbar.toolbarId);
    if (!toolbar) {
        toolbar = document.createElement('div');
        toolbar.id = MonobookToolbar.toolbarId;
        if (UseBetaToolbar && $('.wikiEditor-ui-top').length) {
            $(toolbar).appendTo($('.wikiEditor-ui-top').first());
        }
        else {
            Textarea.parentNode.insertBefore(toolbar, Textarea);	
        }
    }
    MonobookToolbar.toolbarElm = toolbar;

    for (var a = 0, l = MonobookToolbar.buttons.length; a < l; a++) {
        MonobookToolbar.fonctions.InsertButton(a);
    }
    
    importScript('MediaWiki:ToolbarOld.js');
};

MonobookToolbar.fonctions.InsertButton = function (index) {
    var parent = MonobookToolbar.toolbarElm;
    var item = MonobookToolbar.buttons[index];
    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);
};

MonobookToolbar.fonctions.insertTags = function (tagOpen, tagClose, sampleText) {
    var txtarea = document.getElementById('wpTextbox1');
    var selText, isSample = false;

    //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] = {
        'imageFile': imageFile,
        'speedTip': speedTip,
        'tagOpen': tagOpen,
        'tagClose': tagClose,
        'sampleText': sampleText,
        'imageId': imageId
    };
    // if, and only if, Init() has been called already, we have to call InsertButton()
    if (MonobookToolbar.toolbarElm) {
        MonobookToolbar.fonctions.InsertButton(NewIndex);
    }
};

$(MonobookToolbar.fonctions.Init);

}  // Fin test anti-double inclusion