| 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 cr.define('settings', function() { | 5 cr.define('settings', function() { |
| 6 /** | 6 /** |
| 7 * Class for navigable routes. May only be instantiated within this file. | 7 * Class for navigable routes. May only be instantiated within this file. |
| 8 * @constructor | 8 * @constructor |
| 9 * @param {string} path | 9 * @param {string} path |
| 10 * @private | 10 * @private |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 return true; | 71 return true; |
| 72 } | 72 } |
| 73 return false; | 73 return false; |
| 74 }, | 74 }, |
| 75 | 75 |
| 76 /** | 76 /** |
| 77 * Returns true if this route is a subpage of a section. | 77 * Returns true if this route is a subpage of a section. |
| 78 * @return {boolean} | 78 * @return {boolean} |
| 79 */ | 79 */ |
| 80 isSubpage: function() { | 80 isSubpage: function() { |
| 81 return !!this.parent && this.parent.section == this.section; | 81 return !!this.parent && !!this.section && |
| 82 this.parent.section == this.section; |
| 82 }, | 83 }, |
| 83 }; | 84 }; |
| 84 | 85 |
| 85 // Abbreviated variable for easier definitions. | 86 // Abbreviated variable for easier definitions. |
| 86 var r = Route; | 87 var r = Route; |
| 87 | 88 |
| 88 // Root pages. | 89 // Root pages. |
| 89 r.BASIC = new Route('/'); | 90 r.BASIC = new Route('/'); |
| 90 r.ADVANCED = new Route('/advanced'); | 91 r.ADVANCED = new Route('/advanced'); |
| 91 r.ABOUT = new Route('/help'); | 92 r.ABOUT = new Route('/help'); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 121 r.POINTERS = r.DEVICE.createChild('/pointer-overlay'); | 122 r.POINTERS = r.DEVICE.createChild('/pointer-overlay'); |
| 122 r.KEYBOARD = r.DEVICE.createChild('/keyboard-overlay'); | 123 r.KEYBOARD = r.DEVICE.createChild('/keyboard-overlay'); |
| 123 r.DISPLAY = r.DEVICE.createChild('/display'); | 124 r.DISPLAY = r.DEVICE.createChild('/display'); |
| 124 r.NOTES = r.DEVICE.createChild('/note'); | 125 r.NOTES = r.DEVICE.createChild('/note'); |
| 125 </if> | 126 </if> |
| 126 | 127 |
| 127 r.PRIVACY = r.ADVANCED.createSection('/privacy', 'privacy'); | 128 r.PRIVACY = r.ADVANCED.createSection('/privacy', 'privacy'); |
| 128 r.CERTIFICATES = r.PRIVACY.createChild('/certificates'); | 129 r.CERTIFICATES = r.PRIVACY.createChild('/certificates'); |
| 129 | 130 |
| 130 // CLEAR_BROWSER_DATA is the only navigable dialog route. It's the only child | 131 // CLEAR_BROWSER_DATA is the only navigable dialog route. It's the only child |
| 131 // of a section that's not a subpage. Don't add any more routes like these. | 132 // of a root page that's not a section. Don't add any more routes like these. |
| 132 // If more navigable dialogs are needed, add explicit support in Route. | 133 // If more navigable dialogs are needed, add explicit support in Route. |
| 133 r.CLEAR_BROWSER_DATA = r.PRIVACY.createChild('/clearBrowserData'); | 134 r.CLEAR_BROWSER_DATA = r.ADVANCED.createChild('/clearBrowserData'); |
| 134 r.CLEAR_BROWSER_DATA.isSubpage = function() { return false; }; | |
| 135 | 135 |
| 136 r.SITE_SETTINGS = r.PRIVACY.createChild('/siteSettings'); | 136 r.SITE_SETTINGS = r.PRIVACY.createChild('/siteSettings'); |
| 137 r.SITE_SETTINGS_ALL = r.SITE_SETTINGS.createChild('all'); | 137 r.SITE_SETTINGS_ALL = r.SITE_SETTINGS.createChild('all'); |
| 138 r.SITE_SETTINGS_SITE_DETAILS = | 138 r.SITE_SETTINGS_SITE_DETAILS = |
| 139 r.SITE_SETTINGS_ALL.createChild('/siteSettings/siteDetails'); | 139 r.SITE_SETTINGS_ALL.createChild('/siteSettings/siteDetails'); |
| 140 | 140 |
| 141 r.SITE_SETTINGS_HANDLERS = r.SITE_SETTINGS.createChild('handlers'); | 141 r.SITE_SETTINGS_HANDLERS = r.SITE_SETTINGS.createChild('handlers'); |
| 142 | 142 |
| 143 // TODO(tommycli): Find a way to refactor these repetitive category routes. | 143 // TODO(tommycli): Find a way to refactor these repetitive category routes. |
| 144 r.SITE_SETTINGS_AUTOMATIC_DOWNLOADS = | 144 r.SITE_SETTINGS_AUTOMATIC_DOWNLOADS = |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 223 }, | 223 }, |
| 224 | 224 |
| 225 /** @abstract */ | 225 /** @abstract */ |
| 226 currentRouteChanged: assertNotReached, | 226 currentRouteChanged: assertNotReached, |
| 227 }; | 227 }; |
| 228 | 228 |
| 229 /** | 229 /** |
| 230 * Returns the matching canonical route, or null if none matches. | 230 * Returns the matching canonical route, or null if none matches. |
| 231 * @param {string} path | 231 * @param {string} path |
| 232 * @return {?settings.Route} | 232 * @return {?settings.Route} |
| 233 * @private | |
| 234 */ | 233 */ |
| 235 var getRouteForPath = function(path) { | 234 var getRouteForPath = function(path) { |
| 236 // TODO(tommycli): Use Object.values once Closure compilation supports it. | 235 // TODO(tommycli): Use Object.values once Closure compilation supports it. |
| 237 var matchingKey = Object.keys(Route).find(function(key) { | 236 var matchingKey = Object.keys(Route).find(function(key) { |
| 238 return Route[key].path == path; | 237 return Route[key].path == path; |
| 239 }); | 238 }); |
| 240 | 239 |
| 241 return Route[matchingKey] || null; | 240 return Route[matchingKey] || null; |
| 242 }; | 241 }; |
| 243 | 242 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 293 /** @return {!URLSearchParams} */ | 292 /** @return {!URLSearchParams} */ |
| 294 var getQueryParameters = function() { | 293 var getQueryParameters = function() { |
| 295 return new URLSearchParams(currentQueryParameters_); // Defensive copy. | 294 return new URLSearchParams(currentQueryParameters_); // Defensive copy. |
| 296 }; | 295 }; |
| 297 | 296 |
| 298 /** | 297 /** |
| 299 * Navigates to a canonical route and pushes a new history entry. | 298 * Navigates to a canonical route and pushes a new history entry. |
| 300 * @param {!settings.Route} route | 299 * @param {!settings.Route} route |
| 301 * @param {URLSearchParams=} opt_dynamicParameters Navigations to the same | 300 * @param {URLSearchParams=} opt_dynamicParameters Navigations to the same |
| 302 * search parameters in a different order will still push to history. | 301 * search parameters in a different order will still push to history. |
| 303 * @private | |
| 304 */ | 302 */ |
| 305 var navigateTo = function(route, opt_dynamicParameters) { | 303 var navigateTo = function(route, opt_dynamicParameters) { |
| 306 var params = opt_dynamicParameters || new URLSearchParams(); | 304 var params = opt_dynamicParameters || new URLSearchParams(); |
| 307 | 305 |
| 308 var url = route.path; | 306 var url = route.path; |
| 309 if (opt_dynamicParameters) { | 307 if (opt_dynamicParameters) { |
| 310 var queryString = opt_dynamicParameters.toString(); | 308 var queryString = opt_dynamicParameters.toString(); |
| 311 if (queryString) | 309 if (queryString) |
| 312 url += '?' + queryString; | 310 url += '?' + queryString; |
| 313 } | 311 } |
| 314 | 312 |
| 315 // History serializes the state, so we don't push the actual route object. | 313 // History serializes the state, so we don't push the actual route object. |
| 316 window.history.pushState(currentRoute_.path, '', url); | 314 window.history.pushState(currentRoute_.path, '', url); |
| 317 setCurrentRoute(route, params); | 315 setCurrentRoute(route, params); |
| 318 }; | 316 }; |
| 319 | 317 |
| 318 /** |
| 319 * Navigates to the previous route, but will never exit Settings. If there is |
| 320 * no previous route in the history, navigates to the immediate parent. |
| 321 */ |
| 322 var navigateToPreviousRoute = function() { |
| 323 var previousRoute = |
| 324 window.history.state && |
| 325 assert(getRouteForPath(/** @type {string} */ (window.history.state))); |
| 326 |
| 327 if (previousRoute) |
| 328 window.history.back(); |
| 329 else |
| 330 navigateTo(settings.getCurrentRoute().parent || Route.BASIC); |
| 331 }; |
| 332 |
| 320 window.addEventListener('popstate', function(event) { | 333 window.addEventListener('popstate', function(event) { |
| 321 // On pop state, do not push the state onto the window.history again. | 334 // On pop state, do not push the state onto the window.history again. |
| 322 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC, | 335 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC, |
| 323 new URLSearchParams(window.location.search)); | 336 new URLSearchParams(window.location.search)); |
| 324 }); | 337 }); |
| 325 | 338 |
| 326 return { | 339 return { |
| 327 Route: Route, | 340 Route: Route, |
| 328 RouteObserverBehavior: RouteObserverBehavior, | 341 RouteObserverBehavior: RouteObserverBehavior, |
| 329 getRouteForPath: getRouteForPath, | 342 getRouteForPath: getRouteForPath, |
| 330 initializeRouteFromUrl: initializeRouteFromUrl, | 343 initializeRouteFromUrl: initializeRouteFromUrl, |
| 331 getCurrentRoute: getCurrentRoute, | 344 getCurrentRoute: getCurrentRoute, |
| 332 getQueryParameters: getQueryParameters, | 345 getQueryParameters: getQueryParameters, |
| 333 navigateTo: navigateTo, | 346 navigateTo: navigateTo, |
| 347 navigateToPreviousRoute: navigateToPreviousRoute, |
| 334 }; | 348 }; |
| 335 }); | 349 }); |
| OLD | NEW |