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 (test))
m (bugfix)
Line 153: Line 153:
 
a=$(this);if(!a||!a.attr||!T.func(a.attr))return;
 
a=$(this);if(!a||!a.attr||!T.func(a.attr))return;
 
t.help.show();
 
t.help.show();
t.p=a.attr('href');
+
t.p=a.attr('href').replace(wgScript+'/','');
 
$('body').on('keypress',t.key);
 
$('body').on('keypress',t.key);
 
};
 
};
Line 159: Line 159:
 
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();
if(t.a[k])window.location=wgScript+t.a[k].replace('$1',wgPageName);
+
if(t.a[k])window.location=wgScript+t.a[k].replace('$1',t.p);
 
};
 
};
 
t.init=function(){
 
t.init=function(){

Revision as of 19:35, 9 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);
		},
	}
	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);
	
	mscr = (function(){
		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.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.$);
		MS.content=$("<div>").css({padding:'1em'}).appendTo(MS.$);
		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).delegate('[href=#le-menu-show]','click',MS.show);
		$(document).delegate('[href=#le-menu-hide]','click',MS.hide);
		
		_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};
		};
		
		return function(){return MS;};
	})();
	A('menuScreen',mscr);
	
	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
			mscr().addLink(ml.name,ml.func);
		}
		return function(){return PM};
	})();A('PM',PM);
	
	keyf=function(e){
		if(e.metaKey||e.ctrlKey)return;
		M=mscr();
		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){
		t={};
		t.a={e:'?title=$1&action=edit',h:'?title=$1&action=history'}
		t.p='';
		t.help=$("<div>").css({position:'fixed',top:0,left:0,width:'100%','background-color':'#ff9'}).hide().appendTo('body').text('[E]dit [H]istory');
		t.over=function(e){
			console.log('over',e,this);
			a=$(this);if(!a||!a.attr||!T.func(a.attr))return;
			t.help.show();
			t.p=a.attr('href').replace(wgScript+'/','');
			$('body').on('keypress',t.key);
		};
		t.out=function(e){console.log('out',e,this);$('body').off('keypress',t.key);t.help.hide();};
		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);
	
	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");