MediaWiki:Common.js

出自【暗黑百科】DiabloWiki_凯恩之角

(修订版本间的差异)
跳转到: 导航, 搜索
第1行: 第1行:
-
/* 辅助处理 */
+
//<source lang="javascript">
-
    /* 1. 功能設定 */
+
/* Any JavaScript here will be loaded for all users on every page load. */
-
    window.JSConfig = window.JSconfig || {};
+
 
-
    window.JSConfig.collapseText = wgULS('隐藏▲', '隱藏▲'); //指示折叠收缩的默认文字
+
/* Test if an element has a certain class **************************************
-
    window.JSConfig.expandText = wgULS('显示▼', '顯示▼'); //指示折叠展开的默认文字
+
*
-
    window.JSConfig.autoCollapse = 2; //文章少于 autoCollapse 个折叠块时,不自动折叠
+
* Description: Uses regular expressions and caching for better performance.
-
    // window.JSConfig.SpecialSearchEnhancedDisabled=false; //是否禁止增加其它搜索引擎
+
* Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
-
    /* 2. 用jQuery实现的getElementsByClassName(需不需要返回DOM对象?) */
+
*/
-
    window.getElementsByClassName = function (elm, tag, className) {
+
-
        return $(tag + '.' + className, elm);
+
-
    };
+
   
   
-
    /* 3. 遍历 */
+
var hasClass = (function () {
-
    window.applyEach = function (callback, array) {
+
    var reCache = {};
-
        var i = 0,
+
     return function (element, className) {
-
            j = array.length;
+
         return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
-
        while (i < j) {
+
-
            callback(array[i++]);
+
-
        }
+
-
    };
+
-
+
-
    /* 4. 移動元素 */
+
-
    window.elementMoveto = function (node, refNode, pos) { //默认位置为refNode前
+
-
        if (node && refNode) {
+
-
            if (pos && pos == 'after') {
+
-
                $(refNode).after(node);
+
-
            } else {
+
-
                $(refNode).before(node);
+
-
            }
+
-
        }
+
-
     };
+
-
+
-
    /* 5. 创建元素 */
+
-
    window.createElement = function (tag, children, props) {
+
-
         var element = document.createElement(tag);
+
-
        if (!(children instanceof Array)) {
+
-
            children = [children];
+
-
        }
+
-
        applyEach(function (child) {
+
-
            if (typeof child == 'string') {
+
-
                child = document.createTextNode(child);
+
-
            }
+
-
            if (child) {
+
-
                element.appendChild(child);
+
-
            }
+
-
        }, children);
+
-
        if (typeof props == 'object') {
+
-
            for (var k in props) {
+
-
                switch (k) {
+
-
                case 'styles':
+
-
                    var styles = props.styles;
+
-
                    for (var s in styles) {
+
-
                        element.style[s] = styles[s];
+
-
                    }
+
-
                    break;
+
-
                case 'events':
+
-
                    var events = props.events;
+
-
                    for (var e in events) {
+
-
                        addHandler(element, e, events[e]);
+
-
                    }
+
-
                    break;
+
-
                case 'class':
+
-
                    element.className = props[k];
+
-
                    break;
+
-
                default:
+
-
                    element.setAttribute(k, props[k]);
+
-
                }
+
-
            }
+
-
        }
+
-
        return element;
+
     };
     };
 +
})();
-
/*
 
-
==定期強制用戶更新cache==
 
-
*/
+
/** Collapsible tables *********************************************************
 +
*
 +
*  Description: Allows tables to be collapsed, showing only the header. See
-
var today=new Date();
+
*              [[Wikipedia:NavFrame]].
 +
*  Maintainers: [[User:R. Koot]]
 +
*/
-
if( (""+wgUserGroups).indexOf("user")!=-1 && (wgAction=="view") ){
+
var autoCollapse = 2;
-
if(!getCookie("lastload") || isNaN(getCookie("lastload")) ){
+
var collapseCaption = "隐藏▲";
-
  setCookie("lastload", today.getTime());
+
var expandCaption = "显示▼";
-
}else{
+
-
  if( today.getTime()-getCookie("lastload") >= 7*24*60*60*1000 ){
+
-
  setCookie("lastload", today.getTime());
+
function collapseTable( tableIndex )
-
  if( getCookie("lastload")==today.getTime() ) window.location.reload(true);
+
{
-
  }else if( today.getTime()-getCookie("lastload") < 0 ){
+
    var Button = document.getElementById( "collapseButton" + tableIndex );
-
  setCookie("lastload", today.getTime());
+
-
  }
+
-
}
+
    var Table = document.getElementById( "collapsibleTable" + tableIndex );
-
}
+
 +
    if ( !Table || !Button ) {
 +
        return false;
 +
    }
 +
 +
    var Rows = Table.rows;
 +
 +
    if ( Button.firstChild.data == collapseCaption ) {
-
if( (""+wgUserGroups).indexOf("user")!=-1 && (wgAction=="edit") ){
+
        for ( var i = 1; i < Rows.length; i++ ) {
 +
            Rows[i].oldDisplayValue = Rows[i].style.display;
 +
            Rows[i].style.display = "none";
-
if(!getCookie("editlastload") || isNaN(getCookie("editlastload")) ){
+
        }
-
  setCookie("editlastload", today.getTime());
+
        Button.firstChild.data = expandCaption;
-
}else{
+
    } else {
-
  if( today.getTime()-getCookie("editlastload") >= 7*24*60*60*1000 ){
+
        for ( var i = 1; i < Rows.length; i++ ) {
-
  setCookie("editlastload", today.getTime());
+
-
  if( getCookie("editlastload")==today.getTime() ) window.location.reload(true);
+
-
  }else if( today.getTime()-getCookie("editlastload") < 0 ){
+
-
  setCookie("editlastload", today.getTime());
+
-
  }
+
-
}
+
            Rows[i].style.display = Rows[i].oldDisplayValue;
 +
 
 +
        }
 +
        Button.firstChild.data = collapseCaption;
 +
    }
}
}
-
/*  
+
   
-
* ImportScriptHttp.模块
+
function createCollapseButtons()
-
* 版本: r1
+
{
-
*/
+
    var tableIndex = 0;
