| 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 A content-aware textarea control that grows and shrinks |
| 17 |
* automatically. This implementation extends {@link goog.ui.Control}. |
| 18 |
* This code is inspired by Dojo Dijit's Textarea implementation with |
| 19 |
* modifications to support native (when available) textarea resizing and |
| 20 |
* minHeight and maxHeight enforcement. |
| 21 |
* |
| 22 |
* @see ../demos/textarea.html |
| 23 |
*/ |
| 24 |
|
| 25 |
goog.provide('goog.ui.Textarea'); |
| 26 |
goog.provide('goog.ui.Textarea.EventType'); |
| 27 |
|
| 28 |
goog.require('goog.asserts'); |
| 29 |
goog.require('goog.dom'); |
| 30 |
goog.require('goog.dom.classlist'); |
| 31 |
goog.require('goog.events.EventType'); |
| 32 |
goog.require('goog.style'); |
| 33 |
goog.require('goog.ui.Control'); |
| 34 |
goog.require('goog.ui.TextareaRenderer'); |
| 35 |
goog.require('goog.userAgent'); |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* A textarea control to handle growing/shrinking with textarea.value. |
| 41 |
* |
| 42 |
* @param {string} content Text to set as the textarea's value. |
| 43 |
* @param {goog.ui.TextareaRenderer=} opt_renderer Renderer used to render or |
| 44 |
* decorate the textarea. Defaults to {@link goog.ui.TextareaRenderer}. |
| 45 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper, used for |
| 46 |
* document interaction. |
| 47 |
* @constructor |
| 48 |
* @extends {goog.ui.Control} |
| 49 |
*/ |
| 50 |
goog.ui.Textarea = function(content, opt_renderer, opt_domHelper) { |
| 51 |
goog.ui.Control.call( |
| 52 |
this, content, opt_renderer || goog.ui.TextareaRenderer.getInstance(), |
| 53 |
opt_domHelper); |
| 54 |
|
| 55 |
this.setHandleMouseEvents(false); |
| 56 |
this.setAllowTextSelection(true); |
| 57 |
this.hasUserInput_ = (content != ''); |
| 58 |
if (!content) { |
| 59 |
this.setContentInternal(''); |
| 60 |
} |
| 61 |
}; |
| 62 |
goog.inherits(goog.ui.Textarea, goog.ui.Control); |
| 63 |
goog.tagUnsealableClass(goog.ui.Textarea); |
| 64 |
|
| 65 |
|
| 66 |
/** |
| 67 |
* Some UAs will shrink the textarea automatically, some won't. |
| 68 |
* @type {boolean} |
| 69 |
* @private |
| 70 |
*/ |
| 71 |
goog.ui.Textarea.NEEDS_HELP_SHRINKING_ = |
| 72 |
!(goog.userAgent.IE && !goog.userAgent.isDocumentModeOrHigher(11)); |
| 73 |
|
| 74 |
|
| 75 |
/** |
| 76 |
* True if the resizing function is executing, false otherwise. |
| 77 |
* @type {boolean} |
| 78 |
* @private |
| 79 |
*/ |
| 80 |
goog.ui.Textarea.prototype.isResizing_ = false; |
| 81 |
|
| 82 |
|
| 83 |
/** |
| 84 |
* Represents if we have focus on the textarea element, used only |
| 85 |
* to render the placeholder if we don't have native placeholder |
| 86 |
* support. |
| 87 |
* @type {boolean} |
| 88 |
* @private |
| 89 |
*/ |
| 90 |
goog.ui.Textarea.prototype.hasFocusForPlaceholder_ = false; |
| 91 |
|
| 92 |
|
| 93 |
/** |
| 94 |
* @type {boolean} |
| 95 |
* @private |
| 96 |
*/ |
| 97 |
goog.ui.Textarea.prototype.hasUserInput_ = false; |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* The height of the textarea as last measured. |
| 102 |
* @type {number} |
| 103 |
* @private |
| 104 |
*/ |
| 105 |
goog.ui.Textarea.prototype.height_ = 0; |
| 106 |
|
| 107 |
|
| 108 |
/** |
| 109 |
* A maximum height for the textarea. When set to 0, the default, there is no |
| 110 |
* enforcement of this value during resize. |
| 111 |
* @type {number} |
| 112 |
* @private |
| 113 |
*/ |
| 114 |
goog.ui.Textarea.prototype.maxHeight_ = 0; |
| 115 |
|
| 116 |
|
| 117 |
/** |
| 118 |
* A minimum height for the textarea. When set to 0, the default, there is no |
| 119 |
* enforcement of this value during resize. |
| 120 |
* @type {number} |
| 121 |
* @private |
| 122 |
*/ |
| 123 |
goog.ui.Textarea.prototype.minHeight_ = 0; |
| 124 |
|
| 125 |
|
| 126 |
/** |
| 127 |
* Whether or not textarea rendering characteristics have been discovered. |
| 128 |
* Specifically we determine, at runtime: |
| 129 |
* If the padding and border box is included in offsetHeight. |
| 130 |
* @see {goog.ui.Textarea.prototype.needsPaddingBorderFix_} |
| 131 |
* If the padding and border box is included in scrollHeight. |
| 132 |
* @see {goog.ui.Textarea.prototype.scrollHeightIncludesPadding_} and |
| 133 |
* @see {goog.ui.Textarea.prototype.scrollHeightIncludesBorder_} |
| 134 |
* TODO(user): See if we can determine goog.ui.Textarea.NEEDS_HELP_SHRINKING_. |
| 135 |
* @type {boolean} |
| 136 |
* @private |
| 137 |
*/ |
| 138 |
goog.ui.Textarea.prototype.hasDiscoveredTextareaCharacteristics_ = false; |
| 139 |
|
| 140 |
|
| 141 |
/** |
| 142 |
* If a user agent doesn't correctly support the box-sizing:border-box CSS |
| 143 |
* value then we'll need to adjust our height calculations. |
| 144 |
* @see {goog.ui.Textarea.prototype.discoverTextareaCharacteristics_} |
| 145 |
* @type {boolean} |
| 146 |
* @private |
| 147 |
*/ |
| 148 |
goog.ui.Textarea.prototype.needsPaddingBorderFix_ = false; |
| 149 |
|
| 150 |
|
| 151 |
/** |
| 152 |
* Whether or not scrollHeight of a textarea includes the padding box. |
| 153 |
* @type {boolean} |
| 154 |
* @private |
| 155 |
*/ |
| 156 |
goog.ui.Textarea.prototype.scrollHeightIncludesPadding_ = false; |
| 157 |
|
| 158 |
|
| 159 |
/** |
| 160 |
* Whether or not scrollHeight of a textarea includes the border box. |
| 161 |
* @type {boolean} |
| 162 |
* @private |
| 163 |
*/ |
| 164 |
goog.ui.Textarea.prototype.scrollHeightIncludesBorder_ = false; |
| 165 |
|
| 166 |
|
| 167 |
/** |
| 168 |
* For storing the padding box size during enterDocument, to prevent possible |
| 169 |
* measurement differences that can happen after text zooming. |
| 170 |
* Note: runtime padding changes will cause problems with this. |
| 171 |
* @type {goog.math.Box} |
| 172 |
* @private |
| 173 |
*/ |
| 174 |
goog.ui.Textarea.prototype.paddingBox_; |
| 175 |
|
| 176 |
|
| 177 |
/** |
| 178 |
* For storing the border box size during enterDocument, to prevent possible |
| 179 |
* measurement differences that can happen after text zooming. |
| 180 |
* Note: runtime border width changes will cause problems with this. |
| 181 |
* @type {goog.math.Box} |
| 182 |
* @private |
| 183 |
*/ |
| 184 |
goog.ui.Textarea.prototype.borderBox_; |
| 185 |
|
| 186 |
|
| 187 |
/** |
| 188 |
* Default text content for the textarea when it is unchanged and unfocussed. |
| 189 |
* We use the placeholder attribute for all browsers that have support for |
| 190 |
* it (new in HTML5 for the following browsers: |
| 191 |
* |
| 192 |
* Internet Explorer 10.0 |
| 193 |
* Firefox 4.0 |
| 194 |
* Opera 11.6 |
| 195 |
* Chrome 4.0 |
| 196 |
* Safari 5.0 |
| 197 |
* |
| 198 |
* For older browsers, we save the placeholderText_ and set it as the element's |
| 199 |
* value and add the TEXTAREA_PLACEHOLDER_CLASS to indicate that it's a |
| 200 |
* placeholder string. |
| 201 |
* @type {string} |
| 202 |
* @private |
| 203 |
*/ |
| 204 |
goog.ui.Textarea.prototype.placeholderText_ = ''; |
| 205 |
|
| 206 |
|
| 207 |
/** |
| 208 |
* Constants for event names. |
| 209 |
* @enum {string} |
| 210 |
*/ |
| 211 |
goog.ui.Textarea.EventType = { |
| 212 |
RESIZE: 'resize' |
| 213 |
}; |
| 214 |
|
| 215 |
|
| 216 |
/** |
| 217 |
* Sets the default text for the textarea. |
| 218 |
* @param {string} text The default text for the textarea. |
| 219 |
*/ |
| 220 |
goog.ui.Textarea.prototype.setPlaceholder = function(text) { |
| 221 |
this.placeholderText_ = text; |
| 222 |
if (this.getElement()) { |
| 223 |
this.restorePlaceholder_(); |
| 224 |
} |
| 225 |
}; |
| 226 |
|
| 227 |
|
| 228 |
/** |
| 229 |
* @return {number} The padding plus the border box height. |
| 230 |
* @private |
| 231 |
*/ |
| 232 |
goog.ui.Textarea.prototype.getPaddingBorderBoxHeight_ = function() { |
| 233 |
var paddingBorderBoxHeight = this.paddingBox_.top + this.paddingBox_.bottom + |
| 234 |
this.borderBox_.top + this.borderBox_.bottom; |
| 235 |
return paddingBorderBoxHeight; |
| 236 |
}; |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* @return {number} The minHeight value. |
| 241 |
*/ |
| 242 |
goog.ui.Textarea.prototype.getMinHeight = function() { |
| 243 |
return this.minHeight_; |
| 244 |
}; |
| 245 |
|
| 246 |
|
| 247 |
/** |
| 248 |
* @return {number} The minHeight value with a potential padding fix. |
| 249 |
* @private |
| 250 |
*/ |
| 251 |
goog.ui.Textarea.prototype.getMinHeight_ = function() { |
| 252 |
var minHeight = this.minHeight_; |
| 253 |
var textarea = this.getElement(); |
| 254 |
if (minHeight && textarea && this.needsPaddingBorderFix_) { |
| 255 |
minHeight -= this.getPaddingBorderBoxHeight_(); |
| 256 |
} |
| 257 |
return minHeight; |
| 258 |
}; |
| 259 |
|
| 260 |
|
| 261 |
/** |
| 262 |
* Sets a minimum height for the textarea, and calls resize if rendered. |
| 263 |
* @param {number} height New minHeight value. |
| 264 |
*/ |
| 265 |
goog.ui.Textarea.prototype.setMinHeight = function(height) { |
| 266 |
this.minHeight_ = height; |
| 267 |
this.resize(); |
| 268 |
}; |
| 269 |
|
| 270 |
|
| 271 |
/** |
| 272 |
* @return {number} The maxHeight value. |
| 273 |
*/ |
| 274 |
goog.ui.Textarea.prototype.getMaxHeight = function() { |
| 275 |
return this.maxHeight_; |
| 276 |
}; |
| 277 |
|
| 278 |
|
| 279 |
/** |
| 280 |
* @return {number} The maxHeight value with a potential padding fix. |
| 281 |
* @private |
| 282 |
*/ |
| 283 |
goog.ui.Textarea.prototype.getMaxHeight_ = function() { |
| 284 |
var maxHeight = this.maxHeight_; |
| 285 |
var textarea = this.getElement(); |
| 286 |
if (maxHeight && textarea && this.needsPaddingBorderFix_) { |
| 287 |
maxHeight -= this.getPaddingBorderBoxHeight_(); |
| 288 |
} |
| 289 |
return maxHeight; |
| 290 |
}; |
| 291 |
|
| 292 |
|
| 293 |
/** |
| 294 |
* Sets a maximum height for the textarea, and calls resize if rendered. |
| 295 |
* @param {number} height New maxHeight value. |
| 296 |
*/ |
| 297 |
goog.ui.Textarea.prototype.setMaxHeight = function(height) { |
| 298 |
this.maxHeight_ = height; |
| 299 |
this.resize(); |
| 300 |
}; |
| 301 |
|
| 302 |
|
| 303 |
/** |
| 304 |
* Sets the textarea's value. |
| 305 |
* @param {*} value The value property for the textarea, will be cast to a |
| 306 |
* string by the browser when setting textarea.value. |
| 307 |
*/ |
| 308 |
goog.ui.Textarea.prototype.setValue = function(value) { |
| 309 |
this.setContent(String(value)); |
| 310 |
}; |
| 311 |
|
| 312 |
|
| 313 |
/** |
| 314 |
* Gets the textarea's value. |
| 315 |
* @return {string} value The value of the textarea. |
| 316 |
*/ |
| 317 |
goog.ui.Textarea.prototype.getValue = function() { |
| 318 |
// We potentially have the placeholder stored in the value. |
| 319 |
// If a client of this class sets this.getElement().value directly |
| 320 |
// we don't set the this.hasUserInput_ boolean. Thus, we need to |
| 321 |
// explicitly check if the value != the placeholder text. This has |
| 322 |
// the unfortunate edge case of: |
| 323 |
// If the client sets this.getElement().value to the placeholder |
| 324 |
// text, we'll return the empty string. |
| 325 |
// The normal use case shouldn't be an issue, however, since the |
| 326 |
// default placeholderText is the empty string. Also, if the end user |
| 327 |
// inputs text, then this.hasUserInput_ will always be true. |
| 328 |
if (this.getElement().value != this.placeholderText_ || |
| 329 |
this.supportsNativePlaceholder_() || this.hasUserInput_) { |
| 330 |
// We don't do anything fancy here. |
| 331 |
return this.getElement().value; |
| 332 |
} |
| 333 |
return ''; |
| 334 |
}; |
| 335 |
|
| 336 |
|
| 337 |
/** @override */ |
| 338 |
goog.ui.Textarea.prototype.setContent = function(content) { |
| 339 |
goog.ui.Textarea.superClass_.setContent.call(this, content); |
| 340 |
this.hasUserInput_ = (content != ''); |
| 341 |
this.resize(); |
| 342 |
}; |
| 343 |
|
| 344 |
|
| 345 |
/** @override **/ |
| 346 |
goog.ui.Textarea.prototype.setEnabled = function(enable) { |
| 347 |
goog.ui.Textarea.superClass_.setEnabled.call(this, enable); |
| 348 |
this.getElement().disabled = !enable; |
| 349 |
}; |
| 350 |
|
| 351 |
|
| 352 |
/** |
| 353 |
* Resizes the textarea vertically. |
| 354 |
*/ |
| 355 |
goog.ui.Textarea.prototype.resize = function() { |
| 356 |
if (this.getElement()) { |
| 357 |
this.grow_(); |
| 358 |
} |
| 359 |
}; |
| 360 |
|
| 361 |
|
| 362 |
/** |
| 363 |
* @return {boolean} True if the element supports the placeholder attribute. |
| 364 |
* @private |
| 365 |
*/ |
| 366 |
goog.ui.Textarea.prototype.supportsNativePlaceholder_ = function() { |
| 367 |
goog.asserts.assert(this.getElement()); |
| 368 |
return 'placeholder' in this.getElement(); |
| 369 |
}; |
| 370 |
|
| 371 |
|
| 372 |
/** |
| 373 |
* Sets the value of the textarea element to the default text. |
| 374 |
* @private |
| 375 |
*/ |
| 376 |
goog.ui.Textarea.prototype.restorePlaceholder_ = function() { |
| 377 |
if (!this.placeholderText_) { |
| 378 |
// Return early if there is no placeholder to mess with. |
| 379 |
return; |
| 380 |
} |
| 381 |
// Check again in case something changed since this was scheduled. |
| 382 |
// We check that the element is still there since this is called by a timer |
| 383 |
// and the dispose method may have been called prior to this. |
| 384 |
if (this.supportsNativePlaceholder_()) { |
| 385 |
this.getElement().placeholder = this.placeholderText_; |
| 386 |
} else if ( |
| 387 |
this.getElement() && !this.hasUserInput_ && |
| 388 |
!this.hasFocusForPlaceholder_) { |
| 389 |
// We only want to set the value + placeholder CSS if we actually have |
| 390 |
// some placeholder text to show. |
| 391 |
goog.dom.classlist.add( |
| 392 |
goog.asserts.assert(this.getElement()), |
| 393 |
goog.ui.Textarea.TEXTAREA_PLACEHOLDER_CLASS); |
| 394 |
this.getElement().value = this.placeholderText_; |
| 395 |
} |
| 396 |
}; |
| 397 |
|
| 398 |
|
| 399 |
/** @override **/ |
| 400 |
goog.ui.Textarea.prototype.enterDocument = function() { |
| 401 |
goog.ui.Textarea.base(this, 'enterDocument'); |
| 402 |
var textarea = this.getElement(); |
| 403 |
|
| 404 |
// Eliminates the vertical scrollbar and changes the box-sizing mode for the |
| 405 |
// textarea to the border-box (aka quirksmode) paradigm. |
| 406 |
goog.style.setStyle(textarea, { |
| 407 |
'overflowY': 'hidden', |
| 408 |
'overflowX': 'auto', |
| 409 |
'boxSizing': 'border-box', |
| 410 |
'MsBoxSizing': 'border-box', |
| 411 |
'WebkitBoxSizing': 'border-box', |
| 412 |
'MozBoxSizing': 'border-box' |
| 413 |
}); |
| 414 |
|
| 415 |
this.paddingBox_ = goog.style.getPaddingBox(textarea); |
| 416 |
this.borderBox_ = goog.style.getBorderBox(textarea); |
| 417 |
|
| 418 |
this.getHandler() |
| 419 |
.listen(textarea, goog.events.EventType.SCROLL, this.grow_) |
| 420 |
.listen(textarea, goog.events.EventType.FOCUS, this.grow_) |
| 421 |
.listen(textarea, goog.events.EventType.KEYUP, this.grow_) |
| 422 |
.listen(textarea, goog.events.EventType.MOUSEUP, this.mouseUpListener_) |
| 423 |
.listen(textarea, goog.events.EventType.BLUR, this.blur_); |
| 424 |
|
| 425 |
this.restorePlaceholder_(); |
| 426 |
this.resize(); |
| 427 |
}; |
| 428 |
|
| 429 |
|
| 430 |
/** |
| 431 |
* Gets the textarea's content height + padding height + border height. |
| 432 |
* This is done by getting the scrollHeight and adjusting from there. |
| 433 |
* In the end this result is what we want the new offsetHeight to equal. |
| 434 |
* @return {number} The height of the textarea. |
| 435 |
* @private |
| 436 |
*/ |
| 437 |
goog.ui.Textarea.prototype.getHeight_ = function() { |
| 438 |
this.discoverTextareaCharacteristics_(); |
| 439 |
var textarea = this.getElement(); |
| 440 |
// Because enterDocument can be called even when the component is rendered |
| 441 |
// without being in a document, we may not have cached the correct paddingBox |
| 442 |
// data on render(). We try to make up for this here. |
| 443 |
if (isNaN(this.paddingBox_.top)) { |
| 444 |
this.paddingBox_ = goog.style.getPaddingBox(textarea); |
| 445 |
this.borderBox_ = goog.style.getBorderBox(textarea); |
| 446 |
} |
| 447 |
// Accounts for a possible (though unlikely) horizontal scrollbar. |
| 448 |
var height = |
| 449 |
this.getElement().scrollHeight + this.getHorizontalScrollBarHeight_(); |
| 450 |
if (this.needsPaddingBorderFix_) { |
| 451 |
height -= this.getPaddingBorderBoxHeight_(); |
| 452 |
} else { |
| 453 |
if (!this.scrollHeightIncludesPadding_) { |
| 454 |
var paddingBox = this.paddingBox_; |
| 455 |
var paddingBoxHeight = paddingBox.top + paddingBox.bottom; |
| 456 |
height += paddingBoxHeight; |
| 457 |
} |
| 458 |
if (!this.scrollHeightIncludesBorder_) { |
| 459 |
var borderBox = goog.style.getBorderBox(textarea); |
| 460 |
var borderBoxHeight = borderBox.top + borderBox.bottom; |
| 461 |
height += borderBoxHeight; |
| 462 |
} |
| 463 |
} |
| 464 |
return height; |
| 465 |
}; |
| 466 |
|
| 467 |
|
| 468 |
/** |
| 469 |
* Sets the textarea's height. |
| 470 |
* @param {number} height The height to set. |
| 471 |
* @private |
| 472 |
*/ |
| 473 |
goog.ui.Textarea.prototype.setHeight_ = function(height) { |
| 474 |
if (this.height_ != height) { |
| 475 |
this.height_ = height; |
| 476 |
this.getElement().style.height = height + 'px'; |
| 477 |
} |
| 478 |
}; |
| 479 |
|
| 480 |
|
| 481 |
/** |
| 482 |
* Sets the textarea's rows attribute to be the number of newlines + 1. |
| 483 |
* This is necessary when the textarea is hidden, in which case scrollHeight |
| 484 |
* is not available. |
| 485 |
* @private |
| 486 |
*/ |
| 487 |
goog.ui.Textarea.prototype.setHeightToEstimate_ = function() { |
| 488 |
var textarea = this.getElement(); |
| 489 |
textarea.style.height = 'auto'; |
| 490 |
var newlines = textarea.value.match(/\n/g) || []; |
| 491 |
textarea.rows = newlines.length + 1; |
| 492 |
this.height_ = 0; |
| 493 |
}; |
| 494 |
|
| 495 |
|
| 496 |
/** |
| 497 |
* Gets the the height of (possibly present) horizontal scrollbar. |
| 498 |
* @return {number} The height of the horizontal scrollbar. |
| 499 |
* @private |
| 500 |
*/ |
| 501 |
goog.ui.Textarea.prototype.getHorizontalScrollBarHeight_ = function() { |
| 502 |
var textarea = /** @type {!HTMLElement} */ (this.getElement()); |
| 503 |
var height = textarea.offsetHeight - textarea.clientHeight; |
| 504 |
if (!this.scrollHeightIncludesPadding_) { |
| 505 |
var paddingBox = this.paddingBox_; |
| 506 |
var paddingBoxHeight = paddingBox.top + paddingBox.bottom; |
| 507 |
height -= paddingBoxHeight; |
| 508 |
} |
| 509 |
if (!this.scrollHeightIncludesBorder_) { |
| 510 |
var borderBox = goog.style.getBorderBox(textarea); |
| 511 |
var borderBoxHeight = borderBox.top + borderBox.bottom; |
| 512 |
height -= borderBoxHeight; |
| 513 |
} |
| 514 |
// Prevent negative number results, which sometimes show up. |
| 515 |
return height > 0 ? height : 0; |
| 516 |
}; |
| 517 |
|
| 518 |
|
| 519 |
/** |
| 520 |
* In order to assess the correct height for a textarea, we need to know |
| 521 |
* whether the scrollHeight (the full height of the text) property includes |
| 522 |
* the values for padding and borders. We can also test whether the |
| 523 |
* box-sizing: border-box setting is working and then tweak accordingly. |
| 524 |
* Instead of hardcoding a list of currently known behaviors and testing |
| 525 |
* for quirksmode, we do a runtime check out of the flow. The performance |
| 526 |
* impact should be very small. |
| 527 |
* @private |
| 528 |
*/ |
| 529 |
goog.ui.Textarea.prototype.discoverTextareaCharacteristics_ = function() { |
| 530 |
if (!this.hasDiscoveredTextareaCharacteristics_) { |
| 531 |
var textarea = |
| 532 |
/** @type {!HTMLElement} */ (this.getElement().cloneNode(false)); |
| 533 |
// We need to overwrite/write box model specific styles that might |
| 534 |
// affect height. |
| 535 |
goog.style.setStyle(textarea, { |
| 536 |
'position': 'absolute', |
| 537 |
'height': 'auto', |
| 538 |
'top': '-9999px', |
| 539 |
'margin': '0', |
| 540 |
'padding': '1px', |
| 541 |
'border': '1px solid #000', |
| 542 |
'overflow': 'hidden' |
| 543 |
}); |
| 544 |
goog.dom.appendChild(this.getDomHelper().getDocument().body, textarea); |
| 545 |
var initialScrollHeight = textarea.scrollHeight; |
| 546 |
|
| 547 |
textarea.style.padding = '10px'; |
| 548 |
var paddingScrollHeight = textarea.scrollHeight; |
| 549 |
this.scrollHeightIncludesPadding_ = |
| 550 |
paddingScrollHeight > initialScrollHeight; |
| 551 |
|
| 552 |
initialScrollHeight = paddingScrollHeight; |
| 553 |
textarea.style.borderWidth = '10px'; |
| 554 |
var borderScrollHeight = textarea.scrollHeight; |
| 555 |
this.scrollHeightIncludesBorder_ = borderScrollHeight > initialScrollHeight; |
| 556 |
|
| 557 |
// Tests if border-box sizing is working or not. |
| 558 |
textarea.style.height = '100px'; |
| 559 |
var offsetHeightAtHeight100 = textarea.offsetHeight; |
| 560 |
if (offsetHeightAtHeight100 != 100) { |
| 561 |
this.needsPaddingBorderFix_ = true; |
| 562 |
} |
| 563 |
|
| 564 |
goog.dom.removeNode(textarea); |
| 565 |
this.hasDiscoveredTextareaCharacteristics_ = true; |
| 566 |
} |
| 567 |
}; |
| 568 |
|
| 569 |
|
| 570 |
/** |
| 571 |
* The CSS class name to add to the input when the user has not entered a |
| 572 |
* value. |
| 573 |
*/ |
| 574 |
goog.ui.Textarea.TEXTAREA_PLACEHOLDER_CLASS = |
| 575 |
goog.getCssName('textarea-placeholder-input'); |
| 576 |
|
| 577 |
|
| 578 |
/** |
| 579 |
* Called when the element goes out of focus. |
| 580 |
* @param {goog.events.Event=} opt_e The browser event. |
| 581 |
* @private |
| 582 |
*/ |
| 583 |
goog.ui.Textarea.prototype.blur_ = function(opt_e) { |
| 584 |
if (!this.supportsNativePlaceholder_()) { |
| 585 |
this.hasFocusForPlaceholder_ = false; |
| 586 |
if (this.getElement().value == '') { |
| 587 |
// Only transition to the default text if we have |
| 588 |
// no user input. |
| 589 |
this.hasUserInput_ = false; |
| 590 |
this.restorePlaceholder_(); |
| 591 |
} |
| 592 |
} |
| 593 |
}; |
| 594 |
|
| 595 |
|
| 596 |
/** |
| 597 |
* Resizes the textarea to grow/shrink to match its contents. |
| 598 |
* @param {goog.events.Event=} opt_e The browser event. |
| 599 |
* @private |
| 600 |
*/ |
| 601 |
goog.ui.Textarea.prototype.grow_ = function(opt_e) { |
| 602 |
if (this.isResizing_) { |
| 603 |
return; |
| 604 |
} |
| 605 |
var textarea = /** @type {!HTMLElement} */ (this.getElement()); |
| 606 |
// If the element is getting focus and we don't support placeholders |
| 607 |
// natively, then remove the placeholder class. |
| 608 |
if (!this.supportsNativePlaceholder_() && opt_e && |
| 609 |
opt_e.type == goog.events.EventType.FOCUS) { |
| 610 |
// We must have a textarea element, since we're growing it. |
| 611 |
// Remove the placeholder CSS + set the value to empty if we're currently |
| 612 |
// showing the placeholderText_ value if this is the first time we're |
| 613 |
// getting focus. |
| 614 |
if (textarea.value == this.placeholderText_ && this.placeholderText_ && |
| 615 |
!this.hasFocusForPlaceholder_) { |
| 616 |
goog.dom.classlist.remove( |
| 617 |
textarea, goog.ui.Textarea.TEXTAREA_PLACEHOLDER_CLASS); |
| 618 |
textarea.value = ''; |
| 619 |
} |
| 620 |
this.hasFocusForPlaceholder_ = true; |
| 621 |
this.hasUserInput_ = (textarea.value != ''); |
| 622 |
} |
| 623 |
var shouldCallShrink = false; |
| 624 |
this.isResizing_ = true; |
| 625 |
var oldHeight = this.height_; |
| 626 |
if (textarea.scrollHeight) { |
| 627 |
var setMinHeight = false; |
| 628 |
var setMaxHeight = false; |
| 629 |
var newHeight = this.getHeight_(); |
| 630 |
var currentHeight = textarea.offsetHeight; |
| 631 |
var minHeight = this.getMinHeight_(); |
| 632 |
var maxHeight = this.getMaxHeight_(); |
| 633 |
if (minHeight && newHeight < minHeight) { |
| 634 |
this.setHeight_(minHeight); |
| 635 |
setMinHeight = true; |
| 636 |
} else if (maxHeight && newHeight > maxHeight) { |
| 637 |
this.setHeight_(maxHeight); |
| 638 |
// If the content is greater than the height, we'll want the vertical |
| 639 |
// scrollbar back. |
| 640 |
textarea.style.overflowY = ''; |
| 641 |
setMaxHeight = true; |
| 642 |
} else if (currentHeight != newHeight) { |
| 643 |
this.setHeight_(newHeight); |
| 644 |
// Makes sure that height_ is at least set. |
| 645 |
} else if (!this.height_) { |
| 646 |
this.height_ = newHeight; |
| 647 |
} |
| 648 |
if (!setMinHeight && !setMaxHeight && |
| 649 |
goog.ui.Textarea.NEEDS_HELP_SHRINKING_) { |
| 650 |
shouldCallShrink = true; |
| 651 |
} |
| 652 |
} else { |
| 653 |
this.setHeightToEstimate_(); |
| 654 |
} |
| 655 |
this.isResizing_ = false; |
| 656 |
|
| 657 |
if (shouldCallShrink) { |
| 658 |
this.shrink_(); |
| 659 |
} |
| 660 |
if (oldHeight != this.height_) { |
| 661 |
this.dispatchEvent(goog.ui.Textarea.EventType.RESIZE); |
| 662 |
} |
| 663 |
}; |
| 664 |
|
| 665 |
|
| 666 |
/** |
| 667 |
* Resizes the textarea to shrink to fit its contents. The way this works is |
| 668 |
* by increasing the padding of the textarea by 1px (it's important here that |
| 669 |
* we're in box-sizing: border-box mode). If the size of the textarea grows, |
| 670 |
* then the box is filled up to the padding box with text. |
| 671 |
* If it doesn't change, then we can shrink. |
| 672 |
* @private |
| 673 |
*/ |
| 674 |
goog.ui.Textarea.prototype.shrink_ = function() { |
| 675 |
var textarea = this.getElement(); |
| 676 |
if (!this.isResizing_) { |
| 677 |
this.isResizing_ = true; |
| 678 |
var scrollHeight = textarea.scrollHeight; |
| 679 |
if (!scrollHeight) { |
| 680 |
this.setHeightToEstimate_(); |
| 681 |
} else { |
| 682 |
var currentHeight = this.getHeight_(); |
| 683 |
var minHeight = this.getMinHeight_(); |
| 684 |
if (!(minHeight && currentHeight <= minHeight)) { |
| 685 |
// Nudge the padding by 1px. |
| 686 |
var paddingBox = this.paddingBox_; |
| 687 |
textarea.style.paddingBottom = paddingBox.bottom + 1 + 'px'; |
| 688 |
var heightAfterNudge = this.getHeight_(); |
| 689 |
// If the one px of padding had no effect, then we can shrink. |
| 690 |
if (heightAfterNudge == currentHeight) { |
| 691 |
textarea.style.paddingBottom = |
| 692 |
paddingBox.bottom + scrollHeight + 'px'; |
| 693 |
textarea.scrollTop = 0; |
| 694 |
var shrinkToHeight = this.getHeight_() - scrollHeight; |
| 695 |
if (shrinkToHeight >= minHeight) { |
| 696 |
this.setHeight_(shrinkToHeight); |
| 697 |
} else { |
| 698 |
this.setHeight_(minHeight); |
| 699 |
} |
| 700 |
} |
| 701 |
textarea.style.paddingBottom = paddingBox.bottom + 'px'; |
| 702 |
} |
| 703 |
} |
| 704 |
this.isResizing_ = false; |
| 705 |
} |
| 706 |
}; |
| 707 |
|
| 708 |
|
| 709 |
/** |
| 710 |
* We use this listener to check if the textarea has been natively resized |
| 711 |
* and if so we reset minHeight so that we don't ever shrink smaller than |
| 712 |
* the user's manually set height. Note that we cannot check size on mousedown |
| 713 |
* and then just compare here because we cannot capture mousedown on |
| 714 |
* the textarea resizer, while mouseup fires reliably. |
| 715 |
* @param {goog.events.BrowserEvent} e The mousedown event. |
| 716 |
* @private |
| 717 |
*/ |
| 718 |
goog.ui.Textarea.prototype.mouseUpListener_ = function(e) { |
| 719 |
var textarea = /** @type {!HTMLElement} */ (this.getElement()); |
| 720 |
var height = textarea.offsetHeight; |
| 721 |
|
| 722 |
// This solves for when the MSIE DropShadow filter is enabled, |
| 723 |
// as it affects the offsetHeight value, even with MsBoxSizing:border-box. |
| 724 |
if (textarea['filters'] && textarea['filters'].length) { |
| 725 |
var dropShadow = |
| 726 |
textarea['filters']['item']('DXImageTransform.Microsoft.DropShadow'); |
| 727 |
if (dropShadow) { |
| 728 |
height -= dropShadow['offX']; |
| 729 |
} |
| 730 |
} |
| 731 |
|
| 732 |
if (height != this.height_) { |
| 733 |
this.minHeight_ = height; |
| 734 |
this.height_ = height; |
| 735 |
} |
| 736 |
}; |