Jump to content

User:Haza-w.debug/Gadget-dropdown-menus.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Haza-w.debug (talk | contribs) at 15:39, 4 February 2009 (rm itabs). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*********************************************************************
**                ***WARNING: GLOBAL GADGET FILE***                 **
**         any changes to this file will affect many users          **
**          please discuss changes on the talk page or at           **
**             [[Wikipedia talk:Gadget]] before editing             **
**     (consider dropping the script author a note as well...)      **
**                                                                  **
**********************************************************************
**             Script:        Drop-down menus                       **
**            Version:        4.3g                                  **
**             Author:        Haza-w                                **
**      Documentation:        [[User:Haza-w/Drop-down menus]]       **
**                                                                  **
*********************************************************************/

// "Fail gracefully" if skin not supported
switch (skin) {
    case 'modern': case 'monobook':

    // Global variables
    var _cactions = {
        admin: wgUserGroups.join().indexOf('sysop') > -1 ? true : false,
        areqs: [],
        menus: [],
        mouse: null,
        pmenu: [],
        pname: encodeURIComponent(wgPageName),
        timer: []
    };
    
    // Process XMLHttpRequests
    function xhr(request,url,orsc) {
        with (request) {
            request.open('GET',url,true);
            request.onreadystatechange = orsc;
            request.send(null);
        }
    }
    
    // Find absolute position of element
    function findPos(obj,offset) {
        var pos = [0,0];
        do with (obj) {
            pos[0] += offsetLeft;
            pos[1] += offsetTop;
        } while (obj = obj.offsetParent);
        pos[0] += offset[0]; pos[1] += offset[1];
        return pos;
    }
    
    // Create menu div element
    function createMenu(mid,html) {
        var menu = document.createElement('div');
        with (menu) {
            id = mid,
            className = 'ca-menu',
            style.display = 'none',
            onmouseover = function () {showMenu(mid)},
            onmouseout = function () {hideMenu(mid)};
        }
        
        var elements = {
            ul: document.createElement('ul'),
            li: null,
            a: null,
            txt: null
        };
        with (elements) {
            for (var i = 0; i < html.length; i++) if (html[i].length) {
                li = document.createElement('li'); li.id = html[i][0];
                a = document.createElement('a'); a.href = html[i][2];
                txt = document.createTextNode(html[i][1]);
                a.appendChild(txt); li.appendChild(a); ul.appendChild(li);
            }
            menu.appendChild(ul);
        }
        
        document.body.appendChild(menu);
        
        return mid;
    }
    
    // Create cactions LI tab
    function createTab(cid,mid,ltxt) {
        var elements = {
            li: document.createElement('li'),
            a: document.createElement('a'),
            txt: document.createTextNode(ltxt)
        };
        with (elements) {
            li.id = cid,
            a.href = '#',
            a.onmouseover = function () {showMenu(mid,findPos(this,[-10,20]))},
            a.onmouseout = function () {hideMenu(mid)};
            a.appendChild(txt); li.appendChild(a);
        }
        
        return elements.li;
    }
    
    // CSS hide elements
    function hideElements(elements,conditionals) {
        if (typeof(conditionals) == 'undefined') {
            for (var i = 0; i < elements.length; i++) if (document.getElementById(elements[i])) document.getElementById(elements[i]).style.display = 'none';
        }
        else for (var i = 0; i < elements.length; i++) if (document.getElementById(elements[i])) {
            document.getElementById(elements[i]).style.display = 'none';
            if (conditionals[i]) document.getElementById(conditionals[i]).style.display = 'none';
        }
    }
    
    // Show/hide menu functions
    function showMenu(mid,pos) {
        with (_cactions) {
            mouse = mid;
            if (pos) for (var i = 0; i < menus.length; i++) {
                if (timer[menus[i]]) {
                    clearTimeout(timer[menus[i]]);
                    timer[menus[i]] = null;
                }
                if (pmenu[mid] == menus[i]) continue;
                document.getElementById(menus[i]).style.display = 'none';
            }
            if (!timer[mid]) with (document.getElementById(mid).style) {
                display = '';
                if (pos) {
                    left = pos[0]+'px',
                    top = pos[1]+'px';
                }
            }
            else {
                clearTimeout(timer[mid]);
                timer[mid] = null;
            }
        }
    }
    function hideMenu(mid) {
        with (_cactions) {
            if (mid == pmenu[mouse]) timer[mid] = null;
            
            if (timer[mid]) {
                timer[mid] = null;
                document.getElementById(mid).style.display = 'none';
                if (mid == mouse && pmenu[mid]) document.getElementById(pmenu[mid]).style.display = 'none';
            }
            else timer[mid] = setTimeout('hideMenu(\''+mid+'\');',400);
        }
    }
    
    // Delink element
    function removeLink(eid) {
        var element = document.getElementById(eid);
        if (!element.getElementsByTagName('a').length) return false;
        
        var a = element.getElementsByTagName('a')[0];
        element.appendChild(a.removeChild(a.firstChild));
        element.removeChild(a);
        
        element.className = 'ca-disabled';
    }
    
    // User options hook
    addOnloadHook(function () {
        switch (wgNamespaceNumber) {
            case 2: case 3: _cactions['uname'] = encodeURIComponent(wgTitle.split('/')[0].replace(/ /g,'_'));
        }
        if (wgCanonicalSpecialPageName == 'Contributions') for (var i = 0, hl; hl = document.getElementById('contentSub').getElementsByTagName('a')[i]; i++) {
            if (hl.href.indexOf('user=') > -1) {
                _cactions['uname'] = hl.href.split('user=')[1].split('&amp;')[0];
                break;
            }
        }
    
        if (_cactions.uname) {
            with (_cactions) {
                menus[menus.length] = createMenu('opt-user',Array(
                    ['c-ulogs',         'User logs >',      '#']                                                                                                ,
                    ['c-ublocks',       'Blocks >',         '#']                                                                                                ,
                    ['c-contribs',      'Contributions',    wgScript+'?title=Special:Contributions/'+uname+'&action=view']                                      ,
                    ['c-editcount',     'Edit count',       'http://toolserver.org/~interiot/cgi-bin/Tool1/wannabe_kate?site=en.wikipedia.org&username='+uname] ,
                    ['c-mathbot',       'Edit summaries',   'http://toolserver.org/~mathbot/cgi-bin/wp/rfa/edit_summary.cgi?lang=en&user='+uname]               ,
                    ['c-wcuser',        'Edit analysis',    'http://en.wikichecker.com/user/?l=all&t='+uname]                                                   ,
                    ['c-subpages',      'Userspace',        wgScript+'?title=Special:PrefixIndex&action=view&from='+uname+'/&namespace=2']                      ,
                    ['c-email',         'E-mail user',      wgScript+'?title=Special:EmailUser/'+uname+'&action=view']                                          ,
                    ['c-groups',        'User groups',      wgScript+'?title=Special:ListUsers&action=view&limit=1&username='+uname]                            ,
                    ['c-rightslog',     'Rights changes',   wgScript+'?title=Special:Log&action=view&type=rights&page=User:'+uname]
                ));
                
                menus[menus.length] = createMenu('opt-ulogs',Array(
                    ['c-ulogs',         'All user logs',    wgScript+'?title=Special:Log&action=view&user='+uname]              ,
                    ['c-blocks',        'Blocks',           wgScript+'?title=Special:Log&action=view&type=block&user='+uname]   ,
                    ['c-moves',         'Moves',            wgScript+'?title=Special:Log&action=view&type=move&user='+uname]    ,
                    ['c-patrols',       'Patrols',          wgScript+'?title=Special:Log&action=view&type=patrol&user='+uname]  ,
                    ['c-protects',      'Protections',      wgScript+'?title=Special:Log&action=view&type=protect&user='+uname] ,
                    ['c-uploads',       'Uploads',          wgScript+'?title=Special:Log&action=view&type=upload&user='+uname]  ,
                    ['c-rights',        'User rights',      wgScript+'?title=Special:Log&action=view&type=rights&user='+uname]
                ));
                pmenu['opt-ulogs'] = 'opt-user';
                
                menus[menus.length] = createMenu('opt-ublocks',Array(
                    admin?          ['c-block',         'Block user',       wgScript+'?title=Special:BlockIP/'+uname+'&action=view']            :[] ,
                    admin?          ['c-unblock',       'Unblock user',     wgScript+'?title=Special:IPBlockList&action=unblock&ip='+uname]     :[] ,
                                    ['c-ipblock',       'View block',       wgScript+'?title=Special:IPBlockList&action=view&ip='+uname]            ,
                                    ['c-blocklog',      'Block log',        wgScript+'?title=Special:Log&action=view&type=block&page=User:'+uname]
                ));
                pmenu['opt-ublocks'] = 'opt-user';
                
                document.getElementById('p-cactions').getElementsByTagName('div')[0].getElementsByTagName('ul')[0].appendChild(createTab('ca-user','opt-user','User'));
                
                if (sajax_init_object() && wgEnableAPI) {
                    if (uname.search(/(?:\d{1,3}\.){3}\d{1,3}/) == 0) {
                        areqs['ip'] = new sajax_init_object();
                        xhr(areqs['ip'],wgScriptPath+'/api.php?format=json&action=query&list=blocks&bkusers='+uname+'&bkprop=id&xhr='+Math.random(),function () {
                            with (areqs['ip']) if (readyState == 4 && status == 200) {
                                var api = eval('('+responseText+')');
                                if (api.query.blocks.length) {
                                    hideElements(['c-block']);
                                    document.getElementById('c-ipblock').getElementsByTagName('a')[0].style.color = '#EE1111';
                                }
                                else {
                                    hideElements(['c-unblock']);
                                    removeLink('c-ipblock');
                                }
                            }
                        } );
                    }
                    else {
                        areqs['user'] = new sajax_init_object();
                        xhr(areqs['user'],wgScriptPath+'/api.php?format=json&action=query&list=users&ususers='+uname+'&usprop=blockinfo|groups&xhr='+Math.random(),function () {
                            with (areqs['user']) if (readyState == 4 && status == 200) {
                                var api = eval('('+responseText+')');
                                with (api.query.users[0]) {
                                    if (typeof(blockedby) != 'undefined') {
                                        hideElements(['c-block']);
                                        document.getElementById('c-ipblock').getElementsByTagName('a')[0].style.color = '#EE1111';
                                    }
                                    else {
                                        hideElements(['c-unblock']);
                                        removeLink('c-ipblock');
                                    }
                                    
                                    if (typeof(groups) == 'undefined') hideElements(['c-blocks','c-deletes','c-protects','c-rights']);
                                    else if (groups.join().indexOf('sysop') == -1) hideElements(['c-blocks','c-deletes','c-protects','c-rights']);
                                    
                                    if (typeof(missing) != 'undefined') hideElements(['ca-user']);
                                }
                            }
                        } );
                        
                        areqs['uspace'] = new sajax_init_object();
                        xhr(areqs['uspace'],wgScriptPath+'/api.php?format=json&action=query&list=allpages&apprefix='+uname+'%2F&apnamespace=2&aplimit=1&xhr='+Math.random(),function () {
                            with (areqs['uspace']) if (readyState == 4 && status == 200) {
                                var api = eval('('+responseText+')');
                                if (!api.query.allpages.length) removeLink('c-subpages');
                            }
                        } );
                    }
                    
                    areqs['ublocks'] = new sajax_init_object();
                    xhr(areqs['ublocks'],wgScriptPath+'/api.php?format=json&action=query&list=logevents&letype=block&letitle=User:'+uname+'&lelimit=1&xhr='+Math.random(),function () {
                        with (areqs['ublocks']) if (readyState == 4 && status == 200) {
                            var api = eval('('+responseText+')');
                            if (!api.query.logevents.length) removeLink('c-blocklog');
                        }
                    } );
                }
                else hideElements(['c-ipblock','c-blocks','c-deletes','c-protects','c-rights']);
            }
            
            hideElements(['t-contributions','t-log','t-emailuser']);
            
            with (document.getElementById('c-ulogs')) {
                onmouseover = function () {showMenu('opt-ulogs',findPos(this,[40,0]));};
                onmouseout = function () {hideMenu('opt-ulogs');};
                style.fontWeight = 'bold';
            }
            
            with (document.getElementById('c-ublocks')) {
                onmouseover = function () {showMenu('opt-ublocks',findPos(this,[40,0]));};
                onmouseout = function () {hideMenu('opt-ublocks');};
                style.fontWeight = 'bold';
            }
            
            if (_cactions.uname.search(/(?:\d{1,3}\.){3}\d{1,3}/) == 0) hideElements(['c-ulogs','c-editcount','c-mathbot','c-wcuser','c-subpages','c-email','c-groups','c-rightslog']);
        }
    } );
    
    // Page options hook
    addOnloadHook(function () {
        if (!wgCanonicalSpecialPageName) {
            with (_cactions) {
                menus[menus.length] = createMenu('opt-page',Array(
                                    ['c-plogs',         'Page logs >',      '#'],
                    wgArticleId?    ['c-history',       'History',          wgScript+'?title='+pname+'&action=history']                     :[] ,
                    wgArticleId?    ['c-move',          'Move page',        wgScript+'?title=Special:Movepage/'+pname+'&action=view']       :[] ,
                                    ['c-watch',         'Watch page',       wgScript+'?title='+pname+'&action=watch']                           ,
                                    ['c-unwatch',       'Unwatch page',     wgScript+'?title='+pname+'&action=unwatch']                         ,
                    admin?          ['c-protect',       'Protect page',     wgScript+'?title='+pname+'&action=protect']                     :[] ,
                    admin?          ['c-unprotect',     'Unprotect page',   wgScript+'?title='+pname+'&action=unprotect']                   :[] ,
                    admin?          ['c-delete',        'Delete page',      wgScript+'?title='+pname+'&action=delete']                      :[] ,
                    admin?          ['c-undelete',      'Undelete page',    wgScript+'?title=Special:Undelete/'+pname+'&action=view']       :[] ,
                    wgArticleId?    ['c-diff',          'Latest diff',      wgScript+'?title='+pname+'&action=view&diff='+wgCurRevisionId]  :[] ,
                    wgArticleId?    ['c-editzero',      'Edit intro',       wgScript+'?title='+pname+'&action=edit&section=0']              :[] ,
                    wgArticleId?    ['c-wcpage',        'Page analysis',    'http://en.wikichecker.com/article/?a='+pname]                  :[] ,
                                    ['c-purge',         'Purge cache',      wgScript+'?title='+pname+'&action=purge']
                ));
    
                menus[menus.length] = createMenu('opt-plogs',Array(
                                                ['c-plogs',         'All page logs',    wgScript+'?title=Special:Log&action=view&page='+pname]                  ,
                                                ['c-deletelog',     'Deletion log',     wgScript+'?title=Special:Log&type=delete&page='+pname]                  ,
                                                ['c-movelog',       'Move log',         wgScript+'?title=Special:Log&action=view&type=move&page='+pname]        ,
                    wgArticleId?                ['c-patrollog',     'Patrol log',       wgScript+'?title=Special:Log&action=view&type=patrol&page='+pname]  :[] ,
                                                ['c-protectlog',    'Protection log',   wgScript+'?title=Special:Log&action=view&type=protect&page='+pname]     ,
                    wgNamespaceNumber == 6?     ['c-uploadlog',     'Upload log',       wgScript+'?title=Special:Log&action=view&type=upload&page='+pname]  :[]
                ));
                pmenu['opt-plogs'] = 'opt-page';
                
                document.getElementById('p-cactions').getElementsByTagName('div')[0].getElementsByTagName('ul')[0].appendChild(createTab('ca-page','opt-page','Page'));
            }
            
            hideElements(['ca-watch','ca-unwatch','ca-protect','ca-unprotect','ca-delete','ca-undelete','ca-history','ca-move'],['c-unwatch','c-watch','c-unprotect','c-protect','c-undelete','c-delete']);
                
            with (document.getElementById('c-plogs')) {
                onmouseover = function () {showMenu('opt-plogs',findPos(this,[40,0]));};
                onmouseout = function () {hideMenu('opt-plogs');};
                style.fontWeight = 'bold';
            }
        }
    } );
}