| 1 |
define([ |
| 2 |
"dojo/_base/window", |
| 3 |
"dojo/_base/lang", |
| 4 |
"dojo/_base/array", |
| 5 |
"dojo/on", |
| 6 |
"dojo/dom-attr", |
| 7 |
"dojo/dom-class", |
| 8 |
"dijit/_base/manager", |
| 9 |
"dijit/_Widget", |
| 10 |
"dijit/form/_FormWidget", |
| 11 |
"dijit/form/Button", |
| 12 |
"dijit/form/CheckBox", |
| 13 |
"dojo/_base/declare" |
| 14 |
], function(win, lang, array, on, domAttr, domClass, manager, Widget, FormWidget, Button, CheckBox, declare){ |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
var fm = lang.getObject("dojox.form.manager", true), |
| 19 |
|
| 20 |
aa = fm.actionAdapter = function(action){ |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
return function(name, elems, value){ |
| 28 |
if(lang.isArray(elems)){ |
| 29 |
array.forEach(elems, function(elem){ |
| 30 |
action.call(this, name, elem, value); |
| 31 |
}, this); |
| 32 |
}else{ |
| 33 |
action.apply(this, arguments); |
| 34 |
} |
| 35 |
}; |
| 36 |
}, |
| 37 |
|
| 38 |
ia = fm.inspectorAdapter = function(inspector){ |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
return function(name, elem, value){ |
| 45 |
return inspector.call(this, name, lang.isArray(elem) ? elem[0] : elem, value); |
| 46 |
}; |
| 47 |
}, |
| 48 |
|
| 49 |
skipNames = {domNode: 1, containerNode: 1, srcNodeRef: 1, bgIframe: 1}, |
| 50 |
|
| 51 |
keys = fm._keys = function(o){ |
| 52 |
|
| 53 |
var list = [], key; |
| 54 |
for(key in o){ |
| 55 |
if(o.hasOwnProperty(key)){ |
| 56 |
list.push(key); |
| 57 |
} |
| 58 |
} |
| 59 |
return list; |
| 60 |
}, |
| 61 |
|
| 62 |
registerWidget = function(widget){ |
| 63 |
var name = widget.get("name"); |
| 64 |
if(name && widget.isInstanceOf(FormWidget)){ |
| 65 |
if(name in this.formWidgets){ |
| 66 |
var a = this.formWidgets[name].widget; |
| 67 |
if(lang.isArray(a)){ |
| 68 |
a.push(widget); |
| 69 |
}else{ |
| 70 |
this.formWidgets[name].widget = [a, widget]; |
| 71 |
} |
| 72 |
}else{ |
| 73 |
this.formWidgets[name] = {widget: widget, connections: []}; |
| 74 |
} |
| 75 |
}else{ |
| 76 |
name = null; |
| 77 |
} |
| 78 |
return name; |
| 79 |
}, |
| 80 |
|
| 81 |
getObserversFromWidget = function(name){ |
| 82 |
var observers = {}; |
| 83 |
aa(function(_, w){ |
| 84 |
var o = w.get("data-dojo-observer") || w.get("observer"); |
| 85 |
if(o && typeof o == "string"){ |
| 86 |
array.forEach(o.split(","), function(o){ |
| 87 |
o = lang.trim(o); |
| 88 |
if(o && lang.isFunction(this[o])){ |
| 89 |
observers[o] = 1; |
| 90 |
} |
| 91 |
}, this); |
| 92 |
} |
| 93 |
}).call(this, null, this.formWidgets[name].widget); |
| 94 |
return keys(observers); |
| 95 |
}, |
| 96 |
|
| 97 |
connectWidget = function(name, observers){ |
| 98 |
var t = this.formWidgets[name], w = t.widget, c = t.connections; |
| 99 |
if(c.length){ |
| 100 |
array.forEach(c, function(item){ item.remove(); }); |
| 101 |
c = t.connections = []; |
| 102 |
} |
| 103 |
if(lang.isArray(w)){ |
| 104 |
|
| 105 |
array.forEach(w, function(w){ |
| 106 |
array.forEach(observers, function(o){ |
| 107 |
c.push(on(w, "change", lang.hitch(this, function(evt){ |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
if(this.watching && domAttr.get(w.focusNode, "checked")){ |
| 112 |
this[o](w.get("value"), name, w, evt); |
| 113 |
} |
| 114 |
}))); |
| 115 |
}, this); |
| 116 |
}, this); |
| 117 |
}else{ |
| 118 |
|
| 119 |
|
| 120 |
var eventName = w.isInstanceOf(Button) ? |
| 121 |
"click" : "change"; |
| 122 |
array.forEach(observers, function(o){ |
| 123 |
c.push(on(w, eventName, lang.hitch(this, function(evt){ |
| 124 |
if(this.watching){ |
| 125 |
this[o](w.get("value"), name, w, evt); |
| 126 |
} |
| 127 |
}))); |
| 128 |
}, this); |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
var _Mixin = declare("dojox.form.manager._Mixin", null, { |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
watching: true, |
| 144 |
|
| 145 |
startup: function(){ |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
if(this._started){ return; } |
| 151 |
|
| 152 |
this.formWidgets = {}; |
| 153 |
this.formNodes = {}; |
| 154 |
this.registerWidgetDescendants(this); |
| 155 |
|
| 156 |
this.inherited(arguments); |
| 157 |
}, |
| 158 |
|
| 159 |
destroy: function(){ |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
for(var name in this.formWidgets){ |
| 164 |
array.forEach(this.formWidgets[name].connections, function(item){ |
| 165 |
item.remove(); |
| 166 |
}); |
| 167 |
} |
| 168 |
this.formWidgets = {}; |
| 169 |
|
| 170 |
this.inherited(arguments); |
| 171 |
}, |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
registerWidget: function(widget){ |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
if(typeof widget == "string"){ |
| 183 |
widget = manager.byId(widget); |
| 184 |
}else if(widget.tagName && widget.cloneNode){ |
| 185 |
widget = manager.byNode(widget); |
| 186 |
} |
| 187 |
var name = registerWidget.call(this, widget); |
| 188 |
if(name){ |
| 189 |
connectWidget.call(this, name, getObserversFromWidget.call(this, name)); |
| 190 |
} |
| 191 |
return this; |
| 192 |
}, |
| 193 |
|
| 194 |
unregisterWidget: function(name){ |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
if(name in this.formWidgets){ |
| 203 |
array.forEach(this.formWidgets[name].connections, function(item){ |
| 204 |
item.remove(); |
| 205 |
}); |
| 206 |
delete this.formWidgets[name]; |
| 207 |
} |
| 208 |
return this; |
| 209 |
}, |
| 210 |
|
| 211 |
registerWidgetDescendants: function(widget){ |
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
if(typeof widget == "string"){ |
| 221 |
widget = manager.byId(widget); |
| 222 |
}else if(widget.tagName && widget.cloneNode){ |
| 223 |
widget = manager.byNode(widget); |
| 224 |
} |
| 225 |
|
| 226 |
|
| 227 |
var widgets = array.map(widget.getDescendants(), registerWidget, this); |
| 228 |
|
| 229 |
|
| 230 |
array.forEach(widgets, function(name){ |
| 231 |
if(name){ |
| 232 |
connectWidget.call(this, name, getObserversFromWidget.call(this, name)); |
| 233 |
} |
| 234 |
}, this); |
| 235 |
|
| 236 |
|
| 237 |
return this.registerNodeDescendants ? |
| 238 |
this.registerNodeDescendants(widget.domNode) : this; |
| 239 |
}, |
| 240 |
|
| 241 |
unregisterWidgetDescendants: function(widget){ |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
if(typeof widget == "string"){ |
| 251 |
widget = manager.byId(widget); |
| 252 |
}else if(widget.tagName && widget.cloneNode){ |
| 253 |
widget = manager.byNode(widget); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
array.forEach( |
| 258 |
array.map( |
| 259 |
widget.getDescendants(), |
| 260 |
function(w){ |
| 261 |
return w instanceof FormWidget && w.get("name") || null; |
| 262 |
} |
| 263 |
), |
| 264 |
function(name){ |
| 265 |
if(name){ |
| 266 |
this.unregisterWidget(name); |
| 267 |
} |
| 268 |
}, |
| 269 |
this |
| 270 |
); |
| 271 |
|
| 272 |
|
| 273 |
return this.unregisterNodeDescendants ? |
| 274 |
this.unregisterNodeDescendants(widget.domNode) : this; |
| 275 |
}, |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
formWidgetValue: function(elem, value){ |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
var isSetter = arguments.length == 2 && value !== undefined, result; |
| 291 |
|
| 292 |
if(typeof elem == "string"){ |
| 293 |
elem = this.formWidgets[elem]; |
| 294 |
if(elem){ |
| 295 |
elem = elem.widget; |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if(!elem){ |
| 300 |
return null; |
| 301 |
} |
| 302 |
|
| 303 |
if(lang.isArray(elem)){ |
| 304 |
|
| 305 |
if(isSetter){ |
| 306 |
array.forEach(elem, function(widget){ |
| 307 |
widget.set("checked", false, !this.watching); |
| 308 |
}, this); |
| 309 |
array.forEach(elem, function(widget){ |
| 310 |
widget.set("checked", widget.value === value, !this.watching); |
| 311 |
}, this); |
| 312 |
return this; |
| 313 |
} |
| 314 |
|
| 315 |
array.some(elem, function(widget){ |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if(domAttr.get(widget.focusNode, "checked")){ |
| 320 |
|
| 321 |
result = widget; |
| 322 |
return true; |
| 323 |
} |
| 324 |
return false; |
| 325 |
}); |
| 326 |
return result ? result.get("value") : ""; |
| 327 |
} |
| 328 |
|
| 329 |
|
| 330 |
if(elem.isInstanceOf && elem.isInstanceOf(CheckBox)){ |
| 331 |
if(isSetter){ |
| 332 |
elem.set("value", Boolean(value), !this.watching); |
| 333 |
return this; |
| 334 |
} |
| 335 |
return Boolean(elem.get("value")); |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
if(isSetter){ |
| 340 |
elem.set("value", value, !this.watching); |
| 341 |
return this; |
| 342 |
} |
| 343 |
return elem.get("value"); |
| 344 |
}, |
| 345 |
|
| 346 |
formPointValue: function(elem, value){ |
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
if(elem && typeof elem == "string"){ |
| 358 |
elem = this[elem]; |
| 359 |
} |
| 360 |
|
| 361 |
if(!elem || !elem.tagName || !elem.cloneNode){ |
| 362 |
return null; |
| 363 |
} |
| 364 |
|
| 365 |
if(!domClass.contains(elem, "dojoFormValue")){ |
| 366 |
|
| 367 |
return null; |
| 368 |
} |
| 369 |
|
| 370 |
if(arguments.length == 2 && value !== undefined){ |
| 371 |
|
| 372 |
elem.innerHTML = value; |
| 373 |
return this; |
| 374 |
} |
| 375 |
|
| 376 |
return elem.innerHTML; |
| 377 |
}, |
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
inspectFormWidgets: function(inspector, state, defaultValue){ |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
var name, result = {}; |
| 396 |
|
| 397 |
if(state){ |
| 398 |
if(lang.isArray(state)){ |
| 399 |
array.forEach(state, function(name){ |
| 400 |
if(name in this.formWidgets){ |
| 401 |
result[name] = inspector.call(this, name, this.formWidgets[name].widget, defaultValue); |
| 402 |
} |
| 403 |
}, this); |
| 404 |
}else{ |
| 405 |
for(name in state){ |
| 406 |
if(name in this.formWidgets){ |
| 407 |
result[name] = inspector.call(this, name, this.formWidgets[name].widget, state[name]); |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
}else{ |
| 412 |
for(name in this.formWidgets){ |
| 413 |
result[name] = inspector.call(this, name, this.formWidgets[name].widget, defaultValue); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
return result; |
| 418 |
}, |
| 419 |
|
| 420 |
inspectAttachedPoints: function(inspector, state, defaultValue){ |
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
var name, elem, result = {}; |
| 435 |
|
| 436 |
if(state){ |
| 437 |
if(lang.isArray(state)){ |
| 438 |
array.forEach(state, function(name){ |
| 439 |
elem = this[name]; |
| 440 |
if(elem && elem.tagName && elem.cloneNode){ |
| 441 |
result[name] = inspector.call(this, name, elem, defaultValue); |
| 442 |
} |
| 443 |
}, this); |
| 444 |
}else{ |
| 445 |
for(name in state){ |
| 446 |
elem = this[name]; |
| 447 |
if(elem && elem.tagName && elem.cloneNode){ |
| 448 |
result[name] = inspector.call(this, name, elem, state[name]); |
| 449 |
} |
| 450 |
} |
| 451 |
} |
| 452 |
}else{ |
| 453 |
for(name in this){ |
| 454 |
if(!(name in skipNames)){ |
| 455 |
elem = this[name]; |
| 456 |
if(elem && elem.tagName && elem.cloneNode){ |
| 457 |
result[name] = inspector.call(this, name, elem, defaultValue); |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
} |
| 462 |
|
| 463 |
return result; |
| 464 |
}, |
| 465 |
|
| 466 |
inspect: function(inspector, state, defaultValue){ |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
var result = this.inspectFormWidgets(function(name, widget, value){ |
| 482 |
if(lang.isArray(widget)){ |
| 483 |
return inspector.call(this, name, array.map(widget, function(w){ return w.domNode; }), value); |
| 484 |
} |
| 485 |
return inspector.call(this, name, widget.domNode, value); |
| 486 |
}, state, defaultValue); |
| 487 |
if(this.inspectFormNodes){ |
| 488 |
lang.mixin(result, this.inspectFormNodes(inspector, state, defaultValue)); |
| 489 |
} |
| 490 |
return lang.mixin(result, this.inspectAttachedPoints(inspector, state, defaultValue)); |
| 491 |
} |
| 492 |
}); |
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
lang.extend(Widget, { |
| 498 |
observer: "" |
| 499 |
}); |
| 500 |
return _Mixin; |
| 501 |
}); |