TriStateCheckBox.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. require({cache:{
  2. 'url:dojox/form/resources/TriStateCheckBox.html':"<div class=\"dijit dijitReset dijitInline\" role=\"presentation\"\n\t><div class=\"dojoxTriStateCheckBoxInner\" dojoAttachPoint=\"stateLabelNode\"></div\n\t><input ${!nameAttrSetting} type=\"${type}\" dojoAttachPoint=\"focusNode\"\n\tclass=\"dijitReset dojoxTriStateCheckBoxInput\" dojoAttachEvent=\"onclick:_onClick\"\n/></div>"}});
  3. define("dojox/form/TriStateCheckBox", [
  4. "dojo/_base/kernel",
  5. "dojo/_base/declare",
  6. "dojo/_base/array",
  7. "dojo/_base/event",
  8. "dojo/query",
  9. "dojo/dom-attr",
  10. "dojo/text!./resources/TriStateCheckBox.html",
  11. "dijit/form/ToggleButton",
  12. "dojo/NodeList-dom" // NodeList.addClass/removeClass
  13. ], function(kernel, declare, array, event, query, domAttr, template, ToggleButton){
  14. // module:
  15. // dojox/form/TriStateCheckBox
  16. // summary:
  17. // Checkbox with three states
  18. //
  19. /*=====
  20. ToggleButton = dijit.form.ToggleButton;
  21. =====*/
  22. return declare("dojox.form.TriStateCheckBox", ToggleButton,
  23. {
  24. // summary:
  25. // Checkbox with three states
  26. templateString: template,
  27. baseClass: "dojoxTriStateCheckBox",
  28. // type: [private] String
  29. // type attribute on <input> node.
  30. // Overrides `dijit.form.Button.type`. Users should not change this value.
  31. type: "checkbox",
  32. /*=====
  33. // states: Array
  34. // States of TriStateCheckBox.
  35. // The value of This.checked should be one of these three states.
  36. states: [false, true, "mixed"],
  37. =====*/
  38. /*=====
  39. // _stateLabels: Object
  40. // These characters are used to replace the image to show
  41. // current state of TriStateCheckBox in high contrast mode.
  42. _stateLabels: {
  43. "False": '&#63219',
  44. "True": '&#8730;',
  45. "Mixed": '&#8801'
  46. },
  47. =====*/
  48. /*=====
  49. // stateValues: Object
  50. // The values of the TriStateCheckBox in corresponding states.
  51. stateValues: {
  52. "False": "off",
  53. "True": "on",
  54. "Mixed": "mixed"
  55. },
  56. =====*/
  57. // _currentState: Integer
  58. // The current state of the TriStateCheckBox
  59. _currentState: 0,
  60. // _stateType: String
  61. // The current state type of the TriStateCheckBox
  62. // Could be "False", "True" or "Mixed"
  63. _stateType: "False",
  64. // readOnly: Boolean
  65. // Should this widget respond to user input?
  66. // In markup, this is specified as "readOnly".
  67. // Similar to disabled except readOnly form values are submitted.
  68. readOnly: false,
  69. constructor: function(){
  70. // summary:
  71. // Runs on widget initialization to setup arrays etc.
  72. // tags:
  73. // private
  74. this.states = [false, true, "mixed"];
  75. this._stateLabels = {
  76. "False": '&#63219',
  77. "True": '&#8730;',
  78. "Mixed": '&#8801'
  79. };
  80. this.stateValues = {
  81. "False": "off",
  82. "True": "on",
  83. "Mixed": "mixed"
  84. };
  85. },
  86. // Override behavior from Button, since we don't have an iconNode
  87. _setIconClassAttr: null,
  88. _setCheckedAttr: function(/*String|Boolean*/ checked, /*Boolean?*/ priorityChange){
  89. // summary:
  90. // Handler for checked = attribute to constructor, and also calls to
  91. // set('checked', val).
  92. // checked:
  93. // true, false or 'mixed'
  94. // description:
  95. // Controls the state of the TriStateCheckBox. Set this.checked,
  96. // this._currentState, value attribute of the <input type=checkbox>
  97. // according to the value of 'checked'.
  98. this._set("checked", checked);
  99. this._currentState = array.indexOf(this.states, checked);
  100. this._stateType = this._getStateType(checked);
  101. domAttr.set(this.focusNode || this.domNode, "checked", checked);
  102. domAttr.set(this.focusNode, "value", this.stateValues[this._stateType]);
  103. (this.focusNode || this.domNode).setAttribute("aria-checked", checked);
  104. this._handleOnChange(checked, priorityChange);
  105. },
  106. setChecked: function(/*String|Boolean*/ checked){
  107. // summary:
  108. // Deprecated. Use set('checked', true/false) instead.
  109. kernel.deprecated("setChecked("+checked+") is deprecated. Use set('checked',"+checked+") instead.", "", "2.0");
  110. this.set('checked', checked);
  111. },
  112. _setReadOnlyAttr: function(/*Boolean*/ value){
  113. this._set("readOnly", value);
  114. domAttr.set(this.focusNode, "readOnly", value);
  115. this.focusNode.setAttribute("aria-readonly", value);
  116. },
  117. _setValueAttr: function(/*String|Boolean*/ newValue, /*Boolean*/ priorityChange){
  118. // summary:
  119. // Handler for value = attribute to constructor, and also calls to
  120. // set('value', val).
  121. // description:
  122. // During initialization, just saves as attribute to the <input type=checkbox>.
  123. //
  124. // After initialization,
  125. // when passed a boolean or the string 'mixed', controls the state of the
  126. // TriStateCheckBox.
  127. // If passed a string except 'mixed', changes the value attribute of the
  128. // TriStateCheckBox. Sets the state of the TriStateCheckBox to checked.
  129. if(typeof newValue == "string" && (array.indexOf(this.states, newValue) < 0)){
  130. if(newValue == ""){
  131. newValue = "on";
  132. }
  133. this.stateValues["True"] = newValue;
  134. newValue = true;
  135. }
  136. if(this._created){
  137. this._currentState = array.indexOf(this.states, newValue);
  138. this.set('checked', newValue, priorityChange);
  139. domAttr.set(this.focusNode, "value", this.stateValues[this._stateType]);
  140. }
  141. },
  142. _setValuesAttr: function(/*Array*/ newValues){
  143. // summary:
  144. // Handler for values = attribute to constructor, and also calls to
  145. // set('values', val).
  146. // newValues:
  147. // If the length of newValues is 1, it will replace the value of
  148. // the TriStateCheckBox in true state. Otherwise, the values of
  149. // the TriStateCheckBox in true state and 'mixed' state will be
  150. // replaced by the first two values in newValues.
  151. // description:
  152. // Change the value of the TriStateCheckBox in 'mixed' and true states.
  153. this.stateValues["True"] = newValues[0] ? newValues[0] : this.stateValues["True"];
  154. this.stateValues["Mixed"] = newValues[1] ? newValues[1] : this.stateValues["False"];
  155. },
  156. _getValueAttr: function(){
  157. // summary:
  158. // Hook so get('value') works.
  159. // description:
  160. // Returns value according to current state of the TriStateCheckBox.
  161. return this.stateValues[this._stateType];
  162. },
  163. startup: function(){
  164. this.set("checked", this.params.checked || this.states[this._currentState]);
  165. domAttr.set(this.stateLabelNode, 'innerHTML', this._stateLabels[this._stateType]);
  166. this.inherited(arguments);
  167. },
  168. _fillContent: function(/*DomNode*/ source){
  169. // Override Button::_fillContent() since it doesn't make sense for CheckBox,
  170. // since CheckBox doesn't even have a container
  171. },
  172. reset: function(){
  173. this._hasBeenBlurred = false;
  174. this.stateValues = {
  175. "False" : "off",
  176. "True" : "on",
  177. "Mixed" : "mixed"
  178. };
  179. this.set('checked', this.params.checked || this.states[0]);
  180. },
  181. _onFocus: function(){
  182. if(this.id){
  183. query("label[for='"+this.id+"']").addClass("dijitFocusedLabel");
  184. }
  185. this.inherited(arguments);
  186. },
  187. _onBlur: function(){
  188. if(this.id){
  189. query("label[for='"+this.id+"']").removeClass("dijitFocusedLabel");
  190. }
  191. this.inherited(arguments);
  192. },
  193. _onClick: function(/*Event*/ e){
  194. // summary:
  195. // Internal function to handle click actions - need to check
  196. // readOnly and disabled
  197. if(this.readOnly || this.disabled){
  198. event.stop(e);
  199. return false;
  200. }
  201. if(this._currentState >= this.states.length - 1){
  202. this._currentState = 0;
  203. }else{
  204. this._currentState++;
  205. }
  206. this.set("checked", this.states[this._currentState]);
  207. domAttr.set(this.stateLabelNode, 'innerHTML', this._stateLabels[this._stateType]);
  208. return this.onClick(e); // user click actions
  209. },
  210. _getStateType: function(/*String|Boolean*/ state){
  211. // summary:
  212. // Internal function to return the type of a certain state
  213. // false: False
  214. // true: True
  215. // "mixed": Mixed
  216. return state ? (state == "mixed" ? "Mixed" : "True") : "False";
  217. }
  218. }
  219. );
  220. });