Debian Codegraph @

closure-library/closure/goog/ui/bidiinput.js

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