| 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 |
/** |
| 16 |
* @fileoverview Native browser button renderer for {@link goog.ui.Button}s. |
| 17 |
* |
| 18 |
* @author [email protected] (Attila Bodis) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.ui.NativeButtonRenderer'); |
| 22 |
|
| 23 |
goog.require('goog.asserts'); |
| 24 |
goog.require('goog.dom.InputType'); |
| 25 |
goog.require('goog.dom.TagName'); |
| 26 |
goog.require('goog.dom.classlist'); |
| 27 |
goog.require('goog.events.EventType'); |
| 28 |
goog.require('goog.ui.ButtonRenderer'); |
| 29 |
goog.require('goog.ui.Component'); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* Renderer for {@link goog.ui.Button}s. Renders and decorates native HTML |
| 35 |
* button elements. Since native HTML buttons have built-in support for many |
| 36 |
* features, overrides many expensive (and redundant) superclass methods to |
| 37 |
* be no-ops. |
| 38 |
* @constructor |
| 39 |
* @extends {goog.ui.ButtonRenderer} |
| 40 |
*/ |
| 41 |
goog.ui.NativeButtonRenderer = function() { |
| 42 |
goog.ui.ButtonRenderer.call(this); |
| 43 |
}; |
| 44 |
goog.inherits(goog.ui.NativeButtonRenderer, goog.ui.ButtonRenderer); |
| 45 |
goog.addSingletonGetter(goog.ui.NativeButtonRenderer); |
| 46 |
|
| 47 |
|
| 48 |
/** @override */ |
| 49 |
goog.ui.NativeButtonRenderer.prototype.getAriaRole = function() { |
| 50 |
// Native buttons don't need ARIA roles to be recognized by screen readers. |
| 51 |
return undefined; |
| 52 |
}; |
| 53 |
|
| 54 |
|
| 55 |
/** |
| 56 |
* Returns the button's contents wrapped in a native HTML button element. Sets |
| 57 |
* the button's disabled attribute as needed. |
| 58 |
* @param {goog.ui.Control} button Button to render. |
| 59 |
* @return {Element} Root element for the button (a native HTML button element). |
| 60 |
* @override |
| 61 |
*/ |
| 62 |
goog.ui.NativeButtonRenderer.prototype.createDom = function(button) { |
| 63 |
this.setUpNativeButton_(button); |
| 64 |
return button.getDomHelper().createDom( |
| 65 |
goog.dom.TagName.BUTTON, { |
| 66 |
'class': this.getClassNames(button).join(' '), |
| 67 |
'disabled': !button.isEnabled(), |
| 68 |
'title': button.getTooltip() || '', |
| 69 |
'value': button.getValue() || '' |
| 70 |
}, |
| 71 |
button.getCaption() || ''); |
| 72 |
}; |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* Overrides {@link goog.ui.ButtonRenderer#canDecorate} by returning true only |
| 77 |
* if the element is an HTML button. |
| 78 |
* @param {Element} element Element to decorate. |
| 79 |
* @return {boolean} Whether the renderer can decorate the element. |
| 80 |
* @override |
| 81 |
*/ |
| 82 |
goog.ui.NativeButtonRenderer.prototype.canDecorate = function(element) { |
| 83 |
return element.tagName == goog.dom.TagName.BUTTON || |
| 84 |
(element.tagName == goog.dom.TagName.INPUT && |
| 85 |
(element.type == goog.dom.InputType.BUTTON || |
| 86 |
element.type == goog.dom.InputType.SUBMIT || |
| 87 |
element.type == goog.dom.InputType.RESET)); |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
/** @override */ |
| 92 |
goog.ui.NativeButtonRenderer.prototype.decorate = function(button, element) { |
| 93 |
this.setUpNativeButton_(button); |
| 94 |
if (element.disabled) { |
| 95 |
// Add the marker class for the DISABLED state before letting the superclass |
| 96 |
// implementation decorate the element, so its state will be correct. |
| 97 |
var disabledClassName = goog.asserts.assertString( |
| 98 |
this.getClassForState(goog.ui.Component.State.DISABLED)); |
| 99 |
goog.dom.classlist.add(element, disabledClassName); |
| 100 |
} |
| 101 |
return goog.ui.NativeButtonRenderer.superClass_.decorate.call( |
| 102 |
this, button, element); |
| 103 |
}; |
| 104 |
|
| 105 |
|
| 106 |
/** |
| 107 |
* Native buttons natively support BiDi and keyboard focus. |
| 108 |
* @suppress {visibility} getHandler and performActionInternal |
| 109 |
* @override |
| 110 |
*/ |
| 111 |
goog.ui.NativeButtonRenderer.prototype.initializeDom = function(button) { |
| 112 |
// WARNING: This is a hack, and it is only applicable to native buttons, |
| 113 |
// which are special because they do natively what most goog.ui.Controls |
| 114 |
// do programmatically. Do not use your renderer's initializeDom method |
| 115 |
// to hook up event handlers! |
| 116 |
button.getHandler().listen( |
| 117 |
button.getElement(), goog.events.EventType.CLICK, |
| 118 |
button.performActionInternal); |
| 119 |
}; |
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* @override |
| 124 |
* Native buttons don't support text selection. |
| 125 |
*/ |
| 126 |
goog.ui.NativeButtonRenderer.prototype.setAllowTextSelection = |
| 127 |
goog.nullFunction; |
| 128 |
|
| 129 |
|
| 130 |
/** |
| 131 |
* @override |
| 132 |
* Native buttons natively support right-to-left rendering. |
| 133 |
*/ |
| 134 |
goog.ui.NativeButtonRenderer.prototype.setRightToLeft = goog.nullFunction; |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* @override |
| 139 |
* Native buttons are always focusable as long as they are enabled. |
| 140 |
*/ |
| 141 |
goog.ui.NativeButtonRenderer.prototype.isFocusable = function(button) { |
| 142 |
return button.isEnabled(); |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* @override |
| 148 |
* Native buttons natively support keyboard focus. |
| 149 |
*/ |
| 150 |
goog.ui.NativeButtonRenderer.prototype.setFocusable = goog.nullFunction; |
| 151 |
|
| 152 |
|
| 153 |
/** |
| 154 |
* @override |
| 155 |
* Native buttons also expose the DISABLED state in the HTML button's |
| 156 |
* {@code disabled} attribute. |
| 157 |
*/ |
| 158 |
goog.ui.NativeButtonRenderer.prototype.setState = function( |
| 159 |
button, state, enable) { |
| 160 |
goog.ui.NativeButtonRenderer.superClass_.setState.call( |
| 161 |
this, button, state, enable); |
| 162 |
var element = button.getElement(); |
| 163 |
if (element && state == goog.ui.Component.State.DISABLED) { |
| 164 |
element.disabled = enable; |
| 165 |
} |
| 166 |
}; |
| 167 |
|
| 168 |
|
| 169 |
/** |
| 170 |
* @override |
| 171 |
* Native buttons store their value in the HTML button's {@code value} |
| 172 |
* attribute. |
| 173 |
*/ |
| 174 |
goog.ui.NativeButtonRenderer.prototype.getValue = function(element) { |
| 175 |
// TODO(attila): Make this work on IE! This never worked... |
| 176 |
// See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html |
| 177 |
// for a description of the problem. |
| 178 |
return element.value; |
| 179 |
}; |
| 180 |
|
| 181 |
|
| 182 |
/** |
| 183 |
* @override |
| 184 |
* Native buttons also expose their value in the HTML button's {@code value} |
| 185 |
* attribute. |
| 186 |
*/ |
| 187 |
goog.ui.NativeButtonRenderer.prototype.setValue = function(element, value) { |
| 188 |
if (element) { |
| 189 |
// TODO(attila): Make this work on IE! This never worked... |
| 190 |
// See http://www.fourmilab.ch/fourmilog/archives/2007-03/000824.html |
| 191 |
// for a description of the problem. |
| 192 |
element.value = value; |
| 193 |
} |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
/** |
| 198 |
* @override |
| 199 |
* Native buttons don't need ARIA states to support accessibility, so this is |
| 200 |
* a no-op. |
| 201 |
*/ |
| 202 |
goog.ui.NativeButtonRenderer.prototype.updateAriaState = goog.nullFunction; |
| 203 |
|
| 204 |
|
| 205 |
/** |
| 206 |
* Sets up the button control such that it doesn't waste time adding |
| 207 |
* functionality that is already natively supported by native browser |
| 208 |
* buttons. |
| 209 |
* @param {goog.ui.Control} button Button control to configure. |
| 210 |
* @private |
| 211 |
*/ |
| 212 |
goog.ui.NativeButtonRenderer.prototype.setUpNativeButton_ = function(button) { |
| 213 |
button.setHandleMouseEvents(false); |
| 214 |
button.setAutoStates(goog.ui.Component.State.ALL, false); |
| 215 |
button.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 216 |
}; |