Медијавики:Gadget-wikidialog.js

Извор: Викиновости

Напомена: Пошто објавите измене, можда ћете морати да обришете кеш прегледача како бисте их видели.

  • Firefox / Safari: Држите Shift и кликните на Reload или притисните Ctrl-F5 или Ctrl-R (⌘-R на Mac-у).
  • Google Chrome: Притисните Ctrl-Shift-R (⌘-Shift-R на Mac-у).
  • Internet Explorer / Edge: Држите Ctrl и кликните на Refresh или притисните Ctrl-F5.
  • Opera: Притисните Ctrl-F5.
/* This script provides a function to set up dialog fields and buttons based on markup generated by [[:Category:dialog formatting templates|dialog formatting templates]].
Any javascript page importing this page should also contain a commented link to it, for [[Special:WhatLinksHere/{{FULLPAGENAME}}]].

For instructions, see the [[{{TALKPAGENAME}}|talk page]].  [[Category:Dialog javascript pages]] */

if (! window.wikidialog) window.wikidialog = {};
if (! window.wikidialog.show) {
    //
    // version
    //
    window.wikidialog.gadgetVersion = function() { return '0.5 (2015-08-08)'; }
    //
    window.wikidialog.show = function () {
        //
        // [[Template:Dialog/text|text]]
        //
        var things = $('span.wikidialog-text');
        var thingIds = things.map(function () { return $(this).attr('id'); });
        things.replaceWith(function (idx) {
            if ((idx > 0) && (thingIds[idx] == thingIds[idx - 1])) {  return ''; }
            var s = $(this).text(); // .substring(0,1024); // BOUND TEXT CONTENT SIZE
            var d = '';
            if (   s.search(/^size=(\d*)&/) == 0) {
                d = s.match(/^size=(\d*)&(.*)/);
                s = d[2];
                d = d[1];
            }
            for (var j = idx + 1; j < thingIds.length; j++) {
                if (thingIds[idx] == thingIds[j]) {
                    s += ' ' + $(things[j]).text();
                } else break;
            }
            var e = $(document.createElement('input')).attr('type','text').addClass('wikidialog-text').attr('id',thingIds[idx]).val(s).attr('size',d);
            if ($(this).is('.wikidialog-hidden')) e.css('display','none');
            return e;
        });
        //
        // [[Template:Dialog/textarea|textarea]]
        //
        things = $('span.wikidialog-textarea');
        thingIds = things.map(function () { return $(this).attr('id'); });
        things.replaceWith(function (idx) {
            if ((idx > 0) && (thingIds[idx] == thingIds[idx - 1])) { return ' '; }
            var s = $(this).text(); // .substring(0,131072); // BOUND TEXTAREA CONTENT SIZE
            var c = '';
            if (   s.search(/^cols=(\d*)&/) == 0) {
                c = s.match(/^cols=(\d*)&(.*)/);
                s = c[2];
                c = c[1];
            }
            var r = '';
            if (   s.search(/^rows=(\d*)&/) == 0) {
                r = s.match(/^rows=(\d*)&(.*)/);
                s = r[2];
                r = r[1];
            }
            for (var j = idx + 1; j < thingIds.length; j++) {
                if (thingIds[idx] == thingIds[j]) {
                    s += '\n' + $(things[j]).text();
                } else break;
            }
            var e = $(document.createElement('textarea')).addClass('wikidialog-textarea').attr('id',thingIds[idx]).attr('cols',c).attr('rows',r).val(s);
            if ($(this).is('.wikidialog-hidden')) e.css('display','none');
            return e;
        });
        //
        // [[Template:Dialog/checkbox|checkbox]]
        //
        $('span.wikidialog-checkbox').replaceWith(function () {
            var i = $(this).attr('id');
            var s = $(this).text(); // .substring(0,32); // BOUND CHECKBOX CONTENT SIZE
            var result = $(document.createElement('input')).attr('type','checkbox').addClass('wikidialog-checkbox').attr('id',i);
            if (s.length > 8) result.attr('checked','yes');
            return result;
        });
        //
        // [[Template:Dialog/select|select]]
        //
        $('span.wikidialog-select').replaceWith(function () {
            var i = $(this).attr('id');
            var s = $(this).text().split('&'); // .substring(0,8192).split('&'); // BOUND SELECT CONTENT SIZE
            var e = document.createElement('select');
            for (var k=1; (k < s.length); k += 2) if (s[k-1]) {
                e.appendChild(new Option(s[k-1],s[k],false,false));
            }
            return $(e).addClass('wikidialog-select').attr('id',i);
        });
        //
        // [[Template:Dialog/button|button]]
        //
        $('span.wikidialog-button').replaceWith(function () {
            function getWikidialogID() {
                var s = location.search.match(/[\?&]wikidialogid=(\d+)/);
                return (s && s.length && (s.length > 1)) ? Number(s[1]) : 0;
            }
            function handleButton(event) {
                var target = $(event.target);
                var wikidialogButtonData = target.data('wikidialogButtonData');
                var action = wikidialogButtonData.action;
                if (action.substring(0,5).toLowerCase() == 'user:') {
                    var ug = mw.config.get('wgUserGroups');
                    if (!ug || ug.join(' ').indexOf("sysop") === -1) {
                        alert('action requires admin privs: ' + action);
                        return;
                    }
                } else {
                    action = mw.config.get('wgSiteName') + ':Dialog/' + action;
                }
                // action must be fully protected
                $('input.wikidialog-button').attr('disabled',true);
                $.getJSON(
                    mw.util.wikiScript('api'),
                    {
                        format:  'json',
                        action:  'query',
                        prop:    'info',
                        inprop:  'protection',
                        titles:  action
                    }
                ).done(
                    function(data) {
                        var s = '';
                        if (data.query && data.query.pages)
                            for (var p in data.query.pages) {
                                if ("missing" in data.query.pages[p]) s = 'page not found';
                                else if ('invalid' in data.query.pages[p]) s = 'page name invalid';
                                else if ('protection' in data.query.pages[p]) {
                                    if (data.query.pages[p].protection[0] && data.query.pages[p].protection[1]) {
                                        if (('level' in data.query.pages[p].protection[0]) &&
                                            ('level' in data.query.pages[p].protection[1])) {
                                            if ((data.query.pages[p].protection[0].level == 'sysop') &&
                                                (data.query.pages[p].protection[1].level == 'sysop')) {
                                            } else s = 'page unsecured';
                                        } else s = 'page query misfired';
                                    } else s = 'page unsecured';
                                } else s = 'page query misfired';
                            }
                        else s = 'page query failed';
                        if (s) {
                            $('input.wikidialog-button').attr('disabled',false);
                            alert(s + ': ' + action);
                            return;
                        }
                        // action is now known to be fully protected
                        // sessionStorage.wikidialogNextID was assured before setting up the button
                        var id = sessionStorage.wikidialogNextID;
                        s = mw.util.getUrl(action) + '?wikidialogid=' + id;
                        sessionStorage.wikidialogNextID++;
                        sessionStorage['wikidialog:' + id + ':incoming'] = s;
                        sessionStorage['wikidialog:' + id + ':prevurl'] = location.href; // provided for rollback
                        if ('unauth' in wikidialogButtonData) sessionStorage['wikidialog:' + id + ':unauth'] = wikidialogButtonData.unauth;
                        else                                  sessionStorage['wikidialog:' + id + ':unauth'] = mw.config.get('wgPageName');
                        if ('origin' in wikidialogButtonData) sessionStorage['wikidialog:' + id + ':origin'] = wikidialogButtonData.origin;
                        if ('proxy'  in wikidialogButtonData) sessionStorage['wikidialog:' + id + ':proxy']  = wikidialogButtonData.proxy;
                        sessionStorage['wikidialog:' + id + ':params'] = '';
                        for (var j = 0; j < wikidialogButtonData.length; j++) {
                            var d = wikidialogButtonData[j].match(/[\w-]+/g);
                            d[1] = (d[1] ? d[1] : d[0]);
                            d[1] = $('input#wikidialog-' + d[1] + ', textarea#wikidialog-' + d[1] + ', select#wikidialog-' + d[1]);
                            if (d[1].length == 0) {
                            } else {
                                sessionStorage['wikidialog:' + id + ':params'] += d[0] + ',';
                                var elt = d[1].first();
                                d[1] = d[1].add(target);
                                for (var k = 0; k < d[1].length; k++) {
                                    if ($(d[1][k]).hasClass('wikidialog-button')) {
                                        k++;
                                        if (k < d[1].length) elt = $(d[1][k]);
                                        break;
                                    }
                                }
                                switch (elt.attr('type')) {
                                  case 'checkbox':
                                    d[1] = (elt[0].checked ? 'yes' : '');
                                    break;
                                  default:
                                    d[1] = elt.val();
                                    break;
                                }
                                sessionStorage['wikidialog:' + id + ':in:' + d[0]] = d[1];
                            }
                        }
                        if (target.hasClass('wikidialog-rescript')) {
                            delete window.wikidialog;
                        }
                        sessionStorage.wikidialogSequenceBound = 10; // POINT TO ADJUST max action-sequence length
                                                                     //   also occurs in receive
                        location = s;
                    }
                ).fail(function() {
                        $('input.wikidialog-button').attr('disabled',false);
                        alert('action target check failed: ' + action);
                    });
            }
            if (! (sessionStorage)) { // allows handler to assume sessionStorage
                return "<font color=red>&lt;button requires sessionStorage (not found; perhaps your browser is out of date?)&gt;</font>";
            }
            if (sessionStorage.wikidialogNextID) {} else sessionStorage.wikidialogNextID = '1';
            if (Number(sessionStorage.wikidialogNextID) <= getWikidialogID()) {
                alert('Warning: session data may be inaccessible\n(dialog ID should be less than stored count:\n dialog ID is ' +
                    getWikidialogID() + '\n stored count is ' + sessionStorage.wikidialogNextID + ')');
                sessionStorage.wikidialogNextID = getWikidialogID() + 1;
            }
            var s = $(this).text(); // .substring(0,1024); // BOUND BUTTON CONTENT SIZE
            var a = '';
            if (   s.search(/^action=([\w\s-\/:]+)&/) == 0) {
                a = s.match(/^action=([\w\s-\/:]+)&(.*)/);
                s = a[2];
                a = a[1];
            }
            var b = '';
            if (   s.search(/^label=([\w\s-\/\:\(\)\.\,\;']+)&/) == 0) { // STUFF ALLOWED IN BUTTON LABELS
                b = s.match(/^label=([\w\s-\/\:\(\)\.\,\;']+)&(.*)/);   // STUFF ALLOWED IN BUTTON LABELS
                s = b[2];
                b = b[1];
            }
            var wikidialogButtonData = $(this).data();
            if ('wikidialogButtonData' in wikidialogButtonData)
                wikidialogButtonData = wikidialogButtonData.wikidialogButtonData;
            else wikidialogButtondata = {};
            wikidialogButtonData.action = a;
            wikidialogButtonData.length = 0;
            var s = s.match(/([\w-]+:?[\w-]*)&/g);
            if (s != null) {
                wikidialogButtonData.length = s.length;
                for (var j = 0; j < s.length; j++) {
                    wikidialogButtonData[j] = s[j].substring(0,s[j].length - 1);
                }
            } 
            return $(document.createElement('input')).attr('type','button').addClass(this.className).val(b).attr('title',a).data('wikidialogButtonData',wikidialogButtonData).click(handleButton);
        });
        //
        // [[Template:Dialog/ifsupported|ifsupported]]
        //
        if (sessionStorage) {
            $('.wikidialog-alternative').css('display','none');
            $('.wikidialog-dependent').css('display','inline');
        }
    };
}
window.wikidialog.show();