textarearenderer.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright 2010 The Closure Library Authors. All Rights Reserved.
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. /**
  14. * @fileoverview Native browser textarea renderer for {@link goog.ui.Textarea}s.
  15. */
  16. goog.provide('goog.ui.TextareaRenderer');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.ui.Component');
  19. goog.require('goog.ui.ControlRenderer');
  20. /**
  21. * Renderer for {@link goog.ui.Textarea}s. Renders and decorates native HTML
  22. * textarea elements. Since native HTML textareas have built-in support for
  23. * many features, overrides many expensive (and redundant) superclass methods to
  24. * be no-ops.
  25. * @constructor
  26. * @extends {goog.ui.ControlRenderer}
  27. */
  28. goog.ui.TextareaRenderer = function() {
  29. goog.ui.ControlRenderer.call(this);
  30. };
  31. goog.inherits(goog.ui.TextareaRenderer, goog.ui.ControlRenderer);
  32. goog.addSingletonGetter(goog.ui.TextareaRenderer);
  33. /**
  34. * Default CSS class to be applied to the root element of components rendered
  35. * by this renderer.
  36. * @type {string}
  37. */
  38. goog.ui.TextareaRenderer.CSS_CLASS = goog.getCssName('goog-textarea');
  39. /** @override */
  40. goog.ui.TextareaRenderer.prototype.getAriaRole = function() {
  41. // textareas don't need ARIA roles to be recognized by screen readers.
  42. return undefined;
  43. };
  44. /** @override */
  45. goog.ui.TextareaRenderer.prototype.decorate = function(control, element) {
  46. this.setUpTextarea_(control);
  47. goog.ui.TextareaRenderer.superClass_.decorate.call(this, control, element);
  48. control.setContent(element.value);
  49. return element;
  50. };
  51. /**
  52. * Returns the textarea's contents wrapped in an HTML textarea element. Sets
  53. * the textarea's disabled attribute as needed.
  54. * @param {goog.ui.Control} textarea Textarea to render.
  55. * @return {!Element} Root element for the Textarea control (an HTML textarea
  56. * element).
  57. * @override
  58. */
  59. goog.ui.TextareaRenderer.prototype.createDom = function(textarea) {
  60. this.setUpTextarea_(textarea);
  61. var element = textarea.getDomHelper().createDom(
  62. goog.dom.TagName.TEXTAREA, {
  63. 'class': this.getClassNames(textarea).join(' '),
  64. 'disabled': !textarea.isEnabled()
  65. },
  66. textarea.getContent() || '');
  67. return element;
  68. };
  69. /**
  70. * Overrides {@link goog.ui.TextareaRenderer#canDecorate} by returning true only
  71. * if the element is an HTML textarea.
  72. * @param {Element} element Element to decorate.
  73. * @return {boolean} Whether the renderer can decorate the element.
  74. * @override
  75. */
  76. goog.ui.TextareaRenderer.prototype.canDecorate = function(element) {
  77. return element.tagName == goog.dom.TagName.TEXTAREA;
  78. };
  79. /**
  80. * Textareas natively support right-to-left rendering.
  81. * @override
  82. */
  83. goog.ui.TextareaRenderer.prototype.setRightToLeft = goog.nullFunction;
  84. /**
  85. * Textareas are always focusable as long as they are enabled.
  86. * @override
  87. */
  88. goog.ui.TextareaRenderer.prototype.isFocusable = function(textarea) {
  89. return textarea.isEnabled();
  90. };
  91. /**
  92. * Textareas natively support keyboard focus.
  93. * @override
  94. */
  95. goog.ui.TextareaRenderer.prototype.setFocusable = goog.nullFunction;
  96. /**
  97. * Textareas also expose the DISABLED state in the HTML textarea's
  98. * {@code disabled} attribute.
  99. * @override
  100. */
  101. goog.ui.TextareaRenderer.prototype.setState = function(
  102. textarea, state, enable) {
  103. goog.ui.TextareaRenderer.superClass_.setState.call(
  104. this, textarea, state, enable);
  105. var element = textarea.getElement();
  106. if (element && state == goog.ui.Component.State.DISABLED) {
  107. element.disabled = enable;
  108. }
  109. };
  110. /**
  111. * Textareas don't need ARIA states to support accessibility, so this is
  112. * a no-op.
  113. * @override
  114. */
  115. goog.ui.TextareaRenderer.prototype.updateAriaState = goog.nullFunction;
  116. /**
  117. * Sets up the textarea control such that it doesn't waste time adding
  118. * functionality that is already natively supported by browser
  119. * textareas.
  120. * @param {goog.ui.Control} textarea Textarea control to configure.
  121. * @private
  122. */
  123. goog.ui.TextareaRenderer.prototype.setUpTextarea_ = function(textarea) {
  124. textarea.setHandleMouseEvents(false);
  125. textarea.setAutoStates(goog.ui.Component.State.ALL, false);
  126. textarea.setSupportedState(goog.ui.Component.State.FOCUSED, false);
  127. };
  128. /** @override **/
  129. goog.ui.TextareaRenderer.prototype.setContent = function(element, value) {
  130. if (element) {
  131. element.value = value;
  132. }
  133. };
  134. /** @override **/
  135. goog.ui.TextareaRenderer.prototype.getCssClass = function() {
  136. return goog.ui.TextareaRenderer.CSS_CLASS;
  137. };