-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass.js
More file actions
256 lines (209 loc) · 6.21 KB
/
class.js
File metadata and controls
256 lines (209 loc) · 6.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/**
* -----------------------------------
* Simple Class System for JavaScript
* -----------------------------------
*
* Features:
* ´´´´´´´´´
* -> Basic Inheritance
* -> Mixins
* -> Static Properties
* -> Namespaces
* -> Class manager
*
* Example:
* ´´´´´´´´
* Class('ClassName', {
* static: {
* STATICVAR: 'VAR',
* STATICFUNCTION: function(){}
* },
* extends: SuperClass
* mixins: [MixinClass],
* constructor: function(){ }
* });
*
* @author genix
*
*/
var global = (window || this);
Function.prototype.isClass = function(){
return this.hasOwnProperty("className");
}
Function.prototype.isMixin = function(){
return this.prototype.hasOwnProperty("initMixin");
}
/* create given namespace */
function namespace(path, sep){
var current = global;
if(path == "") return current;
var parts = path.split(sep || ".");
for(var i=0; i<parts.length; ++i){
current = current[parts[i]] = current[parts[i]] || {};
}
return current;
}
// add to global scope
global.namespace = namespace;
/* simple classmanager */
function ClassManager(){}
ClassManager.classes = {};
ClassManager.addClass = function(className, klass){
ClassManager.classes[className] = klass;
}
ClassManager.removeClass = function(className){
if(ClassManager.hasClass(className))
delete ClassManager.classes[className];
}
ClassManager.hasClass = function(className){
return ClassManager.classes.hasOwnProperty(className);
}
ClassManager.getClass = function(className){
if(!ClassManager.hasClass(className)) return null;
return ClassManager.classes[className];
}
// add to global scope
global.ClassManager = ClassManager;
/* class system */
var Class = (function(){
function Class(name, definition){
if(ClassManager.hasClass(name)) throw "Class '" + name + "' is already defined.";
var klass,
ns = "",
clsName = name;
reservedKeywords = [
"constructor", "className", "extends",
"instanceNumber", "initMixins", "mixins",
"static", "initConfig", "isInstanceOf"
],
isFunction = function(fn){
return typeof(fn) == 'function';
},
ignoreProperty = function(key){
return reservedKeywords.indexOf(key) !== -1;
},
instanceNumber = 0;
if(name.indexOf('.') !== -1){
var pos = name.lastIndexOf('.');
ns = name.substring(0, pos);
clsName = name.substring(pos + 1);
}
this.extends = function(child, parent){
// copy properties from super class to child class
for(var m in parent){
if(parent.hasOwnProperty(m)) child[m] = parent[m];
}
// create new class and default constructor
function construct() {
this.constructor = child;
}
// copy prototypes
construct.prototype = parent.prototype;
child.prototype = new construct;
child.__super__ = child.super = child.prototype.super = parent.prototype;
// copying mixins and remove reference to super class
child.mixins = [];
var mixins = parent.mixins || [];
for(var i in mixins){
child.mixins.push(mixins[i]);
}
return child;
}
/* create constructor */
if(!definition.hasOwnProperty("constructor")){
klass = function(){
this.initConfig();
}
} else {
klass = function(){
this.initConfig();
definition.constructor.apply(this, arguments);
}
}
/* inheritance (extends) */
if(definition.hasOwnProperty("extends")){
this.extends(klass, definition.extends);
}
/* inheritance (mixins) */
if(definition.hasOwnProperty("mixins")){
klass.mixins = klass.mixins || [];
for(var i in definition.mixins){
var mixin = definition.mixins[i];
if(!mixin.isMixin()) continue;
klass.mixins.push(mixin.className);
for(var key in mixin.prototype){
if(ignoreProperty(key) || key == "initMixin") continue;
klass.prototype[key] = mixin.prototype[key];
}
if(mixin.prototype.hasOwnProperty("static")){
for(var v in mixin.prototype.static){
klass[v] = mixin.prototype.static[v];
}
}
}
}
/* static properties */
if(definition.hasOwnProperty("static")){
for(var v in definition.static){
if(definition.static.hasOwnProperty(v)){
klass[v] = definition.static[v];
}
}
}
/* copy definitions into prototype */
for(var m in definition){
if(ignoreProperty(m)) continue;
klass.prototype[m] = definition[m];
}
// attach the classname as string to the class and prototype
klass.className = klass.prototype.className = name;
// get inheritance path
klass.getInheritance = klass.prototype.getInheritance = function(asClass){
var parents = [];
var current = (klass.super || null);
while(current != null){
parents.push((asClass ? current : current.className));
current = current.super || null;
}
// object is parent of all classes
if(!asClass) parents.push("Object");
return parents;
}
// check if class or instance inherits from given mixin
klass.hasMixin = klass.prototype.hasMixin = function(mixin){
if(typeof(mixin) !== 'string') mixin = mixin.className;
var mixins = klass.mixins || [];
return mixins.indexOf(mixin) !== -1;
}
// initial configuration of instance, has to be called in constructor
klass.prototype.initConfig = function(){
this.instanceNumber = ++instanceNumber;
this.initMixins();
}
// call initMixin of each mixin
klass.prototype.initMixins = function(){
if(klass.hasOwnProperty("mixins")){
for(var i in klass.mixins){
var mixin = ClassManager.getClass(klass.mixins[i]);
if(!mixin.isMixin()) continue;
mixin.prototype.initMixin.call(this);
}
}
}
// check if instance is instance of a given class
klass.prototype.isInstanceOf = function(cls){
if(typeof(cls) === 'string'){
if(!ClassManager.hasClass(cls)) return false;
cls = ClassManager.getClass(cls);
}
return (this instanceof cls);
}
/* export class to global scope */
namespace(ns)[clsName] = klass;
ClassManager.addClass(name, klass);
return klass;
}
return Class;
})();
// add to global scope
global.Class = Class;