1 var yType = function(data){// implements dom + _YOBJID 2 if(!data)return typeof(data); 3 if(data.tagName)return "dom"; 4 if(data._YOBJID)return data._YOBJID; 5 if(data.join)return "array"; 6 return typeof(data); 7 }; 8 var serialise = function(owner,classname){ 9 this.owner = owner; 10 this.owner._YOBJID = classname; 11 this.owner.varname = newVarName(classname); 12 eval(""+this.owner.varname+" = this.owner;"); 13 }; 14 15 /** 16 * class utilities Not instanciable. 17 * @constructor 18 */ 19 var YClass = { 20 /** extends functions and vars from an other class. 21 * @param {object} target 22 * @param {object} source 23 */ 24 xtends : function(target,source){ 25 var opile = [] ; 26 var in_opile = function(value){ 27 for(var i in opile)if(value==opile[i])return true; 28 opile.push(value); 29 return false; 30 }; 31 var _xtendz = function(tgt,src){ 32 for(var pt in src){ 33 if(src[pt]!=null){ 34 if(src[pt].join){ 35 tgt[pt]=[]; _xtendz(tgt[pt],src[pt]); 36 }else if(typeof(src[pt])=="object" && !src.tagName){ 37 if(in_opile(src[pt]))return tgt; 38 tgt[pt]={}; try{_xtendz(tgt[pt],src[pt]);}catch(e){ 39 throw("\nxtends pile overflow at:"+ 40 "\nindex='"+pt+"';"+ 41 "\nsource("+typeof(src[pt])+")="+src[pt]+";");}} 42 else if(typeof(src[pt])=="number")tgt[pt]=0+src[pt]; 43 else if(typeof(src[pt])=="string")tgt[pt]=""+src[pt]; 44 else tgt[pt]=src[pt]; 45 } 46 }return tgt; 47 }; 48 return _xtendz(target,source); 49 }, 50 /** 51 */ 52 extendPrototype:function(target,targetName,parentName){ 53 eval(targetName+".prototype = new "+parentName+"();"); 54 eval(targetName+".prototype.constructor = "+targetName+";"); 55 eval(targetName+".prototype.constructorName = '"+targetName+"';"); 56 eval(targetName+".prototype.supr = "+parentName+".prototype;"); 57 }, 58 /** override function in an inherited object.<br/> 59 * <b>Must be done after object declaration</b><br/> 60 * ex :<br/> 61 * <code> 62 var DoTwo=function(){};<br/> 63 DoTwo.prototype.countMe=function(anynum){ return anynum*2 };<br/> 64 var DoTen=function(){ DoTwo.call( this );}<br/> 65 DoTwo.prototype = new DoTwo();<br/> 66 YClass.override('DoTen','DoTwo','countMe',function(anynum){ return this.countMe(anynum)*5;});<br/> 67 //new DoTwo().countMe(anynum) will return anynum*2<br/> 68 //new DoTen().countMe(anynum) will return anynum*2*5<br/> 69 * </code><br/> 70 * /!\be carefull about override order : any call to your overrided function made in an existing function is replaced. 71 * @param {string} childName the child class name. 72 * @param {string} parentName the parent class name. 73 * @param {string} functionName the overrided function name. 74 * @param {function} Function the overriing new function. 75 */ 76 override:function(childName,parentName,functionName,Function){ 77 var nufname=functionName; 78 eval("var nameUsed=true;"); 79 while(nameUsed){ 80 nufname="_"+nufname; 81 eval("nameUsed= ("+childName+".prototype."+nufname+"||"+parentName+".prototype."+nufname+");"); 82 } 83 eval(childName+".prototype."+nufname+"="+parentName+".prototype."+functionName+";"); 84 this.rflist = []; 85 eval("var rflist = [];for(var i in "+childName+".prototype){if(typeof("+childName+".prototype[i])=='function'&&("+childName+".prototype[i]+'').indexOf('this."+functionName+"(')){this.rflist[i]="+childName+".prototype[i];}}"); 86 this.rflist[functionName]=Function; 87 for(var i in this.rflist){ 88 var fstr = YStr.replace("this."+functionName,"this."+nufname,""+this.rflist[i]); 89 // _alert(i+" : \n"+fstr); 90 eval(childName+".prototype."+i+"="+fstr+";"); 91 } 92 }, 93 /** extends functions from an inner object. 94 * @param {object} target the owner class. 95 * @param {string} sourceName the inner var name of the source class instance. 96 */ 97 xFunctions:function(target,sourceName){ 98 if(!target||!target[sourceName])return null; 99 this.target = target; 100 for(var i in this.target[sourceName]){ 101 if(typeof(this.target[sourceName][i])=="function"){ 102 var fa = YClass.functionArgs(this.target[sourceName][i]); 103 var evst = "this.target."+i+"=function("+fa.join(",")+"){return this."+sourceName+"."+i+"("+fa.join(",")+")};"; 104 eval(evst); 105 } 106 } 107 }, 108 /** get function argments name list. 109 * @param {function} source 110 @ return {array} 111 */ 112 functionArgs:function(source){ 113 if(typeof(source)=="function"||typeof(source)=="string"){ 114 var str = ""+source; 115 var bounds = [str.indexOf("("),str.indexOf(")")]; 116 if(bounds[0]>-1&&bounds[1]>-1&&bounds[0]<bounds[1]){ 117 var ast = str.substr(bounds[0]+1,bounds[1]-bounds[0]-1); 118 var result = YStr.replace(" ","",ast).split(","); 119 //alert(str+"\n"+bounds+"\n"+ast+"\n"+result); 120 return result; 121 }else return []; 122 123 }else return []; 124 } 125 }; 126 127 YClass.___abstractPile = []; 128 YClass.Abstract = function(){// usefull for events with class 129 this.varname = ""; 130 var _____evtlinks=[]; 131 var _____exists=false; 132 this.linkEvent = function(target,evtDesc,functionName){ 133 var index = YEvt.add(target,evtDesc,function(evt,dat){dat.owner[dat.fnam](evt,dat.edesc);}, 134 {owner:this,edesc:evtDesc,fnam:functionName}); 135 _____evtlinks.push(index); 136 }; 137 this.resurrect = function(){ 138 if(!_____exists){ 139 this.varname = "YClass.___abstractPile["+YClass.___abstractPile.length+"]"; 140 YClass.abstractPile.push(this); 141 _____exists=true; 142 } 143 }; 144 this.kill = function(){ 145 if(_____exists){ 146 for(var i in _____evtlinks)YEvt.listeners[_____evtlinks[i]]=null; 147 _____evtlinks=[]; 148 eval(this.varname+" = null;"); 149 _____exists=false; 150 } 151 }; 152 this.resurrect(); 153 }; 154 155 var xtends = YClass.xtends; 156 // 157 var mimetism = function(target,source){ 158 for(var pt in source){ 159 if(source[pt]!=null){ 160 if(target[pt] && (source[pt].join || (typeof(source[pt])=="object" && !source.tagName)) ){ 161 mimetism(target[pt],source[pt]); 162 }else try{target[pt]=source[pt]}catch(e){}; 163 } 164 } 165 }// JavaScript Document