-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathWidgetList.js
286 lines (253 loc) · 12.1 KB
/
WidgetList.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
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
define([
"require",
"dojo/_base/array",
"dojo/_base/lang",
"dojo/_base/declare",
"dijit/_Container",
"dijit/_WidgetBase",
"./Templated"
], function(require, array, lang, declare, _Container, _WidgetBase, Templated){
var childTypeAttr = "data-mvc-child-type",
childMixinsAttr = "data-mvc-child-mixins",
childParamsAttr = "data-mvc-child-props",
childBindingsAttr = "data-mvc-child-bindings",
undef;
function evalParams(params){
return eval("({" + params + "})");
}
function unwatchElements(/*dojox/mvc/WidgetList*/ w){
for(var h = null; h = (w._handles || []).pop();){
h.unwatch();
}
}
function flatten(/*String[][]*/ a){
var flattened = [];
array.forEach(a, function(item){
[].push.apply(flattened, item);
});
return flattened;
}
function loadModules(/*dojo/Stateful[]*/ items, /*Function*/ callback){
// summary:
// Load modules associated with an array of data.
// items: dojo/Stateful[]
// The array of data.
// callback: Function
// Then callback called when the modules have been loaded.
if(this.childClz){
callback(this.childClz);
}else if(this.childType){
var typesForItems = !lang.isFunction(this.childType) && !lang.isFunction(this.childMixins) ? [[this.childType].concat(this.childMixins && this.childMixins.split(",") || [])] :
array.map(items, function(item){
var type = lang.isFunction(this.childType) ? this.childType.call(item, this) : this.childType,
mixins = lang.isFunction(this.childMixins) ? this.childMixins.call(item, this) : this.childMixins;
return type ? [type].concat(lang.isArray(mixins) ? mixins : mixins ? mixins.split(",") : []) : ["dojox/mvc/Templated"];
}, this);
require(array.filter(array.map(flatten(typesForItems), function(type){ return lang.getObject(type) ? undef : type; }), function(type){ return type !== undef; }), function(){
callback.apply(this, array.map(typesForItems, function(types){
var clzList = array.map(types, function(type){ return lang.getObject(type) || require(type); });
return clzList.length > 1 ? declare(clzList, {}) : clzList[0];
}));
});
}else{
callback(Templated);
}
}
var WidgetList = declare("dojox.mvc.WidgetList", [_WidgetBase, _Container], {
// summary:
// A widget that creates child widgets repeatedly based on the children attribute (the repeated data) and childType/childMixins/childParams attributes (determines how to create each child widget).
// example:
// Create multiple instances of dijit/TextBox based on the data in array.
// The text box refers to First property in the array item.
// | <div data-dojo-type="dojox/mvc/WidgetList"
// | data-dojo-props="children: array"
// | data-mvc-child-type="dijit/form/TextBox"
// | data-mvc-child-props="value: at(this.target, 'First')"></div>
// example:
// Create multiple instances of widgets-in-template based on the HTML written in `<script type="dojox/mvc/InlineTemplate">`.
// The label refers to Serial property in the array item, and the text box refers to First property in the array item.
// | <div data-dojo-type="dojox/mvc/WidgetList"
// | data-dojo-mixins="dojox/mvc/_InlineTemplateMixin"
// | data-dojo-props="children: array">
// | <script type="dojox/mvc/InlineTemplate">
// | <div>
// | <span data-dojo-type="dijit/_WidgetBase"
// | data-dojo-props="_setValueAttr: {node: 'domNode', type: 'innerText'}, value: at('rel:', 'Serial')"></span>:
// | <span data-dojo-type="dijit/form/TextBox"
// | data-dojo-props="value: at('rel:', 'First')"></span>
// | </div>
// | </script>
// | </div>
// example:
// Programmatically create multiple instances of widgets-in-template based on the HTML stored in childTemplate.
// (childTemplate may come from dojo/text)
// Also programmatically establish data binding at child widget's startup phase.
// The label refers to Serial property in the array item, and the text box refers to First property in the array item.
// | var childTemplate = '<div>'
// | + '<span data-dojo-type="dijit/_WidgetBase"'
// | + ' data-dojo-attach-point="labelNode"'
// | + ' data-dojo-props="_setValueAttr: {node: \'domNode\', type: \'innerText\'}"></span>'
// | + '<span data-dojo-type="dijit/form/TextBox"'
// | + ' data-dojo-attach-point="inputNode"></span>'
// | + '</div>';
// | (new WidgetList({
// | children: array,
// | childParams: {
// | startup: function(){
// | this.labelNode.set("value", at("rel:", "Serial"));
// | this.inputNode.set("value", at("rel:", "First"));
// | this.inherited("startup", arguments);
// | }
// | },
// | templateString: childTemplate
// | }, dom.byId("programmaticRepeat"))).startup();
// example:
// Using the same childTemplate above, establish data binding for child widgets based on the declaration in childBindings.
// (childBindings may come from dojo/text, by eval()'ing the text)
// | var childBindings = {
// | labelNode: {value: at("rel:", "Serial")},
// | inputNode: {value: at("rel:", "First")}
// | };
// | (new WidgetList({
// | children: array,
// | templateString: childTemplate,
// | childBindings: childBindings
// | }, dom.byId("programmaticRepeatWithSeparateBindingDeclaration"))).startup();
// childClz: Function
// The class of the child widget. Takes precedence over childType/childMixins.
childClz: null,
// childType: String|Function
// The module ID of child widget, or a function that takes child data as the argument and returns the module ID of child widget. childClz takes precedence over this/childMixins.
// Can be specified via data-mvc-child-type attribute of widget declaration.
childType: "",
// childMixins: String|String[]|Function
// The list of module IDs (separated by comma), or a functinon that takes child data as the argument and returns it, of the classes that will be mixed into child widget. childClz takes precedence over childType/this.
// Can be specified via data-mvc-child-mixins attribute of widget declaration.
childMixins: "",
// childParams: Object|Function
// The mixin properties for child widget.
// Can be specified via data-mvc-child-props attribute of widget declaration.
// "this" in data-mvc-child-props will have the following properties:
//
// - parent - This widget's instance.
// - target - The data item in children.
childParams: null,
// childBindings: Object|Function
// Data bindings for child widget.
childBindings: null,
// children: dojox/mvc/StatefulArray
// The array of data model that is used to render child nodes.
children: null,
/*=====
// templateString: String
// The template string for each child items. templateString in child widgets take precedence over this.
templateString: "",
=====*/
// partialRebuild: Boolean
// If true, only rebuild repeat items for changed elements. Otherwise, rebuild everything if there is a change in children.
partialRebuild: false,
// _relTargetProp: String
// The name of the property that is used by child widgets for relative data binding.
_relTargetProp : "children",
postMixInProperties: function(){
this.inherited(arguments);
if(this[childTypeAttr]){
this.childType = this[childTypeAttr];
}
if(this[childMixinsAttr]){
this.childMixins = this[childMixinsAttr];
}
},
startup: function(){
this.inherited(arguments);
this._setChildrenAttr(this.children);
},
_setChildrenAttr: function(/*dojo/Stateful*/ value){
// summary:
// Handler for calls to set("children", val).
var children = this.children;
this._set("children", value);
if(this._started && (!this._builtOnce || children != value)){
this._builtOnce = true;
this._buildChildren(value);
if(lang.isArray(value)){
var _self = this;
value.watch !== {}.watch && (this._handles = this._handles || []).push(value.watch(function(name, old, current){
if(!isNaN(name)){
var w = _self.getChildren()[name - 0];
w && w.set(w._relTargetProp || "target", current);
}
}));
}
}
},
_buildChildren: function(/*dojox/mvc/StatefulArray*/ children){
// summary:
// Create child widgets upon children and inserts them into the container node.
unwatchElements(this);
for(var cw = this.getChildren(), w = null; w = cw.pop();){ this.removeChild(w); w.destroy(); }
if(!lang.isArray(children)){ return; }
var _self = this,
seq = this._buildChildrenSeq = (this._buildChildrenSeq || 0) + 1,
initial = {idx: 0, removals: [], adds: [].concat(children)},
changes = [initial];
function loadedModule(/*Object*/ change){
// summary:
// The callback function called when modules associated with an array splice have been loaded.
// description:
// Looks through the queued array splices and process queue entries whose modules have been loaded, by removing/adding child widgets upon the array splice.
if(this._beingDestroyed || this._buildChildrenSeq > seq){ return; } // If this _WidgetList is being destroyed, or newer _buildChildren call comes during lazy loading, bail
// Associate an object associated with an array splice with the module loaded
var list = [].slice.call(arguments, 1);
change.clz = lang.isFunction(this.childType) || lang.isFunction(this.childMixins) ? list : list[0];
// Looks through the queued array splices
for(var item = null; item = changes.shift();){
// The modules for the array splice have not been loaded, bail
if(!item.clz){
changes.unshift(item);
break;
}
// Remove child widgets upon the array removals
for(var i = 0, l = (item.removals || []).length; i < l; ++i){
this.removeChild(item.idx);
}
// Create/add child widgets upon the array adds
array.forEach(array.map(item.adds, function(child, idx){
var params = {
ownerDocument: this.ownerDocument,
parent: this,
indexAtStartup: item.idx + idx // Won't be updated even if there are removals/adds of repeat items after startup
}, childClz = lang.isArray(item.clz) ? item.clz[idx] : item.clz;
params[(lang.isFunction(this.childParams) && this.childParams.call(params, this) || this.childParams || this[childParamsAttr] && evalParams.call(params, this[childParamsAttr]) || {})._relTargetProp || childClz.prototype._relTargetProp || "target"] = child;
var childParams = this.childParams || this[childParamsAttr] && evalParams.call(params, this[childParamsAttr]),
childBindings = this.childBindings || this[childBindingsAttr] && evalParams.call(params, this[childBindingsAttr]);
if(this.templateString && !params.templateString && !childClz.prototype.templateString){ params.templateString = this.templateString; }
if(childBindings && !params.bindings && !childClz.prototype.bindings){ params.bindings = childBindings; }
return new childClz(lang.delegate(lang.isFunction(childParams) ? childParams.call(params, this) : childParams, params));
}, this), function(child, idx){
this.addChild(child, item.idx + idx);
}, this);
}
}
lang.isFunction(children.watchElements) && (this._handles = this._handles || []).push(children.watchElements(function(idx, removals, adds){
if(!removals || !adds || !_self.partialRebuild){
// If the entire array is changed, or this WidgetList should rebuild the whole child widgets with every change in array, rebuild the whole
_self._buildChildren(children);
}else{
// Otherwise queue the array splice and load modules associated with the additions
var change = {idx: idx, removals: removals, adds: adds};
changes.push(change);
loadModules.call(_self, adds, lang.hitch(_self, loadedModule, change));
}
}));
// Load modules associated with the initial data
loadModules.call(this, children, lang.hitch(this, loadedModule, initial));
},
destroy: function(){
unwatchElements(this);
this.inherited(arguments);
}
});
WidgetList.prototype[childTypeAttr] = WidgetList.prototype[childMixinsAttr] = WidgetList.prototype[childParamsAttr] = WidgetList.prototype[childBindingsAttr] = ""; // Let parser treat these attributes as string
return WidgetList;
});