Return to Snippet

Revision: 557
at February 2, 2007 16:24 by rolandog


Updated Code
/* Begin Type-detection functions */

//A set of helper functions for determining the type of 'a'.
var Is={
	nt:function(a){
		return(a===null||a===undefined);
	},
	Function:function(a){
		return(typeof(a)==='function')?a.constructor.toString().match(/Function/)!==null:false;
	},
	String:function(a){
		return(typeof(a)==='string')?true:(typeof(a)==='object')?a.constructor.toString().match(/string/i)!==null:false;
	},
	Array:function(a){
		return(typeof(a)==='object')?a.constructor.toString().match(/array/i)!==null||a.length!==undefined:false;
	},
	Boolean:function(a){
		return(typeof(a)==='boolean')?true:(typeof(a)==='object')?a.constructor.toString().match(/boolean/i)!==null:false;
	},
	Date:function(a){
		return(typeof(a)==='date')?true:(typeof(a)==='object')?a.constructor.toString().match(/date/i)!==null:false;
	},
	HTML:function(a){
		return(typeof(a)==='object')?a.constructor.toString().match(/html/i)!==null:false;
	},
	Number:function(a){
		return(typeof(a)==='number')?true:(typeof(a)==='object')?a.constructor.toString().match(/Number/)!==null:false;
	},
	Object:function(a){
		return(typeof(a)==='object')?a.constructor.toString().match(/object/i)!==null:false;
	},
	RegExp:function(a){
		return(typeof(a)==='function')?a.constructor.toString().match(/regexp/i)!==null:false;
	}
};

//An improved version of the 'typeof()' function.
var type={
	of:function(a){
		for(var i in Is){
			if(Is[i](a)){
				return i.toLowerCase();
			}
		}
	}
};

/* End Type-detection functions */


//Custom version of gEBC, so that it returns null if nothing is found.
document.getElementsByClass=function(a){
	var f=[],rE=new RegExp('(^|\\s)'+a+'(\\s|$)'),els=document.getElementsByTagName('*'),i;
	for(i=0;i<els.length;i+=1){
		if(rE.test(els[i].className)){
			f.push(els[i]);
		}
	}
	return(f.length)?f:null;
};

//Custom version of gEBTName, so that it returns null if nothing is found.
document.getElementsByTag=function(a){
	var r=document.getElementsByTagName(a);
	return(r.length)?r:null;
};

//shortcuts to the 'getters'.
var by={
	Id:function(a){
		return document.getElementById(a);
	},
	Class:function(a){
		return document.getElementsByClass(a);
	},
	Tag:function(a){
		return document.getElementsByTag(a);
	}
};

/* Custom dollar function ($()) (searches by Ids, Class Names and Tag Names). */
//getElementsByClassName: $('aClassName')
//getElementsByTagName: $('img')
//getElementById: $('someId')
function $(){
	var a=arguments,r=[],e,b,i;
	for(i=0;i<a.length;i+=1){
		e=a[i];
		if(Is.String(e)){
			for(var j in by){
				b=by[j](e);
				if(b && Is.Array(b)){
					for(var k=0;k<b.length;k+=1){
						r.push(b[k]);
					}
				}else if(b && !Is.Array(b)){
					r.push(b);
				}
			}
		}else{
			r.push(e);
		}
	}
	return(r.length)?(r.length===1)?r[0]:r.unique():null;
}

//helper function that weeds out duplicates on the custom $ (dollar) function.
Array.prototype.unique=function(){
	var i,e,t=[];
	for(i=0;i<this.length;i+=1){
		e=this[i];
		t=this.slice(i+1,this.length);
		while(t.indexOf(e)>-1){
			this.splice(t.indexOf(e)+i+1,1);
			t.splice(t.indexOf(e),1);
		}
	}
	return this;
};

