Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: chrome/browser/resources/settings/route.js

Issue 2206613003: Settings Router Refactor: Support dynamic parameters (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@223-settings-router-handle-random-urls-better
Patch Set: compare dynamic params also Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | chrome/test/data/webui/settings/cr_settings_browsertest.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 274 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 var getRouteForPath = function(path) { 285 var getRouteForPath = function(path) {
286 // TODO(tommycli): Use Object.values once Closure compilation supports it. 286 // TODO(tommycli): Use Object.values once Closure compilation supports it.
287 var matchingKey = Object.keys(Route).find(function(key) { 287 var matchingKey = Object.keys(Route).find(function(key) {
288 return Route[key].path == path; 288 return Route[key].path == path;
289 }); 289 });
290 290
291 return Route[matchingKey] || null; 291 return Route[matchingKey] || null;
292 }; 292 };
293 293
294 /** 294 /**
295 * The current active route. This may only be updated via the global 295 * The current active route. This updated only by settings.navigateTo.
296 * function settings.navigateTo.
297 * @private {!settings.Route} 296 * @private {!settings.Route}
298 */ 297 */
299 var currentRoute_ = (function() { 298 var currentRoute_ = Route.BASIC;
299
300 /**
301 * The current query parameters. This updated only by settings.navigateTo.
302 * @private {!URLSearchParams}
303 */
304 var currentQueryParameters_ = new URLSearchParams();
305
306 // Initialize the route and query params from the URL.
307 (function() {
300 var route = getRouteForPath(window.location.pathname); 308 var route = getRouteForPath(window.location.pathname);
301 if (route) 309 if (route) {
302 return route; 310 currentRoute_ = route;
303 311 currentQueryParameters_ = new URLSearchParams(window.location.search);
304 // Reset the URL path to '/' if the user navigates to a nonexistent URL. 312 } else {
305 window.history.replaceState(undefined, '', Route.BASIC.path); 313 window.history.pushState(undefined, '', Route.BASIC.path);
306 return Route.BASIC; 314 }
307 })(); 315 })();
308 316
309 /** 317 /**
310 * Helper function to set the current route and notify all observers. 318 * Helper function to set the current route and notify all observers.
311 * @param {!settings.Route} route 319 * @param {!settings.Route} route
320 * @param {!URLSearchParams} queryParameters
312 */ 321 */
313 var setCurrentRoute = function(route) { 322 var setCurrentRoute = function(route, queryParameters) {
314 currentRoute_ = route; 323 currentRoute_ = route;
324 currentQueryParameters_ = queryParameters;
315 for (var observer of routeObservers_) 325 for (var observer of routeObservers_)
316 observer.currentRouteChanged(); 326 observer.currentRouteChanged();
317 }; 327 };
318 328
319 /** @return {!settings.Route} */ 329 /** @return {!settings.Route} */
320 var getCurrentRoute = function() { return currentRoute_; }; 330 var getCurrentRoute = function() { return currentRoute_; };
321 331
332 /** @return {!URLSearchParams} */
333 var getQueryParameters = function() { return currentQueryParameters_; };
Dan Beam 2016/08/03 17:18:00 can we return a copy?
tommycli 2016/08/03 17:24:39 Done.
334
322 /** 335 /**
323 * Navigates to a canonical route and pushes a new history entry. 336 * Navigates to a canonical route and pushes a new history entry.
324 * @param {!settings.Route} route 337 * @param {!settings.Route} route
338 * @param {URLSearchParams=} opt_dynamicParameters
325 * @private 339 * @private
326 */ 340 */
327 var navigateTo = function(route) { 341 var navigateTo = function(route, opt_dynamicParameters) {
328 if (assert(route) == currentRoute_) 342 var params = opt_dynamicParameters || new URLSearchParams();
343 if (assert(route) == currentRoute_ &&
344 params.toString() == currentQueryParameters_.toString()) {
Dan Beam 2016/08/03 17:04:28 this breaks when append() is used in a different o
Dan Beam 2016/08/03 17:18:00 eh, it's fine
tommycli 2016/08/03 17:24:39 Acknowledged.
329 return; 345 return;
346 }
330 347
331 window.history.pushState(undefined, '', route.path); 348 var url = route.path;
332 setCurrentRoute(route); 349 if (opt_dynamicParameters) {
350 var queryString = opt_dynamicParameters.toString();
351 if (queryString)
352 url += '?' + queryString;
353 }
354
355 setCurrentRoute(route, params);
356 window.history.pushState(undefined, '', url);
333 }; 357 };
334 358
335 window.addEventListener('popstate', function(event) { 359 window.addEventListener('popstate', function(event) {
336 // On pop state, do not push the state onto the window.history again. 360 // On pop state, do not push the state onto the window.history again.
337 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC); 361 setCurrentRoute(getRouteForPath(window.location.pathname) || Route.BASIC,
362 new URLSearchParams(window.location.search));
338 }); 363 });
339 364
340 return { 365 return {
341 Route: Route, 366 Route: Route,
342 RouteObserverBehavior: RouteObserverBehavior, 367 RouteObserverBehavior: RouteObserverBehavior,
343 getRouteForPath: getRouteForPath, 368 getRouteForPath: getRouteForPath,
344 getCurrentRoute: getCurrentRoute, 369 getCurrentRoute: getCurrentRoute,
370 getQueryParameters: getQueryParameters,
345 navigateTo: navigateTo, 371 navigateTo: navigateTo,
346 }; 372 };
347 }); 373 });
OLDNEW
« no previous file with comments | « no previous file | chrome/test/data/webui/settings/cr_settings_browsertest.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698