Line | Hits | Source |
---|---|---|
1 | // Class | |
2 | // ----------------- | |
3 | // Thanks to: | |
4 | // - http://mootools.net/docs/core/Class/Class | |
5 | // - http://ejohn.org/blog/simple-javascript-inheritance/ | |
6 | // - https://github.com/ded/klass | |
7 | // - http://documentcloud.github.com/backbone/#Model-extend | |
8 | // - https://github.com/joyent/node/blob/master/lib/util.js | |
9 | // - https://github.com/kissyteam/kissy/blob/master/src/seed/src/kissy.js | |
10 | ||
11 | ||
12 | // The base Class implementation. | |
13 | 1 | function Class(o) { |
14 | // Convert existed function to Class. | |
15 | 11 | if (!(this instanceof Class) && isFunction(o)) { |
16 | 1 | return classify(o) |
17 | } | |
18 | } | |
19 | ||
20 | 1 | module.exports = Class |
21 | ||
22 | ||
23 | // Create a new Class. | |
24 | // | |
25 | // var SuperPig = Class.create({ | |
26 | // Extends: Animal, | |
27 | // Implements: Flyable, | |
28 | // initialize: function() { | |
29 | // SuperPig.superclass.initialize.apply(this, arguments) | |
30 | // }, | |
31 | // Statics: { | |
32 | // COLOR: 'red' | |
33 | // } | |
34 | // }) | |
35 | // | |
36 | 1 | Class.create = function(parent, properties) { |
37 | 29 | if (!isFunction(parent)) { |
38 | 17 | properties = parent |
39 | 17 | parent = null |
40 | } | |
41 | ||
42 | 29 | properties || (properties = {}) |
43 | 29 | parent || (parent = properties.Extends || Class) |
44 | 29 | properties.Extends = parent |
45 | ||
46 | // The created class constructor | |
47 | 29 | function SubClass() { |
48 | // Call the parent constructor. | |
49 | 26 | parent.apply(this, arguments) |
50 | ||
51 | // Only call initialize in self constructor. | |
52 | 26 | if (this.constructor === SubClass && this.initialize) { |
53 | 8 | this.initialize.apply(this, arguments) |
54 | } | |
55 | } | |
56 | ||
57 | // Inherit class (static) properties from parent. | |
58 | 29 | if (parent !== Class) { |
59 | 18 | mix(SubClass, parent, parent.StaticsWhiteList) |
60 | } | |
61 | ||
62 | // Add instance properties to the subclass. | |
63 | 29 | implement.call(SubClass, properties) |
64 | ||
65 | // Make subclass extendable. | |
66 | 29 | return classify(SubClass) |
67 | } | |
68 | ||
69 | ||
70 | 1 | function implement(properties) { |
71 | 31 | var key, value |
72 | ||
73 | 31 | for (key in properties) { |
74 | 52 | value = properties[key] |
75 | ||
76 | 52 | if (Class.Mutators.hasOwnProperty(key)) { |
77 | 32 | Class.Mutators[key].call(this, value) |
78 | } else { | |
79 | 20 | this.prototype[key] = value |
80 | } | |
81 | } | |
82 | } | |
83 | ||
84 | ||
85 | // Create a sub Class based on `Class`. | |
86 | 1 | Class.extend = function(properties) { |
87 | 6 | properties || (properties = {}) |
88 | 6 | properties.Extends = this |
89 | ||
90 | 6 | return Class.create(properties) |
91 | } | |
92 | ||
93 | ||
94 | 1 | function classify(cls) { |
95 | 30 | cls.extend = Class.extend |
96 | 30 | cls.implement = implement |
97 | 30 | return cls |
98 | } | |
99 | ||
100 | ||
101 | // Mutators define special properties. | |
102 | 1 | Class.Mutators = { |
103 | ||
104 | 'Extends': function(parent) { | |
105 | 29 | var existed = this.prototype |
106 | 29 | var proto = createProto(parent.prototype) |
107 | ||
108 | // Keep existed properties. | |
109 | 29 | mix(proto, existed) |
110 | ||
111 | // Enforce the constructor to be what we expect. | |
112 | 29 | proto.constructor = this |
113 | ||
114 | // Set the prototype chain to inherit from `parent`. | |
115 | 29 | this.prototype = proto |
116 | ||
117 | // Set a convenience property in case the parent's prototype is | |
118 | // needed later. | |
119 | 29 | this.superclass = parent.prototype |
120 | }, | |
121 | ||
122 | 'Implements': function(items) { | |
123 | 1 | isArray(items) || (items = [items]) |
124 | 1 | var proto = this.prototype, item |
125 | ||
126 | 1 | while (item = items.shift()) { |
127 | 2 | mix(proto, item.prototype || item) |
128 | } | |
129 | }, | |
130 | ||
131 | 'Statics': function(staticProperties) { | |
132 | 2 | mix(this, staticProperties) |
133 | } | |
134 | } | |
135 | ||
136 | ||
137 | // Shared empty constructor function to aid in prototype-chain creation. | |
138 | 1 | function Ctor() { |
139 | } | |
140 | ||
141 | // See: http://jsperf.com/object-create-vs-new-ctor | |
142 | 1 | var createProto = Object.__proto__ ? |
143 | function(proto) { | |
144 | 29 | return { __proto__: proto } |
145 | } : | |
146 | function(proto) { | |
147 | 0 | Ctor.prototype = proto |
148 | 0 | return new Ctor() |
149 | } | |
150 | ||
151 | ||
152 | // Helpers | |
153 | // ------------ | |
154 | ||
155 | 1 | function mix(r, s, wl) { |
156 | // Copy "all" properties including inherited ones. | |
157 | 51 | for (var p in s) { |
158 | 60 | if (s.hasOwnProperty(p)) { |
159 | 66 | if (wl && indexOf(wl, p) === -1) continue |
160 | ||
161 | // å¨ iPhone 1 代ç设å¤ç Safari ä¸ï¼prototype ä¹ä¼è¢«æ举åºæ¥ï¼éæé¤ | |
162 | 54 | if (p !== 'prototype') { |
163 | 54 | r[p] = s[p] |
164 | } | |
165 | } | |
166 | } | |
167 | } | |
168 | ||
169 | ||
170 | 1 | var toString = Object.prototype.toString |
171 | 1 | var isArray = Array.isArray |
172 | ||
173 | 1 | var isFunction = function(val) { |
174 | 30 | return toString.call(val) === '[object Function]' |
175 | } | |
176 | ||
177 | 1 | var indexOf = function(arr, item) { |
178 | 7 | return arr.indexOf(item) |
179 | } |
Line | Hits | Source |
---|---|---|
1 | // Events | |
2 | // ----------------- | |
3 | // Thanks to: | |
4 | // - https://github.com/documentcloud/backbone/blob/master/backbone.js | |
5 | // - https://github.com/joyent/node/blob/master/lib/events.js | |
6 | ||
7 | ||
8 | // Regular expression used to split event strings | |
9 | 1 | var eventSplitter = /\s+/ |
10 | ||
11 | ||
12 | // A module that can be mixed in to *any object* in order to provide it | |
13 | // with custom events. You may bind with `on` or remove with `off` callback | |
14 | // functions to an event; `trigger`-ing an event fires all callbacks in | |
15 | // succession. | |
16 | // | |
17 | // var object = new Events(); | |
18 | // object.on('expand', function(){ alert('expanded'); }); | |
19 | // object.trigger('expand'); | |
20 | // | |
21 | 1 | function Events() { |
22 | } | |
23 | ||
24 | 1 | module.exports = Events |
25 | ||
26 | ||
27 | // Bind one or more space separated events, `events`, to a `callback` | |
28 | // function. Passing `"all"` will bind the callback to all events fired. | |
29 | 1 | Events.prototype.on = function(events, callback, context) { |
30 | 37 | var cache, event, list |
31 | 38 | if (!callback) return this |
32 | ||
33 | 36 | cache = this.__events || (this.__events = {}) |
34 | 36 | events = events.split(eventSplitter) |
35 | ||
36 | 36 | while (event = events.shift()) { |
37 | 48 | list = cache[event] || (cache[event] = []) |
38 | 48 | list.push(callback, context) |
39 | } | |
40 | ||
41 | 36 | return this |
42 | } | |
43 | ||
44 | ||
45 | // Remove one or many callbacks. If `context` is null, removes all callbacks | |
46 | // with that function. If `callback` is null, removes all callbacks for the | |
47 | // event. If `events` is null, removes all bound callbacks for all events. | |
48 | 1 | Events.prototype.off = function(events, callback, context) { |
49 | 20 | var cache, event, list, i |
50 | ||
51 | // No events, or removing *all* events. | |
52 | 21 | if (!(cache = this.__events)) return this |
53 | 19 | if (!(events || callback || context)) { |
54 | 3 | delete this.__events |
55 | 3 | return this |
56 | } | |
57 | ||
58 | 16 | events = events ? events.split(eventSplitter) : Object.keys(cache) |
59 | ||
60 | // Loop through the callback list, splicing where appropriate. | |
61 | 16 | while (event = events.shift()) { |
62 | 21 | list = cache[event] |
63 | 21 | if (!list) continue |
64 | ||
65 | 21 | if (!(callback || context)) { |
66 | 6 | delete cache[event] |
67 | 6 | continue |
68 | } | |
69 | ||
70 | 15 | for (i = list.length - 2; i >= 0; i -= 2) { |
71 | 27 | if (!(callback && list[i] !== callback || |
72 | context && list[i + 1] !== context)) { | |
73 | 16 | list.splice(i, 2) |
74 | } | |
75 | } | |
76 | } | |
77 | ||
78 | 16 | return this |
79 | } | |
80 | ||
81 | ||
82 | // Trigger one or many events, firing all bound callbacks. Callbacks are | |
83 | // passed the same arguments as `trigger` is, apart from the event name | |
84 | // (unless you're listening on `"all"`, which will cause your callback to | |
85 | // receive the true name of the event as the first argument). | |
86 | 1 | Events.prototype.trigger = function(events) { |
87 | 35 | var cache, event, all, list, i, len, rest = [], args |
88 | 36 | if (!(cache = this.__events)) return this |
89 | ||
90 | 34 | events = events.split(eventSplitter) |
91 | ||
92 | // Fill up `rest` with the callback arguments. Since we're only copying | |
93 | // the tail of `arguments`, a loop is much faster than Array#slice. | |
94 | 34 | for (i = 1, len = arguments.length; i < len; i++) { |
95 | 0 | rest[i - 1] = arguments[i] |
96 | } | |
97 | ||
98 | // For each event, walk through the list of callbacks twice, first to | |
99 | // trigger the event, then to trigger any `"all"` callbacks. | |
100 | 34 | while (event = events.shift()) { |
101 | // Copy callback lists to prevent modification. | |
102 | 53 | if (all = cache.all) all = all.slice() |
103 | 82 | if (list = cache[event]) list = list.slice() |
104 | ||
105 | // Execute event callbacks. | |
106 | 45 | if (list) { |
107 | 37 | for (i = 0, len = list.length; i < len; i += 2) { |
108 | 38 | list[i].apply(list[i + 1] || this, rest) |
109 | } | |
110 | } | |
111 | ||
112 | // Execute "all" callbacks. | |
113 | 45 | if (all) { |
114 | 8 | args = [event].concat(rest) |
115 | 8 | for (i = 0, len = all.length; i < len; i += 2) { |
116 | 8 | all[i].apply(all[i + 1] || this, args) |
117 | } | |
118 | } | |
119 | } | |
120 | ||
121 | 34 | return this |
122 | } | |
123 | ||
124 | ||
125 | // Mix `Events` to object instance or Class function. | |
126 | 1 | Events.mixTo = function(receiver) { |
127 | 2 | receiver = receiver.prototype || receiver |
128 | 2 | var proto = Events.prototype |
129 | ||
130 | 2 | for (var p in proto) { |
131 | 6 | if (proto.hasOwnProperty(p)) { |
132 | 6 | receiver[p] = proto[p] |
133 | } | |
134 | } | |
135 | } |