/* the de-Konstructor function */
//usage: document.getElementsByTagName("p").d();
//usage: document.getElementById("anId").d();
//usage: "img".d();
Array.prototype.d=Object.prototype.d=String.prototype.d=function(e){
	var t=type.of(this);
	switch(t){
		case "array":for(var i=0;i<this.length;i+=1){try{this[i].parentNode.removeChild(this[i]);}catch(e){}}break;
		case "html":try{this.parentNode.removeChild(this);}catch(e){}break;
		case "string":try{$(this).d();}catch(e){}break;
		default:break;
	}
};


/* Begin DOM custom functions (rigged with 'event monitoring')*/

//usage: someobject.before(anobject);
Object.prototype.before=function(a,e){
	try{a.parentNode.insertBefore(this,a);}catch(e){}queue.check();
};

//usage: someobject.after(anobject);
Object.prototype.after=function(a,e){
	try{this.before(a.nextSibling);}catch(e){}queue.check();
};

//usage: anobject.append(someobject);
Object.prototype.append=function(a,e){
	try{this.appendChild(a);}catch(e){}queue.check();
};

//usage: anobject.prepend(someobject);
Object.prototype.prepend=function(a,e){
	try{this.before(a,this.firstChild);}catch(e){}queue.check();
};

/* End DOM custom functions*/


//sets an object's attribute (attributeName,attributeValue)
//if the attributeName is an 'on-something' event, it sends it to the queue.
Object.prototype.assign=function(aN,aV,e){
	if(aN.match(/^on\w*/)){
		queue.event(this,aN,aV);
	}else{
		try{this[aN]=aV;}catch(e){try{this.setAttribute(aN,aV);}catch(e){}}
	}
};


/* The Events Queue */
var queue={
//whenever a custom DOM insertion method is called, this function checks
//if the targets are in the document, if so, it sets the events properly.
	check:function(){
		var t,aN,aV;
		for(var i=0;i<this.targets.length;i+=1){
			t=by.Id(this.targets[i]);
			aN=this.targetEvents[i];
			aV=this.targetFunctions[i];
			if(t){
				try{t[aN]=aV;}catch(e){try{t.setAttribute(aN,aV);}catch(e){}}
				this.targets.splice(i,1);this.targetEvents.splice(i,1);this.targetFunctions.splice(i,1);
			}
		}
	},
//pushes the object's id, eventName and function into temporary arrays.
	event:function(obj,Name,Func){
		this.tag(obj);
		this.targets.push(obj.id);
		this.targetEvents.push(Name);
		this.targetFunctions.push(Func);
	},
//if the object doesn't have an id, it creates one for it.
	tag:function(obj){
		if(Is.nt(obj.id)||obj.id===""){
			var t=obj.tagName.toLowerCase();
			obj.id=t+document.getElementsByTagName(t).length;
		}
	},
	targetEvents:[],
	targetFunctions:[],
	targets:[]
};

//Creates an HTML element from a string
//adapted from http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
String.prototype.create=function(){
	return(document.createElementNS!==undefined)?document.createElementNS("http://www.w3.org/1999/xhtml",this):document.createElement(this);
};


/* The Konstructor function */
//usage: var asdf=["p","This link points to ",["a",{href:"http://example.com",onclick:function(){alert("Can\'t touch this.");return false;},title:"Clicking won't take you there, as the \'onclick\' function returns \'false\'."},"example.com"],". However, the onclick function won\'t let you go there."].k();
Array.prototype.k=function(){
	//creates the element
	var element=this[0].create();
	//if there is more stuff inside the Array
	if(this.length>1){
		for(var i=1;i<this.length;i+=1){
			//if it's an Object, it has attributes. Set them.
			if(Is.Object(this[i])){
				//attributes,attributeValue
				var a=this[i],aV;
				//attributeName in attributes
				for(var aN in a){
					aV=a[aN];
					//if you're creating a 'param' element, this is a time saver:
					//['param',{'movie':'http://example.com/asdf.avi'},{}]
					if(this[0]==="param"){element.assign('name',aN);element.assign('value',aV);}
					else{element.assign(aN,aV);}
				}
			}
			//if it's an Array, parse it and then append it.
			else if(Is.Array(this[i])){
				var child=this[i].k();
				element.appendChild(child);
			}
			//if it's a String, 
			else if(Is.String(this[i])){
				var txt=this[i].k();
				element.appendChild(txt);
			}
		}
	}
	return element;
};

