v50 Steam/Premium information for editors
  • v50 information can now be added to pages in the main namespace. v0.47 information can still be found in the DF2014 namespace. See here for more details on the new versioning policy.
  • Use this page to report any issues related to the migration.
This notice may be cached—the current version can be found here.

Difference between revisions of "User:Lethosor/main.js"

From Dwarf Fortress Wiki
Jump to navigation Jump to search
m (fix a few bugs when checking for pagename (links))
m (update from local version)
Line 69: Line 69:
 
MS=(function(){
 
MS=(function(){
 
var MS={active:false,};
 
var MS={active:false,};
MS.$=$("<div>").css({position:'absolute', 'z-index':100, top:'10%', left:'10%', width:'80%', height:'80%',
+
MS.$=$("<div>").css({position:'fixed', 'z-index':100, top:'10%', left:'10%', width:'80%', height:'80%',
 
'background-color':'white', padding:'1.5em', margin:'-1.5em', 'border-radius':'.8em'}).hide().appendTo('body');
 
'background-color':'white', padding:'1.5em', margin:'-1.5em', 'border-radius':'.8em'}).hide().appendTo('body');
 
//upper 'o' not #0
 
//upper 'o' not #0
MS.$O=$("<div>").css({position:'absolute', 'z-index':99, top:0, left:0, width:'100%', height:'100%',
+
MS.$O=$("<div>").css({position:'fixed', 'z-index':99, top:0, left:0, width:'100%', height:'100%',
 
'background-color':'rgba(100,100,100,0.5)'}).hide().appendTo('body')
 
'background-color':'rgba(100,100,100,0.5)'}).hide().appendTo('body')
 
.attr({href:'#le-menu-hide'});  
 
.attr({href:'#le-menu-hide'});  
Line 107: Line 107:
 
w=parseInt(w);h=parseInt(h);
 
w=parseInt(w);h=parseInt(h);
 
l=50-w/2;t=50-h/2;
 
l=50-w/2;t=50-h/2;
MS.$.css({width:w+'%',height:h+'%',top:t+'%',left:l+'%'});
+
MS.$.animate({width:w+'%',height:h+'%',top:t+'%',left:l+'%'},300);
 
};
 
};
 
MS.cf=function(n){ //Content frame - switch to specified
 
MS.cf=function(n){ //Content frame - switch to specified
Line 178: Line 178:
 
t.a={e:'?title=$1&action=edit',h:'?title=$1&action=history'}
 
t.a={e:'?title=$1&action=edit',h:'?title=$1&action=history'}
 
t.p='';
 
t.p='';
t.help=$("<span>").css({position:'fixed',bottom:0,right:0,width:'100%','background-color':'#ff9','text-align':'right'}).hide().appendTo('body').text('[E]dit [H]istory');
+
t.to=[];
t.ad=500;t.hs=function(){t.help.stop().fadeIn(t.ad)};
+
t.toca=function(){for(var i=0;i<t.to.length;i++)clearTimeout(t.to[i]);t.to=[]};
t.hh=function(){t.help.stop().fadeOut(t.ad)};
+
t.toa=function(f,s){t.to.push(setTimeout(f,s))};
 +
t.help=$("<div>").css({position:'absolute',width:'15em','background-color':'#ff9','text-align':'center','border-radius':2}).hide().appendTo('body').text('[E]dit [H]istory');
 +
t.ad=500;t.hs=function(){t.help.stop(1,1).fadeIn(t.ad)};
 +
t.hh=function(){t.help.stop(1,1).fadeOut(t.ad)};
 
t.over=function(e){
 
t.over=function(e){
console.log('over',e,this);
+
//console.log('over',e,this);
a=$(this);if(!a||!a.attr||!T.func(a.attr))return;
+
var a=$(this);if(!a||!a.attr||!T.func(a.attr))return;
t.hs();
+
if(a.hasClass('external')||a.attr('href')[0]=='#')return;//prevent hashes and external links
 +
var ao=a.offset();
 +
t.help.css({top:ao.top+a.height()+2,left:ao.left-(t.help.width()/2-a.width()/2)});
 +
t.toca();t.toa(t.hs,1000)
 
t.p=T.pn(a.attr('href'));
 
t.p=T.pn(a.attr('href'));
 
$('body').on('keypress',t.key);
 
$('body').on('keypress',t.key);
 
};
 
};
t.out=function(e){console.log('out',e,this);$('body').off('keypress',t.key);t.hh();};
+
t.out=function(e){
 +
//console.log('out',e,this);
 +
$('body').off('keypress',t.key);
 +
t.hh();t.toca();
 +
};
 
t.key=function(e){
 
t.key=function(e){
 
T.PD(e);k=String.fromCharCode(e.which).toLowerCase();
 
T.PD(e);k=String.fromCharCode(e.which).toLowerCase();
Line 227: Line 237:
 
else window.LE=LEInit(jQuery); return LE;
 
else window.LE=LEInit(jQuery); return LE;
 
};LEInitCheck();
 
};LEInitCheck();
LELL=[];LELoad=function(f){
+
});importScriptURI("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");
 +
if(!'LELL' in window)window.LELL=[];window.LELoad=function(f){
 
LELL.push(f);if('LE'in window){while(LELL.length){if(LE.T.func(LELL[0]))LELL[0](jQuery,LE);LELL.shift();}}
 
LELL.push(f);if('LE'in window){while(LELL.length){if(LE.T.func(LELL[0]))LELL[0](jQuery,LE);LELL.shift();}}
 
};
 
};
});importScriptURI("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");
 

