var Humax = {}
Humax._rootNamespace = this.window; Humax._MemberType = { Unknown : 0, Interface : 1, Class : 2, Delegate : 3, MulticastDelegate : 4
}
Humax.Exception = function(message, source)
{ if(!(source || message))
throw new Error("One or more arguments not found in Humax.Exception ctor"); this._message = message; this._source = source;}
Humax.Exception.prototype = { _message : "", _source : "", getMessage : function()
{ return this._message;}, getSource : function()
{ return this._source;}
}
Humax.Type = function(memberType, typeName)
{ if(! (memberType || typeName))
throw new Humax.Exception("Arguments not found", "Humax.Type.ctor"); this._memberType = memberType; this._typeName = typeName; this._baseTypeNames = []; this._interfaceNames = [];}
Humax.Type.prototype = { _memberType : Humax._MemberType.Unknown, _typeName : "", _baseTypeNames : [], _interfaceNames : [], getName : function()
{ return this._typeName;}, is : function(typeName)
{ if(!typeName)
throw new Humax.Exception("Argument not found", "Humax.Type.is"); if(this._memberType == Humax._MemberType.MulticastDelegate &&
typeName == "Humax.MulticastDelegate")
return true; if(this._memberType == Humax._MemberType.Delegate &&
typeName == "Humax.Delegate")
return true; if(this._typeName == typeName || this._isBaseOrInterfaceNames(typeName))
return true; return false;}, _addBaseTypeName : function(baseTypeName)
{ this._baseTypeNames.push(baseTypeName);}, _addInterfaceName : function(interfaceName)
{ this._interfaceNames.push(interfaceName);}, _isBaseOrInterfaceNames : function(typeName)
{ for(var i = 0; i < this._baseTypeNames.length; i++)
{ if(typeName == this._baseTypeNames[i])
return true;}
for(var i = 0; i < this._interfaceNames.length; i++)
{ if(typeName == this._interfaceNames[i])
return true;}
return false;}
}
Humax.Delegate = function(delegateName, delegateMethod)
{ if(!delegateName)
throw new Error("Delegate name not found - Humax.Delegate.ctor"); if(typeof delegateMethod != "function")
throw new Error("delegateMethod must be a function reference - Humax.Delegate.ctor"); this._delegateName = delegateName; this._delegateMethod = delegateMethod; this._typedef = new Humax.Type(Humax._MemberType.Delegate, this._delegateName);}
Humax.Delegate.prototype = { _delegateName : null, _delegateMethod : null, _targetInvocation : null, _typedef : null, setTarget : function(targetInvocation)
{ if(!targetInvocation || typeof targetInvocation != "function")
throw new Error("delegateMethod must be a function reference - Humax.Delegate.setTarget"); if(targetInvocation.length != this._delegateMethod.length)
throw new Error("Target method does not match with delegate signature. - Humax.Delegate.setTarget"); this._targetInvocation = targetInvocation;}, getTarget : function()
{ return this._targetInvocation;}, invoke : function()
{ if(arguments.length != this._delegateMethod.length)
throw new Error("Argument is mismatched with the delegate " + this._delegateName + " - Humax.Delegate.invoke"); if(this._targetInvocation)
this._targetInvocation.apply(this, arguments);}, getType : function()
{ return this._typedef;}
}
Humax.MulticastDelegate = function(delegateName, delegateMethod)
{ if(!delegateName)
throw new Error("Delegate name not found - Humax.MulticastDelegate.ctor"); if(typeof delegateMethod != "function")
throw new Error("delegateMethod must be a function reference - Humax.MulticastDelegate.ctor"); this._delegateName = delegateName; this._delegateMethod = delegateMethod; this._typedef = new Humax.Type(Humax._MemberType.MulticastDelegate, this._delegateName);}
Humax.MulticastDelegate.prototype = { _delegateName : null, _delegateMethod : null, _typedef : null, _targetInvocationList : [], addTarget : function(targetInvocation)
{ if(!targetInvocation || typeof targetInvocation != "function")
throw new Error("delegateMethod must be a function reference - Humax.Delegate.addTarget"); this._targetInvocationList.push(targetInvocation);}, removeTarget : function(targetInvocation)
{ if(!targetInvocation || typeof targetInvocation != "function")
throw new Error("delegateMethod must be a function reference - Humax.Delegate.removeTarget"); var newTargetInvocationList = []; for(var i = 0; i < this._targetInvocationList.length; i++)
{ if(this._targetInvocationList[i] == targetInvocation)
continue; newTargetInvocationList.push(this._targetInvocationList[i]);}
this._targetInvocationList = newTargetInvocationList;}, removeAllTargets : function()
{ this._targetInvocationList = [];}, getTargetList : function()
{ return this._targetInvocationList;}, getType : function()
{ return this._typedef;}, invoke : function()
{ if(arguments.length != this._delegateMethod.length)
throw new Error("Argument is mismatched with the deletage " + this._delegateName + " - Humax.Delegate.invoke"); for(var i = 0; i < this._targetInvocationList.length; i++)
{ if(this._targetInvocationList[i])
this._targetInvocationList[i].apply(this, arguments);}
}
}
Humax.EventAction = { AttachHandler : 1, DetachHandler : 2, DetachAllHandlers : 3, Trigger : 4
}
Humax.Event = function(multicastDelegateInstance)
{ if(!multicastDelegateInstance)
throw new Error("Multicast Delegate not found - Humax.Event.ctor"); if(!multicastDelegateInstance.getType().is("Humax.MulticastDelegate"))
throw new Error("Instance should be of type MulticastDelegate - Humax.Event.ctor"); var funcPtr = function(eventAction)
{ if(eventAction == Humax.EventAction.Trigger)
{ var delegateArguments = []; for(var i = 1; i < arguments.length; i++)
delegateArguments.push(arguments[i]); Humax.MulticastDelegate.prototype["invoke"].apply(multicastDelegateInstance, delegateArguments);}
else if(eventAction == Humax.EventAction.AttachHandler)
{ if(arguments.length < 2 || typeof arguments[1] != "function")
throw new Error("Expected target is either not found or invalid - Humax.Event.ctor"); multicastDelegateInstance.addTarget(arguments[1]);}
else if(eventAction == Humax.EventAction.DetachHandler)
{ if(arguments.length < 2 || typeof arguments[1] != "function")
throw new Error("Expected target is either not found or invalid - Humax.Event.ctor"); multicastDelegateInstance.removeTarget(arguments[1]);}
else if(eventAction == Humax.EventAction.DetachAllHandlers)
{ multicastDelegateInstance.removeAllTargets();}
}
return funcPtr;}
Humax.declareNamespace = function(name)
{ if(!name)
throw new Error("name not found","Humax.declareNamespace"); var nsList = []; var nsParent = Humax._rootNamespace; if(name.indexOf(".") > -1)
nsList = name.split("."); else
nsList.push(name); for(var i = 0; i < nsList.length; i++)
{ var ns = nsList[i]; if(!nsParent[ns])
nsParent[ns] = {}; else if(nsParent[ns] && typeof nsParent[ns] != "object")
{ throw new Error(nsList.slice(0, i).join(".") + " alredy exists and not a namespace - " + "Humax.declareNamespace");}else
{ throw new Error("Namespace " + name + " already exists - Humax.declareNamespace");}
nsParent = nsParent[ns];}
}
Humax.declareInterface = function(typeName, targetInterface, baseInterfaces)
{ if(!(typeName || targetInterface))
throw new Error("Type name not found - Humax.declareInterface"); var targetInterfacePrototype = targetInterface.prototype; for(targetInterfaceProperty in targetInterfacePrototype)
{ if(typeof targetInterfacePrototype[targetInterfaceProperty] != "function" || targetInterfaceProperty.indexOf("_") == 0 || targetInterfaceProperty.indexOf("$") == 0)
delete targetInterfacePrototype.targetInterfaceProperty;}
targetInterfacePrototype["_typedef"] = new Humax.Type(Humax._MemberType.Interface, typeName); if(baseInterfaces)
{ if(baseInterfaces instanceof Array)
{ Humax._addInterfaces(targetInterfacePrototype, baseInterfaces);}
else if(arguments.length > 2)
{ var baseInterfaceArray = []; for(var i = 2; i < arguments.length; i++)
{ baseInterfaceArray.push(arguments[i]);}
Humax._addInterfaces(targetInterfacePrototype, baseInterfaceArray);}
}
targetInterfacePrototype["getType"] = function()
{ return targetInterfacePrototype["_typedef"];}
Humax._registerTypeToNamespace(typeName, targetInterface);}
Humax.declareClass = function(typeName, targetClass, baseClass, interfaces)
{ if(!(typeName || targetClass))
throw new Error("Type name not found - Humax.declareClass"); var targetClassPrototype = targetClass.prototype; targetClassPrototype["_typedef"] = new Humax.Type(Humax._MemberType.Class, typeName); if(baseClass)
{ var baseClassPrototype = baseClass.prototype; if(!baseClassPrototype._typedef)
throw new Error ("Incompatiable Humax base class for " + typeName + " - Humax.declareClass"); if(!(baseClassPrototype._typedef._memberType == Humax._MemberType.Class || baseClassPrototype._typedef._memberType == Humax._MemberType.Delegate))
throw new Error("Invalid base class in " + typeName + " - Humax.declareClass"); targetClassPrototype["base"] = baseClass; Humax._addBaseTypeTree(targetClassPrototype, baseClassPrototype); for(baseClassProperty in baseClassPrototype)
{ if(Humax._isScriptletProperty(baseClassProperty) || targetClassPrototype[baseClassProperty] || baseClassProperty.indexOf("_") == 0)
continue; targetClassPrototype[baseClassProperty] = baseClassPrototype[baseClassProperty];}
targetClassPrototype["callBaseMethod"] = function(methodName)
{ if(arguments.length == 1)
return baseClassPrototype[methodName].apply(this); else if(arguments.length > 1)
{ return baseClassPrototype[methodName].apply(this, arguments);}
}
}
if(interfaces)
{ if(interfaces instanceof Array)
{ Humax._addInterfaces(targetClassPrototype, interfaces);}
else if(arguments.length > 3)
{ var interfaceArray = []; for(var i = 3; i < arguments.length; i++)
{ interfaceArray.push(arguments[i]);}
Humax._addInterfaces(targetClassPrototype, interfaceArray);}
}
targetClassPrototype["getType"] = function()
{ return targetClassPrototype["_typedef"];}
Humax._registerTypeToNamespace(typeName, targetClass);}
Humax._registerTypeToNamespace = function(typeName, typeRef)
{ var nsList = typeName.split("."); var nsHierarchy = Humax._rootNamespace; for(var i = 0; i < nsList.length - 1; i++)
{ var ns = nsList[i]; if(!(nsHierarchy[ns] && typeof nsHierarchy[ns] == 'object'))
throw new Error("Namespace name not declared for " + typeName + " Humax._assignTypesToNamespace"); nsHierarchy = nsHierarchy[ns];}
var currentTypeName = nsList[nsList.length-1]; nsHierarchy[currentTypeName] = typeRef;}
Humax._addInterfaces = function(targetInterfacePrototype, baseInterfaces)
{ var baseInterfacePrototype = null; for(var i = 0; i < baseInterfaces.length; i++)
{ baseInterfacePrototype = baseInterfaces[i].prototype; if(!baseInterfacePrototype._typedef)
throw new Error("Interface at " + (i + 1) + " is not a Humax compatiable type" + " - Humax._addInterfaces"); if(baseInterfacePrototype._typedef._memberType != Humax._MemberType.Interface)
throw new Error("Interface at " + (i + 1) + " must be an interface type" + " - Humax._addInterfaces"); targetInterfacePrototype._typedef._addInterfaceName(baseInterfacePrototype._typedef._typeName); for(baseInterfaceProperty in baseInterfacePrototype)
{ if(Humax._isScriptletProperty(baseInterfaceProperty) || typeof baseInterfacePrototype[baseInterfaceProperty] != "function" || (targetInterfacePrototype[baseInterfaceProperty] &&
(typeof targetInterfacePrototype[baseInterfaceProperty] == typeof baseInterfacePrototype[baseInterfaceProperty])) || baseInterfaceProperty.indexOf("_") == 0 || baseInterfaceProperty.indexOf("$") == 0)
continue; targetInterfacePrototype[baseInterfaceProperty] = baseInterfacePrototype[baseInterfaceProperty];}
for(var i = 0; i < baseInterfacePrototype._typedef._interfaceNames.length; i++)
{ targetInterfacePrototype._typedef._addInterfaceName(baseInterfacePrototype._typedef._interfaceNames[i]);}
}
}
Humax._addBaseTypeTree = function(targetClassPrototype, baseClassPrototype)
{ targetClassPrototype._typedef._addBaseTypeName(baseClassPrototype._typedef._typeName); for(var i = 0; i < baseClassPrototype._typedef._baseTypeNames.length; i++)
{ targetClassPrototype._typedef._addBaseTypeName(baseClassPrototype._typedef._baseTypeNames[i]);}
for(var i = 0; i < baseClassPrototype._typedef._interfaceNames.length; i++)
{ targetClassPrototype._typedef._addInterfaceName(baseClassPrototype._typedef._interfaceNames[i]);}
}
Humax._isScriptletProperty = function(propName)
{ if(propName == "base" || propName == "_typedef")
return true; else
return false;}
Humax.declareClass("Humax.Exception", Humax.Exception); 