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