OLD | NEW |
---|---|
(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', | |
michaelpg
2017/04/14 20:23:53
could these values be IDs? neon-animated-pages sho
Devlin
2017/04/17 23:29:24
Using ids doesn't seem to work... I thought neon-
michaelpg
2017/04/18 22:03:11
it's using iron-selector, which has the "attrForSe
| |
12 DETAILS: '1', | |
13 SHORTCUTS: '2', | |
14 ERRORS: '3', | |
15 }; | |
16 | |
17 /** @enum {string} */ | |
18 var Subpage = { | |
19 OPTIONS: 'options', | |
20 }; | |
21 | |
22 /** @typedef {{page: Page, | |
23 id: (string|undefined), | |
24 subpage: (!Subpage|undefined)}} */ | |
25 var PageEntry; | |
26 | |
27 cr.define('extensions', function() { | |
28 'use strict'; | |
29 | |
30 /** @constructor */ | |
michaelpg
2017/04/14 20:23:53
document what PageState is for.
also type and docu
Devlin
2017/04/17 23:29:24
Done.
| |
31 function PageState(changePage) { | |
michaelpg
2017/04/14 20:23:53
should this be a singleton, since it manipulates h
Devlin
2017/04/17 23:29:24
Conceptually, it should be a singleton (just as ma
michaelpg
2017/04/18 22:03:11
Sure. The new naming makes it clearer, too.
| |
32 this.changePage_ = changePage; | |
33 window.addEventListener('popstate', this.onPopState_.bind(this)); | |
34 } | |
35 | |
36 PageState.prototype = { | |
37 /** @private */ | |
38 onPopState_: function() { | |
39 var state = this.getCurrentPage(); | |
michaelpg
2017/04/14 20:23:53
nit: skip variable declaration
Devlin
2017/04/17 23:29:24
Done.
| |
40 this.changePage_(state, false); | |
41 }, | |
42 | |
43 /** | |
44 * Returns the page that should be displayed for the current URL. | |
45 * @return {!PageEntry} | |
46 */ | |
47 getCurrentPage: function() { | |
48 if (location.search == '' && location.pathname == '/') | |
49 return {page: Page.LIST}; | |
50 | |
51 var search = new URLSearchParams(location.search); | |
52 var id = search.get('id'); | |
53 if (id) | |
54 return {page: Page.DETAILS, id: id}; | |
55 id = search.get('options'); | |
56 if (id) | |
57 return {page: Page.DETAILS, id: id, subpage: Subpage.OPTIONS}; | |
58 var id = search.get('errors'); | |
michaelpg
2017/04/14 20:23:53
remove "var"
Devlin
2017/04/17 23:29:24
Done.
| |
59 if (id) | |
60 return {page: Page.ERRORS, id: id}; | |
61 | |
62 if (location.pathname == '/shortcuts') | |
63 return {page: Page.SHORTCUTS}; | |
64 | |
65 return {page: Page.LIST}; | |
66 }, | |
67 | |
68 /** | |
69 * Called when a page changes, and pushes state to history to reflect it. | |
70 * @param {!PageEntry} entry | |
71 */ | |
72 onPageChange: function(entry) { | |
73 var relative; | |
74 switch (entry.page) { | |
75 case Page.LIST: | |
76 relative = '/'; | |
77 break; | |
78 case Page.DETAILS: | |
79 if (entry.subpage) { | |
80 assert(entry.subpage == Subpage.OPTIONS); | |
81 relative = '/?options=' + entry.id; | |
82 } else { | |
83 relative = '/?id=' + entry.id; | |
84 } | |
85 break; | |
86 case Page.SHORTCUTS: | |
87 relative = '/shortcuts'; | |
88 break; | |
89 case Page.ERRORS: | |
90 relative = '/?errors=' + entry.id; | |
91 } | |
92 if (relative) { | |
93 var state = {url: relative}; | |
94 var currentPage = this.getCurrentPage(); | |
95 var isSubpageNavigation = | |
96 currentPage.page == entry.page && | |
97 currentPage.id == entry.id; | |
98 // Navigating to a subpage doesn't visually change pages; it just opens | |
99 // a dialog. As such, we replace state rather than pushing a new state | |
100 // on the stack so that hitting the back button doesn't just toggle the | |
101 // dialog. | |
102 if (isSubpageNavigation) | |
103 history.replaceState(state, '', relative); | |
104 else | |
105 history.pushState(state, '', relative); | |
106 | |
107 } | |
108 }, | |
109 }; | |
110 | |
111 return {PageState: PageState}; | |
112 }); | |
OLD | NEW |