//Returns a TextNode from a string.
String.prototype.k=function(){
	return document.createTextNode(this);
};

Revision: 556
at November 25, 2006 18:45 by rolandog


Updated Code
//in case the browser doesn't provide support for indexOf
if(Array.prototype.indexOf===undefined){
	Array.prototype.indexOf=function(n){
		for(var i=0;i<this.length;++i){
			if(this[i]===n){
				return i;}
			}
		return -1;
	};
}

//valid elements for HTML and XHTML
Array.prototype.validElements=("a,abbr,acronym,address,applet,area,b,base,basefont,bdo,big,blockquote,body,br,button,caption,center,cite,code,col,colgroup,dd,del,dfn,dir,div,dl,dt,em,fieldset,font,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,hr,html,i,iframe,img,input,ins,kbd,label,legend,li,link,map,menu,meta,noframes,noscript,object,ol,optgroup,option,p,param,pre,q,s,samp,script,select,small,span,stle,strike,strong,style,sub,sup,table,tbody,td,textarea,tfoot,th,thead,title,tr,tt,u,ul,var").split(",");

Array.prototype.konstruct=function(){
	//determines if something is an Array
	function isArray(b){
		var r=(typeof(b)=='object')?((b.constructor.toString().match(/array/i))!==null):false;
		return r;
	}
	//determines if something is an Object
	function isObject(b){
		var c=b.constructor.toString();
		var r=(typeof(b)=='object')?((c.match(/object/i)!==null)||(c.match(/html/i)!==null)):false;
		return r;
	}
	//if the string is a valid element name, it will return an (X)HTML element; a text node if not.
	function construct(el){
		var isElement=[].validElements.indexOf(el)>-1;
		//valid Element creation in XHTML
		//adapted from http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
		if(isElement&&document.createElementNS!==undefined){
			return document.createElementNS("http://www.w3.org/1999/xhtml",el);
		}
		if(isElement&&document.createElement!==undefined){
			return document.createElement(el);
		}
		return document.createTextNode(el);
	}
	//constructs the element
	var element=construct(this[0]);
	//if there is more stuff inside the Array
	if(this.length>1){
		for(var i=1;i<this.length;++i){
			//if it's an Object, it has attributes. Set them.
			if(isObject(this[i])){
				var attributes=this[i];
				for(var attributeName in attributes){
					try{
						element[attributeName]=attributes[attributeName];
					}
					catch(error){
						element.setAttribute(attributeName,attributes[attributeName]);
					}
				}
			}
			//if it's an Array, parse it and then append it.
			else if(isArray(this[i])){
				var child=this[i].kall();
				element.appendChild(child);
			}
		}
	}
	return element;
};

//this secondary function allows us to form elements within nested arrays
Array.prototype.kall=function(){
	return this.konstruct();
};

/*	Sample usage:
	var a_div_element = ["div",{style:"width:400px;"},["p",["Ni!"]]].konstruct();
	//a_div_element is a 'div' element, 400px wide that has a nested paragraph with a nested text element that says "Ni!".
*/

Revision: 555
at November 25, 2006 18:32 by rolandog


Updated Code
//in case the browser doesn't provide support for indexOf
if(Array.prototype.indexOf===undefined){
	Array.prototype.indexOf=function(n){
		for(var i=0;i<this.length;++i){
			if(this[i]===n){
				return i;}
			}
		return -1;
	};
}

//valid elements for HTML and XHTML
Array.prototype.validElements=("a,abbr,acronym,address,applet,area,b,base,basefont,bdo,big,blockquote,body,br,button,caption,center,cite,code,col,colgroup,dd,del,dfn,dir,div,dl,dt,em,fieldset,font,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,hr,html,i,iframe,img,input,ins,kbd,label,legend,li,link,map,menu,meta,noframes,noscript,object,ol,optgroup,option,p,param,pre,q,s,samp,script,select,small,span,stle,strike,strong,style,sub,sup,table,tbody,td,textarea,tfoot,th,thead,title,tr,tt,u,ul,var").split(",");

