LL={
lastReturn:null,
lastHttpRequest:null,
lastEventObject:null,
loadEventMaps:function(eventMap){
var preInit=new LL.Event(LL.Event.PRE_INIT,true,false);
document.dispatchEvent(preInit);
this._buildTagFactory();
eventMap.forEach(function(eventMap){
this._loadEventHandlers(eventMap);
},this);
var init=new LL.Event(LL.Event.INIT,true,false);
document.dispatchEvent(init);
},
_loadEventHandlers:function(json){
json.EventHandlers.forEach(function(eventHandlerJson){
LL.TagFactory.create("EventHandlers",eventHandlerJson).executeTag();
},this);
},
_buildTagFactory:function(){
LL.TagFactory.add("EventHandlers",LL.EventHandler);
LL.TagFactory.add("ObjectBuilders",LL.ObjectBuilder);
LL.TagFactory.add("MethodInvokers",LL.MethodInvoker);
LL.TagFactory.add("AjaxInvokers",LL.AjaxInvoker);
LL.TagFactory.add("MethodInjectors",LL.MethodInjector);
LL.TagFactory.add("PropertyInjectors",LL.PropertyInjector);
}
};
LL.Utils={
strip:function(str){
str=str.replace(/^\s+/,'');
return str.replace(/\s+$/,'');
},
stripCurlyBraces:function(str){
str=LL.Utils.strip(str);
str=str.substring(1,str.length-1);
return LL.Utils.strip(str);
},
isDynamicValue:function(value){
if(typeof value=='string'){
value=LL.Utils.strip(value);
if(value.charAt(0)=="{"&&value.charAt(value.length-1)=="}")return true;
}
return false;
}
};
LL.Proxy={
instance:function(constructor,args,properties,cache){
if(cache===false)return this.create(constructor,args||[],properties||{});
var instance=(constructor.__singleton__)?constructor.__singleton__:this.create(constructor,args||[],properties||{});
if(!constructor.__singleton__)constructor.__singleton__=instance;
return instance;
},
create:function(constructor,args,properties){
var a=args;
a.concat(new Array(20-a.length));
var instance=new constructor(a[0],a[1],a[2],a[3],a[4],a[5],a[6],a[7],a[8],a[9],a[10],a[11],a[12],a[13],a[14],a[15],a[16],a[17],a[18],a[19],a[20]);
for(p in properties)instance[p]=properties[p];
return instance;
}
};
LL.Tag=function(executeChildren){
this.children=[];
this._evalString="";
this.parse=function(json){
for(key in json){
if(this.childTags&&key in this.childTags){
this._parseChildTag(key,json[key]);
continue;
}
if(LL.Utils.isDynamicValue(json[key])){
this._parseDynamicValue(key,json[key]);
continue;
}
this[key]=json[key];
}
this._setDefaultValues();
};
this.executeTag=function(executeChildTags){
this._updateDynamicValues();
this.callback();
if(executeChildren!==false)this.executeChildTags();
};
var _self=this;
this.executeChildTags=function(){
_self.children.forEach(function(childTag){
childTag.executeTag();
},this);
};
this._updateDynamicValues=function(){
eval(this._evalString);
};
this._parseChildTag=function(tagPlural,json){
json.forEach(function(tagJson){
this.children.push(LL.TagFactory.create(tagPlural,tagJson));
},this);
};
this._parseDynamicValue=function(key,value){
this._evalString+="this['"+key+"']="+LL.Utils.stripCurlyBraces(value)+";";
};
this._setDefaultValues=function(){
if(this.defaultValues){
for(key in this.defaultValues){
if(typeof this[key]=="undefined"){
this[key]=this.defaultValues[key];
}
}
}
};
};
LL.Binding=function(){
var _self=this;
this.__uuid__=new UUID().id;
this.__eventListeners__=[];
this.__bindableProperties__={};
this._dispatchSetEvent=function(key,value){
var event=document.createEvent("Events");
event.initEvent(_self.__uuid__+":"+key,true,false);
event["__binding__value"]=value;
document.dispatchEvent(event);
};
this.__bindable__=function(key){
this.__bindableProperties__[key]=true;
this["__binding__"+key]=this[key];
delete this[key];
var _self=this;
if(this.__defineSetter__){
this.__defineGetter__(key,function(){
return _self["__binding__"+key];
});
this.__defineSetter__(key,function(value){
_self["__binding__"+key]=value;
this._dispatchSetEvent(key,value);
});
}
};
this.get=function(key){
return this["__binding__"+key];
};
this.set=function(key,value){
this[key]=value;
if(this.__bindableProperties__[key])this._dispatchSetEvent(key,value);
};
this.__bindCallback__=function(key,callback,scope){
var _eventListener={
type:this.__uuid__+":"+key,
listener:function(e){
;
callback.apply(scope,[e["__binding__value"]]);
},
useCapture:false
};
document.addEventListener(_eventListener.type,_eventListener.listener,_eventListener.useCapture);
this.__eventListeners__.push(_eventListener);
};
this.__bindProperty__=function(sourceKey,target,targetKey){
var _eventListener={
type:this.__uuid__+":"+sourceKey,
listener:function(e){
target[targetKey]=e["__binding__value"];
},
useCapture:false
};
document.addEventListener(_eventListener.type,_eventListener.listener,_eventListener.useCapture);
this.__eventListeners__.push(_eventListener);
};
this.__unbindAll__=function(){
while(this.__eventListeners__>0){
var eventListener=this.__eventListeners__.pop();
document.removeEventListener(eventListener.type,eventListener.listener,eventListener.useCapture);
}
};
};
LL.Event=function(type,bubbles,cancelable){
var _event=document.createEvent("Events");
_event.initEvent(type,bubbles,cancelable);
return _event;
};
LL.Event.INIT="LL.Event.INIT";
LL.Event.PRE_INIT="LL.Event.PRE_INIT";
LL.XMLHttpRequest=function(){
var httpRequest;
if(window.XMLHttpRequest){
httpRequest=new XMLHttpRequest();
if(httpRequest.overrideMimeType){
httpRequest.overrideMimeType('text/xml');
}
}
else if(window.ActiveXObject){
try{
httpRequest=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e){
httpRequest=new ActiveXObject("Microsoft.XMLHTTP");
}
}
return httpRequest;
};
LL.TagFactory={
parsers:{},
create:function(type,json){
if(this.parsers[type])return new this.parsers[type](json);
return null;
},
add:function(type,parser){
this.parsers[type]=parser;
}
};
LL.ObjectBuilder=function(json){
LL.Tag.apply(this);
this.generator;
this.constructorArguments;
this.properties;
this.cache;
this.parse(json);
};
LL.ObjectBuilder.prototype={
callback:function(){
var instance=LL.Proxy.instance(this.generator,this.constructorArguments,this.properties,this.cache);
return instance;
},
defaultValues:{"cache":true}
};
LL.EventHandler=function(json){
LL.Tag.apply(this,[false]);
this.event;
this.parse(json);
};
LL.EventHandler.prototype={
callback:function(){
var _self=this;
document.addEventListener(this.event,function(e){
LL.lastEventObject=e;
_self.executeChildTags();
},false);
},
childTags:{"ObjectBuilders":true,"MethodInvokers":true,"AjaxInvokers":true,"MethodInjectors":true,"PropertyInjectors":true}
};
LL.MethodInvoker=function(json){
LL.Tag.apply(this);
this.generator;
this.method;
this.arguments;
this.parse(json);
};
LL.MethodInvoker.prototype={
callback:function(){
var instance=LL.Proxy.instance(this.generator);
LL.lastReturn=instance[this.method].apply(instance,this.arguments);
},
defaultValues:{"arguments":[]}
};
LL.Injector=function(){
LL.Tag.apply(this);
this.source;
this.target;
this.mapping;
this.callback=function(){
var sourceInstance=(typeof this.source=='function')?LL.Proxy.instance(this.source):this.source;
var targetInstance=(typeof this.target=='function')?LL.Proxy.instance(this.target):this.target;
if(!sourceInstance.__bindableProperties__)LL.Binding.apply(sourceInstance);
for(sourceKey in this.mapping){
sourceInstance.__bindable__(sourceKey);
this.bind(sourceInstance,targetInstance,sourceKey,this.mapping[sourceKey]);
}
};
};
LL.MethodInjector=function(json){
LL.Tag.apply(this);
LL.Injector.apply(this);
this.parse(json);
};
LL.MethodInjector.prototype={
bind:function(source,target,sourceKey,targetKey){
target[targetKey](source[sourceKey]);
source.__bindCallback__(sourceKey,target[targetKey],target);
}
};
LL.PropertyInjector=function(json){
LL.Tag.apply(this);
LL.Injector.apply(this);
this.parse(json);
};
LL.PropertyInjector.prototype={
bind:function(source,target,sourceKey,targetKey){
target[targetKey]=source[sourceKey];
source.__bindProperty__(sourceKey,target,targetKey);
}
};
LL.AjaxInvoker=function(json){
LL.Tag.apply(this,[false]);
this.method;
this.url;
this.queryString;
this.async;
this.user;
this.password;
this.headers;
this.body;
this.parse(json);
};
LL.AjaxInvoker.prototype={
callback:function(){
var _self=this;
var httpRequest=new LL.XMLHttpRequest();
httpRequest.onreadystatechange=function(){
if(httpRequest.readyState==4){
LL.lastHttpRequest=httpRequest;
_self.executeChildTags();
};
};
var requestUrl=this.url;
if(this.queryString)requestUrl+="?"+this.queryString;
httpRequest.open(this.method,requestUrl,this.async,this.user,this.password);
for(p in this.headers){httpRequest.setRequestHeader(p,this.headers[p]);}
httpRequest.send(this.body);
},
defaultValues:{"queryString":"","async":true,"headers":{}},
childTags:{"ObjectBuilders":true,"MethodInvokers":true,"AjaxInvokers":true,"MethodInjectors":true,"PropertyInjectors":true}
};
