diff options
Diffstat (limited to 'chromium/ui/webui/resources/cr_elements/cr_search_field/cr_search_field.js')
-rw-r--r-- | chromium/ui/webui/resources/cr_elements/cr_search_field/cr_search_field.js | 79 |
1 files changed, 63 insertions, 16 deletions
diff --git a/chromium/ui/webui/resources/cr_elements/cr_search_field/cr_search_field.js b/chromium/ui/webui/resources/cr_elements/cr_search_field/cr_search_field.js index e24e3093538..a507fc34f66 100644 --- a/chromium/ui/webui/resources/cr_elements/cr_search_field/cr_search_field.js +++ b/chromium/ui/webui/resources/cr_elements/cr_search_field/cr_search_field.js @@ -29,21 +29,63 @@ var SearchField = Polymer({ showingSearch_: { type: Boolean, value: false, + observer: 'showingSearchChanged_', }, }, + /** + * Returns the value of the search field. + * @return {string} + */ + getValue: function() { + var searchInput = this.getSearchInput_(); + return searchInput ? searchInput.value : ''; + }, + + /** + * Sets the value of the search field, if it exists. + * @param {string} value + */ + setValue: function(value) { + var searchInput = this.getSearchInput_(); + if (searchInput) + searchInput.value = value; + }, + /** @param {SearchFieldDelegate} delegate */ setDelegate: function(delegate) { this.delegate_ = delegate; }, + /** @return {Promise<boolean>} */ + showAndFocus: function() { + this.showingSearch_ = true; + return this.focus_(); + }, + /** - * Returns the value of the search field. - * @return {string} + * @return {Promise<boolean>} + * @private */ - getValue: function() { - var searchInput = this.$$('#search-input'); - return searchInput ? searchInput.value : ''; + focus_: function() { + return new Promise(function(resolve) { + this.async(function() { + if (this.showingSearch_) { + var searchInput = this.getSearchInput_(); + if (searchInput) + searchInput.focus(); + } + resolve(this.showingSearch_); + }); + }.bind(this)); + }, + + /** + * @return {?Element} + * @private + */ + getSearchInput_: function() { + return this.$$('#search-input'); }, /** @private */ @@ -54,22 +96,27 @@ var SearchField = Polymer({ /** @private */ onSearchTermKeydown_: function(e) { - assert(this.showingSearch_); if (e.keyIdentifier == 'U+001B') // Escape. - this.toggleShowingSearch_(); + this.showingSearch_ = false; + }, + + /** @private */ + showingSearchChanged_: function() { + if (this.showingSearch_) { + this.focus_(); + return; + } + + var searchInput = this.getSearchInput_(); + if (!searchInput) + return; + + searchInput.value = ''; + this.onSearchTermSearch_(); }, /** @private */ toggleShowingSearch_: function() { this.showingSearch_ = !this.showingSearch_; - this.async(function() { - var searchInput = this.$$('#search-input'); - if (this.showingSearch_) { - searchInput.focus(); - } else { - searchInput.value = ''; - this.onSearchTermSearch_(); - } - }); }, }); |