| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 /** @interface */ | |
| 6 var SearchFieldDelegate = function() {}; | |
| 7 | |
| 8 SearchFieldDelegate.prototype = { | |
| 9 /** | |
| 10 * @param {string} value | |
| 11 */ | |
| 12 onSearchTermSearch: assertNotReached, | |
| 13 }; | |
| 14 | |
| 15 var SearchField = Polymer({ | 5 var SearchField = Polymer({ |
| 16 is: 'cr-search-field', | 6 is: 'cr-search-field', |
| 17 | 7 behaviors: [CrSearchFieldBehavior] |
| 18 properties: { | |
| 19 label: { | |
| 20 type: String, | |
| 21 value: '', | |
| 22 }, | |
| 23 | |
| 24 clearLabel: { | |
| 25 type: String, | |
| 26 value: '', | |
| 27 }, | |
| 28 | |
| 29 showingSearch_: { | |
| 30 type: Boolean, | |
| 31 value: false, | |
| 32 observer: 'showingSearchChanged_', | |
| 33 }, | |
| 34 }, | |
| 35 | |
| 36 /** | |
| 37 * Returns the value of the search field. | |
| 38 * @return {string} | |
| 39 */ | |
| 40 getValue: function() { | |
| 41 var searchInput = this.getSearchInput_(); | |
| 42 return searchInput ? searchInput.value : ''; | |
| 43 }, | |
| 44 | |
| 45 /** | |
| 46 * Sets the value of the search field, if it exists. | |
| 47 * @param {string} value | |
| 48 */ | |
| 49 setValue: function(value) { | |
| 50 var searchInput = this.getSearchInput_(); | |
| 51 if (searchInput) | |
| 52 searchInput.value = value; | |
| 53 }, | |
| 54 | |
| 55 /** @param {SearchFieldDelegate} delegate */ | |
| 56 setDelegate: function(delegate) { | |
| 57 this.delegate_ = delegate; | |
| 58 }, | |
| 59 | |
| 60 /** @return {Promise<boolean>} */ | |
| 61 showAndFocus: function() { | |
| 62 this.showingSearch_ = true; | |
| 63 return this.focus_(); | |
| 64 }, | |
| 65 | |
| 66 /** | |
| 67 * @return {Promise<boolean>} | |
| 68 * @private | |
| 69 */ | |
| 70 focus_: function() { | |
| 71 return new Promise(function(resolve) { | |
| 72 this.async(function() { | |
| 73 if (this.showingSearch_) { | |
| 74 var searchInput = this.getSearchInput_(); | |
| 75 if (searchInput) | |
| 76 searchInput.focus(); | |
| 77 } | |
| 78 resolve(this.showingSearch_); | |
| 79 }); | |
| 80 }.bind(this)); | |
| 81 }, | |
| 82 | |
| 83 /** | |
| 84 * @return {?Element} | |
| 85 * @private | |
| 86 */ | |
| 87 getSearchInput_: function() { | |
| 88 return this.$$('#search-input'); | |
| 89 }, | |
| 90 | |
| 91 /** @private */ | |
| 92 onSearchTermSearch_: function() { | |
| 93 if (this.delegate_) | |
| 94 this.delegate_.onSearchTermSearch(this.getValue()); | |
| 95 }, | |
| 96 | |
| 97 /** @private */ | |
| 98 onSearchTermKeydown_: function(e) { | |
| 99 if (e.keyIdentifier == 'U+001B') // Escape. | |
| 100 this.showingSearch_ = false; | |
| 101 }, | |
| 102 | |
| 103 /** @private */ | |
| 104 showingSearchChanged_: function() { | |
| 105 if (this.showingSearch_) { | |
| 106 this.focus_(); | |
| 107 return; | |
| 108 } | |
| 109 | |
| 110 var searchInput = this.getSearchInput_(); | |
| 111 if (!searchInput) | |
| 112 return; | |
| 113 | |
| 114 searchInput.value = ''; | |
| 115 this.onSearchTermSearch_(); | |
| 116 }, | |
| 117 | |
| 118 /** @private */ | |
| 119 toggleShowingSearch_: function() { | |
| 120 this.showingSearch_ = !this.showingSearch_; | |
| 121 }, | |
| 122 }); | 8 }); |
| OLD | NEW |