-
Notifications
You must be signed in to change notification settings - Fork 0
/
class.src.js
296 lines (262 loc) · 7.69 KB
/
class.src.js
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*!
* Class.js v0.1.0
* Small, fast, elegant, powerful, and cross platform JavaScript OOP library. Support class, inheritance, namespace, private and more.
* @snandy 2013-08-11 15:31:15
*
*/
~function(global, undefined) {
var U = {}
var toString = Object.prototype.toString
var slice = Array.prototype.slice
// Iterator
function forEach(obj, iterator, context) {
if ( obj.length === +obj.length ) {
for (var i = 0; i < obj.length; i++) {
iterator(obj[i], i, obj)
}
} else {
for (var k in obj) {
iterator(obj[k], k, obj)
}
}
}
// U.isArray, U.isBoolean, ...
forEach(['Array', 'Boolean', 'Function', 'Object', 'String', 'Number'], function(name) {
U['is' + name] = function(obj) {
return toString.call(obj) === '[object ' + name + ']'
}
})
function mix(obj) {
forEach(slice.call(arguments, 1), function(source) {
if (source) {
for (var prop in source) {
obj[prop] = source[prop]
}
}
})
return obj
}
function Observer(type, context) {
this.type = type
this.scope = context || global
this.listeners = []
}
Observer.prototype = {
subscribe: function(fn, scope, options) {
var listeners = this.listeners
if (!fn || this._find(fn, scope) !== -1) return false
var listener = {
fn: fn,
scope: scope || this.scope,
opt: options
}
if (this.firing) {
// if we are currently firing this event, don't disturb the listener loop
listeners = this.listeners = listeners.slice(0)
}
listeners.push(listener)
return true
},
unsubscribe: function(fn, scope) {
if (!fn) {
return this.clear()
}
var index = this._find(fn, scope)
if (index !== -1) {
this._delete(index)
return true
}
return false
},
publish: function() {
var listeners = this.listeners
var count = listeners.length
var i = 0, xargs, listener
if (count > 0) {
this.firing = true
while (listener = listeners[i++]) {
xargs = slice.call(arguments, 0)
if (listener.opt) {
xargs.push(listener.opt)
}
if (listener && listener.fn.apply(listener.scope, xargs) === false) {
return (this.firing = false)
}
}
}
this.firing = false
return true
},
clear: function() {
var l = this.listeners.length, i = l
while (i--) this._delete(i)
this.listeners = []
return l
},
_find: function(fn, scope) {
var listeners = this.listeners
var i = listeners.length
var listener, s
while (i--) {
if (listener = listeners[i]) {
if (listener.fn === fn && (!scope || listener.scope === scope)) return i
}
}
return -1
},
_delete: function(index) {
var listeners = this.listeners
var o = listeners[index]
if (o) {
delete o.fn
delete o.scope
delete o.opt
}
listeners.splice(index, 1)
}
}
var Event = {
on: function(type, fn, scope, o) {
var config, ev
if (typeof type === 'object') {
o = type
for (type in o) {
if (!o.hasOwnProperty(type)) continue
config = o[type]
this.on(type, config.fn || config, config.scope || o.scope, config.fn ? config : o)
}
} else {
this._events = this._events || {}
ev = this._events[type] || false
if (!ev) {
ev = this._events[type] = new Observer(type, this)
}
ev.subscribe(fn, scope, o)
}
},
off: function(type, fn, scope) {
var config, ev, o, index
if (typeof type === 'object') {
o = type
for (type in o) {
if (!o.hasOwnProperty(type)) continue
config = o[type]
this.un(type, config.fn || config, config.scope || o.scope)
}
} else {
ev = this._events[type]
if (ev) ev.unsubscribe(fn, scope)
}
},
clearEvent: function(type) {
var ev = this._events && this._events[type]
if (ev) ev.clear()
},
fire: function(type) {
var ev
if (!this._events || !(ev = this._events[type])) {
return true
}
return ev.publish.apply(ev, slice.call(arguments, 1))
}
}
// initialize namespace
function namespace(classPath, globalNamespace) {
if ( !U.isString(classPath) ) throw new Error('classPath must be a string')
globalNamespace = U.isObject(globalNamespace) ? globalNamespace : global
var arr = classPath.split('.')
var namespace = globalNamespace
var className = arr.pop()
while (arr.length) {
var name = arr.shift()
var obj = namespace[name]
if (!obj) {
namespace[name] = obj = {}
}
namespace = obj
}
var clazz = namespace[className]
if ( U.isFunction(clazz) ) throw new Error(className + ' is already defined')
namespace[className] = undefined
return {
namespace: namespace,
className: className
}
}
var create = Object.create ?
function(o) { return Object.create(o) } :
(function() { // Reusable constructor function for the Object.create() shim.
function F() {}
return function(o) {
F.prototype = o
return new F
}
}())
// define a class
function Class(name, superClass, factory) {
if (!factory) {
if (!superClass) {
throw new Error('class create failed, verify definitions')
}
factory = superClass
superClass = Object
}
function Constructor() {
if ( U.isFunction(this.init) ) {
this.init.apply(this, arguments)
}
}
Constructor.toString = function() { return name }
var supr = superClass.prototype
// var proto = Constructor.prototype = new superClass
var proto = Constructor.prototype = create(supr)
proto.constructor = factory
factory.call(proto, supr)
mix(proto, Event)
if (Class.amd) return Constructor
var obj = namespace(name, Class.globalNamespace)
obj.namespace[obj.className] = Constructor
}
Class.statics = function(clazz, obj) {
mix(clazz, obj)
}
Class.methods = function(clazz, obj, override) {
var proto = clazz.prototype
for (var m in obj) {
if ( !U.isFunction(obj[m]) ) throw new Error(m + ' is not a function')
if (override) {
proto[m] = obj[m]
} else {
if (!proto[m]) {
proto[m] = obj[m]
}
}
}
return clazz
}
Class.agument = function(clazz) {
var override = false, protos = slice.call(arguments, 1)
if ( U.isBoolean(clazz) ) {
override = true
clazz = arguments[1]
protos = slice.call(arguments, 2)
}
forEach(protos, function(proto) {
Class.methods(clazz, proto, override)
})
return clazz
}
// defaults
// Class.globalNamespace = global /* or window */
// Class.amd = false
if (typeof exports === 'object') { // node.js
exports.Class = Class
} else if (typeof define === 'function' && define.amd) { // Expose IO to the global object or as AMD module
// define.amd.Class = true
Class.amd = true
define('Class', [], function() { return Class } )
} else {
// global.namespace = namespace
global.Class = Class
}
}(this);