11'kiwi public' ;
22
3+ import getState from './state' ;
4+
35export class History {
46 constructor ( baseUrl ) {
57 /**
@@ -12,56 +14,143 @@ export class History {
1214 this . baseUrl = baseUrl ;
1315 }
1416
17+ go ( n ) {
18+ this . log ( 'go' , n ) ;
19+ return this . history . go ( n ) ;
20+ }
21+
22+ log ( ...args ) {
23+ window . kiwi . log . debug ( '[history]' , ...args ) ;
24+ }
25+
1526 prepare ( ) {
1627 if ( ! this . prepared ) {
17- window . addEventListener ( 'popstate' , async ( e ) => {
18- if ( e . state ) {
19- const page = e . state . page ;
20- await this . setPage ( page ) ;
28+ // eslint-disable-next-line
29+ window . addEventListener ( 'popstate' , ( e ) => {
30+ const page = e ?. state ?. page ;
31+ this . log ( 'popstate' , e ) ;
32+ if ( page === undefined ) {
33+ this . currentPage = 0 ;
34+ this . history . replaceState ( { } , '' , this . baseUrl + '/' ) ;
35+ const state = getState ( ) ;
36+ const net = state . getActiveNetwork ( ) ;
37+ const buf = state . getActiveBuffer ( ) ;
38+ const serverBuffer = net . serverBuffer ( ) ;
39+ if ( ! state . activeComponent && buf === serverBuffer ) {
40+ this . history . go ( ) ;
41+ return ;
42+ }
43+ state . $emit ( 'active.component' ) ;
44+ state . setActiveBuffer ( net . id , serverBuffer . name || '*' , false ) ;
45+ // e.preventDefault();
46+ return ;
2147 }
48+ this . setPage ( page ) ;
2249 } ) ;
2350 this . prepared = true ;
2451 }
2552 }
2653
27- async setPage ( page ) {
54+ setPage ( page ) {
2855 this . prepare ( ) ;
56+ this . log ( 'setting page' , page ) ;
2957 const previousHandler = this . handlers [ this . currentPage ] ;
3058 const handler = this . handlers [ page ] ;
31- this . currentPage = page ;
32- await previousHandler . leave ( ) ;
33- await handler . enter ( ) ;
59+ if ( page !== undefined ) {
60+ this . currentPage = page ;
61+ }
62+ previousHandler && previousHandler . leave ( ) ;
63+ handler && handler . enter ( ) ;
3464 }
3565
3666 push ( {
3767 enter,
3868 leave,
3969 path,
70+ query,
71+ hash,
4072 } ) {
73+ // eslint-disable-next-line
74+ this . log ( 'pushing.....' , ...arguments ) ;
4175 const url = new URL ( this . baseUrl ) ;
42- url . pathname += path ;
76+ if ( path ) {
77+ url . pathname = [
78+ ...url . pathname . split ( '/' ) ,
79+ ...path . split ( '/' ) ,
80+ ] . filter ( Boolean ) . join ( '/' ) ;
81+ }
82+ if ( query ) {
83+ Object . entries ( query ) . forEach ( ( e ) => {
84+ url . searchParams . set ( e [ 0 ] , e [ 1 ] . toString ( ) ) ;
85+ } ) ;
86+ }
87+ if ( hash ) {
88+ url . hash = hash ;
89+ }
90+ if ( url . pathname . charAt ( url . pathname . length - 1 ) !== '/' ) {
91+ url . pathname += '/' ;
92+ }
93+ if ( '' + url === '' + window . location ) {
94+ this . doReplace ( { enter, leave, url } ) ;
95+ return ;
96+ }
4397 if ( this . currentPage < this . handlers . length - 1 ) {
4498 this . handlers . splice ( this . currentPage + 1 ) ;
4599 }
46100 const page = this . handlers . length ;
47101 this . history . pushState ( {
48102 page,
49103 } , '' , url ) ;
50- this . handlers . push ( { enter, leave } ) ;
51- return this . setPage ( page ) ;
104+ this . handlers . push ( { enter, leave, path : '' + url } ) ;
105+ this . setPage ( page ) ;
52106 }
53107
54- async replace ( { enter, leave, path } ) {
108+ replace ( {
109+ enter,
110+ leave,
111+ path,
112+ query,
113+ hash,
114+ } ) {
115+ // eslint-disable-next-line
116+ this . log ( 'replacing.....' , ...arguments ) ;
55117 const url = new URL ( this . baseUrl ) ;
56- url . pathname += path ;
118+ if ( path ) {
119+ url . pathname = [
120+ ...url . pathname . split ( '/' ) ,
121+ ...path . split ( '/' ) ,
122+ ] . filter ( Boolean ) . join ( '/' ) ;
123+ }
124+ if ( query ) {
125+ Object . entries ( query ) . forEach ( ( e ) => {
126+ url . searchParams . set ( e [ 0 ] , e [ 1 ] . toString ( ) ) ;
127+ } ) ;
128+ }
129+ if ( hash ) {
130+ url . hash = hash ;
131+ }
132+ if ( url . pathname . charAt ( url . pathname . length - 1 ) !== '/' ) {
133+ url . pathname += '/' ;
134+ }
135+ if ( '' + url === '' + window . location ) {
136+ return ;
137+ }
138+ this . doReplace ( { enter, leave, url } ) ;
139+ }
140+
141+ doReplace ( {
142+ enter,
143+ leave,
144+ url,
145+ } ) {
57146 const page = this . currentPage ;
58147 this . history . replaceState ( {
59148 page,
60149 } , '' , url ) ;
61150 const previousHandler = this . handlers [ page ] ;
62- const handler = { enter, leave } ;
151+ const handler = { enter, leave, path : '' + url } ;
63152 this . handlers [ page ] = handler ;
64- previousHandler && await previousHandler . leave ( ) ;
65- await handler . enter ( ) ;
153+ previousHandler && previousHandler . leave ( ) ;
154+ handler . enter ( ) ;
66155 }
67156}
0 commit comments