1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | var util = require('racer/lib/util');
|
11 | var derbyTemplates = require('derby-templates');
|
12 | var templates = derbyTemplates.templates;
|
13 | var expressions = derbyTemplates.expressions;
|
14 | var Controller = require('./Controller');
|
15 | var slice = [].slice;
|
16 |
|
17 | exports.Component = Component;
|
18 | exports.ComponentAttribute = ComponentAttribute;
|
19 | exports.ComponentAttributeBinding = ComponentAttributeBinding;
|
20 | exports.ComponentFactory = ComponentFactory;
|
21 | exports.SingletonComponentFactory = SingletonComponentFactory;
|
22 | exports.createFactory = createFactory;
|
23 | exports.extendComponent = extendComponent;
|
24 |
|
25 | function Component(parent, context, id, scope) {
|
26 | this.parent = parent;
|
27 | this.context = context;
|
28 | this.id = id;
|
29 | this._scope = scope;
|
30 | }
|
31 |
|
32 | util.mergeInto(Component.prototype, Controller.prototype);
|
33 |
|
34 | Component.prototype.destroy = function() {
|
35 | this.emit('destroy');
|
36 | this.model.removeContextListeners();
|
37 | this.model.destroy();
|
38 | delete this.page._components[this.id];
|
39 | var components = this.page._eventModel.object.$components;
|
40 | if (components) delete components.object[this.id];
|
41 | };
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | Component.prototype.bind = function(callback) {
|
47 | var component = this;
|
48 | this.on('destroy', function() {
|
49 |
|
50 |
|
51 | component = null;
|
52 |
|
53 | callback = null;
|
54 | });
|
55 | return function componentBindWrapper() {
|
56 | if (!callback) return;
|
57 | return callback.apply(component, arguments);
|
58 | };
|
59 | };
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | Component.prototype.throttle = function(callback, delayArg) {
|
77 | var component = this;
|
78 | this.on('destroy', function() {
|
79 |
|
80 |
|
81 | component = null;
|
82 |
|
83 | callback = null;
|
84 | });
|
85 |
|
86 |
|
87 |
|
88 | if (delayArg == null || typeof delayArg === 'number') {
|
89 | var delay = delayArg || 0;
|
90 | var nextArgs;
|
91 | var previous;
|
92 | var boundCallback = function() {
|
93 | var args = nextArgs;
|
94 | nextArgs = null;
|
95 | previous = +new Date();
|
96 | if (callback && args) {
|
97 | callback.apply(component, args);
|
98 | }
|
99 | };
|
100 | return function componentThrottleWrapper() {
|
101 | var queueCall = !nextArgs;
|
102 | nextArgs = slice.call(arguments);
|
103 | if (queueCall) {
|
104 | var now = +new Date();
|
105 | var remaining = Math.max(previous + delay - now, 0);
|
106 | setTimeout(boundCallback, remaining);
|
107 | }
|
108 | };
|
109 | }
|
110 |
|
111 |
|
112 |
|
113 | if (typeof delayArg === 'function') {
|
114 | var nextArgs;
|
115 | var boundCallback = function() {
|
116 | var args = nextArgs;
|
117 | nextArgs = null;
|
118 | if (callback && args) {
|
119 | callback.apply(component, args);
|
120 | }
|
121 | };
|
122 | return function componentThrottleWrapper() {
|
123 | var queueCall = !nextArgs;
|
124 | nextArgs = slice.call(arguments);
|
125 | if (queueCall) delayArg(boundCallback);
|
126 | };
|
127 | }
|
128 |
|
129 | throw new Error('Second argument must be a delay function or number');
|
130 | };
|
131 |
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | Component.prototype.debounce = function(callback, delay) {
|
142 | delay = delay || 0;
|
143 | if (typeof delay !== 'number') {
|
144 | throw new Error('Second argument must be a number');
|
145 | }
|
146 | var component = this;
|
147 | this.on('destroy', function() {
|
148 |
|
149 |
|
150 | component = null;
|
151 |
|
152 | callback = null;
|
153 | });
|
154 | var nextArgs;
|
155 | var timeout;
|
156 | var boundCallback = function() {
|
157 | var args = nextArgs;
|
158 | nextArgs = null;
|
159 | timeout = null;
|
160 | if (callback && args) {
|
161 | callback.apply(component, args);
|
162 | }
|
163 | };
|
164 | return function componentDebounceWrapper() {
|
165 | nextArgs = slice.call(arguments);
|
166 | if (timeout) clearTimeout(timeout);
|
167 | timeout = setTimeout(boundCallback, delay);
|
168 | };
|
169 | };
|
170 |
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 | Component.prototype.debounceAsync = function(callback, delay) {
|
188 | var applyArguments = callback.length !== 1;
|
189 | delay = delay || 0;
|
190 | if (typeof delay !== 'number') {
|
191 | throw new Error('Second argument must be a number');
|
192 | }
|
193 | var component = this;
|
194 | this.on('destroy', function() {
|
195 |
|
196 |
|
197 | component = null;
|
198 |
|
199 | callback = null;
|
200 | });
|
201 | var running = false;
|
202 | var nextArgs;
|
203 | var timeout;
|
204 | function done() {
|
205 | var args = nextArgs;
|
206 | nextArgs = null;
|
207 | timeout = null;
|
208 | if (callback && args) {
|
209 | running = true;
|
210 | args.push(done);
|
211 | callback.apply(component, args);
|
212 | } else {
|
213 | running = false;
|
214 | }
|
215 | }
|
216 | return function componentDebounceAsyncWrapper() {
|
217 | nextArgs = (applyArguments) ? slice.call(arguments) : [];
|
218 | if (timeout) clearTimeout(timeout);
|
219 | if (running) return;
|
220 | timeout = setTimeout(done, delay);
|
221 | };
|
222 | };
|
223 |
|
224 | Component.prototype.get = function(viewName, unescaped) {
|
225 | var view = this.getView(viewName);
|
226 | return view.get(this.context, unescaped);
|
227 | };
|
228 |
|
229 | Component.prototype.getFragment = function(viewName) {
|
230 | var view = this.getView(viewName);
|
231 | return view.getFragment(this.context);
|
232 | };
|
233 |
|
234 | Component.prototype.getView = function(viewName) {
|
235 | var contextView = this.context.getView();
|
236 | return (viewName) ?
|
237 | this.app.views.find(viewName, contextView.namespace) : contextView;
|
238 | };
|
239 |
|
240 | Component.prototype.getAttribute = function(key) {
|
241 | var attributeContext = this.context.forAttribute(key);
|
242 | if (!attributeContext) return;
|
243 | var value = attributeContext.attributes[key];
|
244 | if (value instanceof expressions.Expression) {
|
245 | value = value.get(attributeContext);
|
246 | }
|
247 | return expressions.renderValue(value, this.context);
|
248 | };
|
249 |
|
250 | Component.prototype.setAttribute = function(key, value) {
|
251 | this.context.parent.attributes[key] = value;
|
252 | };
|
253 |
|
254 | Component.prototype.setNullAttribute = function(key, value) {
|
255 | var attributes = this.context.parent.attributes;
|
256 | if (attributes[key] == null) attributes[key] = value;
|
257 | };
|
258 |
|
259 | function ComponentAttribute(expression, model, key) {
|
260 | this.expression = expression;
|
261 | this.model = model;
|
262 | this.key = key;
|
263 | }
|
264 | ComponentAttribute.prototype.update = function(context, binding) {
|
265 | var value = this.expression.get(context);
|
266 | binding.condition = value;
|
267 | this.model.setDiff(this.key, value);
|
268 | };
|
269 | function ComponentAttributeBinding(expression, model, key, context) {
|
270 | this.template = new ComponentAttribute(expression, model, key);
|
271 | this.context = context;
|
272 | this.condition = expression.get(context);
|
273 | }
|
274 | ComponentAttributeBinding.prototype = Object.create(templates.Binding.prototype);
|
275 | ComponentAttributeBinding.prototype.constructor = ComponentAttributeBinding;
|
276 |
|
277 | function setModelAttributes(context, model) {
|
278 | var attributes = context.parent.attributes;
|
279 | if (!attributes) return;
|
280 |
|
281 | for (var key in attributes) {
|
282 | var value = attributes[key];
|
283 | setModelAttribute(context, model, key, value);
|
284 | }
|
285 | }
|
286 |
|
287 | function setModelAttribute(context, model, key, value) {
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | if (value instanceof expressions.Expression) {
|
294 | var segments = value.pathSegments(context);
|
295 | if (segments) {
|
296 | model.root.ref(model._at + '.' + key, segments.join('.'), {updateIndices: true});
|
297 | } else {
|
298 | var binding = new ComponentAttributeBinding(value, model, key, context);
|
299 | context.addBinding(binding);
|
300 | model.set(key, binding.condition);
|
301 | }
|
302 | return;
|
303 | }
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 | if (value instanceof templates.Template) {
|
317 | var template = new templates.ContextClosure(value, context);
|
318 | model.set(key, template);
|
319 | return;
|
320 | }
|
321 |
|
322 |
|
323 |
|
324 |
|
325 | model.set(key, value);
|
326 | }
|
327 |
|
328 | function createFactory(constructor) {
|
329 | return (constructor.prototype.singleton) ?
|
330 | new SingletonComponentFactory(constructor) :
|
331 | new ComponentFactory(constructor);
|
332 | }
|
333 |
|
334 | function emitInitHooks(context, component) {
|
335 | if (!context.initHooks) return;
|
336 |
|
337 | for (var i = 0, len = context.initHooks.length; i < len; i++) {
|
338 | context.initHooks[i].emit(context, component);
|
339 | }
|
340 | }
|
341 |
|
342 | function ComponentFactory(constructor) {
|
343 | this.constructor = constructor;
|
344 | }
|
345 | ComponentFactory.prototype.init = function(context) {
|
346 | var component = new this.constructor();
|
347 |
|
348 | var parent = context.controller;
|
349 | var id = context.id();
|
350 | var scope = ['$components', id];
|
351 | var model = parent.model.root.eventContext(component);
|
352 | model._at = scope.join('.');
|
353 | model.set('id', id);
|
354 |
|
355 |
|
356 | model.data = model.get();
|
357 | parent.page._components[id] = component;
|
358 |
|
359 | var componentContext = context.componentChild(component);
|
360 | Controller.call(component, parent.app, parent.page, model);
|
361 | Component.call(component, parent, componentContext, id, scope);
|
362 | setModelAttributes(componentContext, model);
|
363 |
|
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 | emitInitHooks(context, component);
|
370 | component.emit('init', component);
|
371 | if (component.init) component.init(model);
|
372 |
|
373 | return componentContext;
|
374 | };
|
375 | ComponentFactory.prototype.create = function(context) {
|
376 | var component = context.controller;
|
377 | component.emit('create', component);
|
378 |
|
379 | if (component.create) {
|
380 | component.create(component.model, component.dom);
|
381 | }
|
382 | };
|
383 |
|
384 | function SingletonComponentFactory(constructor) {
|
385 | this.constructor = constructor;
|
386 | this.component = null;
|
387 | }
|
388 | SingletonComponentFactory.prototype.init = function(context) {
|
389 | if (!this.component) this.component = new this.constructor();
|
390 | return context.componentChild(this.component);
|
391 | };
|
392 |
|
393 | SingletonComponentFactory.prototype.create = function() {};
|
394 |
|
395 | function isBasePrototype(object) {
|
396 | return (object === Object.prototype) ||
|
397 | (object === Function.prototype) ||
|
398 | (object === null);
|
399 | }
|
400 | function getRootPrototype(object) {
|
401 | while (true) {
|
402 | var prototype = Object.getPrototypeOf(object);
|
403 | if (isBasePrototype(prototype)) return object;
|
404 | object = prototype;
|
405 | }
|
406 | }
|
407 | var _extendComponent = (Object.setPrototypeOf && Object.getPrototypeOf) ?
|
408 |
|
409 | function(constructor) {
|
410 |
|
411 | var rootPrototype = getRootPrototype(constructor.prototype);
|
412 | // Establish inheritance with the pattern that Node's util.inherits() uses
|
413 | // if Object.setPrototypeOf() is available (all modern browsers & IE11).
|
414 | // This inhertance pattern is not equivalent to class extends, but it does
|
415 | // work to make instances of the constructor inherit the desired prototype
|
416 | // https://github.com/nodejs/node/issues/4179
|
417 | Object.setPrototypeOf(rootPrototype, Component.prototype);
|
418 | } :
|
419 | // Fallback for older browsers
|
420 | function(constructor) {
|
421 |
|
422 |
|
423 |
|
424 |
|
425 | var prototype = constructor.prototype;
|
426 | // Otherwise, modify constructor.prototype. This won't work with ES6
|
427 | // classes, since their prototype property is non-writeable. However, it
|
428 | // does work in older browsers that don't support Object.setPrototypeOf(),
|
429 | // and those browsers don't support ES6 classes either
|
430 | constructor.prototype = Object.create(Component.prototype);
|
431 | constructor.prototype.constructor = constructor;
|
432 | util.mergeInto(constructor.prototype, prototype);
|
433 | };
|
434 | function extendComponent(constructor) {
|
435 |
|
436 | if (constructor.prototype instanceof Component) return;
|
437 | // Otherwise, append Component.prototype to constructor's prototype chain
|
438 | _extendComponent(constructor);
|
439 | }
|