Path: blob/trunk/third_party/closure/goog/ui/buttonrenderer.js
4500 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Default renderer for {@link goog.ui.Button}s.8*/910goog.provide('goog.ui.ButtonRenderer');1112goog.require('goog.a11y.aria');13goog.require('goog.a11y.aria.Role');14goog.require('goog.a11y.aria.State');15goog.require('goog.asserts');16goog.require('goog.ui.ButtonSide');17goog.require('goog.ui.Component');18goog.require('goog.ui.ControlRenderer'); // circular19goog.requireType('goog.ui.Button');20212223/**24* Default renderer for {@link goog.ui.Button}s. Extends the superclass with25* the following button-specific API methods:26* <ul>27* <li>`getValue` - returns the button element's value28* <li>`setValue` - updates the button element to reflect its new value29* <li>`getTooltip` - returns the button element's tooltip text30* <li>`setTooltip` - updates the button element's tooltip text31* <li>`setCollapsed` - removes one or both of the button element's32* borders33* </ul>34* For alternate renderers, see {@link goog.ui.NativeButtonRenderer},35* {@link goog.ui.CustomButtonRenderer}, and {@link goog.ui.FlatButtonRenderer}.36* @constructor37* @extends {goog.ui.ControlRenderer}38*/39goog.ui.ButtonRenderer = function() {40'use strict';41goog.ui.ControlRenderer.call(this);42};43goog.inherits(goog.ui.ButtonRenderer, goog.ui.ControlRenderer);44goog.addSingletonGetter(goog.ui.ButtonRenderer);454647/**48* Default CSS class to be applied to the root element of components rendered49* by this renderer.50* @type {string}51*/52goog.ui.ButtonRenderer.CSS_CLASS = goog.getCssName('goog-button');535455/**56* Returns the ARIA role to be applied to buttons.57* @return {goog.a11y.aria.Role|undefined} ARIA role.58* @override59*/60goog.ui.ButtonRenderer.prototype.getAriaRole = function() {61'use strict';62return goog.a11y.aria.Role.BUTTON;63};646566/**67* Updates the button's ARIA (accessibility) state if the button is being68* treated as a checkbox. Also makes sure that attributes which aren't69* supported by buttons aren't being added.70* @param {Element} element Element whose ARIA state is to be updated.71* @param {goog.ui.Component.State} state Component state being enabled or72* disabled.73* @param {boolean} enable Whether the state is being enabled or disabled.74* @protected75* @override76*/77goog.ui.ButtonRenderer.prototype.updateAriaState = function(78element, state, enable) {79'use strict';80switch (state) {81// If button has CHECKED or SELECTED state, assign aria-pressed82case goog.ui.Component.State.SELECTED:83case goog.ui.Component.State.CHECKED:84goog.asserts.assert(element, 'The button DOM element cannot be null.');85goog.a11y.aria.setState(element, goog.a11y.aria.State.PRESSED, enable);86break;87default:88case goog.ui.Component.State.OPENED:89case goog.ui.Component.State.DISABLED:90goog.ui.ButtonRenderer.base(91this, 'updateAriaState', element, state, enable);92break;93}94};959697/**98* @override99* @suppress {strictMissingProperties} Added to tighten compiler checks100*/101goog.ui.ButtonRenderer.prototype.createDom = function(button) {102'use strict';103var element = goog.ui.ButtonRenderer.base(this, 'createDom', button);104this.setTooltip(element, button.getTooltip());105106/** @suppress {strictMissingProperties} Added to tighten compiler checks */107var value = button.getValue();108if (value) {109this.setValue(element, value);110}111112// If this is a toggle button, set ARIA state113if (button.isSupportedState(goog.ui.Component.State.CHECKED)) {114this.updateAriaState(115element, goog.ui.Component.State.CHECKED, button.isChecked());116}117118return element;119};120121122/**123* @override124* @suppress {strictMissingProperties} Added to tighten compiler checks125*/126goog.ui.ButtonRenderer.prototype.decorate = function(button, element) {127'use strict';128// The superclass implementation takes care of common attributes; we only129// need to set the value and the tooltip.130element =131goog.ui.ButtonRenderer.superClass_.decorate.call(this, button, element);132133button.setValueInternal(this.getValue(element));134button.setTooltipInternal(this.getTooltip(element));135136// If this is a toggle button, set ARIA state137if (button.isSupportedState(goog.ui.Component.State.CHECKED)) {138this.updateAriaState(139element, goog.ui.Component.State.CHECKED, button.isChecked());140}141142return element;143};144145146/**147* Takes a button's root element, and returns the value associated with it.148* No-op in the base class.149* @param {Element} element The button's root element.150* @return {string|undefined} The button's value (undefined if none).151*/152goog.ui.ButtonRenderer.prototype.getValue = function(element) {};153154155/**156* Takes a button's root element and a value, and updates the element to reflect157* the new value. No-op in the base class.158* @param {Element} element The button's root element.159* @param {string} value New value.160*/161goog.ui.ButtonRenderer.prototype.setValue = function(element, value) {};162163164/**165* Takes a button's root element, and returns its tooltip text.166* @param {Element} element The button's root element.167* @return {string|undefined} The tooltip text.168* @suppress {strictMissingProperties} Added to tighten compiler checks169*/170goog.ui.ButtonRenderer.prototype.getTooltip = function(element) {171'use strict';172return element.title;173};174175176/**177* Takes a button's root element and a tooltip string, and updates the element178* with the new tooltip.179* @param {Element} element The button's root element.180* @param {string} tooltip New tooltip text.181* @protected182*/183goog.ui.ButtonRenderer.prototype.setTooltip = function(element, tooltip) {184'use strict';185if (element) {186// Don't set a title attribute if there isn't a tooltip. Blank title187// attributes can be interpreted incorrectly by screen readers.188if (tooltip) {189/**190* @suppress {strictMissingProperties} Added to tighten compiler checks191*/192element.title = tooltip;193} else {194element.removeAttribute('title');195}196}197};198199200/**201* Collapses the border on one or both sides of the button, allowing it to be202* combined with the adjacent button(s), forming a single UI componenet with203* multiple targets.204* @param {goog.ui.Button} button Button to update.205* @param {number} sides Bitmap of one or more {@link goog.ui.ButtonSide}s for206* which borders should be collapsed.207* @protected208*/209goog.ui.ButtonRenderer.prototype.setCollapsed = function(button, sides) {210'use strict';211var isRtl = button.isRightToLeft();212var collapseLeftClassName =213goog.getCssName(this.getStructuralCssClass(), 'collapse-left');214var collapseRightClassName =215goog.getCssName(this.getStructuralCssClass(), 'collapse-right');216217button.enableClassName(218isRtl ? collapseRightClassName : collapseLeftClassName,219!!(sides & goog.ui.ButtonSide.START));220button.enableClassName(221isRtl ? collapseLeftClassName : collapseRightClassName,222!!(sides & goog.ui.ButtonSide.END));223};224225226/** @override */227goog.ui.ButtonRenderer.prototype.getCssClass = function() {228'use strict';229return goog.ui.ButtonRenderer.CSS_CLASS;230};231232233