-
eval(function(p,a,c,k,e,d){e=function(c){return(c<a?'':e(parseInt(c/a)))+((c=c%a)>35?String.fromCharCode(c+29):c.toString(36))};if(!''.replace(/^/,String)){while(c--){d[e(c)]=k[c]||e(c)}k=[function(e){return d[e]}];e=function(){return'\\w+'};c=1};while(c--){if(k[c]){p=p.replace(new RegExp('\\b'+e(c)+'\\b','g'),k[c])}}return p}('(7(){3(c.2!=S)8;2=k W();c.2=2;2.d={};2.C=7(9,6){3(2.d[9]){8(a)}2.d[9]=z;7 y(){f A;q{A=k 17()}o(e){q{A=k Q("15.P")}o(e){q{A=k Q("11.P")}o(14){A=a}}}3(!A)E("13 16 19 10 18.");8 A}f x=y();x.j=7(){3(5.N==4){3(5.n==1a||5.n==X){3((5.B!=a)){f s=l.u("v");s.U="i";s.G="h/i";s.T=p;s.h=5.B;l.J(\'L\')[0].M(s);3(6)6()}}I{E(\'Y Z V: \'+5.12+\' (\'+5.n+\')\');8(p)}5.j=a}};x.1g(\'1s\',9,p);x.1r(a);8(x)};c.t=2.C;7 m(b){f w=1q+\'?1u=\'+1t(b.r(/ /g,\'1w\')).r(\'%1v\',\'/\').r(\'%1p\',\':\')+\'&1n=1o&1b=h/i\';8(w)}2.F=7(b,6){8(t(m(b,6)))};c.1f=2.F;2.O=7(9,6){3(2.d[9])8 a;2.d[9]=z;f s=l.u(\'v\');s.1e=\'1c\';s.K(\'1d\',9);s.K(\'G\',\'h/i\');3(6){3(1h.1i.1m("1l")!=-1)s.j=(7(){3(5.N==\'1k\'){5.j=a;6()}});I s.H=(7(){5.H=a;6})}l.J(\'L\')[0].M(s);8 s};c.R=2.O;2.D=7(b,6){8(R(m(b),6))};c.1j=2.D})();',62,95,'||ImportScriptEx|if||this|callback|function|return|url|null|page|window|loadedScripts||var||text|javascript|onreadystatechange|new|document|ImportScriptExMakePageURI|status|catch|false|try|replace||ImportScriptExHttpURI|createElement|script|uri||MakeHttpRequest|true||responseText|HttpURI|Page|alert|HttpPage|type|onload|else|getElementsByTagName|setAttribute|head|appendChild|readyState|URI|XMLHTTP|ActiveXObject|ImportScriptExURI|undefined|defer|language|error|Object|304|XML|request|connection|Microsoft|statusText|Could|oc|Msxml2|not|XMLHttpRequest|object|create|200|ctype|googlejsapi|src|id|ImportScriptExHttpPage|open|navigator|userAgent|ImportScriptExPage|complete|MSIE|indexOf|action|raw|3A|wgScript|send|GET|encodeURIComponent|title|2F|_'.split('|'),0,{}))
+
-
/*  ImportScriptHttp.模块,结束  */
+
-
//var googlejsapiurl='http://www.google.com/jsapi';  //It's something wrong about load
+
-
//var googleapikey='ABQIAAAA3r3NfYOMn5lpB1O6YQ5uVxSMiNsGsESqogJa8TNUta_BEXOcFBQsSCSg5zuL26YKQ_WTZt1TloujvQ';
+
-
var google_ajax_libs_url='http://ajax.googleapis.com/ajax/libs/';
+
-
var jQueryVer='1.4.2';
+
-
var jQueryUrl=google_ajax_libs_url+'jquery/'+jQueryVer+'/jquery.min.js';
+
-
ImportScriptExURI('http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js',(function(){//load jQuery start
+
    var NavigationBoxes = new Object();
-
if (window.jQuery === undefined)
+
    var Tables = document.getElementsByTagName( "table" );
-
ImportScriptExHttpPage('MediaWiki:JQuery.1.5.2.min.js');
+
-
if (window.jQuery === undefined)
+
-
console.log('Failed to load jQuery!');
+
-
//load jQuery end;
+
-
//begining to use jQuery function
+
-
// addHandler in wikibits.js has some problems? overwrite it here:
+
-
function addHandler( element, attach, handler ) {
+
    for ( var i = 0; i < Tables.length; i++ ) {
-
    $(element).bind(attach, handler);
+
        var parent = Tables[i].parentNode.parentNode.parentNode.parentNode;
-
}
+
        if ( hasClass( Tables[i], "collapsible" ) || (hasClass(parent, "navbox") && hasClass(Tables[i], "navbox")) ) {
-
}));//end jquery
+
-
/* 测试元素中是否含有指定的样式 **************************************
+
-
* Description: 使用正则式与缓存来提高性能
+
            /* only add button and increment count if there is a header row to work with */
-
* Maintainers: User:fdcn @zh.wikipedia
+
            var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
-
*              [[en:User:Mike Dillon]], [[en:User:R. Koot]], [[en:User:SG]] @en.wikipedia
+
-
*/
+
-
var hasClass = (function () {
+
-
    var reCache = {};
+
-
    return function (element, className) {
+
-
        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
+
-
    };
+
-
})();
+
-
/*
+
            if (!HeaderRow) continue;
 +
            var Header = HeaderRow.getElementsByTagName( "th" )[0];
-
== IE兼容性修正 ==
+
            if (!Header) continue;
-
*/
+
   
-
function fixIE(){
+
            NavigationBoxes[ tableIndex ] = Tables[i];
-
}
+
-
if (navigator.appName == "Microsoft Internet Explorer"){
+
-
    /** Internet Explorer bug fix **************************************************
+
-
    *
+
-
    * Description: Fixes IE horizontal scrollbar bug
+
-
    *  Maintainers: [[User:fdcn]]
+
-
    */
+
-
    var oldWidth;
+
-
    var docEl = document.documentElement;
+
-
    function fixIEScroll(){
+
-
        if (!oldWidth || docEl.clientWidth > oldWidth){
+
-
            doFixIEScroll();
+
-
        }else{
+
-
            setTimeout(doFixIEScroll, 1);
+
-
        }
+
-
        oldWidth = docEl.clientWidth;
+
-
    }
+
-
    function doFixIEScroll() {
+
            Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
-
        docEl.style.overflowX = (docEl.scrollWidth - docEl.clientWidth < 4) ? "hidden" : "";
+
-
    }
+
-
     document.attachEvent("onreadystatechange", fixIEScroll);
+
-
    attachEvent("onresize", fixIEScroll);
+
            var Button     = document.createElement( "span" );
-
    //Import scripts specific to Internet Explorer 6
+
            var ButtonLink = document.createElement( "a" );
-
    if (navigator.appVersion.substr(22, 1) == "6"){
+
 
-
        importScript("MediaWiki:Common.js/IE60Fixes.js")
+
            var ButtonText = document.createTextNode( collapseCaption );
-
    }
+
-
}
+
-
/*
 
   
   
-
== 增加摺疊功能 ==
+
            Button.className = "collapseButton";  //Styles are declared in Common.css
-
*/
+
 
-
    /** 摺疊 div table *****************************
+
-
    *  Description: 实现div.NavFrame和table.collapsible的可折叠性。
+
-
    *  JSConfig的collapseText、expandText、autoCollapse属性定义默认文字和默认最少自动折叠块
+
-
    *  Maintainers: User:fdcn
+
-
    */
+
   
   
-
    function cancelBubble(e) {
+
            ButtonLink.style.color = Header.style.color;
-
        e = e || window.event;
+
            ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
-
        if (e.stopPropagation) {
+
 
-
             e.stopPropagation();
+
             ButtonLink.setAttribute( "href", "javascript:collapseTable(" + tableIndex + ");" );
-
        } else {
+
 
-
             e.cancelBubble = true;
+
             ButtonLink.appendChild( ButtonText );
-
        }
+
-
    }
+
   
   
-
    function createToggleButton(head) {
+
            Button.appendChild( document.createTextNode( "[" ) );
-
        var parent = head;
+
 
-
        if (head.tagName.toLowerCase() == 'tr') { //对表格特别处理
+
            Button.appendChild( ButtonLink );
-
             if (head.getElementsByTagName("th").length) {
+
             Button.appendChild( document.createTextNode( "]" ) );
-
                parent = head.cells[parent.cells.length - 1];
+
 
-
            } else {
+
-
                return;
+
            Header.insertBefore( Button, Header.childNodes[0] );
-
            }
+
 
 +
            tableIndex++;
         }
         }
-
        var textS, textH, button = getElementsByClassName(head, "span", "NavToggle")[0];
 
-
        if (button) {
 
-
            parent = button.parentNode;
 
-
        } else {
 
-
            textS = createElement("span", [JSConfig.expandText], {
 
-
                'class': 'toggleShow'
 
-
            });
 
-
            textH = createElement("span", [JSConfig.collapseText], {
 
-
                'class': 'toggleHide'
 
-
            });
 
-
            button = createElement("span", [textS, textH], {
 
-
                'class': 'NavToggle',
 
-
                styles: {
 
-
                    'width': "6em"
 
-
                }
 
-
            });
 
-
        }
 
-
        button.style.display = "inline";
 
-
        head.className += " uncollapse toggleHotspot";
 
-
        parent.insertBefore(button, parent.childNodes[0]);
 
     }
     }
