]>
Commit | Line | Data |
---|---|---|
1 | ||
2 | if (typeof global === "undefined") { | |
3 | global = window; | |
4 | } | |
5 | ||
6 | global.Interface = function Interface(nameOrNameSpace, name) { | |
7 | var nameSpace, interfaceName, factory; | |
8 | nameSpace = (nameOrNameSpace && name) ? nameOrNameSpace : this; | |
9 | interfaceName = (nameOrNameSpace && name) ? name : | |
10 | (nameOrNameSpace) ? nameOrNameSpace : 'interface' + Math.random().toString(); | |
11 | factory = function(definition) { | |
12 | definition.isInterface = true; | |
13 | definition.name = interfaceName; | |
14 | nameSpace[interfaceName] = definition; | |
15 | return nameSpace[interfaceName]; | |
16 | }; | |
17 | return factory; | |
18 | }; | |
19 | ||
20 | global.Module = function Module(nameOrNameSpace, name) { | |
21 | var nameSpace, moduleName, factory, newModule; | |
22 | ||
23 | nameSpace = (nameOrNameSpace && name) ? nameOrNameSpace : this; | |
24 | moduleName = (nameOrNameSpace && name) ? name : | |
25 | (nameOrNameSpace) ? nameOrNameSpace : 'module' + Math.random().toString(); | |
26 | ||
27 | newModule = { | |
28 | moduleName : moduleName, | |
29 | prototype : {}, | |
30 | __includedModules : [], | |
31 | include : function(module) { | |
32 | var property; | |
33 | for (property in module) { | |
34 | if (module.hasOwnProperty(property) | |
35 | && property !== 'prototype' | |
36 | && property !== 'isModule' | |
37 | && property !== '__includedModules' | |
38 | && property !== 'include' | |
39 | && property !== 'moduleName') { | |
40 | newModule[property] = module[property]; | |
41 | } | |
42 | } | |
43 | ||
44 | if (module.hasOwnProperty('prototype') && module.prototype) { | |
45 | for (property in module.prototype) { | |
46 | if (module.prototype.hasOwnProperty(property)) { | |
47 | newModule.prototype[property] = module.prototype[property]; | |
48 | } | |
49 | } | |
50 | } | |
51 | else { | |
52 | module.prototype = {}; | |
53 | } | |
54 | ||
55 | this.__includedModules.push(module); | |
56 | ||
57 | return this; | |
58 | } | |
59 | } | |
60 | ||
61 | factory = function(definition){ | |
62 | var property; | |
63 | ||
64 | newModule.isModule = true; | |
65 | ||
66 | for (property in definition) { | |
67 | if (definition.hasOwnProperty(property) | |
68 | && property !== 'prototype' | |
69 | && property !== 'isModule' | |
70 | && property !== '__includedModules' | |
71 | && property !== 'include' | |
72 | && property !== 'moduleName') { | |
73 | newModule[property] = definition[property]; | |
74 | } | |
75 | } | |
76 | ||
77 | if (definition.hasOwnProperty('prototype') && definition.prototype) { | |
78 | for (property in definition.prototype) { | |
79 | if (definition.prototype.hasOwnProperty(property)) { | |
80 | newModule.prototype[property] = definition.prototype[property]; | |
81 | } | |
82 | } | |
83 | } | |
84 | ||
85 | nameSpace[moduleName] = newModule; | |
86 | ||
87 | return nameSpace[moduleName]; | |
88 | }; | |
89 | ||
90 | factory.includes = function () { | |
91 | for(var i = 0; i < arguments.length; i++){ | |
92 | newModule.include(arguments[i]); | |
93 | } | |
94 | return factory; | |
95 | }; | |
96 | ||
97 | return factory; | |
98 | }; | |
99 | ||
100 | global.Class = function Class(classNameOrNameSpace, className) { | |
101 | var nameSpace, newClass, classFactory; | |
102 | nameSpace = (classNameOrNameSpace && className) ? classNameOrNameSpace : global; | |
103 | className = (classNameOrNameSpace && className) ? className : | |
104 | (classNameOrNameSpace) ? classNameOrNameSpace : 'class' + Math.random().toString(); | |
105 | ||
106 | newClass = function() { | |
107 | if (this.init) { | |
108 | this.init.apply(this, arguments); | |
109 | } | |
110 | }; | |
111 | ||
112 | newClass.__descendants = []; | |
113 | newClass.__implementedInterfaces = []; | |
114 | newClass.__includedModules = []; | |
115 | newClass.className = className; | |
116 | newClass.include = function(module) { | |
117 | var property; | |
118 | for (property in module) { | |
119 | if (module.hasOwnProperty(property) | |
120 | && property != 'prototype' | |
121 | && property != 'constructor' | |
122 | && property != 'isModule' | |
123 | && property != 'superClass' | |
124 | && property != 'include') { | |
125 | newClass[property] = module[property]; | |
126 | } | |
127 | } | |
128 | ||
129 | if (module.hasOwnProperty('prototype') && module.prototype) { | |
130 | for (property in module.prototype) { | |
131 | if (module.prototype.hasOwnProperty(property)) { | |
132 | newClass.prototype[property] = module.prototype[property]; | |
133 | } | |
134 | } | |
135 | } else { | |
136 | module.prototype = {}; | |
137 | } | |
138 | ||
139 | newClass.__includedModules.push(module); | |
140 | return this; | |
141 | }; | |
142 | ||
143 | classFactory = function(classDefinition) { | |
144 | var i, il, j, jl, property, classPrototype = classDefinition.prototype; | |
145 | if (classPrototype) { | |
146 | for (property in classPrototype) { | |
147 | if (classPrototype.hasOwnProperty(property)) { | |
148 | newClass.prototype[property] = classPrototype[property]; | |
149 | } | |
150 | } | |
151 | delete classDefinition.prototype; | |
152 | } | |
153 | for (property in classDefinition) { | |
154 | if (classDefinition.hasOwnProperty(property)) { | |
155 | newClass[property] = classDefinition[property]; | |
156 | } | |
157 | } | |
158 | ||
159 | for (i = 0, il = newClass.__implementedInterfaces.length; i < il; i++) { | |
160 | for (j = 0, jl = newClass.__implementedInterfaces[i].constructor.length; j < jl; j++) { | |
161 | if (!newClass[ newClass.__implementedInterfaces[i].constructor[j] ]) { | |
162 | console.log('must implement static ' + newClass.__implementedInterfaces[i].name); | |
163 | break; | |
164 | } | |
165 | } | |
166 | ||
167 | if (newClass.__implementedInterfaces[i].hasOwnProperty('prototype') | |
168 | && newClass.__implementedInterfaces[i].prototype) { | |
169 | for (j = 0, jl = newClass.__implementedInterfaces[i].prototype.length; j < jl; j++) { | |
170 | if (!newClass.prototype[newClass.__implementedInterfaces[i].prototype[j]]) { | |
171 | console.log('must implement prototype ' + newClass.__implementedInterfaces[i].name); | |
172 | break; | |
173 | } | |
174 | } | |
175 | } | |
176 | } | |
177 | ||
178 | try { | |
179 | if (Li && Li.ObjectSpy && Li.Spy) { | |
180 | newClass.__objectSpy = new Li.ObjectSpy(); | |
181 | newClass.__objectSpy.spy(newClass); | |
182 | newClass.__objectSpy.spy(newClass.prototype); | |
183 | } | |
184 | } catch (error) {} | |
185 | ||
186 | nameSpace[className] = newClass; | |
187 | return newClass; | |
188 | }; | |
189 | ||
190 | classFactory.inherits = function(superClass) { | |
191 | var i, inheritedClass; | |
192 | newClass.superClass = superClass; | |
193 | if (superClass.hasOwnProperty('__descendants')) { | |
194 | superClass.__descendants.push(newClass); | |
195 | } | |
196 | inheritedClass = function() { | |
197 | }; | |
198 | inheritedClass.prototype = superClass.prototype; | |
199 | newClass.prototype = new inheritedClass(); | |
200 | newClass.prototype.constructor = newClass; | |
201 | ||
202 | for (i in superClass) { | |
203 | if (superClass.hasOwnProperty(i) | |
204 | && i != 'prototype' | |
205 | && i !== 'className' | |
206 | && i !== 'superClass' | |
207 | && i !== 'include' | |
208 | && i != '__descendants') { | |
209 | newClass[i] = superClass[i]; | |
210 | } | |
211 | } | |
212 | ||
213 | delete this.inherits; | |
214 | return this; | |
215 | }; | |
216 | ||
217 | classFactory.ensures = function(interfaces) { | |
218 | for (var i = 0; i < arguments.length; i++) { | |
219 | newClass.__implementedInterfaces.push(arguments[i]); | |
220 | } | |
221 | delete this.ensures; | |
222 | return classFactory; | |
223 | }; | |
224 | ||
225 | classFactory.includes = function() { | |
226 | for (var i = 0; i < arguments.length; i++) { | |
227 | newClass.include(arguments[i]); | |
228 | } | |
229 | return classFactory; | |
230 | }; | |
231 | ||
232 | return classFactory; | |
233 | ||
234 | }; |