Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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 is: 'history-router', | 6 is: 'history-router', |
| 7 | 7 |
| 8 properties: { | 8 properties: { |
| 9 selectedPage: { | 9 selectedPage: { |
| 10 type: String, | 10 type: String, |
| 11 observer: 'serializePath_', | |
| 12 notify: true, | 11 notify: true, |
| 13 }, | 12 observer: 'selectedPageChanged_' |
| 14 | |
| 15 path_: { | |
| 16 type: String, | |
| 17 observer: 'pathChanged_', | |
| 18 }, | 13 }, |
| 19 | 14 |
| 20 /** @type {QueryState} */ | 15 /** @type {QueryState} */ |
| 21 queryState: Object, | 16 queryState: Object, |
| 22 | 17 |
| 18 /** @type {QueryResult} */ | |
| 19 queryResult: Object, | |
|
calamity
2017/01/27 03:47:34
Remove.
tsergeant
2017/01/30 02:13:14
Oops, done.
| |
| 20 | |
| 21 grouped: Boolean, | |
| 22 | |
| 23 path_: String, | |
| 24 | |
| 23 queryParams_: Object, | 25 queryParams_: Object, |
| 24 }, | 26 }, |
| 25 | 27 |
| 28 /** @private{boolean} */ | |
|
calamity
2017/01/27 03:47:34
nit: Missing a space.
tsergeant
2017/01/30 02:13:14
Done.
| |
| 29 parsing_: false, | |
| 30 | |
| 26 observers: [ | 31 observers: [ |
| 27 'queryParamsChanged_(queryParams_.*)', | 32 'onUrlChanged_(path_, queryParams_)', |
| 28 'searchTermChanged_(queryState.searchTerm)', | |
| 29 ], | 33 ], |
| 30 | 34 |
| 31 /** @override */ | 35 /** @override */ |
| 32 attached: function() { | 36 attached: function() { |
| 33 // Redirect legacy search URLs to URLs compatible with material history. | 37 // Redirect legacy search URLs to URLs compatible with material history. |
| 34 if (window.location.hash) { | 38 if (window.location.hash) { |
| 35 window.location.href = window.location.href.split('#')[0] + '?' + | 39 window.location.href = window.location.href.split('#')[0] + '?' + |
| 36 window.location.hash.substr(1); | 40 window.location.hash.substr(1); |
| 37 } | 41 } |
| 38 }, | 42 }, |
| 39 | 43 |
| 40 /** @private */ | 44 /** |
| 41 serializePath_: function() { | 45 * Write all relevant page state to the URL. |
| 42 var page = this.selectedPage == 'history' ? '' : this.selectedPage; | 46 */ |
| 43 this.path_ = '/' + page; | 47 serializeUrl: function() { |
| 48 var path = this.selectedPage; | |
| 49 | |
| 50 if (path == 'history' && this.queryState.range != HistoryRange.ALL_TIME) | |
| 51 path += '/' + this.rangeToString_(this.queryState.range); | |
| 52 | |
| 53 if (path == 'history') | |
| 54 path = ''; | |
| 55 | |
| 56 var offsetParam = null; | |
| 57 if (this.selectedPage == 'history' && this.queryState.groupedOffset) | |
| 58 offsetParam = this.queryState.groupedOffset; | |
| 59 | |
| 60 // Make all modifications at the end of the method so observers can't change | |
| 61 // the outcome. | |
| 62 this.path_ = '/' + path; | |
| 63 this.set('queryParams_.offset', offsetParam); | |
| 64 this.set('queryParams_.q', this.queryState.searchTerm || null); | |
| 44 }, | 65 }, |
| 45 | 66 |
| 46 /** @private */ | 67 /** @private */ |
| 47 pathChanged_: function() { | 68 selectedPageChanged_: function() { |
| 48 var sections = this.path_.substr(1).split('/'); | 69 // Update the URL if the page was changed externally, but ignore the update |
| 49 this.selectedPage = sections[0] || 'history'; | 70 // if it came from parseUrl_(). |
| 71 if (!this.parsing_) | |
| 72 this.serializeUrl(); | |
| 50 }, | 73 }, |
| 51 | 74 |
| 52 /** @private */ | 75 /** @private */ |
| 53 queryParamsChanged_: function() { | 76 parseUrl_: function() { |
| 54 this.fire('change-query', {search: this.queryParams_.q || ''}); | 77 this.parsing_ = true; |
| 78 var changes = {}; | |
| 79 var sections = this.path_.substr(1).split('/'); | |
| 80 var page = sections[0] || 'history'; | |
| 81 | |
| 82 if (page == 'history' && this.grouped) { | |
| 83 var range = sections.length > 1 ? this.stringToRange_(sections[1]) : | |
| 84 HistoryRange.ALL_TIME; | |
| 85 changes.range = range; | |
| 86 changes.offset = Number(this.queryParams_.offset) || 0; | |
| 87 } | |
| 88 | |
| 89 changes.search = this.queryParams_.q || ''; | |
| 90 | |
| 91 // Must change selectedPage before `change-query`, otherwise the | |
| 92 // query-manager will call serializeUrl() with the old page. | |
| 93 this.selectedPage = page; | |
| 94 this.fire('change-query', changes); | |
| 95 this.serializeUrl(); | |
| 96 | |
| 97 this.parsing_ = false; | |
| 55 }, | 98 }, |
| 56 | 99 |
| 57 /** @private */ | 100 /** @private */ |
| 58 searchTermChanged_: function() { | 101 onUrlChanged_: function() { |
| 59 this.set('queryParams_.q', this.queryState.searchTerm || null); | 102 // Changing the url and query parameters at the same time will cause two |
| 103 // calls to onUrlChanged_. Debounce the actual work so that these two | |
| 104 // changes get processed together. | |
| 105 this.debounce('parseUrl', this.parseUrl_.bind(this)); | |
| 60 }, | 106 }, |
| 107 | |
| 108 /** | |
| 109 * @param {!HistoryRange} range | |
| 110 * @return {string} | |
| 111 */ | |
| 112 rangeToString_: function(range) { | |
| 113 switch (range) { | |
| 114 case HistoryRange.WEEK: | |
| 115 return 'week'; | |
| 116 case HistoryRange.MONTH: | |
| 117 return 'month'; | |
| 118 default: | |
| 119 return ''; | |
| 120 } | |
| 121 }, | |
| 122 | |
| 123 /** | |
| 124 * @param {string} str | |
| 125 * @return {HistoryRange} | |
| 126 */ | |
| 127 stringToRange_: function(str) { | |
| 128 switch (str) { | |
| 129 case 'week': | |
| 130 return HistoryRange.WEEK; | |
| 131 case 'month': | |
| 132 return HistoryRange.MONTH; | |
| 133 default: | |
| 134 return HistoryRange.ALL_TIME; | |
| 135 } | |
| 136 } | |
| 61 }); | 137 }); |
| OLD | NEW |