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:Latias1290/menucustom.js"

From Dwarf Fortress Wiki
Jump to navigation Jump to search
(this will be annoying, so very annoying)
Line 1: Line 1:
// I am testing with a cheap ripoff of lethosormenu, please dont edit(its not like you can anyway)
+
/* todo: new content frame with a tool to convert text to binary and back
 +
function txt2bin(txt) {
 +
    var pad = '00000000',
 +
        bin = '',
 +
        c = '';
 +
    for(var i = 0, l = txt.length; i < l; i++) {
 +
        c = txt.charCodeAt(i).toString(2);
 +
        bin += (pad + c).substr(c.length);
 +
    }
 +
    return bin;
 +
}
  
// trying to figure out its code without a documentation is pretty hard tho
+
function bin2txt(bin) {
 
+
    var split = bin.match(/.{8}/g),
// there are notes scattered all over the source, feel free to read them
+
        txt = '';
 
+
    for(var i = 0, l = split.length; i < l; i++) {
// bug: page resizes upon clicking the "latimenu" button
+
        txt += String.fromCharCode(parseInt(split[i], 2));
 +
    }
 +
    return txt;
 +
}
 +
*/
  
 +
// make code more readable
 +
// document the whole thing
 +
LEInit=(function($){
 +
MW=mediaWiki;
 +
var _le = {OPTS:{}};
  
;jQuery(function(){
 
MW=mediaWiki;LEInit=(function($){
 
console.log($.fn.jquery);
 
var _le = {OPTS:{}};
 
 
/* _le is the same as window.LE. _le is used here to export stuff to outside code.
 
*
 
*
 
*
 
*
 
*/
 
 
 
_le.DEFAULT_OPTS={
 
_le.DEFAULT_OPTS={
 
menu:{
 
menu:{
Line 25: Line 32:
 
}
 
}
 
};
 
};
_le.OPTS_MD={//metadata. unused, but might use in the future.
+
 
 +
/* unused, may expand later
 +
_le.OPTS_MD={
 
menu:{
 
menu:{
 
desc:'Menu settings',
 
desc:'Menu settings',
Line 36: Line 45:
 
}
 
}
 
};
 
};
+
*/
function update_opts(){ // probably update options
+
 
if(!'LE_USER_OPTS' in window)window.LE_USER_OPTS={} // I am wondering what "window" means in this script like Ive never wondered before but I assume its good because it doesnt break
+
function update_opts(){
 +
if(!'LE_USER_OPTS' in window)window.LE_USER_OPTS={}
 
_le.OPTS=$.extend(_le.DEFAULT_OPTS,window.LE_USER_OPTS);
 
_le.OPTS=$.extend(_le.DEFAULT_OPTS,window.LE_USER_OPTS);
 
}
 
}
+
 
 
update_opts();
 
update_opts();
+
 
T={
+
/* Assigns properties to '_le'. Polymorphic.
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;
 
},
 
};assignProps("T",T);
 
 
 
/* Assigns properties to '_le'. Polymorphic(not like I know what that means anyway).
 
 
* assignProps(String a, mixed b): Assigns b to _le[a]
 
* assignProps(String a, mixed b): Assigns b to _le[a]
 
* assignProps(Object a): Performs assignProps(key, value) for each {key:value} pair in 'a'
 
* assignProps(Object a): Performs assignProps(key, value) for each {key:value} pair in 'a'
Line 76: Line 63:
 
* assignProps('a', 1)('b', 2);
 
* assignProps('a', 1)('b', 2);
 
*/
 
*/
function assignProps(a,b){//renamed to something less cryptic
+
function assignProps(a, b) {
+
if (b == undefined) {
if(!b){
 
 
// 'b' is not defined, so assign all key-value pairs in 'a' (an object) to _le
 
// 'b' is not defined, so assign all key-value pairs in 'a' (an object) to _le
if (typeof a != 'object') return; // if 'a' isn't an object, it doesn't have keys, so looping won't work. This only happens with things like assignProps('foo')
+
if (typeof a != 'object') {
for(i in a){ // Loop through keys of 'a' - 'i' is the key
+
return; // if 'a' isn't an object, it doesn't have keys, so looping won't work. This only happens with things like assignProps('foo')
if(i in {}) continue; // if i is a property of an empty object, skip it. This should never happen unless you add properties to the Object constructor.
+
}
 +
for (i in a) { // Loop through keys of 'a' - 'i' is the key
 +
if (i in {}) {
 +
continue; // if i is a property of an empty object, skip it. This should never happen unless you add properties to the Object constructor.
 +
}
 
_le[i] = a[i] // Assign property 'i' of 'a' to the same property of '_le'. In this loop, effectively assigns all properties of 'a' to '_le'.
 
_le[i] = a[i] // Assign property 'i' of 'a' to the same property of '_le'. In this loop, effectively assigns all properties of 'a' to '_le'.
 
}
 
}
 +
} else {
 +
// b _is_ defined, so set _le[a] to 'b'
 +
_le[a] = b;
 
}
 
}
else _le[a]=b; // b _is_ defined, so set _le[a] to 'b'
+
return assignProps; // returns A so it can be called again, if necessary
+
// returns A so it can be called again, if necessary
 +
return assignProps;
 
}
 
}
+
 
 +
T = { // what is T? maybe text
 +
func:function(o) { // what is o? maybe object
 +
return !!(o&&o.call);
 +
},
 +
 +
// preventDefault
 +
PD:function(e) { // what is e?
 +
if ( !e || !e.preventDefault ) {
 +
return;
 +
}
 +
o = e.preventDefault;
 +
if ( e && !!( o && o.call ) ) { // i have absolutely no idea what !! means except for "its on fire", but this is js not df :P
 +
e.preventDefault();
 +
}
 +
},
 +
 +
log:function(s) { // string s
 +
if (console && console.log && console.log.call) { // prevent that there is no console
 +
console.log(s); // there is a console, so log the value of s
 +
}
 +
},
 +
 +
/* takes a string s and removes the following things:
 +
* "//"
 +
* the value of l.host
 +
* the value of wgScript+"/" (but whats wgScript???)
 +
* the value of wgScript+"?"
 +
* anything after "&"
 +
* anything after "?"
 +
* "title="
 +
*
 +
* in that order.
 +
* then returns the stripped value of s.
 +
*
 +
* probably to strip a string to navigate there.
 +
*/
 +
pn:function(s) { // string 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+'/','');
 +
s = s.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;
 +
},
 +
};
 +
assignProps("T",T);
 +
 
 
assignProps('update_opts',update_opts);
 
assignProps('update_opts',update_opts);
function init(){
+
 
link = $("<a>").attr({href:'#le-menu-show'}).text("LatiMenu").attr({title:'Alt-L'});
+
function init() {
 +
link = $("<a>").attr({
 +
href:'#le-menu-show'
 +
}).text("LatiMenu").attr({
 +
title:'Alt-L'
 +
});
 
menuitem = $("<li>").append(link);
 
menuitem = $("<li>").append(link);
 
$("div#p-personal ul").append(menuitem);
 
$("div#p-personal ul").append(menuitem);
if(window.console&&window.console.log) console.log("Loaded LatiMenu 0.1");
+
if (window.console&&window.console.log) {
 +
console.log("Loaded LatiMenu 0.1.1");
 +
}
 
}
 
}
function user_latias(e){T.PD(e);window.location.href="http://dwarffortresswiki.org/index.php/User:Latias1290";}
+
function user_latias(e){
$(document).delegate('[href=#le-latias]','click',user_latias);
+
T.PD(e);
+
window.location.href="http://dwarffortresswiki.org/index.php/User:Latias1290";
MS=(function(){
+
}
var MS={active:false,};
+
$(document).delegate('[href=#le-latias]','click', user_latias);
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');
+
MS = ( function() {
 +
var MS = {
 +
active:true, // if the thing is not working, setting it to true may be the fix :P
 +
};
 +
 +
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
 
//upper 'o' not #0
MS.$O=$("<div>").css({position:'fixed', 'z-index':99, top:0, left:0, width:'100%', height:'100%',
+
MS.$O = $("<div>").css({
'background-color':'rgba(100,100,100,0.5)'}).hide().appendTo('body')
+
position:'fixed',
.attr({href:'#le-menu-hide'});
+
'z-index':99,
MS.content=$("<div>").addClass("le-cf").css({padding:'1em'}).appendTo(MS.$);
+
top:0,
MS.header=$("<h3>").css({'text-align':'center'}).html('<a href="#le-latias" target="_blank"><span style="color:#093">L</span><span style="color:#084">a</span>'+'<span style="color:#075">t</span><span style="color:#066">i</span></a><span style="color:#c40">Menu</span>')
+
left:0,
.appendTo(MS.content);
+
width:'100%',
MS.cfs={main:MS.content};//content frames
+
height:'100%',
MS.ccf='content';//current
+
'background-color':'rgba(100,100,100,0.5)'
MS.close=$("<a>").attr({href:'#le-menu-hide'}).text('Close').css({'text-align':'right','float':'right','color':'red'})
+
}).hide().appendTo('body').attr({
.attr({title:'Esc'}).prependTo(MS.$);
+
href:'#le-menu-hide'
 +
});
 
 
MS.show=function(evt){ // show the menu
+
MS.content = $("<div>").addClass("le-cf").css({
 +
padding:'1em'
 +
}).appendTo(MS.$);
 +
 +
MS.header = $("<h3>").css({
 +
'text-align':'center'
 +
}).html('<a href="#le-latias" target="_blank"><span style="color:#093">L</span><span style="color:#084">a</span>'+'<span style="color:#075">t</span><span style="color:#066">i</span></a><span style="color:#c40">Menu</span>').appendTo(MS.content);
 +
 +
MS.cfs = { // content frames
 +
main:MS.content
 +
};
 +
 +
MS.ccf = 'content'; // current content frame
 +
 +
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) { // show the menu
 
T.PD(evt);
 
T.PD(evt);
if(_le.OPTS.menu.show_overlay) MS.$O.stop().fadeIn(500);
+
if ( _le.OPTS.menu.show_overlay ) {
 +
MS.$O.stop().fadeIn(500);
 +
}
 +
 
MS.$.stop().fadeIn(350);
 
MS.$.stop().fadeIn(350);
$('body').css({overflow:'hidden'});
+
$('body').css({
MS.active=true;
+
overflow:'hidden'
 +
});
 +
MS.active = true;
 
};
 
};
MS.hide=function(evt){ // hide the menu/close it
+
 +
MS.hide = function(evt) { // hide the menu/close it
 
T.PD(evt);
 
T.PD(evt);
 
MS.$O.stop().fadeOut(400);
 
MS.$O.stop().fadeOut(400);
 
MS.$.stop().fadeOut(250);
 
MS.$.stop().fadeOut(250);
$('body').css({overflow:'auto'});
+
$('body').css({
MS.active=false
+
overflow:'auto'
 +
});
 +
MS.active = false
 
};
 
};
$(document).on('click','[href=#le-menu-show]',MS.show);
+
$(document).on('click', '[href=#le-menu-show]', MS.show);
$(document).on('click','[href=#le-menu-hide]',MS.hide);
+
$(document).on('click', '[href=#le-menu-hide]', MS.hide);
 
 
MS.csize=function(w,h){ // set the size, no need to dismantle it as its clear what it does
+
MS.csize = function(w, h) { // set the size of the screen
if(w+''=='')return;
+
if (w+'' == '') { // maybe an alternate for "if (typeof w != 'number')"
var w,h,l,t;
+
return;
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);
+
var w;
 +
var h;
 +
var l;
 +
var 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.cf = function(n) { // Content frame - switch to specified
 +
if(!n) {
 +
return MS.ccf;
 +
}
 +
 
MS.ccf=n;$('.le-cf').hide();
 
MS.ccf=n;$('.le-cf').hide();
if(!MS.cfs[n])MS.cfs[n]=$('<div class="le-cf">').appendTo(MS.$);
+
 +
if( !MS.cfs[n] ) {
 +
MS.cfs[n] = $('<div class="le-cf">').appendTo(MS.$);
 +
}
 +
 
return MS.cfs[n].show();
 
return MS.cfs[n].show();
 
}; // switch to another menu
 
}; // switch to another menu
MS.reset=function(){ // reset the screen
+
MS.show();MS.cf('main');MS.csize(80,80);
+
MS.reset = function() { // reset the screen
 +
MS.show();
 +
MS.cf('main');
 +
MS.csize(80,80);
 
};
 
};
 
 
_links={};
+
_links = {};
MS.linklist=$("<ul>").appendTo(MS.content);
+
MS.addLink=function(name,func){
+
MS.linklist = $("<ul>").appendTo(MS.content);
el=$("<li>").append($("<a>").attr('href','#')).appendTo(MS.linklist);
+
a=PM().plugs[name].title;
+
el.find('a').text((a)?a:name);
+
/* add a link to the menu.
el.on('click','a',function(e){T.PD(e);func(PM);});
+
* string name
_links[name]={func:func,link:el};
+
* name of the link, or text on the screen
 +
* function func
 +
*  function to execute when link it clicked
 +
*/
 +
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 _;};
+
//var _ = MS;
 +
 +
return function() {
 +
// return _;
 +
return MS;
 +
};
 
})();
 
})();
 
assignProps('menuScreen',MS);
 
assignProps('menuScreen',MS);
+
 
+
PM = (function() { // Plugins manager 0.1
PM=(function(){ //Plugins manager
+
var PM = {
var PM={plugs:{}};
+
plugs:{}
PM.register=function(name,func,opts){
+
};
opts=$.extend({},opts);
+
PM.register = function(name, func, opts) {
if(PM.plugs[name]&&!opts.override) return
+
opts = $.extend({},opts);
if(T.func(func)){
+
if(PM.plugs[name] && !opts.override) {
PM.plugs[name]=func(PM);
+
return;
 +
}
 +
 +
if(T.func(func)) {
 +
PM.plugs[name] = func(PM);
 +
}
 +
if(T.func(PM.plugs[name].init)) {
 +
PM.plugs[name].init(PM);
 
}
 
}
if(T.func(PM.plugs[name].init))PM.plugs[name].init(PM);
 
 
}
 
}
PM.addMenuLink=function(name){
+
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
+
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);
 
MS().addLink(ml.name,ml.func);
 
}
 
}
return function(){return PM} // returns the plugin list, aka the PM variable(not the PM() function)
+
})();assignProps('PM',PM);
+
return function() {
+
return PM
+
} // returns the plugin list, aka the PM variable(not the PM() function)
 +
})();
 +
assignProps('PM',PM);
 +
 
 
plugin = (function() { // plugins manager 0.2
 
plugin = (function() { // plugins manager 0.2
var list = new Array();
+
var plugList = {
 +
plugs:{}
 +
};
 +
var niceList = new Array();
 
var descs = new Array();
 
var descs = new Array();
var ids = new Array();
 
 
 
plugin.register = function(name, desc) {
+
plugin.register = function(name, func, desc) {
 
count = count + 1;
 
count = count + 1;
list[count] = name; // I know, "what if these are not strings?" but its my job to make plugins and Im the only user, so ya
+
niceList[count] = name;
 
descs[count] = desc;
 
descs[count] = desc;
ids[count] = count;
+
 +
opts = $.extend({},opts);
 +
if(niceList.plugs[name] && !opts.override) {
 +
return;
 +
}
 +
 +
if(T.func(func)) {
 +
niceList.plugs[name] = func(PM);
 +
}
 +
if(T.func(PM.plugs[name].init)) {
 +
niceList.plugs[name].init(PM);
 +
}
 
}
 
}
 
 
 
plugin.addMenuLink = function(txt) {
 
plugin.addMenuLink = function(txt) {
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
+
ml = (T.func(PM.plugList[name].menuLinkData))?PM.plugList[name].menuLinkData():{
 +
name:name,
 +
func:PM.plugs[name].main
 +
}; //call menuLinkData if func, else use defaults
 
MS().addLink(ml.name, ml.func);
 
MS().addLink(ml.name, ml.func);
 
}
 
}
 
 
plugin.getPluginCount = function(name) {
+
plugin.getPluginID = function(name) {
return list.length;
+
for (var i = 0; i < list.length; i++) {
 +
if (list[i] == name) {
 +
return i;
 +
}
 +
}
 
}
 
}
 
 
Line 213: Line 411:
 
})();
 
})();
 
assignProps("plugin", plugin);
 
assignProps("plugin", plugin);
 +
 +
keyf=function(e){ // key functions
 +
if (e.metaKey || e.ctrlKey) { // if control is pressed, or is metaKey is true(whatever that means), ignore the rest
 +
return;
 +
}
 +
 +
if (e.altKey && e.shiftKey && e.keyCode == 76) {//S-A-l
 +
if (!MS.active) {
 +
MS.show(); // when SHIFT-ALT-L is pressed, open the mainscreen
 +
return;
 +
}
 +
}
 
 
+
if (!MS.active) { // if mainscreen is not active, ignore the rest
+
return;
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); // ?
T.PD(e);
+
if (e.keyCode == 27) { //esc
if(e.keyCode==27){//esc
+
MS.hide(); // when ESC is pressed, exit the mainscreen
M.hide();
 
 
}
 
}
 
};
 
};
 
$(document).bind('keydown', keyf);
 
$(document).bind('keydown', keyf);
+
 
 
local=(function(){
 
local=(function(){
__t=this;return function(k){return k?__t[k]:__t;}
+
__t=this;
})();assignProps('local',local);
+
return function(k) {
 +
// i was never too good at inline if statements but this is making me thing, wtf
 +
return k ? __t[k] : __t;
 +
// probably:
 +
// if k is defined, return __t at index k, if k is not defined return the whole array __t
 +
}
 +
})();
 +
assignProps('local',local);
 
 
 
init();
 
init();
+
 
 
function link_opts(PM){
 
function link_opts(PM){
 
var t={};
 
var t={};
Line 318: Line 529:
 
}
 
}
 
PM().register("Links",link_opts);
 
PM().register("Links",link_opts);
+
 
+
 
 
function plist(PM){
 
function plist(PM){
 
var t={};
 
var t={};
Line 339: Line 550:
 
}
 
}
 
PM().register("Plist", plist);
 
PM().register("Plist", plist);
+
 
 +
 
 
function tt_disable(PM){ //stack overflow 1027762
 
function tt_disable(PM){ //stack overflow 1027762
 
var t={};
 
var t={};
Line 382: Line 594:
 
}
 
}
 
//PM().register("TTDisable",tt_disable);
 
//PM().register("TTDisable",tt_disable);
+
 
 
function test(PM){
 
function test(PM){
 
return {
 
return {
Line 395: Line 607:
 
}
 
}
 
//PM().register("Test", test);
 
//PM().register("Test", test);
+
 
+
 
 
function anMenu(PM) {
 
function anMenu(PM) {
 
var t = {};
 
var t = {};
Line 420: Line 632:
 
}
 
}
 
PM().register("AnMenu", anMenu);
 
PM().register("AnMenu", anMenu);
+
 
 
function about(PM){
 
function about(PM){
 
var t={};
 
var t={};
Line 440: Line 652:
 
}
 
}
 
PM().register("About",about);
 
PM().register("About",about);
+
 
 
 
 
window.LE=_le;
 
window.LE=_le;
if(T.func(window.LELoad))window.LELoad();return _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();}}
 
};
 

Revision as of 18:22, 9 April 2014

/* todo: new content frame with a tool to convert text to binary and back
function txt2bin(txt) {
    var pad = '00000000',
        bin = '',
        c = '';
    for(var i = 0, l = txt.length; i < l; i++) {
        c = txt.charCodeAt(i).toString(2);
        bin += (pad + c).substr(c.length);
    }
    return bin;
}

function bin2txt(bin) {
    var split = bin.match(/.{8}/g),
        txt = '';
    for(var i = 0, l = split.length; i < l; i++) {
        txt += String.fromCharCode(parseInt(split[i], 2));
    }
    return txt;
}
*/

// make code more readable
// document the whole thing
LEInit=(function($){
	MW=mediaWiki;
	var _le = {OPTS:{}};

	_le.DEFAULT_OPTS={
		menu:{
			show_overlay:true
		}
	};

	/* unused, may expand later
	_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();

	/* Assigns properties to '_le'. Polymorphic.
	 * assignProps(String a, mixed b): Assigns b to _le[a]
	 * assignProps(Object a): Performs assignProps(key, value) for each {key:value} pair in 'a'
	 * 
	 * The following lines are equivalent:
	 * assignProps('a', 1); assignProps('b', 2);
	 * assignProps({a:1, b:2});
	 * assignProps('a', 1)('b', 2);
	 */
	function assignProps(a, b) {
		if (b == undefined) {
			// 'b' is not defined, so assign all key-value pairs in 'a' (an object) to _le
			if (typeof a != 'object') {
				return; // if 'a' isn't an object, it doesn't have keys, so looping won't work. This only happens with things like assignProps('foo')
			}
			for (i in a) { // Loop through keys of 'a' - 'i' is the key
				if (i in {}) {
					continue; // if i is a property of an empty object, skip it. This should never happen unless you add properties to the Object constructor.
				}
				_le[i] = a[i] // Assign property 'i' of 'a' to the same property of '_le'. In this loop, effectively assigns all properties of 'a' to '_le'.
			}
		} else {
			// b _is_ defined, so set _le[a] to 'b'
			_le[a] = b;
		}
		
		// returns A so it can be called again, if necessary
		return assignProps;
	}

	T = { // what is T? maybe text
		func:function(o) { // what is o? maybe object
			return !!(o&&o.call);
		},
		
		// preventDefault
		PD:function(e) { // what is e?
			if ( !e || !e.preventDefault ) {
				return;
			}
			o = e.preventDefault;
			if ( e && !!( o && o.call ) ) { // i have absolutely no idea what !! means except for "its on fire", but this is js not df :P
				e.preventDefault();
			}
		},
		
		log:function(s) { // string s
			if (console && console.log && console.log.call) { // prevent that there is no console
				console.log(s); // there is a console, so log the value of s
			}
		},
		
		/* takes a string s and removes the following things:
		 * "//"
		 * the value of l.host
		 * the value of wgScript+"/" (but whats wgScript???)
		 * the value of wgScript+"?"
		 * anything after "&"
		 * anything after "?"
		 * "title="
		 * 
		 * in that order.
		 * then returns the stripped value of s.
		 * 
		 * probably to strip a string to navigate there.
		 */
		pn:function(s) { // string 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+'/',''); 
			s = s.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;
		},
	};
	assignProps("T",T);

	assignProps('update_opts',update_opts);

	function init() {
		link = $("<a>").attr({
			href:'#le-menu-show'
		}).text("LatiMenu").attr({
			title:'Alt-L'
		});
		menuitem = $("<li>").append(link);
		$("div#p-personal ul").append(menuitem);
		if (window.console&&window.console.log) {
			console.log("Loaded LatiMenu 0.1.1");
		}
	}
	function user_latias(e){
		T.PD(e);
		window.location.href="http://dwarffortresswiki.org/index.php/User:Latias1290";
	}
	$(document).delegate('[href=#le-latias]','click', user_latias);

	MS = ( function() {
		var MS = {
			active:true, // if the thing is not working, setting it to true may be the fix :P
		};
		
		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-latias" target="_blank"><span style="color:#093">L</span><span style="color:#084">a</span>'+'<span style="color:#075">t</span><span style="color:#066">i</span></a><span style="color:#c40">Menu</span>').appendTo(MS.content);
		
		MS.cfs = { // content frames
			main:MS.content
		};
		
		MS.ccf = 'content'; // current content frame
		
		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) { // show the menu
			T.PD(evt);
			if ( _le.OPTS.menu.show_overlay ) {
				MS.$O.stop().fadeIn(500);
			}
			
			MS.$.stop().fadeIn(350);
			$('body').css({
				overflow:'hidden'
			});
			MS.active = true;
		};
		
		MS.hide = function(evt) { // hide the menu/close it
			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) { // set the size of the screen
			if (w+'' == '') { // maybe an alternate for "if (typeof w != 'number')"
				return;
			}
			
			var w;
			var h;
			var l;
			var 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();
		}; // switch to another menu
		
		MS.reset = function() { // reset the screen
			MS.show();
			MS.cf('main');
			MS.csize(80,80);
		};
		
		_links = {};
		
		MS.linklist = $("<ul>").appendTo(MS.content);
		
		
		/* add a link to the menu.
		 * string name
		 * 	name of the link, or text on the screen
		 * function func
		 *  function to execute when link it clicked
		 */
		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 _;
			return MS;
		};
	})();
	assignProps('menuScreen',MS);

	PM = (function() { // Plugins manager 0.1
		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
		} // returns the plugin list, aka the PM variable(not the PM() function)
	})();
	assignProps('PM',PM);

	plugin = (function() { // plugins manager 0.2
		var plugList = {
			plugs:{}
		};
		var niceList = new Array();
		var descs = new Array();
		
		plugin.register = function(name, func, desc) {
			count = count + 1;
			niceList[count] = name;
			descs[count] = desc;
			
			opts = $.extend({},opts);
			if(niceList.plugs[name] && !opts.override) {
				return;
			}
			
			if(T.func(func)) {
				niceList.plugs[name] = func(PM);
			}
			if(T.func(PM.plugs[name].init)) {
				niceList.plugs[name].init(PM);
			}
		}
		
		plugin.addMenuLink = function(txt) {
			ml = (T.func(PM.plugList[name].menuLinkData))?PM.plugList[name].menuLinkData():{
				name:name,
				func:PM.plugs[name].main
			}; //call menuLinkData if func, else use defaults
			MS().addLink(ml.name, ml.func);
		}
		
		plugin.getPluginID = function(name) {
			for (var i = 0; i < list.length; i++) {
				if (list[i] == name) {
					return i;
				}
			}
		}
		
		plugin.getName = function(id) {
			return list[id];
		}
		
		plugin.getPluginDesc = function(id) {
			return descs[id];
		}
	})();
	assignProps("plugin", plugin);

	keyf=function(e){ // key functions
		if (e.metaKey || e.ctrlKey) { // if control is pressed, or is metaKey is true(whatever that means), ignore the rest
			return;
		}
		
		if (e.altKey && e.shiftKey && e.keyCode == 76) {//S-A-l
			if (!MS.active) {
				MS.show(); // when SHIFT-ALT-L is pressed, open the mainscreen
				return;
			}
		}
		
		if (!MS.active) { // if mainscreen is not active, ignore the rest
			return;
		}
		T.PD(e); // ?
		if (e.keyCode == 27) { //esc
			MS.hide(); // when ESC is pressed, exit the mainscreen
		}
	};
	$(document).bind('keydown', keyf);

	local=(function(){
		__t=this;
		return function(k) {
			// i was never too good at inline if statements but this is making me thing, wtf
			return k ? __t[k] : __t;
			// probably:
			// if k is defined, return __t at index k, if k is not defined return the whole array __t
			}
	})();
	assignProps('local',local);
		
	init();

	function link_opts(PM){
		var t={};
		t.a={//actions
			v:['View','/$1'],
			e:['Edit','?title=$1&action=edit'],
			a:['New topic','?title=$1&action=edit&section=new'],
			h:['History','?title=$1&action=history'],
			u:['Subpages','/Special:PrefixIndex/$1/'],			
		};
		t.ac={//action contexts
			content:['v','e','h','u'],
			talk:['v','e','a','h'],
			special:['v'],
			user:['v','e','h','u'],
			user_talk:['v','e','h','a']
		};
		t.acx='content';
		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':'#000',color:'#fff',
			'text-align':'left','border-radius':3,'font-family':'Menlo, Courier, Monaco, monospace',
			'font-size':'10pt','white-space':'pre',padding:2}).hide().appendTo('body');
		t.help.update=function(){
			var a=[];
			var titles=[];
			for(var i=0;i<t.ac[t.acx].length;i++){
				a.push(t.a[t.ac[t.acx][i]])
				titles.push(t.ac[t.acx][i])
			}
			t.help.text('');
			for(var i=0;i<a.length;i++){
				t.help.html(t.help.html()+'<span style="color:#0f0">'+titles[i]+'</span>:'+a[i][0]+'\n');
			}
			t.help.html(t.help.html()+'<span style="color:#0f0">Esc</span>:Hide menu\n<span style="color:#0f0">Shift-Esc</span>:Disable menu');
			return t.help;
		};
		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);
			T.PD(e);
			var a=$(this);if(!a||!a.attr||!T.func(a.attr))return;
			var href=a.attr('href');
			if(a.hasClass('external')||href[0]=='#'||href.toLowerCase().indexOf('javascript')==0)return;//prevent hashes, javascript, and external links
			var ao=a.offset();
			t.help.hide().css({
				top:ao.top+a.outerHeight()+2, // outerHeight helps w/ top-of-page edit tabs, etc
				left:Math.max(0,Math.min($(document).width()-t.help.width(),ao.left-(t.help.width()/2-a.width()/2)))
			});
			t.toca();t.toa(t.hs,1000)
			t.p=T.pn(a.attr('href'));
			t.acx='content';
			if(t.p.split(':')[0].slice(-4).toLowerCase()=='talk')
				t.acx='talk';
			if(t.p.split(':')[0].toLowerCase()=='user'){
				if(t.acx=='talk') t.acx='user_talk';
				else t.acx='user';
			}
			if(t.p.split(':')[0].toLowerCase()=='special')
				t.acx='special';
			t.help.update();
			$('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]&&t.ac[t.acx].indexOf(k)+1)window.location=wgScript+t.a[k][1].replace('$1',t.p);
		};
		t.init=function(){
			$('body').on('mouseover','a',t.over);
			$('body').on('mouseout','a',t.out);
		};
		t.cache={list:{}};
		return t;
	}
	PM().register("Links",link_opts);


	function plist(PM){
		var t={};
		t.main=function(){
			MS().csize(60,80);
			MS().cf('plist');
			t.f=MS().cfs[MS().cf()];
			t.f.html('').append($("<p>").text("Plugin list: Not working yet"));
			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 LatiasMenu";
		return t;
	}
	PM().register("Plist", plist);


	function tt_disable(PM){ //stack overflow 1027762
		var t={};
		t.over=function(){
			$this = $(this);
			$this.data('title', $this.attr('title'));
			$this.attr('title', '');
		};
		t.out=function(){
			$this = $(this);
			$this.attr('title', $this.data('title'));
		};
		t.text=$("<div>");
		t.update=function(){
			t.text.html('<p>');
		};
		t.enabled=0;
		t.enable=function(){
			t.enabled=1;t.update();
			$(document).on('mouseover','a',t.over).on('mouseout','a',t.out);
		};
		$(t.enable)
		t.disable=function(){
			t.enabled=0;t.update();
			$(document).off('mouseover','a',t.over).off('mouseout','a',t.out);
		};
		t.init=function(){
			PM.addMenuLink("TTDisable");
		};
		t.title="Tooltip manager";
		t.main=function(){
			MS().csize(40,30);
			MS().cf('tt_disable');
			t.f=MS().cfs[MS().cf()];
			t.f.html('').append($("<p>").text("Works3"));
			t.ql=$("<a>").attr('href','#').text("Back").on('click',function(e){T.PD(e);t.quit();}).appendTo(t.f);
		};
		t.quit=function(){
			MS().reset();
		};
		return t;
	}
	//PM().register("TTDisable",tt_disable);

	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);


	function anMenu(PM) {
		var t = {};
		
		t.main = function() {
			MS().csize(40,30);
			MS().cf("anmenu");
			t.f = MS().cfs[MS().cf()];
			t.f.html("").append($("<p>").text("Works2"));
			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("AnMenu");
		};
		
		t.title = "AnMenu";
		return t;
	}
	PM().register("AnMenu", anMenu);

	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("LatiasMenu: cheap ripoff of LethosorMenu remodelled for personal use"));
			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 LatiasMenu";
		return t;
	}
	PM().register("About",about);

	window.LE=_le;
	if ( T.func(window.LELoad) ) {
		window.LELoad();
		return _le;
	}
});