Path: blob/trunk/third_party/closure/goog/ui/nativebuttonrenderer.js
4501 views
/**1* @license2* Copyright The Closure Library Authors.3* SPDX-License-Identifier: Apache-2.04*/56/**7* @fileoverview Native browser button renderer for {@link goog.ui.Button}s.8*/910goog.provide('goog.ui.NativeButtonRenderer');1112goog.require('goog.asserts');13goog.require('goog.dom.InputType');14goog.require('goog.dom.TagName');15goog.require('goog.dom.classlist');16goog.require('goog.events.EventType');17goog.require('goog.ui.ButtonRenderer');18goog.require('goog.ui.Component');19goog.requireType('goog.ui.Control');20212223/**24* Renderer for {@link goog.ui.Button}s. Renders and decorates native HTML25* button elements. Since native HTML buttons have built-in support for many26* features, overrides many expensive (and redundant) superclass methods to27* be no-ops.28* @constructor29* @extends {goog.ui.ButtonRenderer}30*/31goog.ui.NativeButtonRenderer = function() {32'use strict';33goog.ui.ButtonRenderer.call(this);34};35goog.inherits(goog.ui.NativeButtonRenderer, goog.ui.ButtonRenderer);36goog.addSingletonGetter(goog.ui.NativeButtonRenderer);373839/** @override */40goog.ui.NativeButtonRenderer.prototype.getAriaRole = function() {41'use strict';42// Native buttons don't need ARIA roles to be recognized by screen readers.43return undefined;44};454647/**48* Returns the button's contents wrapped in a native HTML button element. Sets49* the button's disabled attribute as needed.50* @param {goog.ui.Control} button Button to render.51* @return {!Element} Root element for the button (a native HTML button52* element).53* @override54* @suppress {strictMissingProperties} Added to tighten compiler checks55*/56goog.ui.NativeButtonRenderer.prototype.createDom = function(button) {57'use strict';58this.setUpNativeButton_(button);59return button.getDomHelper().createDom(60goog.dom.TagName.BUTTON, {61'class': this.getClassNames(button).join(' '),62'disabled': !button.isEnabled(),63'title': button.getTooltip() || '',64'value': button.getValue() || ''65},66button.getCaption() || '');67};686970/**71* Overrides {@link goog.ui.ButtonRenderer#canDecorate} by returning true only72* if the element is an HTML button.73* @param {Element} element Element to decorate.74* @return {boolean} Whether the renderer can decorate the element.75* @override76* @suppress {strictMissingProperties} Added to tighten compiler checks77*/78goog.ui.NativeButtonRenderer.prototype.canDecorate = function(element) {79'use strict';80return element.tagName == goog.dom.TagName.BUTTON ||81(element.tagName == goog.dom.TagName.INPUT &&82(element.type == goog.dom.InputType.BUTTON ||83element.type == goog.dom.InputType.SUBMIT ||84element.type == goog.dom.InputType.RESET));85};868788/**89* @override90* @suppress {strictMissingProperties} Added to tighten compiler checks91*/92goog.ui.NativeButtonRenderer.prototype.decorate = function(button, element) {93'use strict';94this.setUpNativeButton_(button);95if (element.disabled) {96// Add the marker class for the DISABLED state before letting the superclass97// implementation decorate the element, so its state will be correct.98var disabledClassName = goog.asserts.assertString(99this.getClassForState(goog.ui.Component.State.DISABLED));100goog.dom.classlist.add(element, disabledClassName);101}102return goog.ui.NativeButtonRenderer.superClass_.decorate.call(103this, button, element);104};105106107/**108* Native buttons natively support BiDi and keyboard focus.109* @suppress {visibility} getHandler and performActionInternal110* @override111*/112goog.ui.NativeButtonRenderer.prototype.initializeDom = function(button) {113'use strict';114// WARNING: This is a hack, and it is only applicable to native buttons,115// which are special because they do natively what most goog.ui.Controls116// do programmatically. Do not use your renderer's initializeDom method117// to hook up event handlers!118button.getHandler().listen(119button.getElement(), goog.events.EventType.CLICK,120button.performActionInternal);121};122123124/**125* @override126* Native buttons don't support text selection.127*/128goog.ui.NativeButtonRenderer.prototype.setAllowTextSelection = function() {};129130131/**132* @override133* Native buttons natively support right-to-left rendering.134*/135goog.ui.NativeButtonRenderer.prototype.setRightToLeft = function() {};136137138/**139* @override140* Native buttons are always focusable as long as they are enabled.141*/142goog.ui.NativeButtonRenderer.prototype.isFocusable = function(button) {143'use strict';144return button.isEnabled();145};146147148/**149* @override150* Native buttons natively support keyboard focus.151*/152goog.ui.NativeButtonRenderer.prototype.setFocusable = function() {};153154155/**156* @override157* Native buttons also expose the DISABLED state in the HTML button's158* `disabled` attribute.159*/160goog.ui.NativeButtonRenderer.prototype.setState = function(161button, state, enable) {162'use strict';163goog.ui.NativeButtonRenderer.superClass_.setState.call(164this, button, state, enable);165var element = button.getElement();166if (element && state == goog.ui.Component.State.DISABLED) {167/** @suppress {strictMissingProperties} Added to tighten compiler checks */168element.disabled = enable;169}170};171172173/**174* @override175* Native buttons store their value in the HTML button's `value`176* attribute.177* @suppress {strictMissingProperties} Added to tighten compiler checks178*/179goog.ui.NativeButtonRenderer.prototype.getValue = function(element) {180'use strict';181// TODO(attila): Make this work on IE! This never worked...182// See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html183// for a description of the problem.184return element.value;185};186187188/**189* @override190* Native buttons also expose their value in the HTML button's `value`191* attribute.192*/193goog.ui.NativeButtonRenderer.prototype.setValue = function(element, value) {194'use strict';195if (element) {196// TODO(attila): Make this work on IE! This never worked...197// See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html198// for a description of the problem.199/** @suppress {strictMissingProperties} Added to tighten compiler checks */200element.value = value;201}202};203204205/**206* @override207* Native buttons don't need ARIA states to support accessibility, so this is208* a no-op.209*/210goog.ui.NativeButtonRenderer.prototype.updateAriaState = function() {};211212213/**214* Sets up the button control such that it doesn't waste time adding215* functionality that is already natively supported by native browser216* buttons.217* @param {goog.ui.Control} button Button control to configure.218* @private219*/220goog.ui.NativeButtonRenderer.prototype.setUpNativeButton_ = function(button) {221'use strict';222button.setHandleMouseEvents(false);223button.setAutoStates(goog.ui.Component.State.ALL, false);224button.setSupportedState(goog.ui.Component.State.FOCUSED, false);225};226227228