Array.prototype.konstruct=function(){
	//determines if something is an Array
	function isArray(b){
		var r=(typeof(b)=='object')?((b.constructor.toString().match(/array/i))!==null):false;
		return r;
	}
	//determines if something is an Object
	function isObject(b){
		var c=b.constructor.toString();
		var r=(typeof(b)=='object')?((c.match(/object/i)!==null)||(c.match(/html/i)!==null)):false;
		return r;
	}
	//if the string is a valid element name, it will return an (X)HTML element; a text node if not.
	function construct(el){
		var isElement=[].validElements.indexOf(el)>-1;
		//valid Element creation in XHTML
		//adapted from http://simon.incutio.com/archive/2003/06/15/javascriptWithXML
		if(isElement&&document.createElementNS!==undefined){
			return document.createElementNS("http://www.w3.org/1999/xhtml",el);
		}
		if(isElement&&document.createElement!==undefined){
			return document.createElement(el);
		}
		return document.createTextNode(el);
	}
	//constructs the element
	var element=construct(this[0]);
	//if there is more stuff inside the Array
	if(this.length>1){
		for(var i=1;i<this.length;++i){
			//if it's an Object, it has attributes. Set them.
			if(isObject(this[i])){
				var attributes=this[i];
				for(var attributeName in attributes){
					try{
						element[attributeName]=attributes[attributeName];
					}
					catch(error){
						element.setAttribute(attributeName,attributes[attributeName]);
					}
				}
			}
			//if it's an Array, parse it and then append it.
			else if(isArray(this[i])){
				var child=this[i].kall();
				element.appendChild(child);
			}
		}
	}
	return element;
};

//this secondary function allows us to form elements within nested arrays
Array.prototype.kall=function(){
	return this.konstruct();
};

Revision: 554
at July 20, 2006 17:55 by rolandog


Updated Code
/*	In case the user doesn't have these fuctions defined	*/
if(typeof Array.prototype.indexOf==='undefined'){Array.prototype.indexOf=function(n){for(var i=0;i<this.length;++i){if(this[i]===n){return i;}}return -1;};}
if(typeof Array.prototype.push==='undefined'){Array.prototype.push=function(){for(var i=0,b=this.length,a=arguments;i<a.length;++i){this[b+i]=a[i];}return this.length;};}

/*	All elements allowed for (X)HTML	*/
Array.prototype.validElements=("a,abbr,acronym,address,applet,area,b,base,basefont,bdo,big,blockquote,body,br,button,caption,center,cite,code,col,colgroup,dd,del,dfn,dir,div,dl,dt,em,fieldset,font,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,hr,html,i,iframe,img,input,ins,kbd,label,legend,li,link,map,menu,meta,noframes,noscript,object,ol,optgroup,option,p,param,pre,q,s,samp,script,select,small,span,stle,strike,strong,style,sub,sup,table,tbody,td,textarea,tfoot,th,thead,title,tr,tt,u,ul,var").split(",");

/*	All attributes allowed for (X)HTML	*/
Object.prototype.validAttributes=("abbr,accept,accept-charsets,accesskey,action,align,alt,archive,axis,bgcolor,border,cellpadding,cellspacing,char,charoff,charset,checked,cite,class,classid,codebase,codetype,cols,colspan,coords,data,datetime,declare,defer,dir,disabled,enctype,for,frame,frameborder,headers,height,href,hreflang,hspace,id,ismap,label,lang,longdesc,marginheight,marginwidth,maxlength,media,method,name,nohref,noresize,onblur,onchange,onclick,ondblclick,onfocus,onkeydown,onkeypress,onkeyup,onload,onmousedown,onmousemove,onmouseout,onmouseover,onmouseup,onreset,onselect,onsubmit,onunload,profile,readonly,rel,rev,rows,rowspan,rules,scrolling,shape,size,src,standby,style,summary,tabindex,target,title,type,usemap,valign,value,vspace,width,xml:lang,xmlL,xmlns").split(",");

