User:Waihorace/userpage.js

出自維基百科,自由嘅百科全書
注意:儲存之後,你可能要兜過你嘅瀏覽器快取至睇到更改。Internet Explorer: 撳住Ctrl掣再撳重新整理掣。 Firefox: 撳住Shift掣再撳重新載入(又或者撳Ctrl-Shift-R)。 Google Chrome同埋Safari用戶就噉撳個重載掣。
//<source lang="javascript">
/*
 
== 增加摺疊功能 ==
*/
/** 摺疊 div table *****************************
 *  Description: 实现div.NavFrame和table.collapsible的可折叠性。
 *  JSConfig的collapseText、expandText、autoCollapse属性定义默认文字和默认最少自动折叠块
 *  Maintainers: User:fdcn
 */
function cancelBubble(e){
    e=e||window.event;
    if(e.stopPropagation){e.stopPropagation();}else{e.cancelBubble=true;}
}
function createToggleButton(head){
    var parent=head;
    if( head.tagName.toLowerCase()=='tr' ){//对表格特别处理
        if(head.getElementsByTagName("th").length){
            parent=head.cells[parent.cells.length-1];
        } else {return;}
    }
    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':"3.8em"}});
    }
    button.style.display="inline";
    head.className+=" uncollapse toggleHotspot";
    parent.insertBefore( button, parent.childNodes[0] );
}
function wgCollapse(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 );//预设的隐藏元素
    function getArray(clsname){
        var r=[],i=0,e,ea=getElementsByClassName(container,"*",clsname);
        while(e=ea[i++]){
            var parent=e.parentNode;
            while(!hasClass(parent,'NavFrame')&&!hasClass(parent,'collapsible')){parent=parent.parentNode;}
            if(parent==container){r.push(e);}
        }
        return r;
    }
    var toggleA=getArray("toggleShow");
    var toggleB=getArray("toggleHide");
    var hotspots=getArray("toggleHotspot");
    function _toggle(list,state){
        var i=0,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";
        addClickHandler(h,function(){self.toggle();});
    }
}
$(function(){
    //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;}});
 
/*