@@ -4,52 +4,58 @@ import event from '@muvon/event'
44let pageEl
55
66export default class Navigation {
7- static init ( ) {
8- pageEl = document . getElementById ( 'page' )
9- const d = delegate ( document )
10-
11- d . on ( 'click' , '[data-load]' , ( ev , el ) => {
12- if ( el . href ) {
13- ev . preventDefault ( )
14- this . load ( el . href )
15- }
16- } )
17-
18- window . onpopstate = ev => {
19- if ( ev . srcElement . location . pathname == ev . target . location . pathname ) {
20- return
21- }
22- this . load ( location . href )
23- }
24- }
25-
26- static load ( url ) {
27- if ( location . href !== url ) {
28- history . pushState ( { } , '' , url )
29- }
30-
31- const opts = {
32- credentials : 'same-origin' ,
33- method : 'GET' ,
34- headers : {
35- 'X-Requested-With' : 'navigation'
36- }
37- }
38-
39- fetch ( url , opts ) . then ( res => res . text ( ) ) . then ( body => {
40- window . requestAnimationFrame ( ( ) => {
41- window . scrollTo ( 0 , 0 )
42- pageEl . innerHTML = body
43- const title_el = pageEl . querySelector ( '#title' )
44- const token_el = pageEl . querySelector ( '#token' )
45- document . title = title_el . innerText
46- pageEl . removeChild ( title_el )
47- pageEl . removeChild ( token_el )
48-
49- const url_path = url . replace ( / h t t p s ? \: \/ \/ [ ^ \/ ] + / , '' )
50- event . send ( 'page_content_loaded' , { ajax : true , url : url_path } , 'navigation' )
51- event . send ( 'token_updated' , token_el . innerText , 'navigation' )
52- } )
53- } )
54- }
7+ static init ( ) {
8+ pageEl = document . getElementById ( 'page' )
9+ const d = delegate ( document )
10+
11+ d . on (
12+ 'click' , '[data-load]' , ( ev , el ) => {
13+ if ( el . href ) {
14+ ev . preventDefault ( )
15+ this . load ( el . href )
16+ }
17+ }
18+ )
19+
20+ window . onpopstate = ev => {
21+ if ( ev . srcElement . location . pathname == ev . target . location . pathname ) {
22+ return
23+ }
24+ this . load ( location . href )
25+ }
26+ }
27+
28+ static load ( url ) {
29+ if ( location . href !== url ) {
30+ history . pushState ( { } , '' , url )
31+ }
32+
33+ const opts = {
34+ credentials : 'same-origin' ,
35+ method : 'GET' ,
36+ headers : {
37+ 'X-Requested-With' : 'navigation'
38+ }
39+ }
40+
41+ fetch ( url , opts ) . then ( res => res . text ( ) ) . then (
42+ body => {
43+ window . requestAnimationFrame (
44+ ( ) => {
45+ window . scrollTo ( 0 , 0 )
46+ pageEl . innerHTML = body
47+ const title_el = pageEl . querySelector ( '#title' )
48+ const token_el = pageEl . querySelector ( '#token' )
49+ document . title = title_el . innerText
50+ pageEl . removeChild ( title_el )
51+ pageEl . removeChild ( token_el )
52+
53+ const url_path = url . replace ( / h t t p s ? \: \/ \/ [ ^ \/ ] + / , '' )
54+ event . send ( 'page_content_loaded' , { ajax : true , url : url_path } , 'navigation' )
55+ event . send ( 'token_updated' , token_el . innerText , 'navigation' )
56+ }
57+ )
58+ }
59+ )
60+ }
5561}
0 commit comments