/*
libTBWL.js
* Project: Toolbar Watchlist, Version: 0.1.1beta, Date: 2007-08-27
* Copyright (C) 2007 ] ([email protected])
* http://www.gnu.org/copyleft/gpl.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE.
* See the GNU General Public License for more details.
Licence:
You may redistribute this script either under the terms of the GNU GPL
or GNU FDL with no invariant sections. But either way, you have to
accept the non-warranty clause as stated in the GNU GPL.
Vars:
vTBWL_Addons
Funcs:
TBWL_LinkTitle
TBWL_UserTitle
TBWL_CommentTitle
TBWL_Cookie
TBWL_CreateElement
TBWL_StructureToHMTL
TBWL_CallArrayFunctions
TBWL_Initialize
TBWL_RegisterAddon
*/
var vTBWL_Addons = ;
var TBWL_LinkTitle = function(ns, title) {
return title;
};
var TBWL_UserTitle = function(name) {
return name;
};
var TBWL_WikiEncodeURI = function(str) {
var elm = /()/g;
var callback = function(a, b) {
var res = b.charCodeAt(0).toString(16).toUpperCase();
if(res.length<2) res = "0"+res;
return "%"+res;
};
return encodeURI(str).replace(elm, callback).replace(/\x22/g, "%22").replace(/\\/g, "%5C");
};
var TBWL_WikiEncodeAnchor = function(str) {
var elm = /()/g;
var callback = function(a, b) {
var res = b.charCodeAt(0).toString(16).toUpperCase();
if(res.length<2) res = "0"+res;
return "%"+res;
};
return encodeURI(str).replace(elm, callback).replace(/\x22/g, "%22").replace(/\\/g, "%5C");
};
var TBWL_CommentTitle = function(depth, res, str, ns, title) {
var doLink = function(text) {
if(!text) return;
var pattern = /^(*)\x23?(*)\x7c?(.*)$/;
var callback = function(a, Link, Abs, Text) {
if(Abs === "") {
if(Text === "") Text = Link; // Text
res.push(depth, ,
depth+1, ); // Link|Text
}
else {
if(Text === "") Text = Link+"#"+Abs; // Link#Abs
res.push(depth, ,
depth+1, ); // Link#Abs|Text
};
};
text.replace(pattern, callback);
};
var doText = function(text) {
if(!text) return;
var pattern = /(.*?)\\]/g;
var callback = function(a, Text, Link) {
res.push(depth, );
doLink(Link);
};
(text+"]").replace(pattern, callback);
};
var doSection = function(text) {
if(!text) return;
var pattern = /\\]|\\]/g;
text = text.replace(pattern, "\1");
res.push(
depth, ,
depth+1, ,
depth+1, ,
depth+2, ,
depth+1, );
};
var pattern = /(.*?)\/\* ?(.*?) ?\*\//g;
var callback = function(a, Text, Section) {
doText(Text);
doSection(Section);
};
(str+"/**/").replace(pattern, callback);
};
var TBWL_GetAjaxObject = function() {
var res;
if((typeof(XMLHttpRequest) == "function") && (res = new XMLHttpRequest())) return res;
if(typeof(ActiveXObject) == "function") {
var MsAjax = ;
for(var i=0; i<MsAjax.length; i++) {
try { if(res = new ActiveXObject(MsAjax)) return res; }
catch(e) {};
};
};
return false;
};
var TBWL_Cookie = {
"Read": (function(name) {
var arr = document.cookie.split(';');
for(var i=0; i<arr.length; i++) {
var b = true;
var c;
var aName = ;
var aText = arr.split('');
var cName = name.split('');
while((c = aText.shift()) === ' ') {};
do {
aName.push(c)
if(c !== cName.shift()) { b = false; break; };
} while('=' !== (c = aText.shift()));
if(b && (cName.length==0))
return aText.join("");
};
return false;
}),
"Write": (function(name, value, expires) {
var expires = new Date((new Date()).getTime() + expires).toGMTString();
document.cookie = name +"="+ value +"; expires="+ expires +";";
}),
"Delete": (function(name) {
document.cookie = name+ "=; expires=Thu, 01-Jan-70 00:00:01 GMT;";
})
};
var TBWL_CreateElement = {
"generic": (function(/* func, */) {
var a = ;
for(var i=1; i<arguments.length; i++) a.push(arguments);
var res = TBWL_CreateElement].apply(this, a);
if(typeof(res) == "object") this.appendChild(res);
return res;
}),
"Node": (function(tag, className) {
var res = document.createElement(tag);
res.className = className;
res.app = TBWL_CreateElement.generic;
return res;
}),
"Text": (function(text, className) {
var res = TBWL_CreateElement.Node("span", className);
res.appendChild((typeof(text) == "string") ?document.createTextNode(text) :text);
return res;
}),
"Space": (function() {
this.appendChild(document.createTextNode(" "));
}),
"Link": (function(text, href, className) {
var res = TBWL_CreateElement.Node("a", "");
res.appendChild((typeof(text) == "string") ?document.createTextNode(text) :text);
res.setAttribute("href", href);
return TBWL_CreateElement.Text(res, className);
}),
"WPLink": (function(server, ns, title, text, data, className, anchor) {
if(server == "") var server = "/w/";
if(ns != "") var title = ns +':'+ title;
var href = "/w/index.php?title=" +TBWL_WikiEncodeURI(title.replace(/ /g, "_"));
if(data != "") href += "&"+data;
if(anchor != "") href += "#" + TBWL_WikiEncodeAnchor(anchor.replace(/ /g, "_")).replace(/%/g, ".");
return TBWL_CreateElement.Link(text, href, className);
}),
"AddClass": (function(className) {
if(className != "") {
var s = this.getAttribute("class");
return(this.className = (s == "" ?"" :s+" ") + className);
};
return className;
}),
"SetAttribute": (function(name, value) {
return this.setAttribute(name, value);
}),
"AddNode": (function(obj) {
if(!obj) { return obj; }
return this.appendChild((typeof(obj)=="string") ?document.createTextNode(obj) :obj);
})
};
var TBWL_StructureToHMTL = function(container, arr) {
if(!arr) return false;
var index = 0;
var e;
var iter = function(container) {
var depth = arr;
while(true) {
if(typeof(e = arr) == "undefined") return;
var i = arr;
var elem = e && container && TBWL_CreateElement.generic.apply(container, e);
index += 2;
switch(i - depth) {
case 1: iter(elem);
case 0: continue;
default: return;
};
};
};
iter(container);
return container;
};
var TBWL_CallArrayFunctions = function(/* funcs, */) {
var funcs = arguments;
var a = ;
for(var i=1; i<arguments.length; i++) a.push(arguments);
for(var i=0; i<funcs.length; i++) {
var func = (typeof(funcs) == "string") ?eval(funcs) :funcs;
func.apply(this, a);
}
};
var TBWL_RegisterAddon = function(friendlyName, scriptName) {
// TODO: Stub
};
var TBWL_Initialize = function() {
TBWL_CallArrayFunctions(vTBWL_Addons);
};
$(function() {
TBWL_Initialize();
});
TBWL_RegisterAddon("library", "libTBWL");