Revision as of 00:48, 24 March 2013

;jQuery(function(){
MW=mediaWiki;LEInit=(function($){
	console.log($.fn.jquery);
	var _le = {OPTS:{}};
	_le.DEFAULT_OPTS={
		menu:{
			show_overlay:true
		}
	};
	_le.OPTS_MD={
		menu:{
			desc:'Menu settings',
			data:{
				show_overlay:{
					desc:'Show overlay',
					type:'b'
				}
			}
		}
	};
	function update_opts(){
		if(!'LE_USER_OPTS' in window)window.LE_USER_OPTS={}
		_le.OPTS=$.extend(_le.DEFAULT_OPTS,window.LE_USER_OPTS);
	}
	update_opts();
	O=function(){return _le.OPTS};
	T={
		func:function(o){return !!(o&&o.call);},
		PD:function(e){//preventDefault
			if(!e||!e.preventDefault)return;
			o=e.preventDefault;if(e&&!!(o&&o.call)) e.preventDefault();
		},
		log:function(s){
			if(console&&console.log&&console.log.call)console.log(s);
		},
		pn:function(s){s=s.toString();
			l=window.location;
			if(!s.indexOf(l.protocol))s=s.replace(l.protocol,'');
			if(!s.indexOf('//'))s=s.replace('//','');
			if(!s.indexOf(l.host))s=s.replace(l.host,'');
			s=s.replace(wgScript+'/','').replace(wgScript+'?','');
			if(s.indexOf('&')+1)s=s.slice(0,s.indexOf('&'));
			if(s.indexOf('?')+1)s=s.slice(0,s.indexOf('?'));
			if(!s.indexOf('title='))s=s.replace('title=','');
			return s;
		},
	};A("T",T);
	function A(a,b){
		if(!b){
			if (typeof a != 'object') return;
			for(i in a){
				if(i in {}) continue;
				_le[i] = a[i]
			}
		}
		else _le[a]=b;
		return A;
	}
	A('update_opts',update_opts);
	function init(){
		if(window.console&&window.console.log) console.log("Loaded LethosorMenu");
		link = $("<a>").attr({href:'#le-menu-show'}).text("LethosorMenu").attr({title:'Shift-Alt-L'});
		menuitem = $("<li>").append(link);
		$("div#p-personal ul").append(menuitem);
	}
	function user_lethosor(e){T.PD(e);window.location.href="http://dwarffortresswiki.org/index.php/User:Lethosor";}
	$(document).delegate('[href=#le-lethosor]','click',user_lethosor);
	
	MS=(function(){
		var MS={active:false,};
		MS.$=$("<div>").css({position:'fixed', 'z-index':100, top:'10%', left:'10%', width:'80%', height:'80%',
			'background-color':'white', padding:'1.5em', margin:'-1.5em', 'border-radius':'.8em'}).hide().appendTo('body');
		//upper 'o' not #0
		MS.$O=$("<div>").css({position:'fixed', 'z-index':99, top:0, left:0, width:'100%', height:'100%',
			'background-color':'rgba(100,100,100,0.5)'}).hide().appendTo('body')
			.attr({href:'#le-menu-hide'}); 
		MS.content=$("<div>").addClass("le-cf").css({padding:'1em'}).appendTo(MS.$);
		MS.header=$("<h3>").css({'text-align':'center'}).html('<a href="#le-lethosor" target="_blank"><span style="color:#093">L</span><span style="color:#084">e</span>'+
			'<span style="color:#075">t</span><span style="color:#066">h</span><span style="color:#057">o</span>'+
			'<span style="color:#048">s</span><span style="color:#039">o</span><span style="color:#02a">r</span></a><span style="color:#c40">Menu</span>')
			.appendTo(MS.content);
		MS.cfs={main:MS.content};//content frames
		MS.ccf='content';//current
		MS.close=$("<a>").attr({href:'#le-menu-hide'}).text('Close').css({'text-align':'right','float':'right','color':'red'})
			.attr({title:'Esc'}).prependTo(MS.$);
		
		MS.show=function(evt){
			T.PD(evt);
			if(O().menu.show_overlay) MS.$O.stop().fadeIn(500);
			MS.$.stop().fadeIn(350);
			$('body').css({overflow:'hidden'});
			MS.active=true;
		};
		MS.hide=function(evt){
			T.PD(evt);
			MS.$O.stop().fadeOut(400);
			MS.$.stop().fadeOut(250);
			$('body').css({overflow:'auto'});
			MS.active=false
		};
		$(document).on('click','[href=#le-menu-show]',MS.show);
		$(document).on('click','[href=#le-menu-hide]',MS.hide);
		
		MS.csize=function(w,h){
			if(w+''=='')return;
			var w,h,l,t;
			w=parseInt(w);h=parseInt(h);
			l=50-w/2;t=50-h/2;
			MS.$.animate({width:w+'%',height:h+'%',top:t+'%',left:l+'%'},300);
		};
		MS.cf=function(n){ //Content frame - switch to specified
			if(!n)return MS.ccf;
			MS.ccf=n;$('.le-cf').hide();
			if(!MS.cfs[n])MS.cfs[n]=$('<div class="le-cf">').appendTo(MS.$);
			return MS.cfs[n].show();
		};
		MS.reset=function(){
			MS.show();MS.cf('main');MS.csize(80,80);
		};
		
		_links={};
		MS.linklist=$("<ul>").appendTo(MS.content);
		MS.addLink=function(name,func){
			el=$("<li>").append($("<a>").attr('href','#')).appendTo(MS.linklist);
			a=PM().plugs[name].title;
			el.find('a').text((a)?a:name);
			el.on('click','a',function(e){T.PD(e);func(PM);});
			_links[name]={func:func,link:el};
		};
		
		var _=MS;return function(){return _;};
	})();
	A('menuScreen',MS);
	
	PM=(function(){ //Plugins manager
		var PM={plugs:{}};
		PM.register=function(name,func,opts){
			opts=$.extend({},opts);
			if(PM.plugs[name]&&!opts.override) return
			if(T.func(func)){
				PM.plugs[name]=func(PM);
			}
			if(T.func(PM.plugs[name].init))PM.plugs[name].init(PM);
		}
		PM.addMenuLink=function(name){
			ml=(T.func(PM.plugs[name].menuLinkData))?PM.plugs[name].menuLinkData():{name:name,func:PM.plugs[name].main}; //call menuLinkData if func, else use defaults
			MS().addLink(ml.name,ml.func);
		}
		return function(){return PM};
	})();A('PM',PM);
	
	keyf=function(e){
		if(e.metaKey||e.ctrlKey)return;
		var M=MS();
		if(e.altKey&&e.shiftKey&&e.keyCode==76){//S-A-l
			if(!M.active)M.show();return;
		}
		if(!M.active)return;
		T.PD(e);
		if(e.keyCode==27){//esc
			M.hide();
		}
	};
	$(document).bind('keydown', keyf);
	
	local=(function(){
		__t=this;return function(k){return k?__t[k]:__t;}
	})();A('local',local);
		
	init();
	
	function test(PM){return {main:function(){alert("This is a test plugin:\n"+PM)},title:"Test plugin v0.0.1", init:function(){PM.addMenuLink("Test")}}}
	PM().register("Test", test);
	//PM().addMenuLink("Test");
	
	function link_opts(PM){
		var t={};
		t.a={e:'?title=$1&action=edit',h:'?title=$1&action=history'}
		t.p='';
		t.to=[];
		t.toca=function(){for(var i=0;i<t.to.length;i++)clearTimeout(t.to[i]);t.to=[]};
		t.toa=function(f,s){t.to.push(setTimeout(f,s))};
		t.help=$("<div>").css({position:'absolute',width:'15em','background-color':'#ff9','text-align':'center','border-radius':2}).hide().appendTo('body').text('[E]dit [H]istory');
		t.ad=500;t.hs=function(){t.help.stop(1,1).fadeIn(t.ad)};
		t.hh=function(){t.help.stop(1,1).fadeOut(t.ad)};
		t.over=function(e){
			//console.log('over',e,this);
			var a=$(this);if(!a||!a.attr||!T.func(a.attr))return;
			if(a.hasClass('external')||a.attr('href')[0]=='#')return;//prevent hashes and external links
			var ao=a.offset();
			t.help.css({top:ao.top+a.height()+2,left:ao.left-(t.help.width()/2-a.width()/2)});
			t.toca();t.toa(t.hs,1000)
			t.p=T.pn(a.attr('href'));
			$('body').on('keypress',t.key);
		};
		t.out=function(e){
			//console.log('out',e,this);
			$('body').off('keypress',t.key);
			t.hh();t.toca();
		};
		t.key=function(e){
			T.PD(e);k=String.fromCharCode(e.which).toLowerCase();
			if(t.a[k])window.location=wgScript+t.a[k].replace('$1',t.p);
		};
		t.init=function(){
			$('body').on('mouseover','a',t.over);
			$('body').on('mouseout','a',t.out);
		};
		return t;
	}
	PM().register("Links",link_opts);
	
	function about(PM){
		var t={};
		t.main=function(){
			MS().csize(60,80);
			MS().cf('about');
			t.f=MS().cfs[MS().cf()];
			t.f.html('').append($("<p>").text("LethosorMenu: JavaScript plugin manager"));
			t.ql=$("<a>").attr('href','#').text("Back").on('click',function(e){T.PD(e);t.quit();}).appendTo(t.f);
		};
		t.quit=function(){
			MS().reset();
		};
		t.init=function(PM){
			PM.addMenuLink("About");
		};
		t.title="About LethosorMenu";
		return t;
	}
	PM().register("About",about);
	window.LE=_le;
	if(T.func(window.LELoad))window.LELoad();return _le;
});
LEInitCheck=function(){
	if($.fn.jquery!='1.9.1') return setTimeout(LEInitCheck, 500);
	else window.LE=LEInit(jQuery); return LE;
};LEInitCheck();
});importScriptURI("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");
if(!'LELL' in window)window.LELL=[];window.LELoad=function(f){
	LELL.push(f);if('LE'in window){while(LELL.length){if(LE.T.func(LELL[0]))LELL[0](jQuery,LE);LELL.shift();}}
};