| 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: [ | 13 behaviors: [ |
| 14 bookmarks.StoreClient, | 14 bookmarks.StoreClient, |
| 15 ], | 15 ], |
| 16 | 16 |
| 17 properties: { | 17 properties: { |
| 18 /** | 18 /** |
| 19 * Parameter q is routed to the searchTerm. | 19 * Parameter q is routed to the searchTerm. |
| 20 * Parameter id is routed to the selectedId. | 20 * Parameter id is routed to the selectedId. |
| 21 * @private | 21 * @private |
| 22 */ | 22 */ |
| 23 queryParams_: Object, | 23 queryParams_: Object, |
| 24 | 24 |
| 25 /** @private */ | 25 /** @private */ |
| 26 searchTerm_: String, | 26 searchTerm_: { |
| 27 type: String, |
| 28 value: '', |
| 29 }, |
| 27 | 30 |
| 28 /** @private {?string} */ | 31 /** @private {?string} */ |
| 29 selectedId_: String, | 32 selectedId_: String, |
| 30 }, | 33 }, |
| 31 | 34 |
| 32 observers: [ | 35 observers: [ |
| 33 'onQueryChanged_(queryParams_.q)', | 36 'onQueryParamsChanged_(queryParams_)', |
| 34 'onFolderChanged_(queryParams_.id)', | |
| 35 'onStateChanged_(searchTerm_, selectedId_)', | 37 'onStateChanged_(searchTerm_, selectedId_)', |
| 36 ], | 38 ], |
| 37 | 39 |
| 38 attached: function() { | 40 attached: function() { |
| 39 this.watch('selectedId_', function(state) { | 41 this.watch('selectedId_', function(state) { |
| 40 return state.selectedFolder; | 42 return state.selectedFolder; |
| 41 }); | 43 }); |
| 42 this.watch('searchTerm_', function(state) { | 44 this.watch('searchTerm_', function(state) { |
| 43 return state.search.term; | 45 return state.search.term; |
| 44 }); | 46 }); |
| 45 this.updateFromStore(); | 47 this.updateFromStore(); |
| 46 }, | 48 }, |
| 47 | 49 |
| 48 /** @private */ | 50 /** @private */ |
| 49 onQueryChanged_: function() { | 51 onQueryParamsChanged_: function() { |
| 50 var searchTerm = this.queryParams_.q || ''; | 52 var searchTerm = this.queryParams_.q || ''; |
| 51 if (searchTerm && searchTerm != this.searchTerm_) { | 53 var selectedId = this.queryParams_.id; |
| 54 if (!selectedId && !searchTerm) |
| 55 selectedId = BOOKMARKS_BAR_ID; |
| 56 |
| 57 if (searchTerm != this.searchTerm_) { |
| 52 this.searchTerm_ = searchTerm; | 58 this.searchTerm_ = searchTerm; |
| 53 this.dispatch(bookmarks.actions.setSearchTerm(searchTerm)); | 59 this.dispatch(bookmarks.actions.setSearchTerm(searchTerm)); |
| 54 } | 60 } |
| 55 }, | |
| 56 | 61 |
| 57 /** @private */ | |
| 58 onFolderChanged_: function() { | |
| 59 var selectedId = this.queryParams_.id; | |
| 60 if (selectedId && selectedId != this.selectedId_) { | 62 if (selectedId && selectedId != this.selectedId_) { |
| 61 this.selectedId_ = selectedId; | 63 this.selectedId_ = selectedId; |
| 62 // Need to dispatch a deferred action so that during page load | 64 // Need to dispatch a deferred action so that during page load |
| 63 // `this.getState()` will only evaluate after the Store is initialized. | 65 // `this.getState()` will only evaluate after the Store is initialized. |
| 64 this.dispatchAsync(function(dispatch) { | 66 this.dispatchAsync(function(dispatch) { |
| 65 dispatch( | 67 dispatch( |
| 66 bookmarks.actions.selectFolder(selectedId, this.getState().nodes)); | 68 bookmarks.actions.selectFolder(selectedId, this.getState().nodes)); |
| 67 }.bind(this)); | 69 }.bind(this)); |
| 68 } | 70 } |
| 69 }, | 71 }, |
| 70 | 72 |
| 71 /** @private */ | 73 /** @private */ |
| 72 onStateChanged_: function() { | 74 onStateChanged_: function() { |
| 73 this.debounce('updateQueryParams', this.updateQueryParams_.bind(this)); | 75 this.debounce('updateQueryParams', this.updateQueryParams_.bind(this)); |
| 74 }, | 76 }, |
| 75 | 77 |
| 76 /** @private */ | 78 /** @private */ |
| 77 updateQueryParams_: function() { | 79 updateQueryParams_: function() { |
| 78 if (this.searchTerm_) | 80 if (this.searchTerm_) |
| 79 this.queryParams_ = {q: this.searchTerm_}; | 81 this.queryParams_ = {q: this.searchTerm_}; |
| 82 else if (this.selectedId_ != BOOKMARKS_BAR_ID) |
| 83 this.queryParams_ = {id: this.selectedId_}; |
| 80 else | 84 else |
| 81 this.queryParams_ = {id: this.selectedId_}; | 85 this.queryParams_ = {}; |
| 82 }, | 86 }, |
| 83 }); | 87 }); |
| OLD | NEW |