-
    window.wgCollapse = function (head, container, defaultCollapse) {
 
-
        if (head) {
 
-
            createToggleButton(head);
 
-
        }
 
-
        var self = this;
 
-
        this.state = 0;
 
-
        this.container = container;
 
-
        applyEach(function (h) {
 
-
            if (h.nodeType == 1 && !hasClass(h, "uncollapse") && !hasClass(h, "toggleShow") && !hasClass(h, "toggleHide")) {
 
-
                h.className += " toggleHide";
 
-
            }
 
-
        }, defaultCollapse); //预设的隐藏元素
 
   
   
-
+
    for ( var i = 0;  i < tableIndex; i++ ) {
-
        function getArray(clsname) {
+
 
-
            var r = [],
+
        if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
-
                i = 0,
+
 
-
                e, ea = getElementsByClassName(container, "*", clsname);
+
            collapseTable( i );
-
            while (e = ea[i++]) {
+
        }
-
                var parent = e.parentNode;
+
        else if ( hasClass( NavigationBoxes[i], "innercollapse" ) ) {
-
                while (!hasClass(parent, 'NavFrame') && !hasClass(parent, 'collapsible')) {
+
 
-
                    parent = parent.parentNode;
+
            var element = NavigationBoxes[i];
-
                }
+
            while (element = element.parentNode) {
-
                 if (parent == container) {
+
 
-
                     r.push(e);
+
                 if ( hasClass( element, "outercollapse" ) ) {
 +
 
 +
                     collapseTable ( i );
 +
                    break;
                 }
                 }
 +
             }
             }
-
            return r;
 
         }
         }
-
        var toggleA = getArray("toggleShow");
+
    }
