注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google ChromeFirefoxMicrosoft EdgeSafari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。

//if (typeof(StringBuilder) == "undefined")
//-----------------------------------------
// StringBuilder(JavaScript版)
// 原始出處
//	StringBuilder:	http://www.codeproject.com/jscript/stringbuilder.asp
//	sprintf:	http://jan.moesen.nu/code/javascript/sprintf-and-printf-in-javascript/
// 改良版
//			http://www.player.idv.tw/prog/index.php?title=StringBuilder.js
 
function StringBuilder(value)
{
    this.strings = new Array("");
    this.append(value);
}
 
// Appends the given value to the end of this instance.
StringBuilder.prototype.append = function (value)
{
    if (value)
    {
        this.strings.push(value);
    }
    return this;
}
 
StringBuilder.prototype.sort = function ()
{
    this.strings.sort();
}

// Appends the given format value to the end of this instance.
StringBuilder.prototype.appendFormat = function ()
{
	var arguments = StringBuilder.prototype.appendFormat.arguments;
 
	if (arguments.length > 0)
	{
		var value = arguments[0];
		for (var i = 1; i < arguments.length; ++i) 
		{
			value = value.replace("{"+(i-1)+"}", arguments[i]);
		}
		this.strings.push(value);
    }
    return this;
}
 
 
// Clears the string buffer
StringBuilder.prototype.clear = function ()
{
    this.strings.length = 1;
}
 
// Converts this instance to a String.
StringBuilder.prototype.toString = function ()
{
    return this.strings.join("");
}
 


//----------------------------------------- 


if ( mw.config.get( 'wgNamespaceNumber' ) == 14 && mw.config.get( 'wgAction' ) == 'edit' ) {
   if ( typeof $ != 'undefined' && typeof $.fn.wikiEditor != 'undefined' ) {
     $( function() {
	$( '#wpTextbox1' ).wikiEditor( 'addToToolbar', {
		'section': 'advanced',
		'group': 'insert',
		'tools': {'CatNav': {
			label: '插入分類導覽模版',
			type: 'button',
			icon: '//upload.wikimedia.org/wikipedia/commons/d/d8/Btn_toolbar_indent_ulist.png',
			action: {type: 'callback',
				execute: function(context){GetCatnav();}
			}
	}}});
    });
  }
}
 
function GetCatnav()
{
   var sbCatnav = new StringBuilder();

   var IsSortMode=confirm("是否排序?");


     //編輯摘要
    var strSummary = $('input#wpSummary').val();
	if (typeof(strSummary) != "undefined") {
    	if ( strSummary.length > 0) {
    		strSummary = strSummary + ", ";
    	} else {
    		strSummary ="";
    	}
		strSummary = strSummary + "調整分類裡的Catnav模板, 透過[[User:P1ayer/CatNavStr.js]]";
		$('input#wpSummary').val(strSummary);
	}



//參考: User:Liangent/Scripts/CatNav.js

    var CatNav = {};
    CatNav.depthLimit = 8;
    CatNav.pageName = mw.config.get('wgPageName').replace(/_/g, ' ');
    CatNav.data = {};
    CatNav.walked = {};
    CatNav.path = [];
    CatNav.pending = 0;
    CatNav.categoryPrefix = new RegExp('^' + mw.config.get('wgFormattedNamespaces')[14] + ':');
    CatNav.titlesLimit = ((mw.config.get('wgUserGroups').indexOf('sysop') != -1 || mw.config.get('wgUserGroups').indexOf('bot') != -1) ? 500 : 50);
    CatNav.cllimitLimit = ((mw.config.get('wgUserGroups').indexOf('sysop') != -1 || mw.config.get('wgUserGroups').indexOf('bot') != -1) ? 5000 : 500);
    CatNav.query = function(pages, clcontinue) {
        if (pages.length > CatNav.titlesLimit) {
            CatNav.query(pages.slice(0, CatNav.titlesLimit));
            CatNav.query(pages.slice(CatNav.titlesLimit));
        } else if (pages.length != 0) {
            var titles = pages.join('|');
            CatNav.pending++;
            // since this will be repeated on every category page, i'd better filter hidden categories out
            // rather than know which are hidden and hide them.
            var data = {
                action: 'query',
                format: 'json',
                titles: titles,
                prop: 'categories',
                clshow: '!hidden',
                cllimit: CatNav.cllimitLimit
            };
            if (clcontinue) {
                data.clcontinue = clcontinue;
            }
            jQuery.post(mw.config.get('wgScriptPath') + '/api.php', data, function(data) {
                CatNav.pending--;
                var toquery = [];
                for (var pageid in data.query.pages) {
                    var page = data.query.pages[pageid];
                    var pagename = page.title;
                    jQuery.each(page.categories ? page.categories : [], function() {
                        CatNav.data[pagename][this.title] = true;
                        if (!CatNav.data[this.title]) {
                            CatNav.data[this.title] = {};
                            toquery.push(this.title);
                        }
                    });
                }
                if (data['query-continue']) {
                    CatNav.query([titles], data['query-continue'].categories.clcontinue);
                }
                CatNav.query(toquery);
                if (CatNav.pending == 0) {
                    CatNav.walk(CatNav.pageName);
                }
            }, 'json');
        }
    };
    CatNav.walk = function(page) {
        if (CatNav.path.length == CatNav.depthLimit) return;
        if (CatNav.walked[page]) {
            CatNav.path.push(page);
            CatNav.display(true);
            CatNav.path.pop();
        } else {
            CatNav.walked[page] = true;
            CatNav.path.push(page);
            var output = true;
            for (var parent in CatNav.data[page]) {
                output = false;
                CatNav.walk(parent);
            }
            if (output) {
                CatNav.display();
            }
            CatNav.path.pop();
            CatNav.walked[page] = false;
        }
    };
    CatNav.display = function(loop) {
        //var div = jQuery('<div />').addClass('catnav');
        var sb = new StringBuilder();

        var isfirst = true;
        jQuery.each(CatNav.path, function() {
            if (!isfirst) {
                sb.append(this.replace(CatNav.categoryPrefix, '')).append('|');
            } else {
                isfirst = false;
                sb.append("}}\n"); //尾
            }
            //if (!isfirst) {
            //    div.prepend('&nbsp;&gt;&nbsp;');
            //} else {
            //    isfirst = false;
            //}
            //div.prepend(jQuery('<a />').attr(
            //    'href', encodeURIComponent(this).replace(/(.*)/, wgArticlePath)
            //).text(this.replace(CatNav.categoryPrefix, '')));
        });
        if (loop) {
            //div.prepend('[...]&nbsp;&gt;&nbsp;');
            sb.append('...').append('|').append('頁面分類').append('|');
        }
        //div.insertBefore('.printfooter');
        sb.append("{{Catnav"); //頭

        //結合前, 要倒轉順序
        sb.strings.reverse();

        if (IsSortMode==true)
        {
            //排序模式用(先分別插插入單行資料到緩衝區)
            sbCatnav.append(sb.toString());
        }
        else
        {
            //單行插入模式
            with($('#wpTextbox1'))
            {
               sb.append(val());
               val(sb.toString());
            }
        }
    };
    CatNav.data[CatNav.pageName] = {};
    CatNav.query([CatNav.pageName]);

    //排序模式用
    if (IsSortMode==true) {
       sbCatnav.sort();
       with($('#wpTextbox1')) {
         sbCatnav.append(val());
         val(sbCatnav.toString());
       }
     }
}