| 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 /** | 5 /** |
| 6 * @fileoverview | 6 * @fileoverview |
| 7 * 'settings-router' is a simple router for settings. Its responsibilities: | 7 * 'settings-router' is only used to propagate route changes to bound elements. |
| 8 * - Update the URL when the routing state changes. | 8 * All the real routing is defined within route.js. |
| 9 * - Initialize the routing state with the initial URL. | 9 * TODO(tommycli): Remove once all elements migrated to RouteObserverBehavior. |
| 10 * - Process and validate all routing state changes. | |
| 11 * | 10 * |
| 12 * Example: | 11 * Example: |
| 13 * | |
| 14 * <settings-router current-route="{{currentRoute}}"> | 12 * <settings-router current-route="{{currentRoute}}"> |
| 15 * </settings-router> | 13 * </settings-router> |
| 16 */ | 14 */ |
| 17 Polymer({ | 15 Polymer({ |
| 18 is: 'settings-router', | 16 is: 'settings-router', |
| 19 | 17 |
| 18 behaviors: [settings.RouteObserverBehavior], |
| 19 |
| 20 properties: { | 20 properties: { |
| 21 /** | 21 /** |
| 22 * The current active route. This may only be updated via the global | 22 * Only used to propagate settings.currentRoute to all the elements bound to |
| 23 * function settings.navigateTo. | 23 * settings-router. |
| 24 * | |
| 25 * currentRoute.section is only non-empty when the user is on a subpage. If | |
| 26 * the user is on Basic, for instance, this is an empty string. | |
| 27 * | |
| 28 * currentRoute.subpage is an Array. The last element is the actual subpage | |
| 29 * the user is on. The previous elements are the ancestor subpages. This | |
| 30 * enables support for multiple paths to the same subpage. This is used by | |
| 31 * both the Back button and the Breadcrumb to determine ancestor subpages. | |
| 32 * @type {!settings.Route} | 24 * @type {!settings.Route} |
| 33 */ | 25 */ |
| 34 currentRoute: { | 26 currentRoute: { |
| 35 notify: true, | 27 notify: true, |
| 36 type: Object, | 28 type: Object, |
| 37 value: function() { | 29 value: function() { return settings.getCurrentRoute(); }, |
| 38 return (settings.getRouteForPath(window.location.pathname) || | |
| 39 settings.Route.BASIC); | |
| 40 }, | |
| 41 }, | 30 }, |
| 42 }, | 31 }, |
| 43 | 32 |
| 44 /** | 33 /** @protected */ |
| 45 * Sets up a history popstate observer. | 34 currentRouteChanged: function() { |
| 46 * @override | 35 this.currentRoute = settings.getCurrentRoute(); |
| 47 */ | |
| 48 created: function() { | |
| 49 window.addEventListener('popstate', function(event) { | |
| 50 // On pop state, do not push the state onto the window.history again. | |
| 51 var historicRoute = settings.getRouteForPath(window.location.pathname); | |
| 52 this.currentRoute = historicRoute || settings.Route.BASIC; | |
| 53 }.bind(this)); | |
| 54 | |
| 55 settings.navigateTo = this.navigateTo_.bind(this); | |
| 56 }, | |
| 57 | |
| 58 /** | |
| 59 * Navigates to a canonical route. | |
| 60 * @param {!settings.Route} route | |
| 61 * @private | |
| 62 */ | |
| 63 navigateTo_: function(route) { | |
| 64 assert(!!route); | |
| 65 | |
| 66 if (route == this.currentRoute) | |
| 67 return; | |
| 68 | |
| 69 window.history.pushState(undefined, document.title, route.path); | |
| 70 this.currentRoute = route; | |
| 71 }, | 36 }, |
| 72 }); | 37 }); |
| OLD | NEW |