checkbox.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. // Copyright 2009 The Closure Library Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS-IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /**
  15. * @fileoverview Tristate checkbox widget.
  16. *
  17. * @see ../demos/checkbox.html
  18. */
  19. goog.provide('goog.ui.Checkbox');
  20. goog.provide('goog.ui.Checkbox.State');
  21. goog.require('goog.a11y.aria');
  22. goog.require('goog.a11y.aria.State');
  23. goog.require('goog.events.EventType');
  24. goog.require('goog.events.KeyCodes');
  25. goog.require('goog.string');
  26. goog.require('goog.ui.CheckboxRenderer');
  27. goog.require('goog.ui.Component');
  28. goog.require('goog.ui.Control');
  29. goog.require('goog.ui.registry');
  30. /**
  31. * 3-state checkbox widget. Fires CHECK or UNCHECK events before toggled and
  32. * CHANGE event after toggled by user.
  33. * The checkbox can also be enabled/disabled and get focused and highlighted.
  34. *
  35. * @param {goog.ui.Checkbox.State=} opt_checked Checked state to set.
  36. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for
  37. * document interaction.
  38. * @param {goog.ui.CheckboxRenderer=} opt_renderer Renderer used to render or
  39. * decorate the checkbox; defaults to {@link goog.ui.CheckboxRenderer}.
  40. * @constructor
  41. * @extends {goog.ui.Control}
  42. */
  43. goog.ui.Checkbox = function(opt_checked, opt_domHelper, opt_renderer) {
  44. var renderer = opt_renderer || goog.ui.CheckboxRenderer.getInstance();
  45. goog.ui.Control.call(this, null, renderer, opt_domHelper);
  46. // The checkbox maintains its own tri-state CHECKED state.
  47. // The control class maintains DISABLED, ACTIVE, and FOCUSED (which enable tab
  48. // navigation, and keyHandling with SPACE).
  49. /**
  50. * Checked state of the checkbox.
  51. * @type {goog.ui.Checkbox.State}
  52. * @private
  53. */
  54. this.checked_ =
  55. goog.isDef(opt_checked) ? opt_checked : goog.ui.Checkbox.State.UNCHECKED;
  56. };
  57. goog.inherits(goog.ui.Checkbox, goog.ui.Control);
  58. goog.tagUnsealableClass(goog.ui.Checkbox);
  59. /**
  60. * Possible checkbox states.
  61. * @enum {?boolean}
  62. */
  63. goog.ui.Checkbox.State = {
  64. CHECKED: true,
  65. UNCHECKED: false,
  66. UNDETERMINED: null
  67. };
  68. /**
  69. * Label element bound to the checkbox.
  70. * @type {Element}
  71. * @private
  72. */
  73. goog.ui.Checkbox.prototype.label_ = null;
  74. /**
  75. * @return {goog.ui.Checkbox.State} Checked state of the checkbox.
  76. */
  77. goog.ui.Checkbox.prototype.getChecked = function() {
  78. return this.checked_;
  79. };
  80. /**
  81. * @return {boolean} Whether the checkbox is checked.
  82. * @override
  83. */
  84. goog.ui.Checkbox.prototype.isChecked = function() {
  85. return this.checked_ == goog.ui.Checkbox.State.CHECKED;
  86. };
  87. /**
  88. * @return {boolean} Whether the checkbox is not checked.
  89. */
  90. goog.ui.Checkbox.prototype.isUnchecked = function() {
  91. return this.checked_ == goog.ui.Checkbox.State.UNCHECKED;
  92. };
  93. /**
  94. * @return {boolean} Whether the checkbox is in partially checked state.
  95. */
  96. goog.ui.Checkbox.prototype.isUndetermined = function() {
  97. return this.checked_ == goog.ui.Checkbox.State.UNDETERMINED;
  98. };
  99. /**
  100. * Sets the checked state of the checkbox.
  101. * @param {?boolean} checked The checked state to set.
  102. * @override
  103. */
  104. goog.ui.Checkbox.prototype.setChecked = function(checked) {
  105. if (checked != this.checked_) {
  106. this.checked_ = /** @type {goog.ui.Checkbox.State} */ (checked);
  107. this.getRenderer().setCheckboxState(this.getElement(), this.checked_);
  108. }
  109. };
  110. /**
  111. * Sets the checked state for the checkbox. Unlike {@link #setChecked},
  112. * doesn't update the checkbox's DOM. Considered protected; to be called
  113. * only by renderer code during element decoration.
  114. * @param {goog.ui.Checkbox.State} checked New checkbox state.
  115. */
  116. goog.ui.Checkbox.prototype.setCheckedInternal = function(checked) {
  117. this.checked_ = checked;
  118. };
  119. /**
  120. * Binds an HTML element to the checkbox which if clicked toggles the checkbox.
  121. * Behaves the same way as the 'label' HTML tag. The label element has to be the
  122. * direct or non-direct ancestor of the checkbox element because it will get the
  123. * focus when keyboard support is implemented.
  124. * Note: Control#enterDocument also sets aria-label on the element but
  125. * Checkbox#enterDocument sets aria-labeledby on the same element which
  126. * overrides the aria-label in all modern screen readers.
  127. *
  128. * @param {?Element} label The label control to set. If null, only the checkbox
  129. * reacts to clicks.
  130. */
  131. goog.ui.Checkbox.prototype.setLabel = function(label) {
  132. if (this.isInDocument()) {
  133. var wasFocused = this.isFocused();
  134. this.exitDocument();
  135. this.label_ = label;
  136. this.enterDocument();
  137. if (wasFocused) {
  138. this.getElementStrict().focus();
  139. }
  140. } else {
  141. this.label_ = label;
  142. }
  143. };
  144. /**
  145. * Toggles the checkbox. State transitions:
  146. * <ul>
  147. * <li>unchecked -> checked
  148. * <li>undetermined -> checked
  149. * <li>checked -> unchecked
  150. * </ul>
  151. */
  152. goog.ui.Checkbox.prototype.toggle = function() {
  153. this.setChecked(
  154. this.checked_ ? goog.ui.Checkbox.State.UNCHECKED :
  155. goog.ui.Checkbox.State.CHECKED);
  156. };
  157. /** @override */
  158. goog.ui.Checkbox.prototype.enterDocument = function() {
  159. goog.ui.Checkbox.base(this, 'enterDocument');
  160. if (this.isHandleMouseEvents()) {
  161. var handler = this.getHandler();
  162. // Listen to the label, if it was set.
  163. if (this.label_) {
  164. // Any mouse events that happen to the associated label should have the
  165. // same effect on the checkbox as if they were happening to the checkbox
  166. // itself.
  167. handler
  168. .listen(
  169. this.label_, goog.events.EventType.CLICK,
  170. this.handleClickOrSpace_)
  171. .listen(
  172. this.label_, goog.events.EventType.MOUSEOVER,
  173. this.handleMouseOver)
  174. .listen(
  175. this.label_, goog.events.EventType.MOUSEOUT, this.handleMouseOut)
  176. .listen(
  177. this.label_, goog.events.EventType.MOUSEDOWN,
  178. this.handleMouseDown)
  179. .listen(
  180. this.label_, goog.events.EventType.MOUSEUP, this.handleMouseUp);
  181. }
  182. // Checkbox needs to explicitly listen for click event.
  183. handler.listen(
  184. this.getElement(), goog.events.EventType.CLICK,
  185. this.handleClickOrSpace_);
  186. }
  187. // Set aria label.
  188. var checkboxElement = this.getElementStrict();
  189. if (this.label_ && checkboxElement != this.label_ &&
  190. goog.string.isEmptyOrWhitespace(
  191. goog.a11y.aria.getLabel(checkboxElement))) {
  192. if (!this.label_.id) {
  193. this.label_.id = this.makeId('lbl');
  194. }
  195. goog.a11y.aria.setState(
  196. checkboxElement, goog.a11y.aria.State.LABELLEDBY, this.label_.id);
  197. }
  198. };
  199. /**
  200. * Fix for tabindex not being updated so that disabled checkbox is not
  201. * focusable. In particular this fails in Chrome.
  202. * Note: in general tabIndex=-1 will prevent from keyboard focus but enables
  203. * mouse focus, however in this case the control class prevents mouse focus.
  204. * @override
  205. */
  206. goog.ui.Checkbox.prototype.setEnabled = function(enabled) {
  207. goog.ui.Checkbox.base(this, 'setEnabled', enabled);
  208. var el = this.getElement();
  209. if (el) {
  210. el.tabIndex = this.isEnabled() ? 0 : -1;
  211. }
  212. };
  213. /**
  214. * Handles the click event.
  215. * @param {!goog.events.BrowserEvent} e The event.
  216. * @private
  217. */
  218. goog.ui.Checkbox.prototype.handleClickOrSpace_ = function(e) {
  219. e.stopPropagation();
  220. var eventType = this.checked_ ? goog.ui.Component.EventType.UNCHECK :
  221. goog.ui.Component.EventType.CHECK;
  222. if (this.isEnabled() && !e.target.href && this.dispatchEvent(eventType)) {
  223. e.preventDefault(); // Prevent scrolling in Chrome if SPACE is pressed.
  224. this.toggle();
  225. this.dispatchEvent(goog.ui.Component.EventType.CHANGE);
  226. }
  227. };
  228. /** @override */
  229. goog.ui.Checkbox.prototype.handleKeyEventInternal = function(e) {
  230. if (e.keyCode == goog.events.KeyCodes.SPACE) {
  231. this.performActionInternal(e);
  232. this.handleClickOrSpace_(e);
  233. }
  234. return false;
  235. };
  236. /**
  237. * Register this control so it can be created from markup.
  238. */
  239. goog.ui.registry.setDecoratorByClassName(
  240. goog.ui.CheckboxRenderer.CSS_CLASS,
  241. function() { return new goog.ui.Checkbox(); });