1 | define([ |
---|
2 | "dojo/_base/array", // array.forEach |
---|
3 | "dojo/_base/declare", // declare |
---|
4 | "dojo/dom-attr", // domAttr.set |
---|
5 | "dojo/_base/lang", // lang.hitch |
---|
6 | "dojo/query", // query |
---|
7 | "../registry" // registry.getEnclosingWidget |
---|
8 | ], function(array, declare, domAttr, lang, query, registry){ |
---|
9 | |
---|
10 | // module: |
---|
11 | // dijit/form/_RadioButtonMixin |
---|
12 | |
---|
13 | return declare("dijit.form._RadioButtonMixin", null, { |
---|
14 | // summary: |
---|
15 | // Mixin to provide widget functionality for an HTML radio button |
---|
16 | |
---|
17 | // type: [private] String |
---|
18 | // type attribute on `<input>` node. |
---|
19 | // Users should not change this value. |
---|
20 | type: "radio", |
---|
21 | |
---|
22 | _getRelatedWidgets: function(){ |
---|
23 | // Private function needed to help iterate over all radio buttons in a group. |
---|
24 | var ary = []; |
---|
25 | query("input[type=radio]", this.focusNode.form || this.ownerDocument).forEach(// can't use name= since query doesn't support [] in the name |
---|
26 | lang.hitch(this, function(inputNode){ |
---|
27 | if(inputNode.name == this.name && inputNode.form == this.focusNode.form){ |
---|
28 | var widget = registry.getEnclosingWidget(inputNode); |
---|
29 | if(widget){ |
---|
30 | ary.push(widget); |
---|
31 | } |
---|
32 | } |
---|
33 | }) |
---|
34 | ); |
---|
35 | return ary; |
---|
36 | }, |
---|
37 | |
---|
38 | _setCheckedAttr: function(/*Boolean*/ value){ |
---|
39 | // If I am being checked then have to deselect currently checked radio button |
---|
40 | this.inherited(arguments); |
---|
41 | if(!this._created){ |
---|
42 | return; |
---|
43 | } |
---|
44 | if(value){ |
---|
45 | array.forEach(this._getRelatedWidgets(), lang.hitch(this, function(widget){ |
---|
46 | if(widget != this && widget.checked){ |
---|
47 | widget.set('checked', false); |
---|
48 | } |
---|
49 | })); |
---|
50 | } |
---|
51 | }, |
---|
52 | |
---|
53 | _getSubmitValue: function(/*String*/ value){ |
---|
54 | return value == null ? "on" : value; |
---|
55 | }, |
---|
56 | |
---|
57 | _onClick: function(/*Event*/ e){ |
---|
58 | if(this.checked || this.disabled){ // nothing to do |
---|
59 | e.stopPropagation(); |
---|
60 | e.preventDefault(); |
---|
61 | return false; |
---|
62 | } |
---|
63 | if(this.readOnly){ // ignored by some browsers so we have to resync the DOM elements with widget values |
---|
64 | e.stopPropagation(); |
---|
65 | e.preventDefault(); |
---|
66 | array.forEach(this._getRelatedWidgets(), lang.hitch(this, function(widget){ |
---|
67 | domAttr.set(this.focusNode || this.domNode, 'checked', widget.checked); |
---|
68 | })); |
---|
69 | return false; |
---|
70 | } |
---|
71 | return this.inherited(arguments); |
---|
72 | } |
---|
73 | }); |
---|
74 | }); |
---|