/
mixin.js
88 lines (80 loc) · 2.33 KB
/
mixin.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
/**
* Object.deepExtend(destination, source) -> Object
*
* A "deep" version of `Object.extend`. Performs a recursive deep extension.
**/
Object.deepExtend = function(destination, source) {
for (var property in source) {
if (source[property] && source[property].constructor &&
source[property].constructor === Object) {
destination[property] = destination[property] || {};
arguments.callee(destination[property], source[property]);
} else {
destination[property] = source[property];
}
}
return destination;
};
/** section: scripty2 ui
* S2.UI.Mixin
**/
S2.UI.Mixin = {};
/** section: scripty2 ui
* mixin S2.UI.Mixin.Configurable
*
* A mixin for hassle-free blending of default options with user-defined
* options.
*
* Expects default options to be defined in a `DEFAULT_OPTIONS` property
* on the class itself.
**/
S2.UI.Mixin.Configurable = {
/**
* S2.UI.Mixin.Configurable#setOptions(options) -> Object
* - options (Object): A set of user-defined options that should override
* the defaults.
*
* Sets options on the class.
**/
setOptions: function(options) {
if (!this.options) {
this.options = {};
var constructor = this.constructor;
if (constructor.superclass) {
// Build the inheritance chain.
var chain = [], klass = constructor;
while (klass = klass.superclass)
chain.push(klass);
chain = chain.reverse();
for (var i = 0, l = chain.length; i < l; i++)
Object.deepExtend(this.options, chain[i].DEFAULT_OPTIONS || {});
}
Object.deepExtend(this.options, constructor.DEFAULT_OPTIONS || {});
}
return Object.deepExtend(this.options, options || {});
}
};
/** section: scripty2 ui
* mixin S2.UI.Mixin.Trackable
**/
S2.UI.Mixin.Trackable = {
register: function() {
var klass = this.constructor;
if (!klass.instances) {
klass.instances = [];
}
if (!klass.instances.include(this)) {
klass.instances.push(this);
}
if (Object.isFunction(klass.onRegister)) {
klass.onRegister.call(klass, this);
}
},
unregister: function() {
var klass = this.constructor;
klass.instances = klass.instances.without(this);
if (Object.isFunction(klass.onRegister)) {
klass.onUnregister.call(klass, this);
}
}
};