function isArray(b){var r=(typeof(b)=='object')?((b.constructor.toString().match(/array/i))!==null):false;return r;}
function isObject(b){var c=b.constructor.toString();var r=(typeof(b)=='object')?((c.match(/object/i)!==null)||(c.match(/html/i)!==null)):false;return r;}

/*	An alternative to document.createElement() using Arrays, and at the end adding ".konstruct();"
	Usage example for a div element with a width of '400px', and paragraph for a child that says 'Ni!':
	var a_div_element = ["div",{style:"width:400px;"},["p","Ni!"]].konstruct();	*/
Array.prototype.konstruct=function(){
	var Es=this.validElements;
	//Creates an element, if it has attributes, it sets them.
	function e(eN,eA){
		var r;
		if(Es.indexOf(eN)>-1){
			r=document.createElement(eN);
			if(eA!==undefined){
				var As=eA.validAttributes; var l=0; var a=[];
				for(var j=0;j<As.length;++j){
					if(eA[As[j]]!==undefined){
						a.push(As[j]);
						++l;
					}
				}
				if(l){
					for(var i=0;i<l;++i){
						try{r[a[i]]=eA[a[i]];}
						catch(e){r.setAttribute(a[i],eA[a[i]]);}
					}
				}
			}
		}
		else{r=document.createTextNode(eN);}
		return r;
	}
	var t=this;var r;var N=t[0];var A=t[1];var ta;
	if(A!==undefined){
		if(isObject(A)){r=e(N,A);}
		else if(isArray(A)){r=e(N,{});ta=A.kall();r.appendChild(ta);}
		else{r=e(N,{});ta=e(A,{});r.appendChild(ta);}
	}
	else{r=e(N,{});}
	if(t.length>2){
		var tb;
		for(var j=2;j<t.length;++j){
			if(isArray(t[j])){tb=t[j].kall();r.appendChild(tb);}
			else if(isObject(t[j])){r.appendChild(t[j]);}
			else{tb=[t[j]].kall();r.appendChild(tb);}
		}
	}
	return r;
};

/*	This allows iteration for arrays inside arrays inside arrays... ad infinitum.	*/
Array.prototype.kall=function(){
	return this.konstruct();
};

Revision: 553
at July 19, 2006 18:31 by rolandog


Updated Code
/*	All elements allowed for (X)HTML	*/
var Es=("a,abbr,acronym,address,applet,area,b,base,basefont,bdo,big,blockquote,body,br,button,caption,center,cite,code,col,colgroup,dd,del,dfn,dir,div,dl,dt,em,fieldset,font,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,hr,html,i,iframe,img,input,ins,kbd,label,legend,li,link,map,menu,meta,noframes,noscript,object,ol,optgroup,option,p,param,pre,q,s,samp,script,select,small,span,stle,strike,strong,style,sub,sup,table,tbody,td,textarea,tfoot,th,thead,title,tr,tt,u,ul,var").split(",");

/*	All attributes allowed for (X)HTML	*/
var As=("abbr,accept,accept-charsets,accesskey,action,align,alt,archive,axis,bgcolor,border,cellpadding,cellspacing,char,charoff,charset,checked,cite,class,classid,codebase,codetype,cols,colspan,coords,data,datetime,declare,defer,dir,disabled,enctype,for,frame,frameborder,headers,height,href,hreflang,hspace,id,ismap,label,lang,longdesc,marginheight,marginwidth,maxlength,media,method,name,nohref,noresize,onblur,onchange,onclick,ondblclick,onfocus,onkeydown,onkeypress,onkeyup,onload,onmousedown,onmousemove,onmouseout,onmouseover,onmouseup,onreset,onselect,onsubmit,onunload,profile,readonly,rel,rev,rows,rowspan,rules,scrolling,shape,size,src,standby,style,summary,tabindex,target,title,type,usemap,valign,value,vspace,width,xml:lang,xmlL,xmlns").split(",");

