]>
Commit | Line | Data |
---|---|---|
2f01fe57 | 1 | /* |
81bea17a | 2 | Copyright (c) 2004-2011, The Dojo Foundation All Rights Reserved. |
2f01fe57 AD |
3 | Available via Academic Free License >= 2.1 OR the modified BSD license. |
4 | see: http://dojotoolkit.org/license for details | |
5 | */ | |
6 | ||
7 | ||
81bea17a AD |
8 | if(!dojo._hasResource["dijit.form.CheckBox"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code. |
9 | dojo._hasResource["dijit.form.CheckBox"] = true; | |
2f01fe57 AD |
10 | dojo.provide("dijit.form.CheckBox"); |
11 | dojo.require("dijit.form.ToggleButton"); | |
81bea17a AD |
12 | |
13 | ||
14 | dojo.declare( | |
15 | "dijit.form.CheckBox", | |
16 | dijit.form.ToggleButton, | |
17 | { | |
18 | // summary: | |
19 | // Same as an HTML checkbox, but with fancy styling. | |
20 | // | |
21 | // description: | |
22 | // User interacts with real html inputs. | |
23 | // On onclick (which occurs by mouse click, space-bar, or | |
24 | // using the arrow keys to switch the selected radio button), | |
25 | // we update the state of the checkbox/radio. | |
26 | // | |
27 | // There are two modes: | |
28 | // 1. High contrast mode | |
29 | // 2. Normal mode | |
30 | // | |
31 | // In case 1, the regular html inputs are shown and used by the user. | |
32 | // In case 2, the regular html inputs are invisible but still used by | |
33 | // the user. They are turned quasi-invisible and overlay the background-image. | |
34 | ||
35 | templateString: dojo.cache("dijit.form", "templates/CheckBox.html", "<div class=\"dijit dijitReset dijitInline\" role=\"presentation\"\n\t><input\n\t \t${!nameAttrSetting} type=\"${type}\" ${checkedAttrSetting}\n\t\tclass=\"dijitReset dijitCheckBoxInput\"\n\t\tdojoAttachPoint=\"focusNode\"\n\t \tdojoAttachEvent=\"onclick:_onClick\"\n/></div>\n"), | |
36 | ||
37 | baseClass: "dijitCheckBox", | |
38 | ||
39 | // type: [private] String | |
40 | // type attribute on <input> node. | |
41 | // Overrides `dijit.form.Button.type`. Users should not change this value. | |
42 | type: "checkbox", | |
43 | ||
44 | // value: String | |
45 | // As an initialization parameter, equivalent to value field on normal checkbox | |
46 | // (if checked, the value is passed as the value when form is submitted). | |
47 | // | |
48 | // However, get('value') will return either the string or false depending on | |
49 | // whether or not the checkbox is checked. | |
50 | // | |
51 | // set('value', string) will check the checkbox and change the value to the | |
52 | // specified string | |
53 | // | |
54 | // set('value', boolean) will change the checked state. | |
55 | value: "on", | |
56 | ||
57 | // readOnly: Boolean | |
58 | // Should this widget respond to user input? | |
59 | // In markup, this is specified as "readOnly". | |
60 | // Similar to disabled except readOnly form values are submitted. | |
61 | readOnly: false, | |
62 | ||
63 | // the attributeMap should inherit from dijit.form._FormWidget.prototype.attributeMap | |
64 | // instead of ToggleButton as the icon mapping has no meaning for a CheckBox | |
65 | attributeMap: dojo.delegate(dijit.form._FormWidget.prototype.attributeMap, { | |
66 | readOnly: "focusNode" | |
67 | }), | |
68 | ||
69 | _setReadOnlyAttr: function(/*Boolean*/ value){ | |
70 | this._set("readOnly", value); | |
71 | dojo.attr(this.focusNode, 'readOnly', value); | |
72 | dijit.setWaiState(this.focusNode, "readonly", value); | |
73 | }, | |
74 | ||
75 | _setValueAttr: function(/*String|Boolean*/ newValue, /*Boolean*/ priorityChange){ | |
76 | // summary: | |
77 | // Handler for value= attribute to constructor, and also calls to | |
78 | // set('value', val). | |
79 | // description: | |
80 | // During initialization, just saves as attribute to the <input type=checkbox>. | |
81 | // | |
82 | // After initialization, | |
83 | // when passed a boolean, controls whether or not the CheckBox is checked. | |
84 | // If passed a string, changes the value attribute of the CheckBox (the one | |
85 | // specified as "value" when the CheckBox was constructed (ex: <input | |
86 | // dojoType="dijit.CheckBox" value="chicken">) | |
87 | if(typeof newValue == "string"){ | |
88 | this._set("value", newValue); | |
89 | dojo.attr(this.focusNode, 'value', newValue); | |
90 | newValue = true; | |
91 | } | |
92 | if(this._created){ | |
93 | this.set('checked', newValue, priorityChange); | |
94 | } | |
95 | }, | |
96 | _getValueAttr: function(){ | |
97 | // summary: | |
98 | // Hook so get('value') works. | |
99 | // description: | |
100 | // If the CheckBox is checked, returns the value attribute. | |
101 | // Otherwise returns false. | |
102 | return (this.checked ? this.value : false); | |
103 | }, | |
104 | ||
105 | // Override dijit.form.Button._setLabelAttr() since we don't even have a containerNode. | |
106 | // Normally users won't try to set label, except when CheckBox or RadioButton is the child of a dojox.layout.TabContainer | |
107 | _setLabelAttr: undefined, | |
108 | ||
109 | postMixInProperties: function(){ | |
110 | if(this.value == ""){ | |
111 | this.value = "on"; | |
112 | } | |
113 | ||
114 | // Need to set initial checked state as part of template, so that form submit works. | |
115 | // dojo.attr(node, "checked", bool) doesn't work on IEuntil node has been attached | |
116 | // to <body>, see #8666 | |
117 | this.checkedAttrSetting = this.checked ? "checked" : ""; | |
118 | ||
119 | this.inherited(arguments); | |
120 | }, | |
121 | ||
122 | _fillContent: function(/*DomNode*/ source){ | |
123 | // Override Button::_fillContent() since it doesn't make sense for CheckBox, | |
124 | // since CheckBox doesn't even have a container | |
125 | }, | |
126 | ||
127 | reset: function(){ | |
128 | // Override ToggleButton.reset() | |
129 | ||
130 | this._hasBeenBlurred = false; | |
131 | ||
132 | this.set('checked', this.params.checked || false); | |
133 | ||
134 | // Handle unlikely event that the <input type=checkbox> value attribute has changed | |
135 | this._set("value", this.params.value || "on"); | |
136 | dojo.attr(this.focusNode, 'value', this.value); | |
137 | }, | |
138 | ||
139 | _onFocus: function(){ | |
140 | if(this.id){ | |
141 | dojo.query("label[for='"+this.id+"']").addClass("dijitFocusedLabel"); | |
142 | } | |
143 | this.inherited(arguments); | |
144 | }, | |
145 | ||
146 | _onBlur: function(){ | |
147 | if(this.id){ | |
148 | dojo.query("label[for='"+this.id+"']").removeClass("dijitFocusedLabel"); | |
149 | } | |
150 | this.inherited(arguments); | |
151 | }, | |
152 | ||
153 | _onClick: function(/*Event*/ e){ | |
154 | // summary: | |
155 | // Internal function to handle click actions - need to check | |
156 | // readOnly, since button no longer does that check. | |
157 | if(this.readOnly){ | |
158 | dojo.stopEvent(e); | |
159 | return false; | |
160 | } | |
161 | return this.inherited(arguments); | |
162 | } | |
163 | } | |
164 | ); | |
165 | ||
166 | dojo.declare( | |
167 | "dijit.form.RadioButton", | |
168 | dijit.form.CheckBox, | |
169 | { | |
170 | // summary: | |
171 | // Same as an HTML radio, but with fancy styling. | |
172 | ||
173 | type: "radio", | |
174 | baseClass: "dijitRadio", | |
175 | ||
176 | _setCheckedAttr: function(/*Boolean*/ value){ | |
177 | // If I am being checked then have to deselect currently checked radio button | |
178 | this.inherited(arguments); | |
179 | if(!this._created){ return; } | |
180 | if(value){ | |
181 | var _this = this; | |
182 | // search for radio buttons with the same name that need to be unchecked | |
183 | dojo.query("INPUT[type=radio]", this.focusNode.form || dojo.doc).forEach( // can't use name= since dojo.query doesn't support [] in the name | |
184 | function(inputNode){ | |
185 | if(inputNode.name == _this.name && inputNode != _this.focusNode && inputNode.form == _this.focusNode.form){ | |
186 | var widget = dijit.getEnclosingWidget(inputNode); | |
187 | if(widget && widget.checked){ | |
188 | widget.set('checked', false); | |
189 | } | |
190 | } | |
191 | } | |
192 | ); | |
193 | } | |
194 | }, | |
195 | ||
196 | _clicked: function(/*Event*/ e){ | |
197 | if(!this.checked){ | |
198 | this.set('checked', true); | |
199 | } | |
200 | } | |
201 | } | |
202 | ); | |
203 | ||
2f01fe57 | 204 | } |