| 1 |
// Copyright 2010 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 textarea renderer for {@link goog.ui.Textarea}s. |
| 17 |
*/ |
| 18 |
|
| 19 |
goog.provide('goog.ui.TextareaRenderer'); |
| 20 |
|
| 21 |
goog.require('goog.dom.TagName'); |
| 22 |
goog.require('goog.ui.Component'); |
| 23 |
goog.require('goog.ui.ControlRenderer'); |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
/** |
| 28 |
* Renderer for {@link goog.ui.Textarea}s. Renders and decorates native HTML |
| 29 |
* textarea elements. Since native HTML textareas have built-in support for |
| 30 |
* many features, overrides many expensive (and redundant) superclass methods to |
| 31 |
* be no-ops. |
| 32 |
* @constructor |
| 33 |
* @extends {goog.ui.ControlRenderer} |
| 34 |
*/ |
| 35 |
goog.ui.TextareaRenderer = function() { |
| 36 |
goog.ui.ControlRenderer.call(this); |
| 37 |
}; |
| 38 |
goog.inherits(goog.ui.TextareaRenderer, goog.ui.ControlRenderer); |
| 39 |
goog.addSingletonGetter(goog.ui.TextareaRenderer); |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Default CSS class to be applied to the root element of components rendered |
| 44 |
* by this renderer. |
| 45 |
* @type {string} |
| 46 |
*/ |
| 47 |
goog.ui.TextareaRenderer.CSS_CLASS = goog.getCssName('goog-textarea'); |
| 48 |
|
| 49 |
|
| 50 |
/** @override */ |
| 51 |
goog.ui.TextareaRenderer.prototype.getAriaRole = function() { |
| 52 |
// textareas don't need ARIA roles to be recognized by screen readers. |
| 53 |
return undefined; |
| 54 |
}; |
| 55 |
|
| 56 |
|
| 57 |
/** @override */ |
| 58 |
goog.ui.TextareaRenderer.prototype.decorate = function(control, element) { |
| 59 |
this.setUpTextarea_(control); |
| 60 |
goog.ui.TextareaRenderer.superClass_.decorate.call(this, control, element); |
| 61 |
control.setContent(element.value); |
| 62 |
return element; |
| 63 |
}; |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Returns the textarea's contents wrapped in an HTML textarea element. Sets |
| 68 |
* the textarea's disabled attribute as needed. |
| 69 |
* @param {goog.ui.Control} textarea Textarea to render. |
| 70 |
* @return {!Element} Root element for the Textarea control (an HTML textarea |
| 71 |
* element). |
| 72 |
* @override |
| 73 |
*/ |
| 74 |
goog.ui.TextareaRenderer.prototype.createDom = function(textarea) { |
| 75 |
this.setUpTextarea_(textarea); |
| 76 |
var element = textarea.getDomHelper().createDom( |
| 77 |
goog.dom.TagName.TEXTAREA, { |
| 78 |
'class': this.getClassNames(textarea).join(' '), |
| 79 |
'disabled': !textarea.isEnabled() |
| 80 |
}, |
| 81 |
textarea.getContent() || ''); |
| 82 |
return element; |
| 83 |
}; |
| 84 |
|
| 85 |
|
| 86 |
/** |
| 87 |
* Overrides {@link goog.ui.TextareaRenderer#canDecorate} by returning true only |
| 88 |
* if the element is an HTML textarea. |
| 89 |
* @param {Element} element Element to decorate. |
| 90 |
* @return {boolean} Whether the renderer can decorate the element. |
| 91 |
* @override |
| 92 |
*/ |
| 93 |
goog.ui.TextareaRenderer.prototype.canDecorate = function(element) { |
| 94 |
return element.tagName == goog.dom.TagName.TEXTAREA; |
| 95 |
}; |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* Textareas natively support right-to-left rendering. |
| 100 |
* @override |
| 101 |
*/ |
| 102 |
goog.ui.TextareaRenderer.prototype.setRightToLeft = goog.nullFunction; |
| 103 |
|
| 104 |
|
| 105 |
/** |
| 106 |
* Textareas are always focusable as long as they are enabled. |
| 107 |
* @override |
| 108 |
*/ |
| 109 |
goog.ui.TextareaRenderer.prototype.isFocusable = function(textarea) { |
| 110 |
return textarea.isEnabled(); |
| 111 |
}; |
| 112 |
|
| 113 |
|
| 114 |
/** |
| 115 |
* Textareas natively support keyboard focus. |
| 116 |
* @override |
| 117 |
*/ |
| 118 |
goog.ui.TextareaRenderer.prototype.setFocusable = goog.nullFunction; |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Textareas also expose the DISABLED state in the HTML textarea's |
| 123 |
* {@code disabled} attribute. |
| 124 |
* @override |
| 125 |
*/ |
| 126 |
goog.ui.TextareaRenderer.prototype.setState = function( |
| 127 |
textarea, state, enable) { |
| 128 |
goog.ui.TextareaRenderer.superClass_.setState.call( |
| 129 |
this, textarea, state, enable); |
| 130 |
var element = textarea.getElement(); |
| 131 |
if (element && state == goog.ui.Component.State.DISABLED) { |
| 132 |
element.disabled = enable; |
| 133 |
} |
| 134 |
}; |
| 135 |
|
| 136 |
|
| 137 |
/** |
| 138 |
* Textareas don't need ARIA states to support accessibility, so this is |
| 139 |
* a no-op. |
| 140 |
* @override |
| 141 |
*/ |
| 142 |
goog.ui.TextareaRenderer.prototype.updateAriaState = goog.nullFunction; |
| 143 |
|
| 144 |
|
| 145 |
/** |
| 146 |
* Sets up the textarea control such that it doesn't waste time adding |
| 147 |
* functionality that is already natively supported by browser |
| 148 |
* textareas. |
| 149 |
* @param {goog.ui.Control} textarea Textarea control to configure. |
| 150 |
* @private |
| 151 |
*/ |
| 152 |
goog.ui.TextareaRenderer.prototype.setUpTextarea_ = function(textarea) { |
| 153 |
textarea.setHandleMouseEvents(false); |
| 154 |
textarea.setAutoStates(goog.ui.Component.State.ALL, false); |
| 155 |
textarea.setSupportedState(goog.ui.Component.State.FOCUSED, false); |
| 156 |
}; |
| 157 |
|
| 158 |
|
| 159 |
/** @override **/ |
| 160 |
goog.ui.TextareaRenderer.prototype.setContent = function(element, value) { |
| 161 |
if (element) { |
| 162 |
element.value = value; |
| 163 |
} |
| 164 |
}; |
| 165 |
|
| 166 |
|
| 167 |
/** @override **/ |
| 168 |
goog.ui.TextareaRenderer.prototype.getCssClass = function() { |
| 169 |
return goog.ui.TextareaRenderer.CSS_CLASS; |
| 170 |
}; |