/*	Makes the properties you pass in an array publicly available	*/
Object.prototype.makePublic=function(array){
	var j=0; this.propertyNames=[]; this.propertyValues=[];
	for(var i=0;i<array.length;++i){
		if(this[array[i]]!==undefined){
			this.propertyNames.push(array[i]);
			this.propertyValues.push(this[array[i]]);
			++j;
		}
	}
	return j;
};
/*	An alternative to document.createElement() using Arrays, and at the end adding ".konstruct();"
	Usage example for a div element with a width of '400px', and paragraph for a child that says 'Ni!':
	var a_div_element = ["div",{style:"width:400px;"},["p",{},["Ni!"]]].konstruct();
	Note that you can pass an empty object or just place two commas for the paragraph.
	It relies on the array called 'As' which has all the attributes allowed, and the array called Es
	which has all the elements allowed.	*/
Array.prototype.konstruct=function(){
	//Creates an element, if it has attributes, it sets them.
	function e(eN,eA){
		var r;
		if(Es.indexOf(eN)>-1){
			r=document.createElement(eN);
			if(eA!==undefined){
				var l=eA.makePublic(As);
				if(l){
					for(var i=0;i<l;++i){r.setAttribute(eA.propertyNames[i],eA.propertyValues[i]);}
				}
			}
		}
		else{r=document.createTextNode(eN);}
		return r;
	}
	var t=this; var r; var N=t[0]; var A=t[1];
	r=e(N,A);
	if(t.length>2){
		var ta;
		for(var j=2;j<t.length;++j){
			ta=t[j].kall();
			r.appendChild(ta);
		}
	}
	return r;
};

/*	This allows iteration for arrays inside arrays inside arrays... ad infinitum.	*/
Array.prototype.kall=function(){
	return this.konstruct();
};

Revision: 552
at July 19, 2006 17:58 by rolandog


Updated Code
/*	All elements allowed for (X)HTML	*/
var Es=("a,abbr,acronym,address,applet,area,b,base,basefont,bdo,big,blockquote,body,br,button,caption,center,cite,code,col,colgroup,dd,del,dfn,dir,div,dl,dt,em,fieldset,font,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,hr,html,i,iframe,img,input,ins,kbd,label,legend,li,link,map,menu,meta,noframes,noscript,object,ol,optgroup,option,p,param,pre,q,s,samp,script,select,small,span,stle,strike,strong,style,sub,sup,table,tbody,td,textarea,tfoot,th,thead,title,tr,tt,u,ul,var").split(",");

/*	All attributes allowed for (X)HTML	*/
var As=("abbr,accept,accept-charsets,accesskey,action,align,alt,archive,axis,bgcolor,border,cellpadding,cellspacing,char,charoff,charset,checked,cite,class,classid,codebase,codetype,cols,colspan,coords,data,datetime,declare,defer,dir,disabled,enctype,for,frame,frameborder,headers,height,href,hreflang,hspace,id,ismap,label,lang,longdesc,marginheight,marginwidth,maxlength,media,method,name,nohref,noresize,onblur,onchange,onclick,ondblclick,onfocus,onkeydown,onkeypress,onkeyup,onload,onmousedown,onmousemove,onmouseout,onmouseover,onmouseup,onreset,onselect,onsubmit,onunload,profile,readonly,rel,rev,rows,rowspan,rules,scrolling,shape,size,src,standby,style,summary,tabindex,target,title,type,usemap,valign,value,vspace,width,xml:lang,xmlL,xmlns").split(",");

/*	Makes the properties you pass in an array publicly available	*/
Object.prototype.makePublic=function(array){
	var j=0; this.propertyNames=[]; this.propertyValues=[];
	for(var i=0;i<array.length;++i){
		if(this[array[i]]!==undefined){
			this.propertyNames.push(array[i]);
			this.propertyValues.push(this[array[i]]);
			++j;
		}
	}
	return j+1;
};
/*	An alternative to document.createElement() using Arrays, and at the end adding ".konstruct();"
	Usage example for a div element with a width of '400px', and paragraph for a child that says 'Ni!':
	var a_div_element = ["div",{style:"width:400px;"},["p",{},["Ni!"]]].konstruct();
	Note that you can pass an empty object or just place two commas for the paragraph.
	It relies on the array called 'As' which has all the attributes allowed, and the array called Es
	which has all the elements allowed.	*/
