22
33import type Router from '../index'
44import { History } from './base'
5- import { inBrowser } from '../util/dom'
65import { cleanPath } from '../util/path'
7- import { handleScroll , saveScrollPosition } from '../util/scroll'
8-
9- // use User Timing api (if present) for more accurate key precision
10- const Time = inBrowser && window . performance && window . performance . now
11- ? window . performance
12- : Date
13-
14- const genKey = ( ) => String ( Time . now ( ) )
15- let _key : string = genKey ( )
6+ import { setupScroll , handleScroll } from '../util/scroll'
7+ import { pushState , replaceState } from '../util/push-state'
168
179export class HTML5History extends History {
1810 constructor ( router : Router , base : ?string ) {
1911 super ( router , base )
2012
2113 const expectScroll = router . options . scrollBehavior
14+
15+ if ( expectScroll ) {
16+ setupScroll ( )
17+ }
18+
2219 window . addEventListener ( 'popstate' , e => {
23- _key = e . state && e . state . key
24- const current = this . current
25- this . transitionTo ( getLocation ( this . base ) , next => {
20+ this . transitionTo ( getLocation ( this . base ) , route => {
2621 if ( expectScroll ) {
27- handleScroll ( router , _key , next , current , true )
22+ handleScroll ( router , route , this . current , true )
2823 }
2924 } )
3025 } )
31-
32- if ( expectScroll ) {
33- window . addEventListener ( 'scroll' , ( ) => {
34- saveScrollPosition ( _key )
35- } )
36- }
3726 }
3827
3928 go ( n : number ) {
4029 window . history . go ( n )
4130 }
4231
4332 push ( location : RawLocation , onComplete ?: Function , onAbort ?: Function ) {
44- const current = this . current
4533 this . transitionTo ( location , route => {
4634 pushState ( cleanPath ( this . base + route . fullPath ) )
47- handleScroll ( this . router , _key , route , current , false )
35+ handleScroll ( this . router , route , this . current , false )
4836 onComplete && onComplete ( route )
4937 } , onAbort )
5038 }
5139
5240 replace ( location : RawLocation , onComplete ? : Function , onAbort ? : Function ) {
53- const current = this . current
5441 this . transitionTo ( location , route => {
5542 replaceState ( cleanPath ( this . base + route . fullPath ) )
56- handleScroll ( this . router , _key , route , current , false )
43+ handleScroll ( this . router , route , this . current , false )
5744 onComplete && onComplete ( route )
5845 } , onAbort )
5946 }
@@ -77,24 +64,3 @@ export function getLocation (base: string): string {
7764 }
7865 return ( path || '/' ) + window . location . search + window . location . hash
7966}
80-
81- function pushState ( url : string , replace ?: boolean ) {
82- // try...catch the pushState call to get around Safari
83- // DOM Exception 18 where it limits to 100 pushState calls
84- const history = window . history
85- try {
86- if ( replace ) {
87- history . replaceState ( { key : _key } , '' , url )
88- } else {
89- _key = genKey ( )
90- history . pushState ( { key : _key } , '' , url )
91- }
92- saveScrollPosition ( _key )
93- } catch ( e ) {
94- window . location [ replace ? 'replace' : 'assign' ] ( url )
95- }
96- }
97-
98- function replaceState ( url : string ) {
99- pushState ( url , true )
100- }
0 commit comments