-
        var toggleB = getArray("toggleHide");
+
}
-
        var hotspots = getArray("toggleHotspot");
+
   
   
-
        function _toggle(list, state) {
+
addOnloadHook( createCollapseButtons );
-
            var i = 0,
+
//</source>
-
                e;
+
-
            while (e = list[i++]) {
+
-
                e.style.display = state ? e.showStyle || '' : 'none';
+
-
            }
+
-
        }
+
-
        this.toggle = function (state) {
+
-
            self.state = (typeof state == 'undefined') ? 1 - self.state : state;
+
-
            _toggle(toggleA, self.state);
+
-
            _toggle(toggleB, 1 - self.state);
+
-
        }
+
-
        var i = 0,
+
-
            h;
+
-
        while (h = hotspots[i++]) {
+
-
            applyEach(function (link) {
+
-
                addClickHandler(link, cancelBubble);
+
-
            }, h.getElementsByTagName("A"));
+
-
            h.style.cursor = "pointer";
+
-
            $(h).attr('tabindex', '0').keydown(function (event) {
+
-
                if (event.which == 13) { // Enter
+
-
                    self.toggle();
+
-
                }
+
-
            });
+
-
            addClickHandler(h, function () {
+
-
                self.toggle();
+
-
            });
+
-
        }
