| 1 |
define([ |
| 2 |
"require", |
| 3 |
"dojo/_base/array", |
| 4 |
"dojo/_base/lang", |
| 5 |
"dojo/_base/declare", |
| 6 |
"dijit/_Container", |
| 7 |
"dijit/_WidgetBase", |
| 8 |
"./Templated" |
| 9 |
], function(require, array, lang, declare, _Container, _WidgetBase, Templated){ |
| 10 |
var childTypeAttr = "data-mvc-child-type", |
| 11 |
childMixinsAttr = "data-mvc-child-mixins", |
| 12 |
childParamsAttr = "data-mvc-child-props", |
| 13 |
childBindingsAttr = "data-mvc-child-bindings", |
| 14 |
undef; |
| 15 |
|
| 16 |
function evalParams(params){ |
| 17 |
return eval("({" + params + "})"); |
| 18 |
} |
| 19 |
|
| 20 |
function unwatchElements( w){ |
| 21 |
for(var h = null; h = (w._handles || []).pop();){ |
| 22 |
h.unwatch(); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
function flatten( a){ |
| 27 |
var flattened = []; |
| 28 |
array.forEach(a, function(item){ |
| 29 |
[].push.apply(flattened, item); |
| 30 |
}); |
| 31 |
return flattened; |
| 32 |
} |
| 33 |
|
| 34 |
function loadModules( items, callback){ |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
if(this.childClz){ |
| 43 |
callback(this.childClz); |
| 44 |
}else if(this.childType){ |
| 45 |
var typesForItems = !lang.isFunction(this.childType) && !lang.isFunction(this.childMixins) ? [[this.childType].concat(this.childMixins && this.childMixins.split(",") || [])] : |
| 46 |
array.map(items, function(item){ |
| 47 |
var type = lang.isFunction(this.childType) ? this.childType.call(item, this) : this.childType, |
| 48 |
mixins = lang.isFunction(this.childMixins) ? this.childMixins.call(item, this) : this.childMixins; |
| 49 |
return type ? [type].concat(lang.isArray(mixins) ? mixins : mixins ? mixins.split(",") : []) : ["dojox/mvc/Templated"]; |
| 50 |
}, this); |
| 51 |
require(array.filter(array.map(flatten(typesForItems), function(type){ return lang.getObject(type) ? undef : type; }), function(type){ return type !== undef; }), function(){ |
| 52 |
callback.apply(this, array.map(typesForItems, function(types){ |
| 53 |
var clzList = array.map(types, function(type){ return lang.getObject(type) || require(type); }); |
| 54 |
return clzList.length > 1 ? declare(clzList, {}) : clzList[0]; |
| 55 |
})); |
| 56 |
}); |
| 57 |
}else{ |
| 58 |
callback(Templated); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
var WidgetList = declare("dojox.mvc.WidgetList", [_WidgetBase, _Container], { |
| 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 |
childClz: null, |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
childType: "", |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
childMixins: "", |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
childParams: null, |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
childBindings: null, |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
children: null, |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
partialRebuild: false, |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
_relTargetProp : "children", |
| 167 |
|
| 168 |
postMixInProperties: function(){ |
| 169 |
this.inherited(arguments); |
| 170 |
if(this[childTypeAttr]){ |
| 171 |
this.childType = this[childTypeAttr]; |
| 172 |
} |
| 173 |
if(this[childMixinsAttr]){ |
| 174 |
this.childMixins = this[childMixinsAttr]; |
| 175 |
} |
| 176 |
}, |
| 177 |
|
| 178 |
startup: function(){ |
| 179 |
this.inherited(arguments); |
| 180 |
this._setChildrenAttr(this.children); |
| 181 |
}, |
| 182 |
|
| 183 |
_setChildrenAttr: function( value){ |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
var children = this.children; |
| 188 |
this._set("children", value); |
| 189 |
if(this._started && (!this._builtOnce || children != value)){ |
| 190 |
this._builtOnce = true; |
| 191 |
this._buildChildren(value); |
| 192 |
if(lang.isArray(value)){ |
| 193 |
var _self = this; |
| 194 |
value.watch !== {}.watch && (this._handles = this._handles || []).push(value.watch(function(name, old, current){ |
| 195 |
if(!isNaN(name)){ |
| 196 |
var w = _self.getChildren()[name - 0]; |
| 197 |
w && w.set(w._relTargetProp || "target", current); |
| 198 |
} |
| 199 |
})); |
| 200 |
} |
| 201 |
} |
| 202 |
}, |
| 203 |
|
| 204 |
_buildChildren: function( children){ |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
unwatchElements(this); |
| 209 |
for(var cw = this.getChildren(), w = null; w = cw.pop();){ this.removeChild(w); w.destroy(); } |
| 210 |
if(!lang.isArray(children)){ return; } |
| 211 |
|
| 212 |
var _self = this, |
| 213 |
seq = this._buildChildrenSeq = (this._buildChildrenSeq || 0) + 1, |
| 214 |
initial = {idx: 0, removals: [], adds: [].concat(children)}, |
| 215 |
changes = [initial]; |
| 216 |
|
| 217 |
function loadedModule( change){ |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
if(this._beingDestroyed || this._buildChildrenSeq > seq){ return; } |
| 224 |
|
| 225 |
|
| 226 |
var list = [].slice.call(arguments, 1); |
| 227 |
change.clz = lang.isFunction(this.childType) || lang.isFunction(this.childMixins) ? list : list[0]; |
| 228 |
|
| 229 |
|
| 230 |
for(var item = null; item = changes.shift();){ |
| 231 |
|
| 232 |
if(!item.clz){ |
| 233 |
changes.unshift(item); |
| 234 |
break; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
for(var i = 0, l = (item.removals || []).length; i < l; ++i){ |
| 239 |
this.removeChild(item.idx); |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
array.forEach(array.map(item.adds, function(child, idx){ |
| 244 |
var params = { |
| 245 |
ownerDocument: this.ownerDocument, |
| 246 |
parent: this, |
| 247 |
indexAtStartup: item.idx + idx |
| 248 |
}, childClz = lang.isArray(item.clz) ? item.clz[idx] : item.clz; |
| 249 |
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; |
| 250 |
|
| 251 |
var childParams = this.childParams || this[childParamsAttr] && evalParams.call(params, this[childParamsAttr]), |
| 252 |
childBindings = this.childBindings || this[childBindingsAttr] && evalParams.call(params, this[childBindingsAttr]); |
| 253 |
if(this.templateString && !params.templateString && !childClz.prototype.templateString){ params.templateString = this.templateString; } |
| 254 |
if(childBindings && !params.bindings && !childClz.prototype.bindings){ params.bindings = childBindings; } |
| 255 |
return new childClz(lang.delegate(lang.isFunction(childParams) ? childParams.call(params, this) : childParams, params)); |
| 256 |
}, this), function(child, idx){ |
| 257 |
this.addChild(child, item.idx + idx); |
| 258 |
}, this); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
lang.isFunction(children.watchElements) && (this._handles = this._handles || []).push(children.watchElements(function(idx, removals, adds){ |
| 263 |
if(!removals || !adds || !_self.partialRebuild){ |
| 264 |
|
| 265 |
_self._buildChildren(children); |
| 266 |
}else{ |
| 267 |
|
| 268 |
var change = {idx: idx, removals: removals, adds: adds}; |
| 269 |
changes.push(change); |
| 270 |
loadModules.call(_self, adds, lang.hitch(_self, loadedModule, change)); |
| 271 |
} |
| 272 |
})); |
| 273 |
|
| 274 |
|
| 275 |
loadModules.call(this, children, lang.hitch(this, loadedModule, initial)); |
| 276 |
}, |
| 277 |
|
| 278 |
destroy: function(){ |
| 279 |
unwatchElements(this); |
| 280 |
this.inherited(arguments); |
| 281 |
} |
| 282 |
}); |
| 283 |
|
| 284 |
WidgetList.prototype[childTypeAttr] = WidgetList.prototype[childMixinsAttr] = WidgetList.prototype[childParamsAttr] = WidgetList.prototype[childBindingsAttr] = ""; |
| 285 |
return WidgetList; |
| 286 |
}); |