1- /* global config document window */
1+ /* global document window */
22
33import { API } from "./Api.js" ;
44import { BeaconsMinionPage } from "./pages/BeaconsMinion.js" ;
@@ -29,6 +29,7 @@ export class Router {
2929 Character . init ( ) ;
3030
3131 this . api = new API ( ) ;
32+ this . api . router = this ;
3233 this . commandbox = new CommandBox ( this , this . api ) ;
3334 this . currentPage = undefined ;
3435 this . pages = [ ] ;
@@ -52,16 +53,20 @@ export class Router {
5253 this . _registerPage ( this . optionsPage = new OptionsPage ( this ) ) ;
5354 this . _registerPage ( new LogoutPage ( this ) ) ;
5455
55- Router . _registerRouterEventListeners ( ) ;
56+ this . _registerRouterEventListeners ( ) ;
5657
5758 this . updateMainMenu ( ) ;
5859
59- // This URL already has its prefix added
60- // therefore is must not be added again
61- this . goTo ( window . location . pathname + window . location . search , true ) ;
60+ const hash = window . location . hash . replace ( / ^ # / , "" ) ;
61+ const search = window . location . search ;
62+ /* eslint-disable compat/compat */
63+ /* URLSearchParams.entries() is not supported in IE 11 */
64+ /* URLSearchParams is not supported in op_mini all, IE 11, Baidu 7.12 */
65+ this . goTo ( hash , Object . fromEntries ( new URLSearchParams ( search ) ) ) ;
66+ /* eslint-enable compat/compat */
6267 }
6368
64- static _registerMenuItem ( pButtonId , pUrl ) {
69+ _registerMenuItem ( pButtonId , pHash ) {
6570 for ( const nr of [ "1" , "2" ] ) {
6671 document . getElementById ( "button-" + pButtonId + nr ) .
6772 addEventListener ( "click" , ( pClickEvent ) => {
@@ -75,32 +80,42 @@ export class Router {
7580 panel . style . display = "" ;
7681 } , 500 ) ;
7782 }
78- window . location . replace ( config . NAV_URL + pUrl ) ;
83+ this . goTo ( pHash ) ;
7984 } ) ;
8085 }
8186 }
8287
83- static _registerRouterEventListeners ( ) {
88+ _registerRouterEventListeners ( ) {
8489 document . getElementById ( "logo" ) .
8590 addEventListener ( "click" , ( ) => {
8691 if ( window . event . ctrlKey ) {
87- window . location . assign ( config . NAV_URL + "/ options") ;
92+ this . goTo ( " options") ;
8893 } else {
89- window . location . assign ( config . NAV_URL + "/ ") ;
94+ this . goTo ( " ") ;
9095 }
9196 } ) ;
9297
93- Router . _registerMenuItem ( "minions" , "/" ) ;
94- Router . _registerMenuItem ( "grains" , "/grains" ) ;
95- Router . _registerMenuItem ( "schedules" , "/schedules" ) ;
96- Router . _registerMenuItem ( "pillars" , "/pillars" ) ;
97- Router . _registerMenuItem ( "beacons" , "/beacons" ) ;
98- Router . _registerMenuItem ( "keys" , "/keys" ) ;
99- Router . _registerMenuItem ( "jobs" , "/jobs" ) ;
100- Router . _registerMenuItem ( "templates" , "/templates" ) ;
101- Router . _registerMenuItem ( "events" , "/eventsview" ) ;
102- Router . _registerMenuItem ( "reactors" , "/reactors" ) ;
103- Router . _registerMenuItem ( "logout" , "/logout" ) ;
98+ addEventListener ( "popstate" , ( popstate ) => {
99+ const hash = popstate . target . location . hash . replace ( / ^ # / , "" ) ;
100+ const search = popstate . target . location . search ;
101+ /* eslint-disable compat/compat */
102+ /* URLSearchParams.entries() is not supported in IE 11 */
103+ /* URLSearchParams is not supported in op_mini all, IE 11, Baidu 7.12 */
104+ this . goTo ( hash , Object . fromEntries ( new URLSearchParams ( search ) ) , 2 ) ;
105+ /* eslint-enable compat/compat */
106+ } ) ;
107+
108+ this . _registerMenuItem ( "minions" , "" ) ;
109+ this . _registerMenuItem ( "grains" , "grains" ) ;
110+ this . _registerMenuItem ( "schedules" , "schedules" ) ;
111+ this . _registerMenuItem ( "pillars" , "pillars" ) ;
112+ this . _registerMenuItem ( "beacons" , "beacons" ) ;
113+ this . _registerMenuItem ( "keys" , "keys" ) ;
114+ this . _registerMenuItem ( "jobs" , "jobs" ) ;
115+ this . _registerMenuItem ( "templates" , "templates" ) ;
116+ this . _registerMenuItem ( "events" , "eventsview" ) ;
117+ this . _registerMenuItem ( "reactors" , "reactors" ) ;
118+ this . _registerMenuItem ( "logout" , "logout" ) ;
104119 }
105120
106121 _registerPage ( pPage ) {
@@ -126,36 +141,77 @@ export class Router {
126141 }
127142 }
128143
129- goTo ( pPath , hasPathPrefix = false ) {
130- if ( this . switchingPage ) {
131- return ;
132- }
133- if ( window . location . pathname === config . NAV_URL + pPath && this . currentPage ) {
134- return ;
135- }
136- if ( pPath === "/" && Utils . getStorageItem ( "session" , "login-response" ) === null ) {
144+ // pForward = 0 --> normal navigation
145+ // pForward = 1 --> back navigation using regular gui
146+ // pForward = 2 --> back navigation using browser
147+ goTo ( pHash , pQuery = { } , pForward = 0 ) {
148+
149+ if ( Utils . getStorageItem ( "session" , "login-response" ) === null ) {
137150 // the fact that we don't have a session will be caught later
138151 // but this was shows less error messages on the console
139- pPath = "/login" ;
152+ pHash = "login" ;
153+ pQuery = { "reason" : "no-session" } ;
140154 }
141- const pathUrl = ( hasPathPrefix ? "" : config . NAV_URL ) + pPath . split ( "?" ) [ 0 ] ;
155+
156+ // save the details from the parent
157+ const parentHash = document . location . hash . replace ( / ^ # / , "" ) ;
158+ const search = window . location . search ;
159+ /* eslint-disable compat/compat */
160+ /* URLSearchParams.entries() is not supported in IE 11 */
161+ /* URLSearchParams is not supported in op_mini all, IE 11, Baidu 7.12 */
162+ const parentQuery = Object . fromEntries ( new URLSearchParams ( search ) ) ;
163+ /* eslint-enable compat/compat */
164+
142165 for ( const route of this . pages ) {
143- if ( ! route . path . test ( pathUrl ) ) {
166+ if ( route . path !== pHash ) {
144167 continue ;
145168 }
146- // push history state for login (including redirect to /)
147- if ( pathUrl === config . NAV_URL + "/login" || pathUrl === config . NAV_URL + "/" ) {
148- window . history . pushState ( { } , undefined , pPath ) ;
169+ // push history state, so that the address bar holds the correct
170+ // deep-link; and so that we can use the back-button
171+ let url = "/" ;
172+ let sep = "?" ;
173+ for ( const key in pQuery ) {
174+ const value = pQuery [ key ] ;
175+ if ( ! value || value === "undefined" ) {
176+ continue ;
177+ }
178+ url += sep + key + "=" + encodeURIComponent ( value ) ;
179+ sep = "&" ;
180+ }
181+ url += "#" + pHash ;
182+ if ( parentHash === route . path ) {
183+ // page refresh
184+ // prevents being detected as "forward navigation"
185+ // do nothing
186+ } else if ( pForward === 0 ) {
187+ // forward navigation
188+ window . history . pushState ( { } , undefined , url ) ;
189+ route . parentHash = parentHash ;
190+ route . parentQuery = parentQuery ;
191+ } else if ( pForward === 1 ) {
192+ // close-icon on a panel
193+ // do not save parent details
194+ // these were already registered on the way forward
195+ window . history . pushState ( { } , undefined , url ) ;
196+ } else if ( pForward === 2 ) {
197+ // backward navigation from browser
198+ // do nothing extra
149199 }
150200 this . _showPage ( route ) ;
151201 return ;
152202 }
153203 // route could not be found
154204 // just go to the main page
155- this . goTo ( "/" ) ;
205+ if ( pHash === "" ) {
206+ console . log ( "cannot find default page" ) ;
207+ return ;
208+ }
209+ this . goTo ( "" ) ;
156210 }
157211
158212 _showPage ( pPage ) {
213+ pPage . clearPage ( ) ;
214+
159215 pPage . pageElement . style . display = "" ;
160216
161217 const activeMenuItems = Array . from ( document . querySelectorAll ( ".menu-item-active" ) ) ;
@@ -191,21 +247,18 @@ export class Router {
191247 elem2 . classList . add ( "menu-item-active" ) ;
192248 }
193249
194- this . switchingPage = true ;
195-
196250 pPage . onShow ( ) ;
197251
198252 // start the event-pipe (again)
199253 // it is either not started, or needs restarting
200254 API . getEvents ( this ) ;
201255
202- if ( this . currentPage ) {
256+ if ( this . currentPage && this . currentPage !== pPage ) {
203257 Router . _hidePage ( this . currentPage ) ;
204258 }
205-
206259 this . currentPage = pPage ;
260+
207261 this . currentPage . pageElement . classList . add ( "current" ) ;
208- this . switchingPage = false ;
209262 }
210263
211264 static _hidePage ( pPage ) {
0 commit comments