11import DOMHistory from './DOMHistory' ;
2- import { getWindowPath , supportsHistory } from './DOMUtils' ;
2+ import { getWindowPath , getWindowScrollPosition , supportsHistory } from './DOMUtils' ;
33import NavigationTypes from './NavigationTypes' ;
4-
5- function createRandomKey ( ) {
6- return Math . random ( ) . toString ( 36 ) . substr ( 2 ) ;
7- }
4+ import Location from './Location' ;
85
96/**
107 * A history implementation for DOM environments that support the
@@ -18,26 +15,31 @@ function createRandomKey() {
1815 */
1916export class BrowserHistory extends DOMHistory {
2017
21- constructor ( getScrollPosition ) {
22- super ( getScrollPosition ) ;
18+ constructor ( getScrollPosition = getWindowScrollPosition ) {
19+ super ( ) ;
20+ this . getScrollPosition = getScrollPosition ;
2321 this . handlePopState = this . handlePopState . bind ( this ) ;
2422 this . isSupported = supportsHistory ( ) ;
2523 }
2624
2725 _updateLocation ( navigationType ) {
28- var key = null ;
26+ var state = null ;
2927
3028 if ( this . isSupported ) {
31- var state = window . history . state ;
32- key = state && state . key ;
29+ state = window . history . state || { } ;
3330
34- if ( ! key ) {
35- key = createRandomKey ( ) ;
36- window . history . replaceState ( { key } , '' ) ;
31+ if ( ! state . key ) {
32+ state . key = createRandomKey ( ) ;
33+ window . history . replaceState ( state , '' ) ;
3734 }
3835 }
3936
40- this . location = this . _createLocation ( getWindowPath ( ) , key , navigationType ) ;
37+ this . location = new Location ( state , getWindowPath ( ) , navigationType ) ;
38+ }
39+
40+ setup ( ) {
41+ if ( this . location == null )
42+ this . _updateLocation ( ) ;
4143 }
4244
4345 handlePopState ( event ) {
@@ -72,31 +74,33 @@ export class BrowserHistory extends DOMHistory {
7274 }
7375 }
7476
75- setup ( ) {
76- if ( this . location == null )
77- this . _updateLocation ( ) ;
78- }
79-
8077 // http://www.w3.org/TR/2011/WD-html5-20110113/history.html#dom-history-pushstate
81- push ( path ) {
78+ pushState ( state , path ) {
8279 if ( this . isSupported ) {
83- this . _recordScrollPosition ( ) ;
80+ var currentState = window . history . state ;
8481
85- var key = createRandomKey ( ) ;
86- window . history . pushState ( { key } , '' , path ) ;
87- this . location = this . _createLocation ( path , key , NavigationTypes . PUSH ) ;
82+ if ( currentState ) {
83+ Object . assign ( currentState , this . getScrollPosition ( ) ) ;
84+ window . history . replaceState ( currentState , '' ) ;
85+ }
86+
87+ state = this . _createState ( state ) ;
88+
89+ window . history . pushState ( state , '' , path ) ;
90+ this . location = new Location ( state , path , NavigationTypes . PUSH ) ;
8891 this . _notifyChange ( ) ;
8992 } else {
9093 window . location = path ;
9194 }
9295 }
9396
9497 // http://www.w3.org/TR/2011/WD-html5-20110113/history.html#dom-history-replacestate
95- replace ( path ) {
98+ replaceState ( state , path ) {
9699 if ( this . isSupported ) {
97- var key = createRandomKey ( ) ;
98- window . history . replaceState ( { key } , '' , path ) ;
99- this . location = this . _createLocation ( path , key , NavigationTypes . REPLACE ) ;
100+ state = this . _createState ( state ) ;
101+
102+ window . history . replaceState ( state , '' , path ) ;
103+ this . location = new Location ( state , path , NavigationTypes . REPLACE ) ;
100104 this . _notifyChange ( ) ;
101105 } else {
102106 window . location . replace ( path ) ;
0 commit comments