| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 Polymer({ | 5 Polymer({ |
| 6 /** | 6 /** |
| 7 * This element is a one way bound interface that routes the page URL to | 7 * This element is a one way bound interface that routes the page URL to |
| 8 * the searchTerm and selectedId. Clients must initialize themselves by | 8 * the searchTerm and selectedId. Clients must initialize themselves by |
| 9 * reading the router's fields after attach. | 9 * reading the router's fields after attach. |
| 10 */ | 10 */ |
| 11 is: 'bookmarks-router', | 11 is: 'bookmarks-router', |
| 12 | 12 |
| 13 behaviors: [ |
| 14 bookmarks.StoreClient, |
| 15 ], |
| 16 |
| 13 properties: { | 17 properties: { |
| 14 // Parameter q is routed to the searchTerm. | 18 // Parameter q is routed to the searchTerm. |
| 15 // Parameter id is routed to the selectedId. | 19 // Parameter id is routed to the selectedId. |
| 16 queryParams_: Object, | 20 queryParams_: Object, |
| 17 | 21 |
| 18 searchTerm: { | 22 searchTerm: { |
| 19 type: String, | 23 type: String, |
| 20 observer: 'onSearchTermChanged_', | |
| 21 }, | 24 }, |
| 22 | 25 |
| 23 /** @type {?string} */ | 26 /** @type {?string} */ |
| 24 selectedId: { | 27 selectedId: { |
| 25 type: String, | 28 type: String, |
| 26 observer: 'onSelectedIdChanged_', | |
| 27 }, | 29 }, |
| 28 }, | 30 }, |
| 29 | 31 |
| 30 observers: [ | 32 observers: [ |
| 31 'onQueryChanged_(queryParams_.*)', | 33 'onQueryChanged_(queryParams_.q)', |
| 34 'onFolderChanged_(queryParams_.id)', |
| 35 'onStateChanged_(searchTerm, selectedId)', |
| 32 ], | 36 ], |
| 33 | 37 |
| 38 attached: function() { |
| 39 this.observe('selectedId', function(state) { |
| 40 return state.selectedFolder; |
| 41 }); |
| 42 this.observe('searchTerm', function(state) { |
| 43 return state.search.term; |
| 44 }); |
| 45 }, |
| 46 |
| 34 /** @private */ | 47 /** @private */ |
| 35 onQueryChanged_: function() { | 48 onQueryChanged_: function() { |
| 36 this.searchTerm = this.queryParams_.q || ''; | 49 var searchTerm = this.queryParams_.q || ''; |
| 37 this.selectedId = this.queryParams_.id; | 50 if (searchTerm && searchTerm != this.searchTerm) { |
| 38 | 51 this.searchTerm = searchTerm; |
| 39 if (this.searchTerm) | 52 this.dispatch(bookmarks.actions.setSearchTerm(searchTerm)); |
| 40 this.fire('search-term-changed', this.searchTerm); | 53 } |
| 41 else | |
| 42 this.fire('selected-folder-changed', this.selectedId); | |
| 43 }, | 54 }, |
| 44 | 55 |
| 45 /** @private */ | 56 /** @private */ |
| 46 onSelectedIdChanged_: function() { | 57 onFolderChanged_: function() { |
| 47 this.set('queryParams_.id', this.selectedId || null); | 58 var selectedId = this.queryParams_.id; |
| 59 if (selectedId && selectedId != this.selectedId) { |
| 60 this.selectedId = selectedId; |
| 61 this.dispatch(bookmarks.actions.selectFolder(selectedId)); |
| 62 } |
| 48 }, | 63 }, |
| 49 | 64 |
| 50 /** @private */ | 65 /** @private */ |
| 51 onSearchTermChanged_: function() { | 66 onStateChanged_: function() { |
| 52 this.set('queryParams_.q', this.searchTerm || null); | 67 this.debounce('updateQueryParams', this.updateQueryParams_.bind(this)); |
| 68 }, |
| 69 |
| 70 /** @private */ |
| 71 updateQueryParams_: function() { |
| 72 if (this.searchTerm) |
| 73 this.queryParams_ = {q: this.searchTerm}; |
| 74 else |
| 75 this.queryParams_ = {id: this.selectedId}; |
| 53 }, | 76 }, |
| 54 }); | 77 }); |
| OLD | NEW |