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

Side by Side Diff: chrome/browser/resources/md_extensions/navigation_helper.js

Issue 2811993004: [MD Extensions] Add support for URL navigation (Closed)
Patch Set: Michael's Created 3 years, 8 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
OLDNEW
(Empty)
1 // Copyright 2017 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 /**
6 * The different pages that can be shown at a time.
7 * Note: This must remain in sync with the order in manager.html!
8 * @enum {string}
9 */
10 var Page = {
11 LIST: '0',
12 DETAILS: '1',
13 SHORTCUTS: '2',
14 ERRORS: '3',
15 };
16
17 /** @enum {string} */
18 var Dialog = {
19 OPTIONS: 'options',
20 };
21
22 /** @typedef {{page: Page,
23 id: (string|undefined),
24 subpage: (!Dialog|undefined)}} */
25 var PageState;
26
27 cr.define('extensions', function() {
28 'use strict';
29
30 /**
31 * A helper object to manage in-page navigations. Since the extensions page
32 * needs to support different urls for different subpages (like the details
33 * page), we use this object to manage the history and url conversions.
34 * @param {!function(!PageState:void)} changePage A function to call when the
michaelpg 2017/04/18 22:03:11 It also takes a boolean (maybe)
Devlin 2017/04/27 13:41:26 It doesn't. Below was a mistake.
35 * page has changed as a result of the user going back or forward in
36 * history; called with the new active page.
37 * @constructor */
38 function NavigationHelper(changePage) {
michaelpg 2017/04/18 22:03:12 nit: changePageObserver? onChangePage?
Devlin 2017/04/27 13:41:26 onHistoryChange, to match below?
39 this.changePage_ = changePage;
40 window.addEventListener('popstate', this.onPopState_.bind(this));
41 }
42
43 NavigationHelper.prototype = {
44 /** @private */
45 onPopState_: function() {
46 this.changePage_(this.getCurrentPage(), false);
michaelpg 2017/04/18 22:03:12 Is |false| unused here? I think the interface is
Devlin 2017/04/27 13:41:25 Yes, false is unused and shouldn't be there. Remo
47 },
48
49 /**
50 * Returns the page that should be displayed for the current URL.
51 * @return {!PageState}
52 */
53 getCurrentPage: function() {
54 if (location.search == '' && location.pathname == '/')
55 return {page: Page.LIST};
56
57 var search = new URLSearchParams(location.search);
58 var id = search.get('id');
59 if (id)
60 return {page: Page.DETAILS, id: id};
61 id = search.get('options');
62 if (id)
63 return {page: Page.DETAILS, id: id, subpage: Dialog.OPTIONS};
64 id = search.get('errors');
65 if (id)
66 return {page: Page.ERRORS, id: id};
67
68 if (location.pathname == '/shortcuts')
69 return {page: Page.SHORTCUTS};
70
71 return {page: Page.LIST};
72 },
73
74 /**
75 * Called when a page changes, and pushes state to history to reflect it.
76 * @param {!PageState} entry
77 */
78 onPageChange: function(entry) {
michaelpg 2017/04/18 22:03:12 If you don't mind some more naming bikeshedding, h
Devlin 2017/04/27 13:41:26 Sure! Done.
79 var relative;
80 switch (entry.page) {
81 case Page.LIST:
82 relative = '/';
83 break;
84 case Page.DETAILS:
85 if (entry.subpage) {
86 assert(entry.subpage == Dialog.OPTIONS);
87 relative = '/?options=' + entry.id;
88 } else {
89 relative = '/?id=' + entry.id;
90 }
91 break;
92 case Page.SHORTCUTS:
93 relative = '/shortcuts';
94 break;
95 case Page.ERRORS:
96 relative = '/?errors=' + entry.id;
97 }
michaelpg 2017/04/18 22:03:12 instead of `if (relative)`, maybe `default: return
Devlin 2017/04/27 13:41:26 I think we can actually assert(relative) here sinc
98 if (relative) {
99 var state = {url: relative};
100 var currentPage = this.getCurrentPage();
101 var isDialogNavigation =
102 currentPage.page == entry.page &&
103 currentPage.id == entry.id;
104 // Navigating to a dialog doesn't visually change pages; it just opens
105 // a dialog. As such, we replace state rather than pushing a new state
106 // on the stack so that hitting the back button doesn't just toggle the
107 // dialog.
108 if (isDialogNavigation)
109 history.replaceState(state, '', relative);
110 else
111 history.pushState(state, '', relative);
112
michaelpg 2017/04/18 22:03:12 remove line
Devlin 2017/04/27 13:41:26 Whoops, done.
113 }
114 },
115 };
116
117 return {NavigationHelper: NavigationHelper};
118 });
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698