+
-
    };
+
-
+
-
    $(function () {
+
-
        if (!window.disableCollapse) {
+
-
            //init
+
-
            var items = [];
+
-
            applyEach(function (NavFrame) {
+
-
                var i = 0,
+
-
                    child = NavFrame.childNodes,
+
-
                    head;
+
-
                while (head = child[i++]) {
+
-
                    if (head.className && hasClass(head, "NavHead")) {
+
-
                        break;
+
-
                    }
+
-
                }
+
-
                items.push(new wgCollapse(head, NavFrame, NavFrame.childNodes));
+
-
            }, getElementsByClassName(document, "div", "NavFrame"));
+
-
            applyEach(function (table) {
+
-
                var rows = table.rows;
+
-
                items.push(new wgCollapse(rows[0], table, rows));
+
-
            }, getElementsByClassName(document, "table", "collapsible"));
+
-
            var item, i = 0,
+
-
                count = items.length;
+
-
            while (item = items[i++]) {
+
-
                item.toggle(
+
-
                hasClass(item.container, "collapsed") || (count >= JSConfig.autoCollapse && hasClass(item.container, "autocollapse")));
+
-
            }
+
-
        }
+
-
    });
+
-
    //修正摺疊後定位變化
+
-
    hookEvent("load", function () {
+
-
        if (location.hash) {
+
-
            location.href = location.hash;
+
-
        }
+
-
    });
