-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapplication.js
More file actions
1184 lines (996 loc) · 42 KB
/
application.js
File metadata and controls
1184 lines (996 loc) · 42 KB
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
define(['knockout', 'jquery'],
function (ko, $)
{
'use strict';
// Add the 'binding' property to jquery event objects for certain events
// to get the currently bound object easier
function addBindingProperty(e)
{
if(!e.binding)
{
e.binding = ko.dataFor(e.target);
}
return e;
}
var fixedEvents = [
'blur',
'change',
'click',
'dblclick',
'focus',
'focusin',
'focusout',
'hover',
'keydown',
'keypress',
'keyup',
'mousedown',
'mouseenter',
'mouseleave',
'mousemove',
'mouseout',
'mouseover',
'mouseup',
'resize',
'scroll',
'select',
'submit'
];
fixedEvents.forEach(function (ename)
{
var fhObj = {};
if($.event.fixHooks[ename])
{
fhObj = $.event.fixHooks[ename];
}
if(!fhObj.filter)
{
fhObj.filter = addBindingProperty;
}
else
{
var originalFilter = fhObj.filter;
fhObj.filter = function(e, oe)
{
e = originalFilter(e,oe);
return addBindingProperty(e);
};
}
$.event.fixHooks[ename] = fhObj;
});
// Add the guard and sentinel binding handlers
ko.bindingHandlers.guard = {
init: function(element, valueAccessor, allBindings, viewModel, bindingContext)
{
var redirect = ko.observable();
var fallback = {};
if(allBindings.has('sentinel'))
fallback = allBindings.get('sentinel');
// Trick: since we can't share a state between the init and update
// functions (the redirect and fallback variables are the state),
// keep the state local and use a ko.computed. The ko.computed
// function is called whenever any observable it reads is updated,
// regardless of whether or not it is attached to a view, allowing
// us to catch updates to the binding without providing an update
// function AND to keep our state.
ko.computed(function() {
var value = valueAccessor();
var valueUnwrapped = ko.unwrap(value);
if(valueUnwrapped)
{
redirect(valueUnwrapped);
}
else
{
redirect(fallback);
}
}, null, { disposeWhenNodeIsRemoved: element });
// Another trick: bind descendants to our observable and switch its value
// so that we can supply whatever we want to the child nodes
ko.applyBindingsToDescendants(bindingContext.createChildContext(redirect), element);
return { controlsDescendantBindings: true };
}
};
// Add the activate binding handler
var activationParameters = {};
ko.bindingHandlers.activate = {
update: function(element, valueAccessor, allBindings, viewModel, bindingContext)
{
var guid = $(element).prop('id');
var value = valueAccessor();
var activationParameter = ko.unwrap(value);
activationParameters[guid] = activationParameter;
}
};
// Add some extra Array functions
// Flatten an array of arrays (with any depth of arrays) into a single array
Array.prototype.flatten = function ()
{
return this.reduce(function (flat, toFlatten)
{
if(Array.isArray(toFlatten) && toFlatten.some(Array.isArray))
return flat.concat(toFlatten.flatten());
else
return flat.concat(toFlatten);
}, []);
};
// Polyfill Array.prototype.find to find an element matching a callback
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find#Polyfill
if (!Array.prototype.find) {
Object.defineProperty(Array.prototype, 'find', {
enumerable: false,
configurable: true,
writable: true,
value: function(predicate) {
if (this === null) { // Minor correction to this line to fix jshint error
throw new TypeError('Array.prototype.find called on null or undefined');
}
if (typeof predicate !== 'function') {
throw new TypeError('predicate must be a function');
}
var list = Object(this);
var length = list.length >>> 0;
var thisArg = arguments[1];
var value;
for (var i = 0; i < length; i++) {
if (i in list) {
value = list[i];
if (predicate.call(thisArg, value, i, list)) {
return value;
}
}
}
return undefined;
}
});
}
// Add extra Object functions
// Get an array of the values in an object
Object.values = function (obj)
{
return Object.keys(obj).map(function (key) { return obj[key]; });
};
// Turn an object into an array of key-value pairs with each element in the form [key, value]
Object.pairs = function (obj)
{
return Object.keys(obj).map(function (key) { return [key, obj[key]]; });
};
// Application public interface
var application = {
guid: {
newGuid: function ()
{
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c)
{
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
},
model: ko.observable({}),
name: ko.observable(''),
components: ko.observableArray([]),
visible: ko.observable(true),
children: function ()
{
// Grab direct children
var children = Object.values(this)
.filter(ko.isObservable)
.filter(function (obs)
{
return obs() instanceof application.ViewModel;
})
.map(function (vm)
{
return vm();
});
// Grab children in collections
var collectionChildren = Object.values(this)
.filter(ko.isObservable)
.filter(function (prop) { return prop() instanceof application.ViewModelCollection; })
.map(function (vmc) { return vmc().viewModels(); })
.flatten()
.map(function (vm)
{
return vm();
});
// Merge the two lists
return children.concat(collectionChildren);
},
// Composes the page and applies data bindings
compose: function (components)
{
$('body').attr('data-bind', 'visible: visible');
if(!components)
{
$.ajaxSetup({ async: false });
$.getJSON('/components.json', function (c) { components = c; });
$.ajaxSetup({ async: true });
}
this.components(components);
loadComponents((function()
{
// Apply initial component types
typeComponents($('body'));
// get the top level components for expansion
var topLevelComponents = findChildComponents($('body'));
// prepare the application object for top-level collection components
var topLevelCollections = topLevelComponents.filter(function (component)
{
return $(component).data('componentType') == 'collection';
});
prepareCollections(topLevelCollections, this);
// expand the viewmodel heirarchy
var expandedModels = expandComponents(topLevelComponents);
// trigger loaded event from bottom up
expandedModels.reverse();
expandedModels.forEach(function (comp) { comp.loaded.trigger(); });
// Apply databindings
ko.applyBindings(this);
// Trigger that the application as a whole is loaded
this.loaded.trigger();
// Activate any immediate children that are set to auto-activate
this.children().forEach(function (vm)
{
var instanceInfo = getComponentInstanceInfo(vm.view());
if(!vm.active() && vm.view && instanceInfo.activate !== undefined)
{
if(vm.uid in activationParameters)
{
vm.activate(activationParameters[vm.uid]);
}
else
{
vm.activate();
}
}
});
}).bind(this));
},
// Find an component by id using a depth first search (recursive)
find: function (componentId)
{
var target = null;
this.children().forEach(function (child)
{
var foundChild = null;
if (child.uid == componentId)
{
foundChild = child;
}
else
{
foundChild = child.find(componentId);
}
if (foundChild !== null)
{
target = foundChild;
}
});
return target;
},
// Inject a component dynamically
injectComponent: function (component)
{
// Expand the component into the DOM
var viewModels = expandComponent(component);
viewModels.reverse();
// Trigger loaded handlers from the bottom up
viewModels.forEach(function (vm) { vm.loaded.trigger(); });
// Apply bindings to the root level injected node using its parent
var parent = viewModels.reverse()[0].parent();
ko.applyBindingsToNode(component, null, parent);
// Add the root component UID to the dynamically added components list
addedNodes.push(viewModels[0].uid);
// Return the root of the injected componentsQueue
return viewModels[0];
},
// Event prototype
Event: function ()
{
// Generate a unique id if one was not provided
this.id = application.guid.newGuid();
// Attaches an event handler
this.on = (function ()
{
var args = $.makeArray(arguments);
return $(this).on.apply($(this), [this.id].concat(args));
}).bind(this);
// Detaches an event handler
this.off = (function ()
{
var args = $.makeArray(arguments);
return $(this).off.apply($(this), [this.id].concat(args));
}).bind(this);
// Triggers the event
this.trigger = (function ()
{
return $(this).triggerHandler(this.id, arguments);
}).bind(this);
},
RoutedEvent: function ()
{
// Track the routed signal using a private event that can only be triggered internally
var event = new application.Event();
// Triggers the routed events
var triggerRoute = (function ()
{
var argsWithoutEvent = $.makeArray(arguments).slice(1);
event.trigger.apply(this, argsWithoutEvent);
}).bind(this);
// Adds an event whose signal will be routed
this.addRoute = (function (ev)
{
ev.on(triggerRoute);
}).bind(this);
// Removes an event from routing
this.removeRoute = (function (ev)
{
ev.off(triggerRoute);
}).bind(this);
// Allow turning on and off subscriptions to the routed signals
this.on = event.on;
this.off = event.off;
},
// Viewmodel prototype parent
ViewModel: function ()
{
// Observable property controls visibility
this.visible = ko.observable(false);
// Parent, children, and find child function
this.parent = ko.observable({});
this.children = application.children;
this.find = application.find;
// Loaded event, fired after the components view is added and its viewmodel is set up
this.loaded = new application.Event();
// Tracks if a component is active, not settable
var isActive = false;
this.active = function () { return isActive; }; // Tracks if the component is active
// Activates the component, any number of arguments can be passed to the activation handlers
this.activate = function ()
{
// Only activate a component if its view exists in the DOM
if(this.view().length > 0)
{
var instanceInfo = getComponentInstanceInfo(this.view());
this.visible(true);
isActive = true;
if (this.view && instanceInfo.type == 'collection')
{
var data = ko.dataFor(this.view().get(0));
var args = $.makeArray(arguments);
var params = args.concat(data);
this.activated.trigger.apply(this.activated, params);
}
else
{
this.activated.trigger.apply(this.activated, arguments);
}
this.children().forEach(function (vm)
{
var cii = getComponentInstanceInfo(vm.view());
if(!vm.active() && vm.view && cii.activate !== undefined)
{
if(vm.uid in activationParameters)
{
vm.activate(activationParameters[vm.uid]);
}
else
{
vm.activate();
}
}
});
}
};
this.activated = new application.Event(); // Activated event
// Finishes the component, any number of arguments can be passed to the finish handlers
this.finish = function ()
{
this.visible(false);
isActive = false;
this.finished.trigger.apply(this.finished, arguments);
this.children().forEach(function (vm)
{
if(vm.active())
vm.finish();
});
};
this.finished = new application.Event(); // Finish event
// Unique identifier
this.uid = application.guid.newGuid();
// Gets the root of the components view
this.view = (function () { return $('#' + this.uid); }).bind(this);
// Removes the component's view which triggers removal of the entire component, this will
// happen automatically (along with triggering of any events) when the DOM observer
// catches this change
this.remove = (function () { this.view().remove(); }).bind(this);
this.removed = new application.Event(); // Removed event
this.childRemoved = new application.Event(); // Child removed event
function filterProperties(filterFunction)
{
/*jshint validthis:true */
return Object.pairs(this)
.map(function (pair)
{
return { name: pair[0], property: pair[1] };
})
.filter(filterFunction);
}
// Gets any events attached to the viewmodel
this.events = function ()
{
return filterProperties.call(this, function (desc)
{
return desc.property instanceof application.Event;
});
};
// Get functions of the viewmodel
this.functions = function ()
{
return filterProperties.call(this, function (desc)
{
return typeof(desc.property) == 'function' && !ko.isObservable(desc.property);
});
};
this.observables = function ()
{
return filterProperties.call(this, function (desc)
{
return ko.isObservable(desc.property);
});
};
// Get non-function, non-event proprties of the viewmodel
this.properties = function ()
{
return filterProperties.call(this, function (desc)
{
return typeof(desc.property) != 'function' && !(desc.property instanceof application.Event);
});
};
},
// Manages a collection of viewmodels
ViewModelCollection: function (CollectionType)
{
// Get a prototype of the components this collection will manage
var collectionPrototype = new CollectionType();
// Create routed events for any of the user defined events so that handlers can be attached collection-wide
var events = collectionPrototype.events();
events.forEach(function (ev)
{
this[ev.name] = new application.RoutedEvent();
}, this);
// Create functions that can be called collection wide
var functions = collectionPrototype.functions();
functions.forEach(function (f)
{
this[f.name] = function ()
{
var args = $.makeArray(arguments);
var returns = [];
this.viewModels().forEach(function (vm)
{
var ret = vm()[f.name].apply(vm(), args);
returns.push(ret);
});
return returns;
};
}, this);
// Create observables that can be get/set collection wide
var observables = collectionPrototype.observables();
observables.forEach(function (o)
{
this[o.name] = function (val)
{
if(val !== undefined)
{
this.viewModels().forEach(function (vm)
{
vm()[o.name](val);
});
}
else
{
var vals = [];
this.viewModels().forEach(function (vm)
{
vals.push(vm()[o.name]());
});
return vals;
}
};
}, this);
// Create properties that can be get/set collection wide
var properties = collectionPrototype.properties();
var addedProperties = [];
properties.forEach(function (p)
{
var vmc = this;
Object.defineProperty(this, p.name, {
enumerable: true,
get: function ()
{
var vals = [];
vmc.viewModels().forEach(function (vm)
{
vals.push(vm()[p.name]);
});
return vals;
},
set: function (val)
{
vmc.viewModels().forEach(function (vm)
{
vm()[p.name] = val;
});
}
});
addedProperties.push(p.name);
}, this);
// Gets the viewmodels in the collection
this.viewModels = function ()
{
// Remove properties added as children to avoid infinite recursion
var safeProperties = [];
for(var key in this)
{
if(addedProperties.indexOf(key) == -1)
safeProperties.push(key);
}
return safeProperties.map(function (name)
{
return this[name];
}, this)
.filter(ko.isObservable)
.filter(function (prop)
{
return prop() instanceof application.ViewModel;
});
};
// Activate all viewmodels in the collection
this.activate = (function ()
{
var args = arguments;
this.viewModels().forEach(function (vm)
{
vm().activate.apply(vm(), args);
});
}).bind(this);
this.activated = new application.RoutedEvent();
// Finish all viewmodels in the collection
this.finish = (function ()
{
var args = arguments;
this.viewModels().forEach(function (vm)
{
vm().finish.apply(vm(), args);
});
}).bind(this);
this.finished = new application.RoutedEvent();
// Add a viewmodel to the collection
this.viewModelAdded = new application.Event();
this.add = (function (vm)
{
// Route user events
vm.events().forEach(function (ev)
{
if (ev.name in this)
{
this[ev.name].addRoute(ev.property);
}
}, this);
// Route standard events
this.loaded.addRoute(vm.loaded);
this.activated.addRoute(vm.activated);
this.finished.addRoute(vm.finished);
this.removed.addRoute(vm.removed);
this.childRemoved.addRoute(vm.childRemoved);
// Add to the collection
this[vm.uid] = ko.observable(vm);
this.viewModelAdded.trigger(vm);
}).bind(this);
this.loaded = new application.RoutedEvent();
// Remove a viewmodel from the collection
this.viewModelRemoved = new application.Event();
this.remove = (function (vm)
{
// Make sure the component is in the collection
if (vm.uid in this)
{
// Remove the component
delete this[vm.uid];
// Remove routing for standard event handlers
this.loaded.removeRoute(vm.loaded);
this.activated.removeRoute(vm.activated);
this.finished.removeRoute(vm.finished);
this.removed.removeRoute(vm.removed);
this.childRemoved.removeRoute(vm.childRemoved);
// Remove routing for user events
vm.events().forEach(function (ev)
{
if (ev.name in this)
{
this[ev.name].removeRoute(ev.property);
}
}, this);
this.viewModelRemoved.trigger(vm);
}
}).bind(this);
this.removed = new application.RoutedEvent();
this.childRemoved = new application.RoutedEvent();
}
};
// Application events
application.loaded = new application.Event(); // Triggered when the application is finished loading
application.childRemoved = new application.Event(); // Triggered when a direct child of Application is removed
// Application private interface
// Loads view templates and styles for each component
function _loadComponents(callback)
{
/*jshint validthis:true */
// Put JQuery AJAX into synchronous mode for this algorithm to work, we will clear this flag once page loading is complete
$.ajaxSetup({ async: false });
var viewmodelPaths = [];
var viewmodelIdx = [];
// Preload styles and templates for each component
this.components().forEach(function (comp, i)
{
// Register a custom element type
if(comp.name.indexOf('-') !== -1)
{
comp.name = comp.name.toLowerCase();
var ce = document.registerElement(comp.name);
comp.element = ce;
}
// Append the style node to the pages head
if ('style' in comp)
{
var styleLink = $('<link rel="stylesheet" type="text/css" href="' + comp.style + '"/>');
$('head').append(styleLink);
}
// Load the view template and store it for later
if ('view' in comp)
{
$.get(comp.view, {}, function (viewData)
{
comp.template = viewData;
});
}
if(typeof comp.viewModel == 'string')
{
viewmodelPaths.push(comp.viewModel);
viewmodelIdx.push(i);
}
});
// Put JQuery back into asynchronous mode for future ajax requests
$.ajaxSetup({ async: true });
if(viewmodelPaths.length > 0)
{
// Dynamically load missing viewmodels
require(viewmodelPaths, (function ()
{
for(var i = 0; i < arguments.length; i++)
{
this.components()[viewmodelIdx[i]].viewModel = arguments[i];
}
callback();
}).bind(this));
}
else
{
callback();
}
}
var loadComponents = _loadComponents.bind(application);
function _findChildComponents(componentRoot)
{
var childComponentsData = componentRoot.find('[data-component]').toArray();
var childComponentsCustom = componentRoot.find(this.components().map(function (c) { return c.name; }).join(',')).toArray();
return childComponentsData.concat(childComponentsCustom);
}
var findChildComponents = _findChildComponents.bind(application);
function _isComponentRoot(element)
{
var isDataElement = element.data().hasOwnProperty('component');
if(isDataElement)
return true;
var tagName = element.prop('tagName');
if(tagName)
{
var componentName = tagName.toLowerCase();
return this.components().map(function (c) { return c.name; }).indexOf(componentName) > -1;
}
return false;
}
var isComponentRoot = _isComponentRoot.bind(application);
function _getComponentInstanceInfo(componentRoot)
{
var isDataElement = componentRoot.data().hasOwnProperty('component');
var component = isDataElement ? componentRoot.data('component') : componentRoot.prop('tagName').toLowerCase();
var name = isDataElement ? componentRoot.data('name') : componentRoot.attr('name');
var type = componentRoot.data('componentType');
var activate = isDataElement ? componentRoot.data('activate') : componentRoot.attr('activate');
return {
component: component,
name: name,
type: type,
activate: activate
};
}
var getComponentInstanceInfo = _getComponentInstanceInfo.bind(application);
// Shorthand to expand a single component
// returns a tree structure in an array of the expanded components
function _expandComponent(component)
{
return expandComponents([component]);
}
var expandComponent = _expandComponent.bind(application);
// Expands a data-component element into its component, uses a breadth-first traversal
// Returns a list of the constructed view models that were expanded
function _expandComponents(components)
{
/*jshint validthis:true */
var componentsQueue = components;
var viewModels = [];
var enqueueComponent = function (c) { componentsQueue.push(c); };
var matchComponentName = function (cname, c) { return c.name.toLowerCase() == cname.toLowerCase(); };
while (componentsQueue.length > 0)
{
var componentRoot = $(componentsQueue.shift()); // dequeue operation
// Get the instance info
var componentInstanceInfo = getComponentInstanceInfo(componentRoot);
// Check the component type to make sure it isnt a collection component
if (componentInstanceInfo.type != 'collection' && componentInstanceInfo.type != 'conditional')
{
// Find the component description
var component = this.components().find(matchComponentName.bind(undefined, componentInstanceInfo.component));
if (component)
{
var viewModel = buildComponent(componentRoot, component);
// Add the view model to the list of view models that were processed
viewModels.push(viewModel);
// Find any child data-component nodes and push them onto the queue
var childComponents = findChildComponents(componentRoot);
childComponents.forEach(enqueueComponent);
}
}
}
return viewModels;
}
var expandComponents = _expandComponents.bind(application);
var addedNodes = [];
// Dom node removal observer, used to clean up viewmodels when their views are removed and auto-activate dynamically loaded components
var domObserver = new MutationObserver(function (mutations)
{
// Clean up removed collection components
mutations.filter(function (mutation) { return mutation.removedNodes.length > 0; }) // Remove any mutation events that arent removals
.map(function (m) { return m.removedNodes; }) // Operate only on the lists of removed nodes
.map(function (nodeList) { return $.makeArray(nodeList); }) // Turn the nodelists into a real array
.flatten() // Flatten the removals into one array of data to prevent nested pipelines
.filter(function (node) // Remove any DOM events not referring to component nodes
{
return $(node).data('component') || application.components().map(function (c) { return c.name; }).indexOf($(node).prop('tagName')) !== -1;
})
.forEach(function (node) // Finally, process each component node
{
// Get the unique id of the component
var uid = $(node).attr('id');
// Find the viewmodel
var viewModel = application.find(uid);
// If no viewmodel was found, its parent was removed already, this can be ignored
if (viewModel)
{
// Get the parent and field name
var instanceInfo = getComponentInstanceInfo($(node));
var fieldName = instanceInfo.name;
var parent = viewModel.parent();
// Collection nodes need extra processing
if (instanceInfo.type == 'collection')
{
// Remove the viewmodel from the collection in the parent
parent[fieldName]().remove(viewModel);
}
else
{
// Non collection components can have their property removed from the parent
delete parent[fieldName];
}
// Trigger removal events
viewModel.removed.trigger();
parent.childRemoved.trigger(viewModel);
}
});
// Auto-activate dynamically added components
mutations.filter(function (mutation) { return mutation.addedNodes.length > 0; }) // Remove any events that are not adds
.map(function (m) { return m.addedNodes; }) // Operate only on the addedNodes list
.map(function (nodeList) { return $.makeArray(nodeList); }) // Turn them into a real array
.flatten() // Flatten
.filter(function (node) // Remove DOM events for non components
{
return $(node).data('component') || application.components().map(function (c) { return c.name; }).indexOf($(node).prop('tagName')) !== -1;
})
.forEach(function (node) // Process
{
// Get the unique ID
var uid = $(node).attr('id');
// See if the node was dynamically added
if(addedNodes.indexOf(uid) !== -1)
{
// If it was, find its component
var viewModel = application.find(uid);
// Check to see if it needs activation
if(viewModel.parent() === application || viewModel.parent().active())
{
var cii = getComponentInstanceInfo($(node));
if(!viewModel.active() && viewModel.view && cii.activate !== undefined)
{
if(viewModel.uid in activationParameters)
{
viewModel.activate(activationParameters[viewModel.uid]);
}
else
{
viewModel.activate();
}
}
}
// Remove it from the dynamically added nodes list
addedNodes.splice(addedNodes.indexOf(uid), 1);
}
});
});
// prepare a viewmodel for child collection components
function _prepareCollections(childCollections, viewModel)
{
childCollections.forEach(function (collection)
{
// Get the name of the collection
var colInstanceInfo = getComponentInstanceInfo($(collection));
var collectionName = colInstanceInfo.name;