-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathviewContainer.js
139 lines (121 loc) · 3.94 KB
/
viewContainer.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
var createSpec = require('spec-js'),
Bindable = require('./bindable'),
View = require('./view'),
initialiseViewItem = require('./initialiseViewItem'),
removeViews = require('./removeViews'),
requestInsersion = require('./requestInsersion'),
arrayProto = Array.prototype;
function ViewContainer(viewContainerDescription){
Bindable.call(this);
var viewContainer = this;
this._deferredViews = [];
if(viewContainerDescription instanceof ViewContainer){
return viewContainerDescription._clone();
}
if(viewContainerDescription instanceof Array){
viewContainer.add(viewContainerDescription);
}
this.on('bind', function(parent){
for(var i = 0; i < this.length; i++){
this.add(this[i], i);
}
return this;
});
}
ViewContainer = createSpec(ViewContainer, Array);
for(var key in Bindable.prototype){
ViewContainer.prototype[key] = Bindable.prototype[key];
}
ViewContainer.prototype.constructor = ViewContainer;
ViewContainer.prototype._render = true;
ViewContainer.prototype.getPath = function(){
return getItemPath(this);
};
/*
ViewContainers handle their own array state.
A View that is added to a ViewContainer will
be automatically removed from its current
container, if it has one.
*/
ViewContainer.prototype.add = function(view, insertIndex){
// If passed an array
if(view instanceof Array){
// Clone the array so splicing can't cause issues
var views = view.slice();
for(var i = 0; i < view.length; i++){
this.add(view[i]);
}
return this;
}
if(view.parentContainer !== this || this.indexOf(view) !== insertIndex){
if(view.parentContainer instanceof ViewContainer){
view.parentContainer.splice(view.parentContainer.indexOf(view),1);
}
this.splice(insertIndex >= 0 ? insertIndex : this.length,0,view);
this.emit('inserted', view);
}
view.parentContainer = this;
if(this._bound && this._render){
if(!(view instanceof View)){
view = this[this.indexOf(view)] = this.gaffa.initialiseView(view);
}
if(!view._bound){
view.gaffa = this.parent.gaffa;
if(!view.renderedElement){
view.render();
view.renderedElement.__iuid = view.__iuid;
if(view.gaffa.debug && !(view.gaffa.browser.msie && view.gaffa.browser.version <9)){
view.renderedElement.viewModel = view;
}
}
view.once('bind', function(){
view.insert(view.parentContainer, insertIndex);
});
view.bind(this.parent, this.parent.scope);
}else{
view.insert(this, insertIndex);
}
}
return view;
};
/*
Adds children to the view container over time.
*/
ViewContainer.prototype.deferredAdd = function(view, insertIndex){
this._deferredViews.push([view, insertIndex]);
requestInsersion(this, this._deferredViews);
};
ViewContainer.prototype.abortDeferredAdd = function(){
while(this._deferredViews.length){
var view = this._deferredViews.pop()[0];
if(view instanceof View && !view._bound){
view.destroy();
}
}
};
ViewContainer.prototype.render = function(){
this._render = true;
for(var i = 0; i < this.length; i++){
this.deferredAdd(this[i], i);
}
};
ViewContainer.prototype.derender = function(){
this._render = false;
for(var i = 0; i < this.length; i++){
var childView = this[i];
if(childView._bound){
childView.detach();
childView.debind();
}
}
};
ViewContainer.prototype.remove = function(view){
view.remove();
this.emit('removed', view);
};
ViewContainer.prototype.empty = function(){
removeViews(this);
this.emit('empty');
};
ViewContainer.prototype.__serialiseExclude__ = ['element'];
module.exports = ViewContainer;