-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathbaseComponent.js
executable file
·250 lines (198 loc) · 6.53 KB
/
baseComponent.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
var is = require('./is'),
GENERIC = '_generic',
EventEmitter = require('events').EventEmitter,
slice = Array.prototype.slice;
function flatten(item){
return Array.isArray(item) ? item.reduce(function(result, element){
if(element == null){
return result;
}
return result.concat(flatten(element));
},[]) : item;
}
function attachProperties(object, firm){
for(var key in this._properties){
this._properties[key].attach(object, firm);
}
}
function onRender(){
// Ensure all bindings are somewhat attached just before rendering
this.attach(undefined, 0);
for(var key in this._properties){
this._properties[key].update();
}
}
function detachProperties(firm){
for(var key in this._properties){
this._properties[key].detach(firm);
}
}
function destroyProperties(){
for(var key in this._properties){
this._properties[key].destroy();
}
}
function clone(){
return this.fastn(this.component._type, this.component._settings, this.component._children.filter(function(child){
return !child._templated;
}).map(function(child){
return typeof child === 'object' ? child.clone() : child;
})
);
}
function getSetBinding(newBinding){
if(!arguments.length){
return this.binding;
}
if(!is.binding(newBinding)){
newBinding = this.fastn.binding(newBinding);
}
if(this.binding && this.binding !== newBinding){
this.binding.removeListener('change', this.emitAttach);
newBinding.attach(this.binding._model, this.binding._firm);
}
this.binding = newBinding;
this.binding.on('change', this.emitAttach);
this.binding.on('detach', this.emitDetach);
this.emitAttach();
return this.component;
};
function emitAttach(){
var newBound = this.binding();
if(newBound !== this.lastBound){
this.lastBound = newBound;
this.scope.attach(this.lastBound);
this.component.emit('attach', this.scope, 1);
}
}
function emitDetach(){
this.component.emit('detach', 1);
}
function getScope(){
return this.scope;
}
function destroy(){
if(this.destroyed){
return;
}
this.destroyed = true;
this.component
.removeAllListeners('render')
.removeAllListeners('attach');
this.component.emit('destroy');
this.component.element = null;
this.scope.destroy();
this.binding.destroy(true);
return this.component;
}
function attachComponent(object, firm){
this.binding.attach(object, firm);
return this.component;
}
function detachComponent(firm){
this.binding.detach(firm);
return this.component;
}
function isDestroyed(){
return this.destroyed;
}
function setProperty(key, property){
// Add a default property or use the one already there
if(!property){
property = this.component[key] || this.fastn.property();
}
this.component[key] = property;
this.component._properties[key] = property;
return this.component;
}
function bindInternalProperty(component, model, propertyName, propertyTransform){
if(!(propertyName in component)){
component.setProperty(propertyName);
}
component[propertyName].on('change', function(value){
model.set(propertyName, propertyTransform ? propertyTransform(value) : value);
});
}
function createInternalScope(data, propertyTransforms){
var componentScope = this;
var model = new componentScope.fastn.Model(data);
for(var key in data){
bindInternalProperty(componentScope.component, model, key, propertyTransforms[key]);
}
return {
binding: function(){
return componentScope.fastn.binding.apply(null, arguments).attach(model);
},
model: model
};
}
function extendComponent(type, settings, children){
var component = this.component;
if(type in this.types){
return component;
}
if(!(type in this.fastn.components)){
if(!(GENERIC in this.fastn.components)){
throw new Error('No component of type "' + type + '" is loaded');
}
component = this.fastn.components._generic(this.fastn, this.component, type, settings, children, createInternalScope.bind(this));
if(component){
this.types._generic = true;
}
}else{
component = this.fastn.components[type](this.fastn, this.component, type, settings, children, createInternalScope.bind(this));
}
if(component){
this.types[type] = true;
}
return component;
};
function isType(type){
return type in this.types;
}
function FastnComponent(fastn, type, settings, children){
var component = this;
var componentScope = {
types: {},
fastn: fastn,
component: component,
binding: fastn.binding('.'),
destroyed: false,
scope: new fastn.Model(false),
lastBound: null
};
componentScope.emitAttach = emitAttach.bind(componentScope);
componentScope.emitDetach = emitDetach.bind(componentScope);
componentScope.binding._default_binding = true;
component._type = type;
component._properties = {};
component._settings = settings || {};
component._children = children ? flatten(children) : [];
component.attach = attachComponent.bind(componentScope);
component.detach = detachComponent.bind(componentScope);
component.scope = getScope.bind(componentScope);
component.destroy = destroy.bind(componentScope);
component.destroyed = isDestroyed.bind(componentScope);
component.binding = getSetBinding.bind(componentScope);
component.setProperty = setProperty.bind(componentScope);
component.clone = clone.bind(componentScope);
component.children = slice.bind(component._children);
component.extend = extendComponent.bind(componentScope);
component.is = isType.bind(componentScope);
component.binding(componentScope.binding);
component.on('attach', attachProperties.bind(this));
component.on('render', onRender.bind(this));
component.on('detach', detachProperties.bind(this));
component.on('destroy', destroyProperties.bind(this));
if(fastn.debug){
component.on('render', function(){
if(component.element && typeof component.element === 'object'){
component.element._component = component;
}
});
}
}
FastnComponent.prototype = Object.create(EventEmitter.prototype);
FastnComponent.prototype.constructor = FastnComponent;
FastnComponent.prototype._fastn_component = true;
module.exports = FastnComponent;