Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // TODO(xiaoyinh@): This file is a stub class of | |
| 6 // chrome/browser/resources/settings/route.js. And it is used to integrate | |
| 7 // with the polymer elements in settings. | |
| 8 // It should be removed once we don't need to support options. | |
|
stevenjb
2016/08/16 20:59:52
This is one of the pieces that strikes me as somew
xiaoyinh(OOO Sep 11-29)
2016/08/17 20:52:11
I have removed most of it, please take a look. Tha
| |
| 9 | |
| 10 cr.define('settings', function() { | |
| 11 /** | |
| 12 * Class for navigable routes. May only be instantiated within this file. | |
| 13 * @constructor | |
| 14 * @param {string} path | |
| 15 * @private | |
| 16 */ | |
| 17 var Route = function(path) { | |
| 18 this.path = path; | |
| 19 }; | |
| 20 | |
| 21 // Abbreviated variable for easier definitions. | |
| 22 var r = Route; | |
| 23 | |
| 24 // Root pages. | |
| 25 r.BASIC = new Route('/'); | |
| 26 r.PEOPLE = r.BASIC; | |
| 27 <if expr="chromeos"> | |
|
jdufault
2016/08/16 17:43:14
This file should only be included if we're on cros
xiaoyinh(OOO Sep 11-29)
2016/08/17 20:52:11
Removed, thanks!
| |
| 28 r.LOCK_SCREEN = new Route('/quickUnlockConfigureOverlay'); | |
| 29 </if> | |
| 30 | |
| 31 var routeObservers_ = new Set(); | |
| 32 | |
| 33 /** @polymerBehavior */ | |
| 34 var RouteObserverBehavior = { | |
| 35 /** @override */ | |
| 36 attached: function() { | |
| 37 assert(!routeObservers_.has(this)); | |
| 38 routeObservers_.add(this); | |
| 39 | |
| 40 // Emulating Polymer data bindings, the observer is called when the | |
| 41 // element starts observing the route. | |
| 42 this.currentRouteChanged(currentRoute_, undefined); | |
| 43 }, | |
| 44 | |
| 45 /** @override */ | |
| 46 detached: function() { | |
| 47 assert(routeObservers_.delete(this)); | |
| 48 }, | |
| 49 | |
| 50 /** @abstract */ | |
| 51 currentRouteChanged: assertNotReached, | |
| 52 }; | |
| 53 | |
| 54 /** | |
| 55 * Returns the matching canonical route, or null if none matches. | |
| 56 * @param {string} path | |
| 57 * @return {?settings.Route} | |
| 58 * @private | |
| 59 */ | |
| 60 var getRouteForPath = function(path) { | |
| 61 var matchingKey = Object.keys(Route).find(function(key) { | |
| 62 return Route[key].path == path; | |
| 63 }); | |
| 64 | |
| 65 return Route[matchingKey] || null; | |
| 66 }; | |
| 67 | |
| 68 /** | |
| 69 * The current active route. This updated only by settings.navigateTo. | |
| 70 * @private {!settings.Route} | |
| 71 */ | |
| 72 var currentRoute_ = Route.BASIC; | |
| 73 | |
| 74 | |
| 75 // Initialize the route from the URL. | |
| 76 (function() { | |
| 77 var route = getRouteForPath(window.location.pathname); | |
| 78 if (route) { | |
| 79 currentRoute_ = route; | |
| 80 } else { | |
| 81 // Do nothing | |
| 82 } | |
| 83 })(); | |
| 84 | |
| 85 /** | |
| 86 * Helper function to set the current route and notify all observers. | |
| 87 * @param {!settings.Route} route | |
| 88 */ | |
| 89 var setCurrentRoute = function(route) { | |
| 90 var oldRoute = currentRoute_; | |
| 91 currentRoute_ = route; | |
| 92 for (var observer of routeObservers_) | |
| 93 observer.currentRouteChanged(currentRoute_, oldRoute); | |
| 94 }; | |
| 95 | |
| 96 var getCurrentRoute = function() { return currentRoute_; }; | |
| 97 | |
| 98 var navigateTo = function(route) { | |
| 99 setCurrentRoute(route); | |
| 100 }; | |
| 101 | |
| 102 window.addEventListener('popstate', function(event) { | |
| 103 // On pop state, do not push the state onto the window.history again. | |
| 104 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC); | |
| 105 }); | |
| 106 | |
| 107 return { | |
|
tommycli
2016/08/17 21:19:58
I think in this whole file, all you need is:
var
xiaoyinh(OOO Sep 11-29)
2016/08/18 22:41:12
This file has been removed. Thanks!
| |
| 108 Route: Route, | |
| 109 RouteObserverBehavior: RouteObserverBehavior, | |
| 110 getRouteForPath: getRouteForPath, | |
| 111 getCurrentRoute: getCurrentRoute, | |
| 112 navigateTo: navigateTo, | |
| 113 }; | |
| 114 }); | |
| OLD | NEW |