123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613 |
- goog.provide('goog.ui.LabelInput');
- goog.require('goog.Timer');
- goog.require('goog.a11y.aria');
- goog.require('goog.a11y.aria.State');
- goog.require('goog.asserts');
- goog.require('goog.dom');
- goog.require('goog.dom.InputType');
- goog.require('goog.dom.TagName');
- goog.require('goog.dom.classlist');
- goog.require('goog.events.EventHandler');
- goog.require('goog.events.EventType');
- goog.require('goog.ui.Component');
- goog.require('goog.userAgent');
- goog.ui.LabelInput = function(opt_label, opt_domHelper) {
- goog.ui.Component.call(this, opt_domHelper);
-
- this.label_ = opt_label || '';
- };
- goog.inherits(goog.ui.LabelInput, goog.ui.Component);
- goog.tagUnsealableClass(goog.ui.LabelInput);
- goog.ui.LabelInput.prototype.ffKeyRestoreValue_ = null;
- goog.ui.LabelInput.prototype.labelRestoreDelayMs = 10;
- goog.ui.LabelInput.prototype.inFocusAndSelect_;
- goog.ui.LabelInput.prototype.formAttached_;
- goog.ui.LabelInput.supportsPlaceholder_;
- goog.ui.LabelInput.isPlaceholderSupported_ = function() {
- if (!goog.isDefAndNotNull(goog.ui.LabelInput.supportsPlaceholder_)) {
- goog.ui.LabelInput.supportsPlaceholder_ =
- ('placeholder' in goog.dom.createElement(goog.dom.TagName.INPUT));
- }
- return goog.ui.LabelInput.supportsPlaceholder_;
- };
- goog.ui.LabelInput.prototype.eventHandler_;
- goog.ui.LabelInput.prototype.hasFocus_ = false;
- goog.ui.LabelInput.prototype.createDom = function() {
- this.setElementInternal(
- this.getDomHelper().createDom(
- goog.dom.TagName.INPUT, {'type': goog.dom.InputType.TEXT}));
- };
- goog.ui.LabelInput.prototype.decorateInternal = function(element) {
- goog.ui.LabelInput.superClass_.decorateInternal.call(this, element);
- if (!this.label_) {
- this.label_ = element.getAttribute('label') || '';
- }
-
- if (goog.dom.getActiveElement(goog.dom.getOwnerDocument(element)) ==
- element) {
- this.hasFocus_ = true;
- var el = this.getElement();
- goog.asserts.assert(el);
- goog.dom.classlist.remove(el, this.labelCssClassName);
- }
- if (goog.ui.LabelInput.isPlaceholderSupported_()) {
- this.getElement().placeholder = this.label_;
- }
- var labelInputElement = this.getElement();
- goog.asserts.assert(
- labelInputElement, 'The label input element cannot be null.');
- goog.a11y.aria.setState(
- labelInputElement, goog.a11y.aria.State.LABEL, this.label_);
- };
- goog.ui.LabelInput.prototype.enterDocument = function() {
- goog.ui.LabelInput.superClass_.enterDocument.call(this);
- this.attachEvents_();
- this.check_();
-
-
- this.getElement().labelInput_ = this;
- };
- goog.ui.LabelInput.prototype.exitDocument = function() {
- goog.ui.LabelInput.superClass_.exitDocument.call(this);
- this.detachEvents_();
- this.getElement().labelInput_ = null;
- };
- goog.ui.LabelInput.prototype.attachEvents_ = function() {
- var eh = new goog.events.EventHandler(this);
- eh.listen(this.getElement(), goog.events.EventType.FOCUS, this.handleFocus_);
- eh.listen(this.getElement(), goog.events.EventType.BLUR, this.handleBlur_);
- if (goog.ui.LabelInput.isPlaceholderSupported_()) {
- this.eventHandler_ = eh;
- return;
- }
- if (goog.userAgent.GECKO) {
- eh.listen(
- this.getElement(),
- [
- goog.events.EventType.KEYPRESS, goog.events.EventType.KEYDOWN,
- goog.events.EventType.KEYUP
- ],
- this.handleEscapeKeys_);
- }
-
- var d = goog.dom.getOwnerDocument(this.getElement());
- var w = goog.dom.getWindow(d);
- eh.listen(w, goog.events.EventType.LOAD, this.handleWindowLoad_);
- this.eventHandler_ = eh;
- this.attachEventsToForm_();
- };
- goog.ui.LabelInput.prototype.attachEventsToForm_ = function() {
-
-
- if (!this.formAttached_ && this.eventHandler_ && this.getElement().form) {
- this.eventHandler_.listen(
- this.getElement().form, goog.events.EventType.SUBMIT,
- this.handleFormSubmit_);
- this.formAttached_ = true;
- }
- };
- goog.ui.LabelInput.prototype.detachEvents_ = function() {
- if (this.eventHandler_) {
- this.eventHandler_.dispose();
- this.eventHandler_ = null;
- }
- };
- goog.ui.LabelInput.prototype.disposeInternal = function() {
- goog.ui.LabelInput.superClass_.disposeInternal.call(this);
- this.detachEvents_();
- };
- goog.ui.LabelInput.prototype.labelCssClassName =
- goog.getCssName('label-input-label');
- goog.ui.LabelInput.prototype.handleFocus_ = function(e) {
- this.hasFocus_ = true;
- var el = this.getElement();
- goog.asserts.assert(el);
- goog.dom.classlist.remove(el, this.labelCssClassName);
- if (goog.ui.LabelInput.isPlaceholderSupported_()) {
- return;
- }
- if (!this.hasChanged() && !this.inFocusAndSelect_) {
- var me = this;
- var clearValue = function() {
-
- if (me.getElement()) {
- me.getElement().value = '';
- }
- };
- if (goog.userAgent.IE) {
- goog.Timer.callOnce(clearValue, 10);
- } else {
- clearValue();
- }
- }
- };
- goog.ui.LabelInput.prototype.handleBlur_ = function(e) {
-
-
-
-
- if (!goog.ui.LabelInput.isPlaceholderSupported_()) {
- this.eventHandler_.unlisten(
- this.getElement(), goog.events.EventType.CLICK, this.handleFocus_);
- this.ffKeyRestoreValue_ = null;
- }
- this.hasFocus_ = false;
- this.check_();
- };
- goog.ui.LabelInput.prototype.handleEscapeKeys_ = function(e) {
- if (e.keyCode == 27) {
- if (e.type == goog.events.EventType.KEYDOWN) {
- this.ffKeyRestoreValue_ = this.getElement().value;
- } else if (e.type == goog.events.EventType.KEYPRESS) {
- this.getElement().value = (this.ffKeyRestoreValue_);
- } else if (e.type == goog.events.EventType.KEYUP) {
- this.ffKeyRestoreValue_ = null;
- }
- e.preventDefault();
- }
- };
- goog.ui.LabelInput.prototype.handleFormSubmit_ = function(e) {
- if (!this.hasChanged()) {
- this.getElement().value = '';
-
- goog.Timer.callOnce(this.handleAfterSubmit_, 10, this);
- }
- };
- goog.ui.LabelInput.prototype.handleAfterSubmit_ = function() {
- if (!this.hasChanged()) {
- this.getElement().value = this.label_;
- }
- };
- goog.ui.LabelInput.prototype.handleWindowLoad_ = function(e) {
- this.check_();
- };
- goog.ui.LabelInput.prototype.hasFocus = function() {
- return this.hasFocus_;
- };
- goog.ui.LabelInput.prototype.hasChanged = function() {
- return !!this.getElement() && this.getElement().value != '' &&
- this.getElement().value != this.label_;
- };
- goog.ui.LabelInput.prototype.clear = function() {
- this.getElement().value = '';
-
- if (this.ffKeyRestoreValue_ != null) {
- this.ffKeyRestoreValue_ = '';
- }
- };
- goog.ui.LabelInput.prototype.reset = function() {
- if (this.hasChanged()) {
- this.clear();
- this.check_();
- }
- };
- goog.ui.LabelInput.prototype.setValue = function(s) {
- if (this.ffKeyRestoreValue_ != null) {
- this.ffKeyRestoreValue_ = s;
- }
- this.getElement().value = s;
- this.check_();
- };
- goog.ui.LabelInput.prototype.getValue = function() {
- if (this.ffKeyRestoreValue_ != null) {
-
-
- return this.ffKeyRestoreValue_;
- }
- return this.hasChanged() ? (this.getElement().value) :
- '';
- };
- goog.ui.LabelInput.prototype.setLabel = function(label) {
- var labelInputElement = this.getElement();
- if (goog.ui.LabelInput.isPlaceholderSupported_()) {
- if (labelInputElement) {
- labelInputElement.placeholder = label;
- }
- this.label_ = label;
- } else if (!this.hasChanged()) {
-
-
-
- if (labelInputElement) {
- labelInputElement.value = '';
- }
- this.label_ = label;
- this.restoreLabel_();
- }
-
- if (labelInputElement) {
- goog.a11y.aria.setState(
- labelInputElement, goog.a11y.aria.State.LABEL, this.label_);
- }
- };
- goog.ui.LabelInput.prototype.getLabel = function() {
- return this.label_;
- };
- goog.ui.LabelInput.prototype.check_ = function() {
- var labelInputElement = this.getElement();
- goog.asserts.assert(
- labelInputElement, 'The label input element cannot be null.');
- if (!goog.ui.LabelInput.isPlaceholderSupported_()) {
-
- this.attachEventsToForm_();
- } else if (this.getElement().placeholder != this.label_) {
- this.getElement().placeholder = this.label_;
- }
- goog.a11y.aria.setState(
- labelInputElement, goog.a11y.aria.State.LABEL, this.label_);
- if (!this.hasChanged()) {
- if (!this.inFocusAndSelect_ && !this.hasFocus_) {
- var el = this.getElement();
- goog.asserts.assert(el);
- goog.dom.classlist.add(el, this.labelCssClassName);
- }
-
- if (!goog.ui.LabelInput.isPlaceholderSupported_()) {
- goog.Timer.callOnce(this.restoreLabel_, this.labelRestoreDelayMs, this);
- }
- } else {
- var el = this.getElement();
- goog.asserts.assert(el);
- goog.dom.classlist.remove(el, this.labelCssClassName);
- }
- };
- goog.ui.LabelInput.prototype.focusAndSelect = function() {
-
- var hc = this.hasChanged();
- this.inFocusAndSelect_ = true;
- this.getElement().focus();
- if (!hc && !goog.ui.LabelInput.isPlaceholderSupported_()) {
- this.getElement().value = this.label_;
- }
- this.getElement().select();
-
-
-
-
- if (goog.ui.LabelInput.isPlaceholderSupported_()) {
- return;
- }
- if (this.eventHandler_) {
- this.eventHandler_.listenOnce(
- this.getElement(), goog.events.EventType.CLICK, this.handleFocus_);
- }
-
- goog.Timer.callOnce(this.focusAndSelect_, 10, this);
- };
- goog.ui.LabelInput.prototype.setEnabled = function(enabled) {
- this.getElement().disabled = !enabled;
- var el = this.getElement();
- goog.asserts.assert(el);
- goog.dom.classlist.enable(
- el, goog.getCssName(this.labelCssClassName, 'disabled'), !enabled);
- };
- goog.ui.LabelInput.prototype.isEnabled = function() {
- return !this.getElement().disabled;
- };
- goog.ui.LabelInput.prototype.focusAndSelect_ = function() {
- this.inFocusAndSelect_ = false;
- };
- goog.ui.LabelInput.prototype.restoreLabel_ = function() {
-
-
-
- if (this.getElement() && !this.hasChanged() && !this.hasFocus_) {
- this.getElement().value = this.label_;
- }
- };
|