| 1 |
// Copyright 2006 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 Character counter widget implementation. |
| 17 |
* |
| 18 |
* @author [email protected] (Emil A Eklund) |
| 19 |
* @see ../demos/charcounter.html |
| 20 |
*/ |
| 21 |
|
| 22 |
goog.provide('goog.ui.CharCounter'); |
| 23 |
goog.provide('goog.ui.CharCounter.Display'); |
| 24 |
|
| 25 |
goog.require('goog.dom'); |
| 26 |
goog.require('goog.events'); |
| 27 |
goog.require('goog.events.EventTarget'); |
| 28 |
goog.require('goog.events.InputHandler'); |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
/** |
| 33 |
* CharCounter widget. Counts the number of characters in a input field or a |
| 34 |
* text box and displays the number of additional characters that may be |
| 35 |
* entered before the maximum length is reached. |
| 36 |
* |
| 37 |
* @extends {goog.events.EventTarget} |
| 38 |
* @param {HTMLInputElement|HTMLTextAreaElement} elInput Input or text area |
| 39 |
* element to count the number of characters in. |
| 40 |
* @param {Element} elCount HTML element to display the remaining number of |
| 41 |
* characters in. You can pass in null for this if you don't want to expose |
| 42 |
* the number of chars remaining. |
| 43 |
* @param {number} maxLength The maximum length. |
| 44 |
* @param {goog.ui.CharCounter.Display=} opt_displayMode Display mode for this |
| 45 |
* char counter. Defaults to {@link goog.ui.CharCounter.Display.REMAINING}. |
| 46 |
* @constructor |
| 47 |
* @final |
| 48 |
*/ |
| 49 |
goog.ui.CharCounter = function(elInput, elCount, maxLength, opt_displayMode) { |
| 50 |
goog.events.EventTarget.call(this); |
| 51 |
|
| 52 |
/** |
| 53 |
* Input or text area element to count the number of characters in. |
| 54 |
* @type {HTMLInputElement|HTMLTextAreaElement} |
| 55 |
* @private |
| 56 |
*/ |
| 57 |
this.elInput_ = elInput; |
| 58 |
|
| 59 |
/** |
| 60 |
* HTML element to display the remaining number of characters in. |
| 61 |
* @type {Element} |
| 62 |
* @private |
| 63 |
*/ |
| 64 |
this.elCount_ = elCount; |
| 65 |
|
| 66 |
/** |
| 67 |
* The maximum length. |
| 68 |
* @type {number} |
| 69 |
* @private |
| 70 |
*/ |
| 71 |
this.maxLength_ = maxLength; |
| 72 |
|
| 73 |
/** |
| 74 |
* The display mode for this char counter. |
| 75 |
* @type {!goog.ui.CharCounter.Display} |
| 76 |
* @private |
| 77 |
*/ |
| 78 |
this.display_ = opt_displayMode || goog.ui.CharCounter.Display.REMAINING; |
| 79 |
|
| 80 |
elInput.removeAttribute('maxlength'); |
| 81 |
|
| 82 |
/** |
| 83 |
* The input handler that provides the input event. |
| 84 |
* @type {goog.events.InputHandler} |
| 85 |
* @private |
| 86 |
*/ |
| 87 |
this.inputHandler_ = new goog.events.InputHandler(elInput); |
| 88 |
|
| 89 |
goog.events.listen( |
| 90 |
this.inputHandler_, goog.events.InputHandler.EventType.INPUT, |
| 91 |
this.onChange_, false, this); |
| 92 |
|
| 93 |
this.checkLength(); |
| 94 |
}; |
| 95 |
goog.inherits(goog.ui.CharCounter, goog.events.EventTarget); |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* Display mode for the char counter. |
| 100 |
* @enum {number} |
| 101 |
*/ |
| 102 |
goog.ui.CharCounter.Display = { |
| 103 |
/** Widget displays the number of characters remaining (the default). */ |
| 104 |
REMAINING: 0, |
| 105 |
/** Widget displays the number of characters entered. */ |
| 106 |
INCREMENTAL: 1 |
| 107 |
}; |
| 108 |
|
| 109 |
|
| 110 |
/** |
| 111 |
* Sets the maximum length. |
| 112 |
* |
| 113 |
* @param {number} maxLength The maximum length. |
| 114 |
*/ |
| 115 |
goog.ui.CharCounter.prototype.setMaxLength = function(maxLength) { |
| 116 |
this.maxLength_ = maxLength; |
| 117 |
this.checkLength(); |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* Returns the maximum length. |
| 123 |
* |
| 124 |
* @return {number} The maximum length. |
| 125 |
*/ |
| 126 |
goog.ui.CharCounter.prototype.getMaxLength = function() { |
| 127 |
return this.maxLength_; |
| 128 |
}; |
| 129 |
|
| 130 |
|
| 131 |
/** |
| 132 |
* Sets the display mode. |
| 133 |
* |
| 134 |
* @param {!goog.ui.CharCounter.Display} displayMode The display mode. |
| 135 |
*/ |
| 136 |
goog.ui.CharCounter.prototype.setDisplayMode = function(displayMode) { |
| 137 |
this.display_ = displayMode; |
| 138 |
this.checkLength(); |
| 139 |
}; |
| 140 |
|
| 141 |
|
| 142 |
/** |
| 143 |
* Returns the display mode. |
| 144 |
* |
| 145 |
* @return {!goog.ui.CharCounter.Display} The display mode. |
| 146 |
*/ |
| 147 |
goog.ui.CharCounter.prototype.getDisplayMode = function() { |
| 148 |
return this.display_; |
| 149 |
}; |
| 150 |
|
| 151 |
|
| 152 |
/** |
| 153 |
* Change event handler for input field. |
| 154 |
* |
| 155 |
* @param {goog.events.BrowserEvent} event Change event. |
| 156 |
* @private |
| 157 |
*/ |
| 158 |
goog.ui.CharCounter.prototype.onChange_ = function(event) { |
| 159 |
this.checkLength(); |
| 160 |
}; |
| 161 |
|
| 162 |
|
| 163 |
/** |
| 164 |
* Checks length of text in input field and updates the counter. Truncates text |
| 165 |
* if the maximum lengths is exceeded. |
| 166 |
*/ |
| 167 |
goog.ui.CharCounter.prototype.checkLength = function() { |
| 168 |
var count = this.elInput_.value.length; |
| 169 |
|
| 170 |
// There's no maxlength property for textareas so instead we truncate the |
| 171 |
// text if it gets too long. It's also used to truncate the text in a input |
| 172 |
// field if the maximum length is changed. |
| 173 |
if (count > this.maxLength_) { |
| 174 |
var scrollTop = this.elInput_.scrollTop; |
| 175 |
var scrollLeft = this.elInput_.scrollLeft; |
| 176 |
|
| 177 |
this.elInput_.value = this.elInput_.value.substring(0, this.maxLength_); |
| 178 |
count = this.maxLength_; |
| 179 |
|
| 180 |
this.elInput_.scrollTop = scrollTop; |
| 181 |
this.elInput_.scrollLeft = scrollLeft; |
| 182 |
} |
| 183 |
|
| 184 |
if (this.elCount_) { |
| 185 |
var incremental = this.display_ == goog.ui.CharCounter.Display.INCREMENTAL; |
| 186 |
goog.dom.setTextContent( |
| 187 |
this.elCount_, String(incremental ? count : this.maxLength_ - count)); |
| 188 |
} |
| 189 |
}; |
| 190 |
|
| 191 |
|
| 192 |
/** @override */ |
| 193 |
goog.ui.CharCounter.prototype.disposeInternal = function() { |
| 194 |
goog.ui.CharCounter.superClass_.disposeInternal.call(this); |
| 195 |
delete this.elInput_; |
| 196 |
this.inputHandler_.dispose(); |
| 197 |
this.inputHandler_ = null; |
| 198 |
}; |