bidiinput.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // Copyright 2007 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. * @fileoverview Component for an input field with bidi direction automatic
  16. * detection. The input element directionality is automatically set according
  17. * to the contents (value) of the element.
  18. *
  19. * @see ../demos/bidiinput.html
  20. */
  21. goog.provide('goog.ui.BidiInput');
  22. goog.require('goog.dom');
  23. goog.require('goog.dom.InputType');
  24. goog.require('goog.dom.TagName');
  25. goog.require('goog.events');
  26. goog.require('goog.events.InputHandler');
  27. goog.require('goog.i18n.bidi');
  28. goog.require('goog.ui.Component');
  29. /**
  30. * Default implementation of BidiInput.
  31. *
  32. * @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper.
  33. * @constructor
  34. * @extends {goog.ui.Component}
  35. */
  36. goog.ui.BidiInput = function(opt_domHelper) {
  37. goog.ui.Component.call(this, opt_domHelper);
  38. };
  39. goog.inherits(goog.ui.BidiInput, goog.ui.Component);
  40. goog.tagUnsealableClass(goog.ui.BidiInput);
  41. /**
  42. * The input handler that provides the input event.
  43. * @type {goog.events.InputHandler?}
  44. * @private
  45. */
  46. goog.ui.BidiInput.prototype.inputHandler_ = null;
  47. /**
  48. * Decorates the given HTML element as a BidiInput. The HTML element can be an
  49. * input element with type='text', a textarea element, or any contenteditable.
  50. * Overrides {@link goog.ui.Component#decorateInternal}. Considered protected.
  51. * @param {Element} element Element to decorate.
  52. * @protected
  53. * @override
  54. */
  55. goog.ui.BidiInput.prototype.decorateInternal = function(element) {
  56. goog.ui.BidiInput.superClass_.decorateInternal.call(this, element);
  57. this.init_();
  58. };
  59. /**
  60. * Creates the element for the text input.
  61. * @protected
  62. * @override
  63. */
  64. goog.ui.BidiInput.prototype.createDom = function() {
  65. this.setElementInternal(
  66. this.getDomHelper().createDom(
  67. goog.dom.TagName.INPUT, {'type': goog.dom.InputType.TEXT}));
  68. this.init_();
  69. };
  70. /**
  71. * Initializes the events and initial text direction.
  72. * Called from either decorate or createDom, after the input field has
  73. * been created.
  74. * @private
  75. */
  76. goog.ui.BidiInput.prototype.init_ = function() {
  77. // Set initial direction by current text
  78. this.setDirection_();
  79. // Listen to value change events
  80. this.inputHandler_ = new goog.events.InputHandler(this.getElement());
  81. goog.events.listen(
  82. this.inputHandler_, goog.events.InputHandler.EventType.INPUT,
  83. this.setDirection_, false, this);
  84. };
  85. /**
  86. * Set the direction of the input element based on the current value. If the
  87. * value does not have any strongly directional characters, remove the dir
  88. * attribute so that the direction is inherited instead.
  89. * This method is called when the user changes the input element value, or
  90. * when a program changes the value using
  91. * {@link goog.ui.BidiInput#setValue}
  92. * @private
  93. */
  94. goog.ui.BidiInput.prototype.setDirection_ = function() {
  95. var element = this.getElement();
  96. if (element) {
  97. var text = this.getValue();
  98. goog.i18n.bidi.setElementDirByTextDirectionality(element, text);
  99. }
  100. };
  101. /**
  102. * Returns the direction of the input element.
  103. * @return {?string} Return 'rtl' for right-to-left text,
  104. * 'ltr' for left-to-right text, or null if the value itself is not
  105. * enough to determine directionality (e.g. an empty value), and the
  106. * direction is inherited from a parent element (typically the body
  107. * element).
  108. */
  109. goog.ui.BidiInput.prototype.getDirection = function() {
  110. var dir = this.getElement().dir;
  111. if (dir == '') {
  112. dir = null;
  113. }
  114. return dir;
  115. };
  116. /**
  117. * Sets the value of the underlying input field, and sets the direction
  118. * according to the given value.
  119. * @param {string} value The Value to set in the underlying input field.
  120. */
  121. goog.ui.BidiInput.prototype.setValue = function(value) {
  122. var element = this.getElement();
  123. if (goog.isDefAndNotNull(element.value)) {
  124. element.value = value;
  125. } else {
  126. goog.dom.setTextContent(element, value);
  127. }
  128. this.setDirection_();
  129. };
  130. /**
  131. * Returns the value of the underlying input field.
  132. * @return {string} Value of the underlying input field.
  133. */
  134. goog.ui.BidiInput.prototype.getValue = function() {
  135. var element = this.getElement();
  136. return goog.isDefAndNotNull(element.value) ?
  137. element.value :
  138. goog.dom.getRawTextContent(element);
  139. };
  140. /** @override */
  141. goog.ui.BidiInput.prototype.disposeInternal = function() {
  142. if (this.inputHandler_) {
  143. goog.events.removeAll(this.inputHandler_);
  144. this.inputHandler_.dispose();
  145. this.inputHandler_ = null;
  146. goog.ui.BidiInput.superClass_.disposeInternal.call(this);
  147. }
  148. };