define([ "dijit/_Container", "dijit/form/_FormMixin", "dijit/registry", "dojo/_base/array", "dojo/_base/declare", "dojo/_base/event", "dojo/_base/lang", "dojo/on" ], function(_Container, _FormMixin, registry, array, declare, event, lang, on) { return declare([_Container, _FormMixin], { name: "", value: null, disabled: false, readOnly: false, _onChangeActive: false, _onChangeHandle: null, constructor: function() { this._listenerMap = {}; }, buildRendering: function() { // capture child events this.inherited(arguments); if ( this.domNode.tagName.toLowerCase() !== "form" ) { console.warn("Not scoping a _ComplexValueMixin in a form element can cause name clashes. E.g. radio buttons might stop working correctly. It is recommended to use
as the root element in your template for", this.declaredClass); } this.own(on(this.domNode,'submit', lang.hitch(this,'_handleSubmit'))); }, create: function() { this.inherited(arguments); this._onChangeActive = true; }, postCreate: function() { array.forEach(this._getDescendantFormWidgets(), this.connectChildsChanges, this); }, connectChildsChanges: function(child) { this.own(child.on('change', lang.hitch(this,'_handleChildChange'))); }, // Yuk, _setValueAttr is taken directly from _FromMixin only // to add the priorityChange parameter _setValueAttr: function(obj, priorityChange) { this._setValueInternal(obj, priorityChange); var map = { }; array.forEach(this._getDescendantFormWidgets(), function(widget){ if(!widget.name){ return; } var entry = map[widget.name] || (map[widget.name] = [] ); entry.push(widget); }); for(var name in map){ if (map.hasOwnProperty(name)) { var widgets = map[name], values = lang.getObject(name, false, obj); if (values === undefined) { continue; } values = [].concat(values); if (typeof widgets[0].checked === 'boolean') { array.forEach(widgets, function(w){ w.set('value', array.indexOf(values,w._get('value'))!==-1, priorityChange); }); } else if (widgets[0].multiple) { widgets[0].set('value', values, priorityChange); } else { array.forEach(widgets, function(w, i){ w.set('value', values[i], priorityChange); }); } } } if ( priorityChange !== null ) { this._handleOnChange(this.value); } }, // use _setValueInternal instead of 'this.value = ?' or // 'this._set('value',?)' so proper events will be generated. _setValueInternal: function(value,priorityChange) { this._set('value',value); if ( priorityChange !== null ){ this._handleOnChange(this.value); } }, _setDisabledAttr: function(disabled) { this.inherited(arguments); this._set('disabled',disabled); array.forEach( this._getDescendantFormWidgets(), function(child) { child.set('disabled', disabled); },this); }, _setReadOnlyAttr: function(readOnly) { this.inherited(arguments); this._set('readOnly',readOnly); array.forEach( this._getDescendantFormWidgets(), function(child) { child.set('readOnly', readOnly); },this); }, focus: function() { /*var children = this._getDescendantFormWidgets(); if ( children.length > 0 ) { children[0].focus(); }*/ }, addChild: function(child) { this.inherited(arguments); this.connectChildsChanges(child); }, _handleSubmit: function(evt) { if ( this._started ) { var node = this.domNode; var widget; while ( node ) { node = node.parentNode; widget = registry.byNode(node); if ( widget && typeof widget._onSubmit === "function" ) { widget._onSubmit(evt); break; } } } if (evt) { event.stop(evt); } return false; }, _handleChildChange: function() { this._setValueInternal(this.get('value')); return false; }, _handleOnChange: function(value) { if ( this._onChangeActive && !(this.readOnly || this.disabled) ) { if ( this._onChangeHandle ) { this._onChangeHandle.remove(); } this._onChangeHandle = this.defer(function(){ this._onChangeHandle = null; this.onChange(value); }); } }, onChange: function() {} }); });