buttonrenderer.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Copyright 2008 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 Default renderer for {@link goog.ui.Button}s.
  16. *
  17. * @author attila@google.com (Attila Bodis)
  18. */
  19. goog.provide('goog.ui.ButtonRenderer');
  20. goog.require('goog.a11y.aria');
  21. goog.require('goog.a11y.aria.Role');
  22. goog.require('goog.a11y.aria.State');
  23. goog.require('goog.asserts');
  24. goog.require('goog.ui.ButtonSide');
  25. goog.require('goog.ui.Component');
  26. goog.require('goog.ui.ControlRenderer');
  27. goog.forwardDeclare('goog.ui.Button'); // circular
  28. /**
  29. * Default renderer for {@link goog.ui.Button}s. Extends the superclass with
  30. * the following button-specific API methods:
  31. * <ul>
  32. * <li>{@code getValue} - returns the button element's value
  33. * <li>{@code setValue} - updates the button element to reflect its new value
  34. * <li>{@code getTooltip} - returns the button element's tooltip text
  35. * <li>{@code setTooltip} - updates the button element's tooltip text
  36. * <li>{@code setCollapsed} - removes one or both of the button element's
  37. * borders
  38. * </ul>
  39. * For alternate renderers, see {@link goog.ui.NativeButtonRenderer},
  40. * {@link goog.ui.CustomButtonRenderer}, and {@link goog.ui.FlatButtonRenderer}.
  41. * @constructor
  42. * @extends {goog.ui.ControlRenderer}
  43. */
  44. goog.ui.ButtonRenderer = function() {
  45. goog.ui.ControlRenderer.call(this);
  46. };
  47. goog.inherits(goog.ui.ButtonRenderer, goog.ui.ControlRenderer);
  48. goog.addSingletonGetter(goog.ui.ButtonRenderer);
  49. /**
  50. * Default CSS class to be applied to the root element of components rendered
  51. * by this renderer.
  52. * @type {string}
  53. */
  54. goog.ui.ButtonRenderer.CSS_CLASS = goog.getCssName('goog-button');
  55. /**
  56. * Returns the ARIA role to be applied to buttons.
  57. * @return {goog.a11y.aria.Role|undefined} ARIA role.
  58. * @override
  59. */
  60. goog.ui.ButtonRenderer.prototype.getAriaRole = function() {
  61. return goog.a11y.aria.Role.BUTTON;
  62. };
  63. /**
  64. * Updates the button's ARIA (accessibility) state if the button is being
  65. * treated as a checkbox. Also makes sure that attributes which aren't
  66. * supported by buttons aren't being added.
  67. * @param {Element} element Element whose ARIA state is to be updated.
  68. * @param {goog.ui.Component.State} state Component state being enabled or
  69. * disabled.
  70. * @param {boolean} enable Whether the state is being enabled or disabled.
  71. * @protected
  72. * @override
  73. */
  74. goog.ui.ButtonRenderer.prototype.updateAriaState = function(
  75. element, state, enable) {
  76. switch (state) {
  77. // If button has CHECKED or SELECTED state, assign aria-pressed
  78. case goog.ui.Component.State.SELECTED:
  79. case goog.ui.Component.State.CHECKED:
  80. goog.asserts.assert(element, 'The button DOM element cannot be null.');
  81. goog.a11y.aria.setState(element, goog.a11y.aria.State.PRESSED, enable);
  82. break;
  83. default:
  84. case goog.ui.Component.State.OPENED:
  85. case goog.ui.Component.State.DISABLED:
  86. goog.ui.ButtonRenderer.base(
  87. this, 'updateAriaState', element, state, enable);
  88. break;
  89. }
  90. };
  91. /** @override */
  92. goog.ui.ButtonRenderer.prototype.createDom = function(button) {
  93. var element = goog.ui.ButtonRenderer.base(this, 'createDom', button);
  94. this.setTooltip(element, button.getTooltip());
  95. var value = button.getValue();
  96. if (value) {
  97. this.setValue(element, value);
  98. }
  99. // If this is a toggle button, set ARIA state
  100. if (button.isSupportedState(goog.ui.Component.State.CHECKED)) {
  101. this.updateAriaState(
  102. element, goog.ui.Component.State.CHECKED, button.isChecked());
  103. }
  104. return element;
  105. };
  106. /** @override */
  107. goog.ui.ButtonRenderer.prototype.decorate = function(button, element) {
  108. // The superclass implementation takes care of common attributes; we only
  109. // need to set the value and the tooltip.
  110. element =
  111. goog.ui.ButtonRenderer.superClass_.decorate.call(this, button, element);
  112. button.setValueInternal(this.getValue(element));
  113. button.setTooltipInternal(this.getTooltip(element));
  114. // If this is a toggle button, set ARIA state
  115. if (button.isSupportedState(goog.ui.Component.State.CHECKED)) {
  116. this.updateAriaState(
  117. element, goog.ui.Component.State.CHECKED, button.isChecked());
  118. }
  119. return element;
  120. };
  121. /**
  122. * Takes a button's root element, and returns the value associated with it.
  123. * No-op in the base class.
  124. * @param {Element} element The button's root element.
  125. * @return {string|undefined} The button's value (undefined if none).
  126. */
  127. goog.ui.ButtonRenderer.prototype.getValue = goog.nullFunction;
  128. /**
  129. * Takes a button's root element and a value, and updates the element to reflect
  130. * the new value. No-op in the base class.
  131. * @param {Element} element The button's root element.
  132. * @param {string} value New value.
  133. */
  134. goog.ui.ButtonRenderer.prototype.setValue = goog.nullFunction;
  135. /**
  136. * Takes a button's root element, and returns its tooltip text.
  137. * @param {Element} element The button's root element.
  138. * @return {string|undefined} The tooltip text.
  139. */
  140. goog.ui.ButtonRenderer.prototype.getTooltip = function(element) {
  141. return element.title;
  142. };
  143. /**
  144. * Takes a button's root element and a tooltip string, and updates the element
  145. * with the new tooltip.
  146. * @param {Element} element The button's root element.
  147. * @param {string} tooltip New tooltip text.
  148. * @protected
  149. */
  150. goog.ui.ButtonRenderer.prototype.setTooltip = function(element, tooltip) {
  151. if (element) {
  152. // Don't set a title attribute if there isn't a tooltip. Blank title
  153. // attributes can be interpreted incorrectly by screen readers.
  154. if (tooltip) {
  155. element.title = tooltip;
  156. } else {
  157. element.removeAttribute('title');
  158. }
  159. }
  160. };
  161. /**
  162. * Collapses the border on one or both sides of the button, allowing it to be
  163. * combined with the adjacent button(s), forming a single UI componenet with
  164. * multiple targets.
  165. * @param {goog.ui.Button} button Button to update.
  166. * @param {number} sides Bitmap of one or more {@link goog.ui.ButtonSide}s for
  167. * which borders should be collapsed.
  168. * @protected
  169. */
  170. goog.ui.ButtonRenderer.prototype.setCollapsed = function(button, sides) {
  171. var isRtl = button.isRightToLeft();
  172. var collapseLeftClassName =
  173. goog.getCssName(this.getStructuralCssClass(), 'collapse-left');
  174. var collapseRightClassName =
  175. goog.getCssName(this.getStructuralCssClass(), 'collapse-right');
  176. button.enableClassName(
  177. isRtl ? collapseRightClassName : collapseLeftClassName,
  178. !!(sides & goog.ui.ButtonSide.START));
  179. button.enableClassName(
  180. isRtl ? collapseLeftClassName : collapseRightClassName,
  181. !!(sides & goog.ui.ButtonSide.END));
  182. };
  183. /** @override */
  184. goog.ui.ButtonRenderer.prototype.getCssClass = function() {
  185. return goog.ui.ButtonRenderer.CSS_CLASS;
  186. };