OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 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 | 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 /** | 5 /** |
6 * The different pages that can be shown at a time. | 6 * The different pages that can be shown at a time. |
7 * Note: This must remain in sync with the page ids in manager.html! | 7 * Note: This must remain in sync with the page ids in manager.html! |
8 * @enum {string} | 8 * @enum {string} |
9 */ | 9 */ |
10 var Page = { | 10 var Page = { |
(...skipping 13 matching lines...) Expand all Loading... |
24 subpage: (!Dialog|undefined)}} */ | 24 subpage: (!Dialog|undefined)}} */ |
25 var PageState; | 25 var PageState; |
26 | 26 |
27 cr.define('extensions', function() { | 27 cr.define('extensions', function() { |
28 'use strict'; | 28 'use strict'; |
29 | 29 |
30 /** | 30 /** |
31 * A helper object to manage in-page navigations. Since the extensions page | 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 | 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. | 33 * page), we use this object to manage the history and url conversions. |
34 * @param {!function(!PageState):void} onHistoryChange A function to call when | 34 */ |
35 * the page has changed as a result of the user going back or forward in | 35 class NavigationHelper { |
36 * history; called with the new active page. | 36 /** |
37 * @constructor */ | 37 * @param {!function(!PageState):void} onHistoryChange A function to call |
38 function NavigationHelper(onHistoryChange) { | 38 * when the page has changed as a result of the user going back or |
39 this.onHistoryChange_ = onHistoryChange; | 39 * forward in history; called with the new active page. |
40 window.addEventListener('popstate', this.onPopState_.bind(this)); | 40 */ |
41 } | 41 constructor(onHistoryChange) { |
| 42 this.onHistoryChange_ = onHistoryChange; |
| 43 window.addEventListener('popstate', this.onPopState_.bind(this)); |
| 44 } |
42 | 45 |
43 NavigationHelper.prototype = { | |
44 /** @private */ | 46 /** @private */ |
45 onPopState_: function() { | 47 onPopState_() { |
46 this.onHistoryChange_(this.getCurrentPage()); | 48 this.onHistoryChange_(this.getCurrentPage()); |
47 }, | 49 } |
48 | 50 |
49 /** | 51 /** |
50 * Returns the page that should be displayed for the current URL. | 52 * @return {!PageState} The page that should be displayed for the current |
51 * @return {!PageState} | 53 * URL. |
52 */ | 54 */ |
53 getCurrentPage: function() { | 55 getCurrentPage() { |
54 var search = new URLSearchParams(location.search); | 56 var search = new URLSearchParams(location.search); |
55 var id = search.get('id'); | 57 var id = search.get('id'); |
56 if (id) | 58 if (id) |
57 return {page: Page.DETAILS, extensionId: id}; | 59 return {page: Page.DETAILS, extensionId: id}; |
58 id = search.get('options'); | 60 id = search.get('options'); |
59 if (id) | 61 if (id) |
60 return {page: Page.DETAILS, extensionId: id, subpage: Dialog.OPTIONS}; | 62 return {page: Page.DETAILS, extensionId: id, subpage: Dialog.OPTIONS}; |
61 id = search.get('errors'); | 63 id = search.get('errors'); |
62 if (id) | 64 if (id) |
63 return {page: Page.ERRORS, extensionId: id}; | 65 return {page: Page.ERRORS, extensionId: id}; |
64 | 66 |
65 if (location.pathname == '/shortcuts') | 67 if (location.pathname == '/shortcuts') |
66 return {page: Page.SHORTCUTS}; | 68 return {page: Page.SHORTCUTS}; |
67 | 69 |
68 return {page: Page.LIST}; | 70 return {page: Page.LIST}; |
69 }, | 71 } |
70 | 72 |
71 /** | 73 /** |
72 * Called when a page changes, and pushes state to history to reflect it. | 74 * Called when a page changes, and pushes state to history to reflect it. |
73 * @param {!PageState} entry | 75 * @param {!PageState} entry |
74 */ | 76 */ |
75 updateHistory: function(entry) { | 77 updateHistory(entry) { |
76 var path; | 78 var path; |
77 switch (entry.page) { | 79 switch (entry.page) { |
78 case Page.LIST: | 80 case Page.LIST: |
79 path = '/'; | 81 path = '/'; |
80 break; | 82 break; |
81 case Page.DETAILS: | 83 case Page.DETAILS: |
82 if (entry.subpage) { | 84 if (entry.subpage) { |
83 assert(entry.subpage == Dialog.OPTIONS); | 85 assert(entry.subpage == Dialog.OPTIONS); |
84 path = '/?options=' + entry.extensionId; | 86 path = '/?options=' + entry.extensionId; |
85 } else { | 87 } else { |
(...skipping 13 matching lines...) Expand all Loading... |
99 var isDialogNavigation = currentPage.page == entry.page && | 101 var isDialogNavigation = currentPage.page == entry.page && |
100 currentPage.extensionId == entry.extensionId; | 102 currentPage.extensionId == entry.extensionId; |
101 // Navigating to a dialog doesn't visually change pages; it just opens | 103 // Navigating to a dialog doesn't visually change pages; it just opens |
102 // a dialog. As such, we replace state rather than pushing a new state | 104 // a dialog. As such, we replace state rather than pushing a new state |
103 // on the stack so that hitting the back button doesn't just toggle the | 105 // on the stack so that hitting the back button doesn't just toggle the |
104 // dialog. | 106 // dialog. |
105 if (isDialogNavigation) | 107 if (isDialogNavigation) |
106 history.replaceState(state, '', path); | 108 history.replaceState(state, '', path); |
107 else | 109 else |
108 history.pushState(state, '', path); | 110 history.pushState(state, '', path); |
109 }, | 111 } |
110 }; | 112 } |
111 | 113 |
112 return {NavigationHelper: NavigationHelper}; | 114 return {NavigationHelper: NavigationHelper}; |
113 }); | 115 }); |
OLD | NEW |