MediaWiki:Gadget-MonobookToolbar.js

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.
(function () {
    'use strict';

    // Test anti-double inclusion
    if (window.MonobookToolbar) {
        return;
    }

    function _InsertButton(item) {
        var toolbar = getToolbar();
        if (!toolbar) {
            return;
        }
        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.alt = item.speedTip;
        image.className = 'mw-toolbar-editbutton';
        image.title = item.speedTip;
        image.onclick = function (e) {
            e.preventDefault();
            if (item.callback) {
                item.callback();
            } else {
                insertTags(item.tagOpen, item.tagClose, item.sampleText);
            }
        };
        toolbar.appendChild(image);
    }

    // Get the toolbar, lazy-creating it the first time this function is called
    function getToolbar() {
        if (!getToolbar.cache) {
            var toolbarId = mw.user.options.get('usebetatoolbar') ? 'monobooktoolbar' : 'toolbar';
            var toolbar = document.getElementById(toolbarId);

            if (!toolbar) {
                var Textarea = _getTextbox();
                if (!Textarea) {
                    return null;
                }
                toolbar = document.createElement('div');
                toolbar.id = toolbarId;
                if (toolbarId === 'toolbar') {
                    // Undo #toolbar {height:22px} added by MediaWiki,
                    // which causes troubles if the toolbar takes several lines
                    toolbar.style.height = 'auto';
                }
                Textarea.parentNode.insertBefore(toolbar, Textarea);
            }

            getToolbar.cache = toolbar;
        }

        return getToolbar.cache;
    }

    function _getTextbox() {
        if (!_getTextbox.cache) {
            _getTextbox.cache = document.getElementById('wpTextbox1');
        }

        return _getTextbox.cache;
    }

    function getCurrentFocused() {
        if (!currentFocused) {
            currentFocused = _getTextbox();
        }

        return currentFocused;
    }

    function insertTags(tagOpen, tagClose, sampleText) {
        var target = getCurrentFocused();
        if (!target) {
            return;
        }
        $(target).textSelection('encapsulateSelection', {
            pre: tagOpen || '',
            peri: sampleText || '',
            post: tagClose || ''
        });
    }

    function addButton(button, speedTip, tagOpen, tagClose, sampleText, imageId) {
        deferred.done(function () {
            // Backwards compatibility
            if (typeof button !== 'object') {
                button = {
                    'imageFile': button,
                    'speedTip': speedTip,
                    'tagOpen': tagOpen,
                    'tagClose': tagClose,
                    'sampleText': sampleText,
                    'imageId': imageId
                };
            }
            _InsertButton(button);
        });
    }

    var deferred = $.Deferred();
    var currentFocused;

    if (['edit', 'submit'].includes(mw.config.get('wgAction'))) {
        mw.loader.using(['user.options', 'jquery.textSelection'], function () {
            $(function ($) {
                if (!mw.user.options.get('usebetatoolbar') || mw.user.options.get('gadget-ForceMonobookToolbar')) {
                    deferred.resolve();
                } else {
                    deferred.reject();
                }
                // We always register the following, as insertTags() can be used independently from the toolbar
                // Apply to dynamically created textboxes as well as normal ones
                $(document).on('focus', 'textarea, input:text, .CodeMirror', function () {
                    if (this.classList.contains('CodeMirror')) {
                        // CodeMirror hooks into #wpTextbox1 for textSelection changes
                        currentFocused = _getTextbox();
                    } else {
                        currentFocused = this;
                    }
                });
            });
        });
    } else {
        deferred.reject();
    }

    window.MonobookToolbar = {
        addButton: addButton,
        insertTags: insertTags,
        getToolbar: getToolbar,
        getCurrentFocused: getCurrentFocused,
    };

})();