1 | define([ |
---|
2 | "require", |
---|
3 | "dojo/_base/declare", // declare |
---|
4 | "dojo/dom-attr", // domAttr.set |
---|
5 | "dojo/_base/kernel", |
---|
6 | "dojo/query", // query |
---|
7 | "dojo/ready", |
---|
8 | "./ToggleButton", |
---|
9 | "./_CheckBoxMixin", |
---|
10 | "dojo/text!./templates/CheckBox.html", |
---|
11 | "dojo/NodeList-dom" // NodeList.addClass/removeClass |
---|
12 | ], function(require, declare, domAttr, kernel, query, ready, ToggleButton, _CheckBoxMixin, template){ |
---|
13 | |
---|
14 | /*===== |
---|
15 | var ToggleButton = dijit.form.ToggleButton; |
---|
16 | var _CheckBoxMixin = dijit.form._CheckBoxMixin; |
---|
17 | =====*/ |
---|
18 | |
---|
19 | // module: |
---|
20 | // dijit/form/CheckBox |
---|
21 | // summary: |
---|
22 | // Checkbox widget |
---|
23 | |
---|
24 | // Back compat w/1.6, remove for 2.0 |
---|
25 | if(!kernel.isAsync){ |
---|
26 | ready(0, function(){ |
---|
27 | var requires = ["dijit/form/RadioButton"]; |
---|
28 | require(requires); // use indirection so modules not rolled into a build |
---|
29 | }); |
---|
30 | } |
---|
31 | |
---|
32 | return declare("dijit.form.CheckBox", [ToggleButton, _CheckBoxMixin], { |
---|
33 | // summary: |
---|
34 | // Same as an HTML checkbox, but with fancy styling. |
---|
35 | // |
---|
36 | // description: |
---|
37 | // User interacts with real html inputs. |
---|
38 | // On onclick (which occurs by mouse click, space-bar, or |
---|
39 | // using the arrow keys to switch the selected radio button), |
---|
40 | // we update the state of the checkbox/radio. |
---|
41 | // |
---|
42 | // There are two modes: |
---|
43 | // 1. High contrast mode |
---|
44 | // 2. Normal mode |
---|
45 | // |
---|
46 | // In case 1, the regular html inputs are shown and used by the user. |
---|
47 | // In case 2, the regular html inputs are invisible but still used by |
---|
48 | // the user. They are turned quasi-invisible and overlay the background-image. |
---|
49 | |
---|
50 | templateString: template, |
---|
51 | |
---|
52 | baseClass: "dijitCheckBox", |
---|
53 | |
---|
54 | _setValueAttr: function(/*String|Boolean*/ newValue, /*Boolean*/ priorityChange){ |
---|
55 | // summary: |
---|
56 | // Handler for value= attribute to constructor, and also calls to |
---|
57 | // set('value', val). |
---|
58 | // description: |
---|
59 | // During initialization, just saves as attribute to the <input type=checkbox>. |
---|
60 | // |
---|
61 | // After initialization, |
---|
62 | // when passed a boolean, controls whether or not the CheckBox is checked. |
---|
63 | // If passed a string, changes the value attribute of the CheckBox (the one |
---|
64 | // specified as "value" when the CheckBox was constructed (ex: <input |
---|
65 | // data-dojo-type="dijit.CheckBox" value="chicken">) |
---|
66 | // widget.set('value', string) will check the checkbox and change the value to the |
---|
67 | // specified string |
---|
68 | // widget.set('value', boolean) will change the checked state. |
---|
69 | if(typeof newValue == "string"){ |
---|
70 | this._set("value", newValue); |
---|
71 | domAttr.set(this.focusNode, 'value', newValue); |
---|
72 | newValue = true; |
---|
73 | } |
---|
74 | if(this._created){ |
---|
75 | this.set('checked', newValue, priorityChange); |
---|
76 | } |
---|
77 | }, |
---|
78 | _getValueAttr: function(){ |
---|
79 | // summary: |
---|
80 | // Hook so get('value') works. |
---|
81 | // description: |
---|
82 | // If the CheckBox is checked, returns the value attribute. |
---|
83 | // Otherwise returns false. |
---|
84 | return (this.checked ? this.value : false); |
---|
85 | }, |
---|
86 | |
---|
87 | // Override behavior from Button, since we don't have an iconNode |
---|
88 | _setIconClassAttr: null, |
---|
89 | |
---|
90 | postMixInProperties: function(){ |
---|
91 | this.inherited(arguments); |
---|
92 | |
---|
93 | // Need to set initial checked state as part of template, so that form submit works. |
---|
94 | // domAttr.set(node, "checked", bool) doesn't work on IE until node has been attached |
---|
95 | // to <body>, see #8666 |
---|
96 | this.checkedAttrSetting = this.checked ? "checked" : ""; |
---|
97 | }, |
---|
98 | |
---|
99 | _fillContent: function(){ |
---|
100 | // Override Button::_fillContent() since it doesn't make sense for CheckBox, |
---|
101 | // since CheckBox doesn't even have a container |
---|
102 | }, |
---|
103 | |
---|
104 | _onFocus: function(){ |
---|
105 | if(this.id){ |
---|
106 | query("label[for='"+this.id+"']").addClass("dijitFocusedLabel"); |
---|
107 | } |
---|
108 | this.inherited(arguments); |
---|
109 | }, |
---|
110 | |
---|
111 | _onBlur: function(){ |
---|
112 | if(this.id){ |
---|
113 | query("label[for='"+this.id+"']").removeClass("dijitFocusedLabel"); |
---|
114 | } |
---|
115 | this.inherited(arguments); |
---|
116 | } |
---|
117 | }); |
---|
118 | }); |
---|