Chromium Code Reviews| Index: ui/webui/resources/cr_elements/cr_search_field/cr_search_field_behavior.js |
| diff --git a/ui/webui/resources/cr_elements/cr_search_field/cr_search_field.js b/ui/webui/resources/cr_elements/cr_search_field/cr_search_field_behavior.js |
| similarity index 57% |
| copy from ui/webui/resources/cr_elements/cr_search_field/cr_search_field.js |
| copy to ui/webui/resources/cr_elements/cr_search_field/cr_search_field_behavior.js |
| index a507fc34f663939c5889c21c3a7455d533265b6d..d0e31b4e7b82099ac274538206e79b8355ee53e1 100644 |
| --- a/ui/webui/resources/cr_elements/cr_search_field/cr_search_field.js |
| +++ b/ui/webui/resources/cr_elements/cr_search_field/cr_search_field_behavior.js |
| @@ -1,4 +1,4 @@ |
| -// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -12,9 +12,12 @@ SearchFieldDelegate.prototype = { |
| onSearchTermSearch: assertNotReached, |
| }; |
| -var SearchField = Polymer({ |
| - is: 'cr-search-field', |
| - |
| +/** |
| + * Implements an incremental search field which can be shown and hidden. |
| + * Canonical implementation is <cr-search-field>. |
| + * @polymerBehavior |
| + */ |
| +var CrSearchFieldBehavior = { |
| properties: { |
| label: { |
| type: String, |
| @@ -26,30 +29,35 @@ var SearchField = Polymer({ |
| value: '', |
| }, |
| - showingSearch_: { |
| + showingSearch: { |
| type: Boolean, |
| value: false, |
| + notify: true, |
| observer: 'showingSearchChanged_', |
| + reflectToAttribute: true |
| }, |
| + |
| + hasSearchText: { |
|
dpapad
2016/05/26 00:42:52
Nit: I think this is equivalent to the shorthand
tsergeant
2016/05/26 01:34:51
Done.
|
| + type: Boolean, |
| + } |
| }, |
| /** |
| - * Returns the value of the search field. |
| - * @return {string} |
| + * @return {!string} The value of the search field. |
|
dpapad
2016/05/26 00:42:52
"!" is implied for primitive types (number, boolea
tsergeant
2016/05/26 01:34:51
Makes sense, done.
|
| */ |
| getValue: function() { |
| - var searchInput = this.getSearchInput_(); |
| - return searchInput ? searchInput.value : ''; |
| + return this.$.searchInput.value; |
| }, |
| /** |
| * Sets the value of the search field, if it exists. |
| - * @param {string} value |
| + * @param {!string} value |
| */ |
| setValue: function(value) { |
| - var searchInput = this.getSearchInput_(); |
| - if (searchInput) |
| - searchInput.value = value; |
| + // Use bindValue when setting the input value so that changes propagate |
| + // correctly. |
| + this.$.searchInput.bindValue = value; |
| + this.hasSearchText = value != ''; |
| }, |
| /** @param {SearchFieldDelegate} delegate */ |
| @@ -57,39 +65,30 @@ var SearchField = Polymer({ |
| this.delegate_ = delegate; |
| }, |
| - /** @return {Promise<boolean>} */ |
| + /** @return {!Promise<boolean>} */ |
| showAndFocus: function() { |
| - this.showingSearch_ = true; |
| + this.showingSearch = true; |
| return this.focus_(); |
| }, |
| /** |
| - * @return {Promise<boolean>} |
| + * @return {!Promise<boolean>} |
| * @private |
| */ |
| focus_: function() { |
| return new Promise(function(resolve) { |
| this.async(function() { |
| - if (this.showingSearch_) { |
| - var searchInput = this.getSearchInput_(); |
| - if (searchInput) |
| - searchInput.focus(); |
| + if (this.showingSearch) { |
| + this.$.searchInput.focus(); |
| } |
| - resolve(this.showingSearch_); |
| + resolve(this.showingSearch); |
| }); |
| }.bind(this)); |
| }, |
| - /** |
| - * @return {?Element} |
| - * @private |
| - */ |
| - getSearchInput_: function() { |
| - return this.$$('#search-input'); |
| - }, |
| - |
| /** @private */ |
| onSearchTermSearch_: function() { |
| + this.hasSearchText = this.getValue() != ''; |
| if (this.delegate_) |
| this.delegate_.onSearchTermSearch(this.getValue()); |
| }, |
| @@ -97,26 +96,22 @@ var SearchField = Polymer({ |
| /** @private */ |
| onSearchTermKeydown_: function(e) { |
| if (e.keyIdentifier == 'U+001B') // Escape. |
| - this.showingSearch_ = false; |
| + this.showingSearch = false; |
| }, |
| /** @private */ |
| showingSearchChanged_: function() { |
| - if (this.showingSearch_) { |
| + if (this.showingSearch) { |
| this.focus_(); |
| return; |
| } |
| - var searchInput = this.getSearchInput_(); |
| - if (!searchInput) |
| - return; |
| - |
| - searchInput.value = ''; |
| + this.setValue(''); |
| this.onSearchTermSearch_(); |
| }, |
| /** @private */ |
| toggleShowingSearch_: function() { |
| - this.showingSearch_ = !this.showingSearch_; |
| + this.showingSearch = !this.showingSearch; |
| }, |
| -}); |
| +}; |