Array.prototype.konstruct=function(){
	//Creates an element, if it has attributes, it sets them.
	function e(eN,eA){
		var r;
		if(Es.indexOf(eN)>-1){
			r=document.createElement(eN);
			if(eA!==undefined){
				var l=eA.makePublic(As);
				if(l){
					for(var i=0;i<l;++i){r.setAttribute(eA.propertyNames[i],eA.propertyValues[i]);}
				}
			}
		}
		else{r=document.createTextNode(eN);}
		return r;
	}
	var t=this; var r; var N=t[0]; var A=t[1];
	r=e(N,A);
	if(t.length>2){
		var ta;
		for(var j=2;j<t.length;++j){
			ta=t[j].kall();
			r.appendChild(ta);
		}
	}
	return r;
};

/*	This allows iteration for arrays inside arrays inside arrays... ad infinitum.	*/
Array.prototype.kall=function(){
	return this.konstruct();
};

Revision: 551
at July 19, 2006 11:54 by rolandog


Updated Code
/*	All elements allowed for (X)HTML	*/
var Es=("a,abbr,acronym,address,applet,area,b,base,basefont,bdo,big,blockquote,body,br,button,caption,center,cite,code,col,colgroup,dd,del,dfn,dir,div,dl,dt,em,fieldset,font,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,hr,html,i,iframe,img,input,ins,kbd,label,legend,li,link,map,menu,meta,noframes,noscript,object,ol,optgroup,option,p,param,pre,q,s,samp,script,select,small,span,stle,strike,strong,style,sub,sup,table,tbody,td,textarea,tfoot,th,thead,title,tr,tt,u,ul,var").split(",");

/*	All attributes allowed for (X)HTML	*/
var As=("abbr,accept,accept-charsets,accesskey,action,align,alt,archive,axis,bgcolor,border,cellpadding,cellspacing,char,charoff,charset,checked,cite,class,classid,codebase,codetype,cols,colspan,coords,data,datetime,declare,defer,dir,disabled,enctype,for,frame,frameborder,headers,height,href,hreflang,hspace,id,ismap,label,lang,longdesc,marginheight,marginwidth,maxlength,media,method,name,nohref,noresize,onblur,onchange,onclick,ondblclick,onfocus,onkeydown,onkeypress,onkeyup,onload,onmousedown,onmousemove,onmouseout,onmouseover,onmouseup,onreset,onselect,onsubmit,onunload,profile,readonly,rel,rev,rows,rowspan,rules,scrolling,shape,size,src,standby,style,summary,tabindex,target,title,type,usemap,valign,value,vspace,width,xml:lang,xmlL,xmlns").split(",");

/*	Makes the properties you pass in an array publicly available	*/
Object.prototype.makePublic=function(array){
	var j=0; this.propertyNames=[]; this.propertyValues=[];
	for(var i=0;i<array.length;++i){
		if(this[array[i]]!==undefined){
			this.propertyNames.push(array[i]);
			this.propertyValues.push(this[array[i]]);
			++j;
		}
	}
	return j+1;
};
/*	An alternative to document.createElement() using Arrays, and at the end adding ".konstruct();"
	Usage example for a div element with a width of '400px', and paragraph for a child that says 'Ni!':
	var a_div_element = ["div",{style:"width:400px;"},["p",{},["Ni!"]]].konstruct();
	Note that you can pass an empty object or just place two commas for the paragraph.
	It relies on the array called 'As' which has all the attributes allowed, and the array called Es
	which has all the elements allowed.	*/
