1 | define([ |
---|
2 | "dijit/_Container", |
---|
3 | "dijit/form/_FormMixin", |
---|
4 | "dijit/registry", |
---|
5 | "dojo/_base/array", |
---|
6 | "dojo/_base/declare", |
---|
7 | "dojo/_base/event", |
---|
8 | "dojo/_base/lang", |
---|
9 | "dojo/on" |
---|
10 | ], function(_Container, _FormMixin, registry, array, declare, event, lang, on) { |
---|
11 | return declare([_Container, _FormMixin], { |
---|
12 | name: "", |
---|
13 | value: null, |
---|
14 | disabled: false, |
---|
15 | readOnly: false, |
---|
16 | _onChangeActive: false, |
---|
17 | constructor: function() { |
---|
18 | this._listenerMap = {}; |
---|
19 | }, |
---|
20 | buildRendering: function() { |
---|
21 | // capture child change events |
---|
22 | this.inherited(arguments); |
---|
23 | this.own(on(this.domNode,'change', |
---|
24 | lang.hitch(this,'_handleChange'))); |
---|
25 | }, |
---|
26 | create: function() { |
---|
27 | this.inherited(arguments); |
---|
28 | this._onChangeActive = true; |
---|
29 | }, |
---|
30 | postCreate: function() { |
---|
31 | this.inherited(arguments); |
---|
32 | if ( this.domNode.tagName.toLowerCase() !== "form" ) { |
---|
33 | 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 <form> as the root element in your template for", this.declaredClass); |
---|
34 | } |
---|
35 | this.own(on(this.domNode, 'submit', lang.hitch(this,'_handleSubmit'))); |
---|
36 | }, |
---|
37 | startup: function() { |
---|
38 | if (this._started) { return; } |
---|
39 | this.inherited(arguments); |
---|
40 | }, |
---|
41 | _setDisabledAttr: function(value) { |
---|
42 | this._set("disabled", value); |
---|
43 | array.forEach(this._getDescendantFormWidgets(), function(child) { |
---|
44 | child.set("disabled", value); |
---|
45 | }); |
---|
46 | }, |
---|
47 | // Yuk, this function is taken directly from _FromMixin only |
---|
48 | // to add the priorityChange parameter |
---|
49 | _getValueAttr: function() { |
---|
50 | this.value = this.inherited(arguments); |
---|
51 | return this.value; |
---|
52 | }, |
---|
53 | _setValueAttr: function(obj, priorityChange) { |
---|
54 | this.value = obj; |
---|
55 | |
---|
56 | var map = { }; |
---|
57 | array.forEach(this._getDescendantFormWidgets(), function(widget){ |
---|
58 | if(!widget.name){ return; } |
---|
59 | var entry = map[widget.name] || (map[widget.name] = [] ); |
---|
60 | entry.push(widget); |
---|
61 | }); |
---|
62 | |
---|
63 | for(var name in map){ |
---|
64 | if (map.hasOwnProperty(name)) { |
---|
65 | var widgets = map[name], |
---|
66 | values = lang.getObject(name, false, obj); |
---|
67 | |
---|
68 | if (values === undefined) { |
---|
69 | continue; |
---|
70 | } |
---|
71 | values = [].concat(values); |
---|
72 | if (typeof widgets[0].checked === 'boolean') { |
---|
73 | array.forEach(widgets, function(w){ |
---|
74 | w.set('value', array.indexOf(values, w._get('value')) !== -1, priorityChange); |
---|
75 | }); |
---|
76 | } else if (widgets[0].multiple) { |
---|
77 | widgets[0].set('value', values, priorityChange); |
---|
78 | } else { |
---|
79 | array.forEach(widgets, function(w, i){ |
---|
80 | w.set('value', values[i], priorityChange); |
---|
81 | }); |
---|
82 | } |
---|
83 | } |
---|
84 | } |
---|
85 | }, |
---|
86 | _setReadOnlyAttr: function(value) { |
---|
87 | this._set("readOnly", value); |
---|
88 | array.forEach(this._getDescendantFormWidgets(), function(child) { |
---|
89 | child.set("readOnly", value); |
---|
90 | }); |
---|
91 | }, |
---|
92 | focus: function() { |
---|
93 | var children = this._getDescendantFormWidgets(); |
---|
94 | if ( children.length > 0 ) { |
---|
95 | children[0].focus(); |
---|
96 | } |
---|
97 | }, |
---|
98 | _handleChange: function(evt) { |
---|
99 | if ( evt.target !== this.domNode ) { |
---|
100 | if ( this._onChangeActive && !(this.readOnly || this.disabled) ) { |
---|
101 | this.emit('change',this.get('value')); |
---|
102 | } |
---|
103 | if ( evt ) { event.stop(evt); } |
---|
104 | return false; |
---|
105 | } |
---|
106 | return true; |
---|
107 | }, |
---|
108 | _handleSubmit: function(evt) { |
---|
109 | var node = this.domNode; |
---|
110 | var widget; |
---|
111 | while ( node ) { |
---|
112 | node = node.parentNode; |
---|
113 | widget = registry.byNode(node); |
---|
114 | if ( widget && typeof widget._onSubmit === "function" ) { |
---|
115 | widget._onSubmit(evt); |
---|
116 | break; |
---|
117 | } |
---|
118 | } |
---|
119 | if (evt) { event.stop(evt); } |
---|
120 | return false; |
---|
121 | } |
---|
122 | }); |
---|
123 | }); |
---|