MediaWiki:Common.js

出自宅犬維基
於 2007年11月20日 (二) 12:27 由 Cyesuta討論 | 貢獻 所做的修訂 (新頁面: →‎所有用戶在加載任何頁面時,這裡的JavaScript都會加載 == 輔助處理 == //功能設定 var JSConfig={ //showBarA:(wgUserName == null),//設定JS Anonnotice的匿...
(差異) ←上個修訂 | 最新修訂 (差異) | 下個修訂→ (差異)
跳至導覽 跳至搜尋

注意:在您儲存之後您必須清除瀏覽器快取才可看到最新的變更。

  • Firefox / Safari:按住 Shift 時點選 重新整理,或按 Ctrl-F5Ctrl-R (Mac 則為 ⌘-R)
  • Google Chrome:Ctrl-Shift-R (Mac 則為 ⌘-Shift-R)
  • Internet Explorer:按住 Ctrl 時點選 重新整理,或按 Ctrl-F5
  • Opera:前往 選單 → 設定 (在 Mac 為 Opera → 偏好設定) 然後再到 隱私 & 安全性 → 清除瀏覽資料 → 已快取的圖片與檔案
/*
所有用戶在加載任何頁面時,這裡的JavaScript都會加載

== 輔助處理 ==
*/
//功能設定
var JSConfig={
	//showBarA:(wgUserName == null),//設定JS Anonnotice的匿名出現條件
	//showBarU:false,//設定登入用戶使用JS Anonnotice的開關
	isEdit0:true, //設置是否顯示編輯首段按鈕
	editSectionLink:'right',//設置編輯按鈕是否在右側
	collapseText:wgULS('隱藏▲','隱藏▲'),//指示折疊收縮的默認文字
	expandText:wgULS('顯示▼','顯示▼'),//指示折疊展開的默認文字
	autoCollapse:2,  //文章少於 autoCollapse 個折疊塊時,不自動折疊
}
// 兼容性修正
if (document.attachEvent && document.compatMode && document.compatMode == "CSS1Compat")
{
	var oldWidth;
	var docEl = document.documentElement;

	function fixIEScroll()
	{
		if (!oldWidth || docEl.clientWidth > oldWidth){
			doFixIEScroll();
		}else{
			setTimeout(doFixIEScroll, 1);
		}
		oldWidth = docEl.clientWidth;
	}

	function doFixIEScroll() {
		docEl.style.overflowX = (docEl.scrollWidth - docEl.clientWidth < 4) ? "hidden" : "";
	}

	document.attachEvent("onreadystatechange", fixIEScroll);
	attachEvent("onresize", fixIEScroll);
}

// 移動元素
function elementMoveto(node, refNode, pos){//默認位置為refNode前
	if(node && refNode){
		var parent=refNode.parentNode;
		if (pos && pos=='after') {refNode=refNode.nextSibling;}
		try {
			if(refNode){
				parent.insertBefore(node, refNode);
			}else{
				parent.appendChild(node);
			}
		} catch (DOMException) {};
	}
}
//創建元素
function createElement(tag,children,props){
	var element = document.createElement(tag);
	if(!(children instanceof Array)){children=[children];}
	for(var i=0;i<children.length;i++){
		var child=children[i];
		if(typeof child=='string'){child=document.createTextNode(child);}
		if(child){element.appendChild(child);}
	}
	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;
}



//JS、CSS導入模塊
function importScript( page ) {
	var src= /^https?:\/\//.test(page) ? page : getWikiPath(page,{'ctype':'text/javascript'});
	var scriptElem = createElement( 'script',null,{'src':src,'type':'text/javascript'} );
	document.getElementsByTagName( 'head' )[0].appendChild( scriptElem );
}
function importStylesheet( page ) {
	var sheet= /^https?:\/\//.test(page) ? page : getWikiPath(page,{'ctype':'text/css'});
	var styleElem = createElement( 'style', ['@import "'+sheet+'";'], {'type':'text/css'} );
	document.getElementsByTagName( 'head' )[0].appendChild( styleElem );
}



== 段落編輯連結 ==
*/
//JSConfig.isEdit0 設置是否出現「編輯首段」按鈕
//對不需要編輯首段的頁面,還可在頁面中加 Template:NoEdit 模板來禁用。
//JSConfig.editSectionLink 設置「編輯」按鈕的位置
if (wgIsArticle && wgAction == "view") {
	//增加編輯首段按鈕
	addOnloadHook(function(){
		if(!JSConfig.isEdit0||(document.getElementById&&document.getElementById('no-edit-0'))){return;}
		var caEdit=document.getElementById&&document.getElementById('ca-edit');
		if (!caEdit){return;}
		var linkAttributes={
			'href':caEdit.firstChild.href + '&section=0',
			'title':wgULS('編輯正文所有標題之前的文字(通常稱為首段或導言)','編輯正文所有標題之前的文字(通常稱首段或導言)'),
			'accesskey':'0'
		}

		//增加0按鈕標籤
		var caEdit0 = createElement(
			'li',
			[createElement('A',['0'],linkAttributes)],
			{id:'ca-edit-0'}
		);
		caEdit.className = 'istalk';
		elementMoveto(caEdit0,caEdit,"after");

		//增加條目標題下的「編輯首段」按鈕
		var editsection0= createElement(
			'span',
			['[',createElement('A',[wgULS('編輯首段','編輯首段')],linkAttributes),']'],
			{'class':'editsection'}
		);
		var siteSub=document.getElementById&&document.getElementById('siteSub');
		elementMoveto( editsection0 , siteSub.firstChild );
	});
	
	//設置編輯按鈕位置是否浮動
	addOnloadHook(function(){
		if (JSConfig.editSectionLink=='right') { return; }
		var editLinks=getElementsByClassName(document.getElementById('bodyContent'),"span","editsection");
		for(var i = 0; i < editLinks.length; i++) {
			editLinks[i].style.cssFloat = editLinks[i].style.float = 'none';
			editLinks[i].style.textAlign = "left" ;
			editLinks[i].parentNode.appendChild(editLinks[i]);
		}
	});
}

/*

== 增加折疊功能 ==
*/
/** 折疊 div table *****************************
 *  Description: 實現div.NavFrame和table.collapsible的可折疊性。
 *  JSConfig的collapseText、expandText、autoCollapse屬性定義默認文字和默認最少自動折疊塊
 *  Maintainers: User:fdcn
 */
addOnloadHook(function(){
	function toggleState(item){
		var oldState=item.state;
		item.state=1-oldState;
		if(item.text[0]){
			item.text[oldState].style.display = 'inline';
			item.text[item.state].style.display='none';
		}
		item.action(item);
	}

	function cancelBubble(e){
		e=e||window.event;
		if(e.stopPropagation){e.stopPropagation();}else{e.cancelBubble=true;}
	}
	function createToggleButton(head,frame,toggle){
		var textS,textH;
		var button=getElementsByClassName(head,"span","NavToggle")[0];
		if(button){
			textS=getElementsByClassName(button,"span","NavToggleShow")[0]
			textH=getElementsByClassName(button,"span","NavToggleHide")[0];
		}else {
			textS=createElement("span",[JSConfig.expandText]);
			textH=createElement('span',[JSConfig.collapseText]);
			button=createElement("span",[textS,textH],{'class':"NavToggle",styles:{'width':"3.8em"}});
		}
		if(textS){textS.style.display='none';}
		button.style.display='inline';
		var item={'state':0, 'text':[textS,textH],'frame':frame,'action':toggle}

		var links=head.getElementsByTagName("A");
		for(var i=0,l;l=links[i];i++){ addClickHandler(l,cancelBubble); }
		
		head.insertBefore( button, head.childNodes[0] );
		head.style.cursor = "pointer";
		addClickHandler( head, function(){toggleState(item);} );
		return item;
	}
	
	// 折疊div 
	function toggleNavigationBar(item)
	{
		var cls=item.state?'none':'block';
		for (
			var NavChild = item.frame.firstChild;
			NavChild != null;
			NavChild = NavChild.nextSibling
		){
			if (NavChild.className == 'NavPic' || NavChild.className == 'NavContent') {
				NavChild.style.display = cls;
			}
		}
	}
	
	// 折疊表格
	function collapseTable( item )
	{
		var rows = item.frame.getElementsByTagName( "tr" );
		var display = item.state? 'none':rows[0].style.display;
		for (var i=1,row; row=rows[i]; i++) { row.style.display = display; }
	}
	
	//init
	var item,items=[];
	var NavFrames=getElementsByClassName(document,"div","NavFrame");
	for(var i=0,NavFrame;NavFrame = NavFrames[i];i++) {
		var heads=getElementsByClassName(NavFrame,"div","NavHead");
		for(var ih=0,head; head = heads[ih]; ih++ ) {
			if (head.parentNode != NavFrame) {continue;}
			items.push(createToggleButton(head,NavFrame,toggleNavigationBar));
			break;
		}
	}

	var tables = getElementsByClassName(document,"table","collapsible");
	for ( var i = 0,table; table= tables[i]; i++ ) {
		var head = table.getElementsByTagName( "tr" )[0].getElementsByTagName( "th" )[0];
		items.push(createToggleButton(head,table,collapseTable));
	}

	var count=items.length;
	for ( var i = 0;  i<count; i++ ) {
		item=items[i];
		if ( hasClass( item.frame, "collapsed" ) || ( count >= JSConfig.autoCollapse && hasClass( item.frame, "autocollapse" ) ) ) {
			toggleState(item);
		}
	}
});

//修正折疊後定位變化
hookEvent("load",function(){if(location.hash){location.href=location.hash;}});

/*


==Anonnotice顯示==

*/
/** anonnotice for IP user **************************
  *
  * Maintainced by Alexsh@zhwiki, PhiLiP@zhwiki
  */
addOnloadHook(function(){
	document.writeln('<div style="position:absolute; z-index:40; left:155px; top:0px; clear:both; float:left;text-align:left; margin:0 auto;" id="donate">'+ ( wgUserName ? JSConfig.siteNotice : JSConfig.blockMsg )+'</div>');
});