| 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 */ | 5 /** @interface */ |
| 6 var SearchFieldDelegate = function() {}; | 6 var SearchFieldDelegate = function() {}; |
| 7 | 7 |
| 8 SearchFieldDelegate.prototype = { | 8 SearchFieldDelegate.prototype = { |
| 9 /** | 9 /** |
| 10 * @param {string} value | 10 * @param {string} value |
| 11 */ | 11 */ |
| 12 onSearchTermSearch: assertNotReached, | 12 onSearchTermSearch: assertNotReached, |
| 13 }; | 13 }; |
| 14 | 14 |
| 15 var SearchField = Polymer({ | 15 var SearchField = Polymer({ |
| 16 is: 'cr-search-field', | 16 is: 'cr-search-field', |
| 17 | |
| 18 properties: { | 17 properties: { |
| 19 label: { | 18 label: { |
| 20 type: String, | 19 type: String, |
| 21 value: '', | 20 value: '', |
| 22 }, | 21 }, |
| 23 | 22 |
| 24 clearLabel: { | 23 clearLabel: { |
| 25 type: String, | 24 type: String, |
| 26 value: '', | 25 value: '', |
| 27 }, | 26 }, |
| 28 | 27 |
| 29 showingSearch_: { | 28 showingSearch: { |
| 30 type: Boolean, | 29 type: Boolean, |
| 31 value: false, | 30 value: false, |
| 31 notify: true, |
| 32 observer: 'showingSearchChanged_', | 32 observer: 'showingSearchChanged_', |
| 33 reflectToAttribute: true |
| 33 }, | 34 }, |
| 35 |
| 36 /** @private */ |
| 37 hasSearchText_: { |
| 38 type: Boolean, |
| 39 } |
| 40 }, |
| 41 |
| 42 listeners: { |
| 43 'tap': 'onTap_' |
| 44 }, |
| 45 |
| 46 onTap_: function(e) { |
| 47 if (e.target != this.$.clearSearch) |
| 48 this.showAndFocus(); |
| 34 }, | 49 }, |
| 35 | 50 |
| 36 /** | 51 /** |
| 37 * Returns the value of the search field. | 52 * @return {string} The value of the search field. |
| 38 * @return {string} | |
| 39 */ | 53 */ |
| 40 getValue: function() { | 54 getValue: function() { |
| 41 var searchInput = this.getSearchInput_(); | 55 var searchInput = this.getSearchInput_(); |
| 42 return searchInput ? searchInput.value : ''; | 56 return searchInput ? searchInput.value : ''; |
| 43 }, | 57 }, |
| 44 | 58 |
| 45 /** | 59 /** |
| 46 * Sets the value of the search field, if it exists. | 60 * Sets the value of the search field, if it exists. |
| 47 * @param {string} value | 61 * @param {string} value |
| 48 */ | 62 */ |
| 49 setValue: function(value) { | 63 setValue: function(value) { |
| 50 var searchInput = this.getSearchInput_(); | 64 var searchInput = this.getSearchInput_(); |
| 51 if (searchInput) | 65 if (searchInput) |
| 52 searchInput.value = value; | 66 searchInput.value = value; |
| 67 this.hasSearchText_ = this.getValue() != ''; |
| 53 }, | 68 }, |
| 54 | 69 |
| 55 /** @param {SearchFieldDelegate} delegate */ | 70 /** @param {SearchFieldDelegate} delegate */ |
| 56 setDelegate: function(delegate) { | 71 setDelegate: function(delegate) { |
| 57 this.delegate_ = delegate; | 72 this.delegate_ = delegate; |
| 58 }, | 73 }, |
| 59 | 74 |
| 60 /** @return {Promise<boolean>} */ | 75 /** @return {!Promise<boolean>} */ |
| 61 showAndFocus: function() { | 76 showAndFocus: function() { |
| 62 this.showingSearch_ = true; | 77 this.showingSearch = true; |
| 63 return this.focus_(); | 78 return this.focus_(); |
| 64 }, | 79 }, |
| 65 | 80 |
| 66 /** | 81 /** |
| 67 * @return {Promise<boolean>} | 82 * @return {!Promise<boolean>} |
| 68 * @private | 83 * @private |
| 69 */ | 84 */ |
| 70 focus_: function() { | 85 focus_: function() { |
| 71 return new Promise(function(resolve) { | 86 return new Promise(function(resolve) { |
| 72 this.async(function() { | 87 this.async(function() { |
| 73 if (this.showingSearch_) { | 88 if (this.showingSearch) { |
| 74 var searchInput = this.getSearchInput_(); | 89 var searchInput = this.getSearchInput_(); |
| 75 if (searchInput) | 90 if (searchInput) |
| 76 searchInput.focus(); | 91 searchInput.focus(); |
| 77 } | 92 } |
| 78 resolve(this.showingSearch_); | 93 resolve(this.showingSearch); |
| 79 }); | 94 }); |
| 80 }.bind(this)); | 95 }.bind(this)); |
| 81 }, | 96 }, |
| 82 | 97 |
| 83 /** | 98 /** |
| 84 * @return {?Element} | 99 * @return {?Element} |
| 85 * @private | 100 * @private |
| 86 */ | 101 */ |
| 87 getSearchInput_: function() { | 102 getSearchInput_: function() { |
| 88 return this.$$('#search-input'); | 103 return this.$.searchInput; |
| 89 }, | 104 }, |
| 90 | 105 |
| 91 /** @private */ | 106 /** @private */ |
| 92 onSearchTermSearch_: function() { | 107 onSearchTermSearch_: function() { |
| 108 this.hasSearchText_ = this.getValue() != ''; |
| 93 if (this.delegate_) | 109 if (this.delegate_) |
| 94 this.delegate_.onSearchTermSearch(this.getValue()); | 110 this.delegate_.onSearchTermSearch(this.getValue()); |
| 95 }, | 111 }, |
| 96 | 112 |
| 97 /** @private */ | 113 /** @private */ |
| 98 onSearchTermKeydown_: function(e) { | 114 onSearchTermKeydown_: function(e) { |
| 99 if (e.keyIdentifier == 'U+001B') // Escape. | 115 if (e.keyIdentifier == 'U+001B') // Escape. |
| 100 this.showingSearch_ = false; | 116 this.showingSearch = false; |
| 101 }, | 117 }, |
| 102 | 118 |
| 103 /** @private */ | 119 /** @private */ |
| 104 showingSearchChanged_: function() { | 120 showingSearchChanged_: function() { |
| 105 if (this.showingSearch_) { | 121 if (this.showingSearch) { |
| 106 this.focus_(); | 122 this.focus_(); |
| 107 return; | 123 return; |
| 108 } | 124 } |
| 109 | 125 |
| 110 var searchInput = this.getSearchInput_(); | 126 var searchInput = this.getSearchInput_(); |
| 111 if (!searchInput) | 127 if (!searchInput) |
| 112 return; | 128 return; |
| 113 | 129 |
| 114 searchInput.value = ''; | 130 searchInput.value = ''; |
| 115 this.onSearchTermSearch_(); | 131 this.onSearchTermSearch_(); |
| 116 }, | 132 }, |
| 117 | 133 |
| 118 /** @private */ | 134 /** @private */ |
| 119 toggleShowingSearch_: function() { | 135 toggleShowingSearch_: function() { |
| 120 this.showingSearch_ = !this.showingSearch_; | 136 this.showingSearch = !this.showingSearch; |
| 121 }, | 137 } |
| 122 }); | 138 }); |
| OLD | NEW |