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 (update from local version)
m (fix a few bugs when checking for pagename (links))
Line 34: Line 34:
 
if(console&&console.log&&console.log.call)console.log(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){
 
function A(a,b){
 
if(!b){
 
if(!b){
Line 174: Line 185:
 
a=$(this);if(!a||!a.attr||!T.func(a.attr))return;
 
a=$(this);if(!a||!a.attr||!T.func(a.attr))return;
 
t.hs();
 
t.hs();
t.p=a.attr('href').replace(wgScript+'/','');
+
t.p=T.pn(a.attr('href'));
 
$('body').on('keypress',t.key);
 
$('body').on('keypress',t.key);
 
};
 
};
Line 209: Line 220:
 
}
 
}
 
PM().register("About",about);
 
PM().register("About",about);
+
window.LE=_le;
return _le;
+
if(T.func(window.LELoad))window.LELoad();return _le;
 
});
 
});
 
LEInitCheck=function(){
 
LEInitCheck=function(){
 
if($.fn.jquery!='1.9.1') return setTimeout(LEInitCheck, 500);
 
if($.fn.jquery!='1.9.1') return setTimeout(LEInitCheck, 500);
 
else window.LE=LEInit(jQuery); return LE;
 
else window.LE=LEInit(jQuery); return LE;
};LEInitCheck()
+
};LEInitCheck();
 +
LELL=[];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();}}
 +
};
 
});importScriptURI("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");
 
});importScriptURI("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");

Revision as of 21:54, 10 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:'absolute', '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:'absolute', '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.$.css({width:w+'%',height:h+'%',top:t+'%',left:l+'%'});
		};
		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.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.ad=500;t.hs=function(){t.help.stop().fadeIn(t.ad)};
		t.hh=function(){t.help.stop().fadeOut(t.ad)};
		t.over=function(e){
			console.log('over',e,this);
			a=$(this);if(!a||!a.attr||!T.func(a.attr))return;
			t.hs();
			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.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();
LELL=[];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();}}
};
});importScriptURI("http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js");