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