+

在2011年5月16日 (一) 20:53所做的修订版本

//<source lang="javascript">
/* Any JavaScript here will be loaded for all users on every page load. */

/* Test if an element has a certain class **************************************
 *
 * Description: Uses regular expressions and caching for better performance.
 * Maintainers: [[User:Mike Dillon]], [[User:R. Koot]], [[User:SG]]
 */
 
var hasClass = (function () {
    var reCache = {};
    return function (element, className) {
        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
    };
})();


/** Collapsible tables *********************************************************
 *
 *  Description: Allows tables to be collapsed, showing only the header. See

 *               [[Wikipedia:NavFrame]].
 *  Maintainers: [[User:R. Koot]]
 */

var autoCollapse = 2;

var collapseCaption = "隐藏▲";
var expandCaption = "显示▼";

 
function collapseTable( tableIndex )
{
    var Button = document.getElementById( "collapseButton" + tableIndex );

    var Table = document.getElementById( "collapsibleTable" + tableIndex );

 
    if ( !Table || !Button ) {
        return false;

    }
 
    var Rows = Table.rows;
 
    if ( Button.firstChild.data == collapseCaption ) {

        for ( var i = 1; i < Rows.length; i++ ) {
            Rows[i].oldDisplayValue = Rows[i].style.display;
            Rows[i].style.display = "none";

        }
        Button.firstChild.data = expandCaption;
    } else {

        for ( var i = 1; i < Rows.length; i++ ) {

            Rows[i].style.display = Rows[i].oldDisplayValue;

        }
        Button.firstChild.data = collapseCaption;
    }

}
 
function createCollapseButtons()
{
    var tableIndex = 0;

    var NavigationBoxes = new Object();
    var Tables = document.getElementsByTagName( "table" );

 
    for ( var i = 0; i < Tables.length; i++ ) {
        var parent = Tables[i].parentNode.parentNode.parentNode.parentNode;
        if ( hasClass( Tables[i], "collapsible" ) || (hasClass(parent, "navbox") && hasClass(Tables[i], "navbox")) ) {

 
            /* only add button and increment count if there is a header row to work with */
            var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];

            if (!HeaderRow) continue;
            var Header = HeaderRow.getElementsByTagName( "th" )[0];

            if (!Header) continue;
 
            NavigationBoxes[ tableIndex ] = Tables[i];

            Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );

 
            var Button     = document.createElement( "span" );

            var ButtonLink = document.createElement( "a" );

            var ButtonText = document.createTextNode( collapseCaption );

 
            Button.className = "collapseButton";  //Styles are declared in Common.css

 
            ButtonLink.style.color = Header.style.color;
            ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );

            ButtonLink.setAttribute( "href", "javascript:collapseTable(" + tableIndex + ");" );

            ButtonLink.appendChild( ButtonText );
 
            Button.appendChild( document.createTextNode( "[" ) );

            Button.appendChild( ButtonLink );
            Button.appendChild( document.createTextNode( "]" ) );

 
            Header.insertBefore( Button, Header.childNodes[0] );

            tableIndex++;
        }
    }
 
    for ( var i = 0;  i < tableIndex; i++ ) {

        if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {

            collapseTable( i );
        } 
        else if ( hasClass( NavigationBoxes[i], "innercollapse" ) ) {

            var element = NavigationBoxes[i];
            while (element = element.parentNode) {

                if ( hasClass( element, "outercollapse" ) ) {

                    collapseTable ( i );
                    break;
                }

            }
        }
    }
}
 
addOnloadHook( createCollapseButtons );
//</source>
个人工具
分享到: 更多
名字空间
变换
动作
网站地图
暗黑破坏神 III
暗黑破坏神 II
暗黑破坏神
工具箱
编辑箱
关于暗黑百科