| 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 This behavior is applied to a text input and it shows a text |
| 17 |
* message inside the element if the user hasn't entered any text. |
| 18 |
* |
| 19 |
* This uses the HTML5 placeholder attribute where it is supported. |
| 20 |
* |
| 21 |
* This is ported from http://go/labelinput.js |
| 22 |
* |
| 23 |
* Known issue: Safari does not allow you get to the window object from a |
| 24 |
* document. We need that to listen to the onload event. For now we hard code |
| 25 |
* the window to the current window. |
| 26 |
* |
| 27 |
* Known issue: We need to listen to the form submit event but we attach the |
| 28 |
* event only once (when created or when it is changed) so if you move the DOM |
| 29 |
* node to another form it will not be cleared correctly before submitting. |
| 30 |
* |
| 31 |
* @author [email protected] (Erik Arvidsson) |
| 32 |
* @see ../demos/labelinput.html |
| 33 |
*/ |
| 34 |
|
| 35 |
goog.provide('goog.ui.LabelInput'); |
| 36 |
|
| 37 |
goog.require('goog.Timer'); |
| 38 |
goog.require('goog.a11y.aria'); |
| 39 |
goog.require('goog.a11y.aria.State'); |
| 40 |
goog.require('goog.asserts'); |
| 41 |
goog.require('goog.dom'); |
| 42 |
goog.require('goog.dom.InputType'); |
| 43 |
goog.require('goog.dom.TagName'); |
| 44 |
goog.require('goog.dom.classlist'); |
| 45 |
goog.require('goog.events.EventHandler'); |
| 46 |
goog.require('goog.events.EventType'); |
| 47 |
goog.require('goog.ui.Component'); |
| 48 |
goog.require('goog.userAgent'); |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
/** |
| 53 |
* This creates the label input object. |
| 54 |
* @param {string=} opt_label The text to show as the label. |
| 55 |
* @param {goog.dom.DomHelper=} opt_domHelper Optional DOM helper. |
| 56 |
* @extends {goog.ui.Component} |
| 57 |
* @constructor |
| 58 |
*/ |
| 59 |
goog.ui.LabelInput = function(opt_label, opt_domHelper) { |
| 60 |
goog.ui.Component.call(this, opt_domHelper); |
| 61 |
|
| 62 |
/** |
| 63 |
* The text to show as the label. |
| 64 |
* @type {string} |
| 65 |
* @private |
| 66 |
*/ |
| 67 |
this.label_ = opt_label || ''; |
| 68 |
}; |
| 69 |
goog.inherits(goog.ui.LabelInput, goog.ui.Component); |
| 70 |
goog.tagUnsealableClass(goog.ui.LabelInput); |
| 71 |
|
| 72 |
|
| 73 |
/** |
| 74 |
* Variable used to store the element value on keydown and restore it on |
| 75 |
* keypress. See {@link #handleEscapeKeys_} |
| 76 |
* @type {?string} |
| 77 |
* @private |
| 78 |
*/ |
| 79 |
goog.ui.LabelInput.prototype.ffKeyRestoreValue_ = null; |
| 80 |
|
| 81 |
|
| 82 |
/** |
| 83 |
* The label restore delay after leaving the input. |
| 84 |
* @type {number} Delay for restoring the label. |
| 85 |
* @protected |
| 86 |
*/ |
| 87 |
goog.ui.LabelInput.prototype.labelRestoreDelayMs = 10; |
| 88 |
|
| 89 |
|
| 90 |
/** @private {boolean} */ |
| 91 |
goog.ui.LabelInput.prototype.inFocusAndSelect_; |
| 92 |
|
| 93 |
|
| 94 |
/** @private {boolean} */ |
| 95 |
goog.ui.LabelInput.prototype.formAttached_; |
| 96 |
|
| 97 |
|
| 98 |
/** |
| 99 |
* Indicates whether the browser supports the placeholder attribute, new in |
| 100 |
* HTML5. |
| 101 |
* @type {?boolean} |
| 102 |
* @private |
| 103 |
*/ |
| 104 |
goog.ui.LabelInput.supportsPlaceholder_; |
| 105 |
|
| 106 |
|
| 107 |
/** |
| 108 |
* Checks browser support for placeholder attribute. |
| 109 |
* @return {boolean} Whether placeholder attribute is supported. |
| 110 |
* @private |
| 111 |
*/ |
| 112 |
goog.ui.LabelInput.isPlaceholderSupported_ = function() { |
| 113 |
if (!goog.isDefAndNotNull(goog.ui.LabelInput.supportsPlaceholder_)) { |
| 114 |
goog.ui.LabelInput.supportsPlaceholder_ = |
| 115 |
('placeholder' in goog.dom.createElement(goog.dom.TagName.INPUT)); |
| 116 |
} |
| 117 |
return goog.ui.LabelInput.supportsPlaceholder_; |
| 118 |
}; |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* @type {goog.events.EventHandler} |
| 123 |
* @private |
| 124 |
*/ |
| 125 |
goog.ui.LabelInput.prototype.eventHandler_; |
| 126 |
|
| 127 |
|
| 128 |
/** |
| 129 |
* @type {boolean} |
| 130 |
* @private |
| 131 |
*/ |
| 132 |
goog.ui.LabelInput.prototype.hasFocus_ = false; |
| 133 |
|
| 134 |
|
| 135 |
/** |
| 136 |
* Creates the DOM nodes needed for the label input. |
| 137 |
* @override |
| 138 |
*/ |
| 139 |
goog.ui.LabelInput.prototype.createDom = function() { |
| 140 |
this.setElementInternal( |
| 141 |
this.getDomHelper().createDom( |
| 142 |
goog.dom.TagName.INPUT, {'type': goog.dom.InputType.TEXT})); |
| 143 |
}; |
| 144 |
|
| 145 |
|
| 146 |
/** |
| 147 |
* Decorates an existing HTML input element as a label input. If the element |
| 148 |
* has a "label" attribute then that will be used as the label property for the |
| 149 |
* label input object. |
| 150 |
* @param {Element} element The HTML input element to decorate. |
| 151 |
* @override |
| 152 |
*/ |
| 153 |
goog.ui.LabelInput.prototype.decorateInternal = function(element) { |
| 154 |
goog.ui.LabelInput.superClass_.decorateInternal.call(this, element); |
| 155 |
if (!this.label_) { |
| 156 |
this.label_ = element.getAttribute('label') || ''; |
| 157 |
} |
| 158 |
|
| 159 |
// Check if we're attaching to an element that already has focus. |
| 160 |
if (goog.dom.getActiveElement(goog.dom.getOwnerDocument(element)) == |
| 161 |
element) { |
| 162 |
this.hasFocus_ = true; |
| 163 |
var el = this.getElement(); |
| 164 |
goog.asserts.assert(el); |
| 165 |
goog.dom.classlist.remove(el, this.labelCssClassName); |
| 166 |
} |
| 167 |
|
| 168 |
if (goog.ui.LabelInput.isPlaceholderSupported_()) { |
| 169 |
this.getElement().placeholder = this.label_; |
| 170 |
} |
| 171 |
var labelInputElement = this.getElement(); |
| 172 |
goog.asserts.assert( |
| 173 |
labelInputElement, 'The label input element cannot be null.'); |
| 174 |
goog.a11y.aria.setState( |
| 175 |
labelInputElement, goog.a11y.aria.State.LABEL, this.label_); |
| 176 |
}; |
| 177 |
|
| 178 |
|
| 179 |
/** @override */ |
| 180 |
goog.ui.LabelInput.prototype.enterDocument = function() { |
| 181 |
goog.ui.LabelInput.superClass_.enterDocument.call(this); |
| 182 |
this.attachEvents_(); |
| 183 |
this.check_(); |
| 184 |
|
| 185 |
// Make it easy for other closure widgets to play nicely with inputs using |
| 186 |
// LabelInput: |
| 187 |
this.getElement().labelInput_ = this; |
| 188 |
}; |
| 189 |
|
| 190 |
|
| 191 |
/** @override */ |
| 192 |
goog.ui.LabelInput.prototype.exitDocument = function() { |
| 193 |
goog.ui.LabelInput.superClass_.exitDocument.call(this); |
| 194 |
this.detachEvents_(); |
| 195 |
|
| 196 |
this.getElement().labelInput_ = null; |
| 197 |
}; |
| 198 |
|
| 199 |
|
| 200 |
/** |
| 201 |
* Attaches the events we need to listen to. |
| 202 |
* @private |
| 203 |
*/ |
| 204 |
goog.ui.LabelInput.prototype.attachEvents_ = function() { |
| 205 |
var eh = new goog.events.EventHandler(this); |
| 206 |
eh.listen(this.getElement(), goog.events.EventType.FOCUS, this.handleFocus_); |
| 207 |
eh.listen(this.getElement(), goog.events.EventType.BLUR, this.handleBlur_); |
| 208 |
|
| 209 |
if (goog.ui.LabelInput.isPlaceholderSupported_()) { |
| 210 |
this.eventHandler_ = eh; |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
if (goog.userAgent.GECKO) { |
| 215 |
eh.listen( |
| 216 |
this.getElement(), |
| 217 |
[ |
| 218 |
goog.events.EventType.KEYPRESS, goog.events.EventType.KEYDOWN, |
| 219 |
goog.events.EventType.KEYUP |
| 220 |
], |
| 221 |
this.handleEscapeKeys_); |
| 222 |
} |
| 223 |
|
| 224 |
// IE sets defaultValue upon load so we need to test that as well. |
| 225 |
var d = goog.dom.getOwnerDocument(this.getElement()); |
| 226 |
var w = goog.dom.getWindow(d); |
| 227 |
eh.listen(w, goog.events.EventType.LOAD, this.handleWindowLoad_); |
| 228 |
|
| 229 |
this.eventHandler_ = eh; |
| 230 |
this.attachEventsToForm_(); |
| 231 |
}; |
| 232 |
|
| 233 |
|
| 234 |
/** |
| 235 |
* Adds a listener to the form so that we can clear the input before it is |
| 236 |
* submitted. |
| 237 |
* @private |
| 238 |
*/ |
| 239 |
goog.ui.LabelInput.prototype.attachEventsToForm_ = function() { |
| 240 |
// in case we have are in a form we need to make sure the label is not |
| 241 |
// submitted |
| 242 |
if (!this.formAttached_ && this.eventHandler_ && this.getElement().form) { |
| 243 |
this.eventHandler_.listen( |
| 244 |
this.getElement().form, goog.events.EventType.SUBMIT, |
| 245 |
this.handleFormSubmit_); |
| 246 |
this.formAttached_ = true; |
| 247 |
} |
| 248 |
}; |
| 249 |
|
| 250 |
|
| 251 |
/** |
| 252 |
* Stops listening to the events. |
| 253 |
* @private |
| 254 |
*/ |
| 255 |
goog.ui.LabelInput.prototype.detachEvents_ = function() { |
| 256 |
if (this.eventHandler_) { |
| 257 |
this.eventHandler_.dispose(); |
| 258 |
this.eventHandler_ = null; |
| 259 |
} |
| 260 |
}; |
| 261 |
|
| 262 |
|
| 263 |
/** @override */ |
| 264 |
goog.ui.LabelInput.prototype.disposeInternal = function() { |
| 265 |
goog.ui.LabelInput.superClass_.disposeInternal.call(this); |
| 266 |
this.detachEvents_(); |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
/** |
| 271 |
* The CSS class name to add to the input when the user has not entered a |
| 272 |
* value. |
| 273 |
*/ |
| 274 |
goog.ui.LabelInput.prototype.labelCssClassName = |
| 275 |
goog.getCssName('label-input-label'); |
| 276 |
|
| 277 |
|
| 278 |
/** |
| 279 |
* Handler for the focus event. |
| 280 |
* @param {goog.events.Event} e The event object passed in to the event handler. |
| 281 |
* @private |
| 282 |
*/ |
| 283 |
goog.ui.LabelInput.prototype.handleFocus_ = function(e) { |
| 284 |
this.hasFocus_ = true; |
| 285 |
var el = this.getElement(); |
| 286 |
goog.asserts.assert(el); |
| 287 |
goog.dom.classlist.remove(el, this.labelCssClassName); |
| 288 |
if (goog.ui.LabelInput.isPlaceholderSupported_()) { |
| 289 |
return; |
| 290 |
} |
| 291 |
if (!this.hasChanged() && !this.inFocusAndSelect_) { |
| 292 |
var me = this; |
| 293 |
var clearValue = function() { |
| 294 |
// Component could be disposed by the time this is called. |
| 295 |
if (me.getElement()) { |
| 296 |
me.getElement().value = ''; |
| 297 |
} |
| 298 |
}; |
| 299 |
if (goog.userAgent.IE) { |
| 300 |
goog.Timer.callOnce(clearValue, 10); |
| 301 |
} else { |
| 302 |
clearValue(); |
| 303 |
} |
| 304 |
} |
| 305 |
}; |
| 306 |
|
| 307 |
|
| 308 |
/** |
| 309 |
* Handler for the blur event. |
| 310 |
* @param {goog.events.Event} e The event object passed in to the event handler. |
| 311 |
* @private |
| 312 |
*/ |
| 313 |
goog.ui.LabelInput.prototype.handleBlur_ = function(e) { |
| 314 |
// We listen to the click event when we enter focusAndSelect mode so we can |
| 315 |
// fake an artificial focus when the user clicks on the input box. However, |
| 316 |
// if the user clicks on something else (and we lose focus), there is no |
| 317 |
// need for an artificial focus event. |
| 318 |
if (!goog.ui.LabelInput.isPlaceholderSupported_()) { |
| 319 |
this.eventHandler_.unlisten( |
| 320 |
this.getElement(), goog.events.EventType.CLICK, this.handleFocus_); |
| 321 |
this.ffKeyRestoreValue_ = null; |
| 322 |
} |
| 323 |
this.hasFocus_ = false; |
| 324 |
this.check_(); |
| 325 |
}; |
| 326 |
|
| 327 |
|
| 328 |
/** |
| 329 |
* Handler for key events in Firefox. |
| 330 |
* |
| 331 |
* If the escape key is pressed when a text input has not been changed manually |
| 332 |
* since being focused, the text input will revert to its previous value. |
| 333 |
* Firefox does not honor preventDefault for the escape key. The revert happens |
| 334 |
* after the keydown event and before every keypress. We therefore store the |
| 335 |
* element's value on keydown and restore it on keypress. The restore value is |
| 336 |
* nullified on keyup so that {@link #getValue} returns the correct value. |
| 337 |
* |
| 338 |
* IE and Chrome don't have this problem, Opera blurs in the input box |
| 339 |
* completely in a way that preventDefault on the escape key has no effect. |
| 340 |
* |
| 341 |
* @param {goog.events.BrowserEvent} e The event object passed in to |
| 342 |
* the event handler. |
| 343 |
* @private |
| 344 |
*/ |
| 345 |
goog.ui.LabelInput.prototype.handleEscapeKeys_ = function(e) { |
| 346 |
if (e.keyCode == 27) { |
| 347 |
if (e.type == goog.events.EventType.KEYDOWN) { |
| 348 |
this.ffKeyRestoreValue_ = this.getElement().value; |
| 349 |
} else if (e.type == goog.events.EventType.KEYPRESS) { |
| 350 |
this.getElement().value = /** @type {string} */ (this.ffKeyRestoreValue_); |
| 351 |
} else if (e.type == goog.events.EventType.KEYUP) { |
| 352 |
this.ffKeyRestoreValue_ = null; |
| 353 |
} |
| 354 |
e.preventDefault(); |
| 355 |
} |
| 356 |
}; |
| 357 |
|
| 358 |
|
| 359 |
/** |
| 360 |
* Handler for the submit event of the form element. |
| 361 |
* @param {goog.events.Event} e The event object passed in to the event handler. |
| 362 |
* @private |
| 363 |
*/ |
| 364 |
goog.ui.LabelInput.prototype.handleFormSubmit_ = function(e) { |
| 365 |
if (!this.hasChanged()) { |
| 366 |
this.getElement().value = ''; |
| 367 |
// allow form to be sent before restoring value |
| 368 |
goog.Timer.callOnce(this.handleAfterSubmit_, 10, this); |
| 369 |
} |
| 370 |
}; |
| 371 |
|
| 372 |
|
| 373 |
/** |
| 374 |
* Restore value after submit |
| 375 |
* @private |
| 376 |
*/ |
| 377 |
goog.ui.LabelInput.prototype.handleAfterSubmit_ = function() { |
| 378 |
if (!this.hasChanged()) { |
| 379 |
this.getElement().value = this.label_; |
| 380 |
} |
| 381 |
}; |
| 382 |
|
| 383 |
|
| 384 |
/** |
| 385 |
* Handler for the load event the window. This is needed because |
| 386 |
* IE sets defaultValue upon load. |
| 387 |
* @param {Event} e The event object passed in to the event handler. |
| 388 |
* @private |
| 389 |
*/ |
| 390 |
goog.ui.LabelInput.prototype.handleWindowLoad_ = function(e) { |
| 391 |
this.check_(); |
| 392 |
}; |
| 393 |
|
| 394 |
|
| 395 |
/** |
| 396 |
* @return {boolean} Whether the control is currently focused on. |
| 397 |
*/ |
| 398 |
goog.ui.LabelInput.prototype.hasFocus = function() { |
| 399 |
return this.hasFocus_; |
| 400 |
}; |
| 401 |
|
| 402 |
|
| 403 |
/** |
| 404 |
* @return {boolean} Whether the value has been changed by the user. |
| 405 |
*/ |
| 406 |
goog.ui.LabelInput.prototype.hasChanged = function() { |
| 407 |
return !!this.getElement() && this.getElement().value != '' && |
| 408 |
this.getElement().value != this.label_; |
| 409 |
}; |
| 410 |
|
| 411 |
|
| 412 |
/** |
| 413 |
* Clears the value of the input element without resetting the default text. |
| 414 |
*/ |
| 415 |
goog.ui.LabelInput.prototype.clear = function() { |
| 416 |
this.getElement().value = ''; |
| 417 |
|
| 418 |
// Reset ffKeyRestoreValue_ when non-null |
| 419 |
if (this.ffKeyRestoreValue_ != null) { |
| 420 |
this.ffKeyRestoreValue_ = ''; |
| 421 |
} |
| 422 |
}; |
| 423 |
|
| 424 |
|
| 425 |
/** |
| 426 |
* Clears the value of the input element and resets the default text. |
| 427 |
*/ |
| 428 |
goog.ui.LabelInput.prototype.reset = function() { |
| 429 |
if (this.hasChanged()) { |
| 430 |
this.clear(); |
| 431 |
this.check_(); |
| 432 |
} |
| 433 |
}; |
| 434 |
|
| 435 |
|
| 436 |
/** |
| 437 |
* Use this to set the value through script to ensure that the label state is |
| 438 |
* up to date |
| 439 |
* @param {string} s The new value for the input. |
| 440 |
*/ |
| 441 |
goog.ui.LabelInput.prototype.setValue = function(s) { |
| 442 |
if (this.ffKeyRestoreValue_ != null) { |
| 443 |
this.ffKeyRestoreValue_ = s; |
| 444 |
} |
| 445 |
this.getElement().value = s; |
| 446 |
this.check_(); |
| 447 |
}; |
| 448 |
|
| 449 |
|
| 450 |
/** |
| 451 |
* Returns the current value of the text box, returning an empty string if the |
| 452 |
* search box is the default value |
| 453 |
* @return {string} The value of the input box. |
| 454 |
*/ |
| 455 |
goog.ui.LabelInput.prototype.getValue = function() { |
| 456 |
if (this.ffKeyRestoreValue_ != null) { |
| 457 |
// Fix the Firefox from incorrectly reporting the value to calling code |
| 458 |
// that attached the listener to keypress before the labelinput |
| 459 |
return this.ffKeyRestoreValue_; |
| 460 |
} |
| 461 |
return this.hasChanged() ? /** @type {string} */ (this.getElement().value) : |
| 462 |
''; |
| 463 |
}; |
| 464 |
|
| 465 |
|
| 466 |
/** |
| 467 |
* Sets the label text as aria-label, and placeholder when supported. |
| 468 |
* @param {string} label The text to show as the label. |
| 469 |
*/ |
| 470 |
goog.ui.LabelInput.prototype.setLabel = function(label) { |
| 471 |
var labelInputElement = this.getElement(); |
| 472 |
|
| 473 |
if (goog.ui.LabelInput.isPlaceholderSupported_()) { |
| 474 |
if (labelInputElement) { |
| 475 |
labelInputElement.placeholder = label; |
| 476 |
} |
| 477 |
this.label_ = label; |
| 478 |
} else if (!this.hasChanged()) { |
| 479 |
// The this.hasChanged() call relies on non-placeholder behavior checking |
| 480 |
// prior to setting this.label_ - it also needs to happen prior to the |
| 481 |
// this.restoreLabel_() call. |
| 482 |
if (labelInputElement) { |
| 483 |
labelInputElement.value = ''; |
| 484 |
} |
| 485 |
this.label_ = label; |
| 486 |
this.restoreLabel_(); |
| 487 |
} |
| 488 |
// Check if this has been called before DOM structure building |
| 489 |
if (labelInputElement) { |
| 490 |
goog.a11y.aria.setState( |
| 491 |
labelInputElement, goog.a11y.aria.State.LABEL, this.label_); |
| 492 |
} |
| 493 |
}; |
| 494 |
|
| 495 |
|
| 496 |
/** |
| 497 |
* @return {string} The text to show as the label. |
| 498 |
*/ |
| 499 |
goog.ui.LabelInput.prototype.getLabel = function() { |
| 500 |
return this.label_; |
| 501 |
}; |
| 502 |
|
| 503 |
|
| 504 |
/** |
| 505 |
* Checks the state of the input element |
| 506 |
* @private |
| 507 |
*/ |
| 508 |
goog.ui.LabelInput.prototype.check_ = function() { |
| 509 |
var labelInputElement = this.getElement(); |
| 510 |
goog.asserts.assert( |
| 511 |
labelInputElement, 'The label input element cannot be null.'); |
| 512 |
if (!goog.ui.LabelInput.isPlaceholderSupported_()) { |
| 513 |
// if we haven't got a form yet try now |
| 514 |
this.attachEventsToForm_(); |
| 515 |
} else if (this.getElement().placeholder != this.label_) { |
| 516 |
this.getElement().placeholder = this.label_; |
| 517 |
} |
| 518 |
goog.a11y.aria.setState( |
| 519 |
labelInputElement, goog.a11y.aria.State.LABEL, this.label_); |
| 520 |
|
| 521 |
if (!this.hasChanged()) { |
| 522 |
if (!this.inFocusAndSelect_ && !this.hasFocus_) { |
| 523 |
var el = this.getElement(); |
| 524 |
goog.asserts.assert(el); |
| 525 |
goog.dom.classlist.add(el, this.labelCssClassName); |
| 526 |
} |
| 527 |
|
| 528 |
// Allow browser to catchup with CSS changes before restoring the label. |
| 529 |
if (!goog.ui.LabelInput.isPlaceholderSupported_()) { |
| 530 |
goog.Timer.callOnce(this.restoreLabel_, this.labelRestoreDelayMs, this); |
| 531 |
} |
| 532 |
} else { |
| 533 |
var el = this.getElement(); |
| 534 |
goog.asserts.assert(el); |
| 535 |
goog.dom.classlist.remove(el, this.labelCssClassName); |
| 536 |
} |
| 537 |
}; |
| 538 |
|
| 539 |
|
| 540 |
/** |
| 541 |
* This method focuses the input and selects all the text. If the value hasn't |
| 542 |
* changed it will set the value to the label so that the label text is |
| 543 |
* selected. |
| 544 |
*/ |
| 545 |
goog.ui.LabelInput.prototype.focusAndSelect = function() { |
| 546 |
// We need to check whether the input has changed before focusing |
| 547 |
var hc = this.hasChanged(); |
| 548 |
this.inFocusAndSelect_ = true; |
| 549 |
this.getElement().focus(); |
| 550 |
if (!hc && !goog.ui.LabelInput.isPlaceholderSupported_()) { |
| 551 |
this.getElement().value = this.label_; |
| 552 |
} |
| 553 |
this.getElement().select(); |
| 554 |
|
| 555 |
// Since the object now has focus, we won't get a focus event when they |
| 556 |
// click in the input element. The expected behavior when you click on |
| 557 |
// the default text is that it goes away and allows you to type...so we |
| 558 |
// have to fire an artificial focus event when we're in focusAndSelect mode. |
| 559 |
if (goog.ui.LabelInput.isPlaceholderSupported_()) { |
| 560 |
return; |
| 561 |
} |
| 562 |
if (this.eventHandler_) { |
| 563 |
this.eventHandler_.listenOnce( |
| 564 |
this.getElement(), goog.events.EventType.CLICK, this.handleFocus_); |
| 565 |
} |
| 566 |
|
| 567 |
// set to false in timer to let IE trigger the focus event |
| 568 |
goog.Timer.callOnce(this.focusAndSelect_, 10, this); |
| 569 |
}; |
| 570 |
|
| 571 |
|
| 572 |
/** |
| 573 |
* Enables/Disables the label input. |
| 574 |
* @param {boolean} enabled Whether to enable (true) or disable (false) the |
| 575 |
* label input. |
| 576 |
*/ |
| 577 |
goog.ui.LabelInput.prototype.setEnabled = function(enabled) { |
| 578 |
this.getElement().disabled = !enabled; |
| 579 |
var el = this.getElement(); |
| 580 |
goog.asserts.assert(el); |
| 581 |
goog.dom.classlist.enable( |
| 582 |
el, goog.getCssName(this.labelCssClassName, 'disabled'), !enabled); |
| 583 |
}; |
| 584 |
|
| 585 |
|
| 586 |
/** |
| 587 |
* @return {boolean} True if the label input is enabled, false otherwise. |
| 588 |
*/ |
| 589 |
goog.ui.LabelInput.prototype.isEnabled = function() { |
| 590 |
return !this.getElement().disabled; |
| 591 |
}; |
| 592 |
|
| 593 |
|
| 594 |
/** |
| 595 |
* @private |
| 596 |
*/ |
| 597 |
goog.ui.LabelInput.prototype.focusAndSelect_ = function() { |
| 598 |
this.inFocusAndSelect_ = false; |
| 599 |
}; |
| 600 |
|
| 601 |
|
| 602 |
/** |
| 603 |
* Sets the value of the input element to label. |
| 604 |
* @private |
| 605 |
*/ |
| 606 |
goog.ui.LabelInput.prototype.restoreLabel_ = function() { |
| 607 |
// Check again in case something changed since this was scheduled. |
| 608 |
// We check that the element is still there since this is called by a timer |
| 609 |
// and the dispose method may have been called prior to this. |
| 610 |
if (this.getElement() && !this.hasChanged() && !this.hasFocus_) { |
| 611 |
this.getElement().value = this.label_; |
| 612 |
} |
| 613 |
}; |