Array.prototype.konstruct=function(){
	//Creates an element, if it has attributes, it sets them.
	function e(eN,eA){
		var r;
		if(Es.indexOf(eN)>-1){
			r=document.createElement(eN);
			if(eA!==undefined){
				var l=eA.makePublic(As);
				if(l){
					for(var i=0;i<l;++i){r.setAttribute(eA.propertyNames[0],eA.propertyValues[0]);}
				}
			}
		}
		else{r=document.createTextNode(eN);}
		return r;
	}
	var t=this; var r; var N=t[0]; var A=t[1];
	r=e(N,A);
	if(t.length>2){
		var ta;
		for(var j=2;j<t.length;++j){
			ta=t[j].kall();
			r.appendChild(ta);
		}
	}
	return r;
};

/*	This allows iteration for arrays inside arrays inside arrays... ad infinitum.	*/
Array.prototype.kall=function(){
	return this.konstruct();
};

Revision: 550
at July 18, 2006 15:16 by rolandog


Initial Code
var Es=("a,abbr,acronym,address,applet,area,b,base,basefont,bdo,big,blockquote,body,br,button,caption,center,cite,code,col,colgroup,dd,del,dfn,dir,div,dl,dt,em,fieldset,font,form,frame,frameset,h1,h2,h3,h4,h5,h6,head,hr,html,i,iframe,img,input,ins,kbd,label,legend,li,link,map,menu,meta,noframes,noscript,object,ol,optgroup,option,p,param,pre,q,s,samp,script,select,small,span,stle,strike,strong,style,sub,sup,table,tbody,td,textarea,tfoot,th,thead,title,tr,tt,u,ul,var").split(",");
var As=("abbr,accept,accept-charsets,accesskey,action,align,alt,archive,axis,bgcolor,border,cellpadding,cellspacing,char,charoff,charset,checked,cite,class,classid,codebase,codetype,cols,colspan,coords,data,datetime,declare,defer,dir,disabled,enctype,for,frame,frameborder,headers,height,href,hreflang,hspace,id,ismap,label,lang,longdesc,marginheight,marginwidth,maxlength,media,method,name,nohref,noresize,onblur,onchange,onclick,ondblclick,onfocus,onkeydown,onkeypress,onkeyup,onload,onmousedown,onmousemove,onmouseout,onmouseover,onmouseup,onreset,onselect,onsubmit,onunload,profile,readonly,rel,rev,rows,rowspan,rules,scrolling,shape,size,src,standby,style,summary,tabindex,target,title,type,usemap,valign,value,vspace,width,xml:lang,xmlL,xmlns").split(",");
function e(eN,eA){
	function gA(o){
		var a=[];var aN=[];var aV=[];var l=As.length;
		for(var i=0;i<l;++i){
			if(o[As[i]]!==undefined){aN.push(As[i]);aV.push(o[As[i]]);}
		}
		if(aN.length>0){a.push(aN); a.push(aV);	return a;}
		return false;
	}
	var r;
	if(Es.indexOf(eN)>-1){
		r=document.createElement(eN);
		if(eA!==undefined){
			var a=gA(eA);
			if(a){
				var l=a[0].length;
				for(var i=0;i<l;++i){r.setAttribute(a[0][i],a[1][i]);}
			}
		}
		var b=arguments;
		if(b.length>2){
			for(var j=2;j<b.length;++j){var f=document.createDocumentFragment();f.appendChild(b[j]);r.appendChild(f);}
		}
	}
	else{r=document.createTextNode(eN);}
	return r;
}
Array.prototype.konstruct=function(){
	var t=this; var r; var eN=t[0]; var eA=t[1];
	r=e(eN,eA);
	if(t.length>2){
		var ta;
		for(var j=2;j<t.length;++j){
			ta=t[j].kall();
			r.appendChild(ta);
		}
	}
	return r;
};
Array.prototype.kall=function(){
	return this.konstruct();
};

Initial URL
http://rolandog.com/archives/2006/07/18/konstructor

Initial Description
Constructs an element out of arrays. Using nested arrays translates into nested ChildNodes.
Update 2006-07-19: Added documentation (comments).
Update 2006-11-25: Reduced the ammount of code needed.

Initial Title
Konstructor: Dynamic Element Kreator

Initial Tags
javascript, DOM

Initial Language
JavaScript