Skip to content
This repository was archived by the owner on Oct 8, 2021. It is now read-only.

Commit 76663b0

Browse files
committed
fix initial state for alternate init case
1 parent 47f82a0 commit 76663b0

File tree

2 files changed

+121
-117
lines changed

2 files changed

+121
-117
lines changed

js/jquery.mobile.init.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,12 @@ define([
104104
$.mobile.urlHistory.initialDst = hash.replace( "#", "" );
105105
}
106106

107-
var loc = path.parseLocation();
108-
// $.navigate.history.add( loc.href, {hash: loc.hash} );
107+
// make sure to set initial popstate state if it exists
108+
// so that navigation back to the initial page works properly
109+
if( $.event.special.navigate.isPushStateEnabled() ) {
110+
$.navigate.navigator.squash( path.parseLocation().href );
111+
}
112+
109113
$.mobile.changePage( $.mobile.firstPage, {
110114
transition: "none",
111115
reverse: true,
@@ -115,7 +119,7 @@ define([
115119
} else {
116120
// trigger hashchange or navigate to squash and record the correct
117121
// history entry for an initial hash path
118-
if( $.event.special.navigate.originalEventName === "hashchange" ) {
122+
if( !$.event.special.navigate.isPushStateEnabled() ) {
119123
$window.trigger( "hashchange", [true] );
120124
} else {
121125
// TODO figure out how to simplify this interaction with the initial history entry

js/navigation/navigate.js

Lines changed: 114 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -17,121 +17,121 @@ define([
1717
this.history = history;
1818
};
1919

20-
$.extend($.Navigator.prototype, {
21-
squash: function( url, data ) {
22-
var state, href,
23-
hash = path.isPath(url) ? path.stripHash(url) : url;
24-
25-
href = path.squash( url );
26-
27-
// make sure to provide this information when it isn't explicitly set in the
28-
// data object that was passed to the squash method
29-
state = $.extend({
30-
hash: hash,
31-
url: href
32-
}, data);
33-
34-
// replace the current url with the new href and store the state
35-
// Note that in some cases we might be replacing an url with the
36-
// same url. We do this anyways because we need to make sure that
37-
// all of our history entries have a state object associated with
38-
// them. This allows us to work around the case where $.mobile.back()
39-
// is called to transition from an external page to an embedded page.
40-
// In that particular case, a hashchange event is *NOT* generated by the browser.
41-
// Ensuring each history entry has a state object means that onPopState()
42-
// will always trigger our hashchange callback even when a hashchange event
43-
// is not fired.
44-
window.history.replaceState( state, state.title || document.title, href );
45-
46-
return state;
47-
},
48-
49-
go: function( url, data, noEvents ) {
50-
var state, href, parsed, loc, hash, popstateEvent,
51-
isPopStateEvent = $.event.special.navigate.isPushStateEnabled(),
52-
resolutionUrl = path.isPath( url ) ? path.getLocation() : $.mobile.getDocumentUrl();
53-
54-
// Get the url as it would look squashed on to the current resolution url
55-
href = path.squash( url );
56-
57-
// Grab the hash for recording. If the passed url is a path
58-
// we used the parsed version of the squashed url to reconstruct,
59-
// otherwise we assume it's a hash and store it directly
60-
parsed = path.parseUrl( url );
61-
loc = path.parseLocation();
62-
63-
if( loc.pathname + loc.search === parsed.pathname + parsed.search ) {
64-
// If the pathname and search of the passed url is identical to the current loc
65-
// then we must use the hash. Otherwise there will be no event
66-
// eg, url = "/foo/bar?baz#bang", location.href = "http://example.com/foo/bar?baz"
67-
hash = parsed.hash ? parsed.hash : parsed.pathname + parsed.search;
68-
} else if ( path.isPath(url) ) {
69-
var resolved = path.parseUrl( href );
70-
// If the passed url is a path, make it domain relative and remove any trailing hash
71-
hash = resolved.pathname + resolved.search + (path.isPreservableHash( resolved.hash )? resolved.hash.replace( "#", "" ) : "");
72-
} else {
73-
hash = url;
74-
}
75-
76-
// Here we prevent the next hash change or popstate event from doing any
77-
// history management. In the case of hashchange we don't swallow it
78-
// if there will be no hashchange fired (since that won't reset the value)
79-
// and will swallow the following hashchange
80-
history.ignoreNextHashChange = true;
81-
if( noEvents && hash !== path.stripHash(path.parseLocation().hash) ) {
82-
history.preventNextHashChange = noEvents;
83-
}
84-
85-
// IMPORTANT in the case where popstate is supported the event will be triggered
86-
// directly, stopping further execution - ie, interupting the flow of this
87-
// method call to fire bindings at this expression. Below the navigate method
88-
// there is a binding to catch this event and stop its propagation.
89-
//
90-
// We then trigger a new popstate event on the window with a null state
91-
// so that the navigate events can conclude their work properly
92-
//
93-
// if the url is a path we want to preserve the query params that are available on
94-
// the current url.
95-
window.location.hash = hash;
96-
97-
state = $.extend({
98-
url: href,
99-
hash: hash,
100-
title: document.title
101-
}, data);
102-
103-
if( isPopStateEvent ) {
104-
popstateEvent = new $.Event( "popstate" );
105-
popstateEvent.originalEvent = {
106-
type: "popstate",
107-
state: null
108-
};
109-
110-
this.squash( url, state );
111-
112-
// Trigger a new faux popstate event to replace the one that we
113-
// caught that was triggered by the hash setting above.
114-
if( !noEvents ) {
115-
history.ignoreNextPopState = true;
116-
$( window ).trigger( popstateEvent );
117-
}
118-
}
119-
120-
// record the history entry so that the information can be included
121-
// in hashchange event driven navigate events in a similar fashion to
122-
// the state that's provided by popstate
123-
history.add( state.url, state );
124-
}
125-
});
126-
127-
// TODO replace singleton history object
128-
$.History = function() {
129-
this.stack = [];
130-
this.activeIndex = 0;
131-
this.initialDst = path.parseLocation().hash.replace( /^#/, "" );
20+
$.extend($.Navigator.prototype, {
21+
squash: function( url, data ) {
22+
var state, href,
23+
hash = path.isPath(url) ? path.stripHash(url) : url;
24+
25+
href = path.squash( url );
26+
27+
// make sure to provide this information when it isn't explicitly set in the
28+
// data object that was passed to the squash method
29+
state = $.extend({
30+
hash: hash,
31+
url: href
32+
}, data);
33+
34+
// replace the current url with the new href and store the state
35+
// Note that in some cases we might be replacing an url with the
36+
// same url. We do this anyways because we need to make sure that
37+
// all of our history entries have a state object associated with
38+
// them. This allows us to work around the case where $.mobile.back()
39+
// is called to transition from an external page to an embedded page.
40+
// In that particular case, a hashchange event is *NOT* generated by the browser.
41+
// Ensuring each history entry has a state object means that onPopState()
42+
// will always trigger our hashchange callback even when a hashchange event
43+
// is not fired.
44+
window.history.replaceState( state, state.title || document.title, href );
45+
46+
return state;
47+
},
48+
49+
// TODO reconsider name
50+
go: function( url, data, noEvents ) {
51+
var state, href, parsed, loc, hash, popstateEvent,
52+
isPopStateEvent = $.event.special.navigate.isPushStateEnabled(),
53+
resolutionUrl = path.isPath( url ) ? path.getLocation() : $.mobile.getDocumentUrl();
54+
55+
// Get the url as it would look squashed on to the current resolution url
56+
href = path.squash( url );
57+
58+
// Grab the hash for recording. If the passed url is a path
59+
// we used the parsed version of the squashed url to reconstruct,
60+
// otherwise we assume it's a hash and store it directly
61+
parsed = path.parseUrl( url );
62+
loc = path.parseLocation();
63+
64+
if( loc.pathname + loc.search === parsed.pathname + parsed.search ) {
65+
// If the pathname and search of the passed url is identical to the current loc
66+
// then we must use the hash. Otherwise there will be no event
67+
// eg, url = "/foo/bar?baz#bang", location.href = "http://example.com/foo/bar?baz"
68+
hash = parsed.hash ? parsed.hash : parsed.pathname + parsed.search;
69+
} else if ( path.isPath(url) ) {
70+
var resolved = path.parseUrl( href );
71+
// If the passed url is a path, make it domain relative and remove any trailing hash
72+
hash = resolved.pathname + resolved.search + (path.isPreservableHash( resolved.hash )? resolved.hash.replace( "#", "" ) : "");
73+
} else {
74+
hash = url;
75+
}
76+
77+
// Here we prevent the next hash change or popstate event from doing any
78+
// history management. In the case of hashchange we don't swallow it
79+
// if there will be no hashchange fired (since that won't reset the value)
80+
// and will swallow the following hashchange
81+
history.ignoreNextHashChange = true;
82+
if( noEvents && hash !== path.stripHash(path.parseLocation().hash) ) {
83+
history.preventNextHashChange = noEvents;
84+
}
85+
86+
// IMPORTANT in the case where popstate is supported the event will be triggered
87+
// directly, stopping further execution - ie, interupting the flow of this
88+
// method call to fire bindings at this expression. Below the navigate method
89+
// there is a binding to catch this event and stop its propagation.
90+
//
91+
// We then trigger a new popstate event on the window with a null state
92+
// so that the navigate events can conclude their work properly
93+
//
94+
// if the url is a path we want to preserve the query params that are available on
95+
// the current url.
96+
window.location.hash = hash;
97+
98+
state = $.extend({
99+
url: href,
100+
hash: hash,
101+
title: document.title
102+
}, data);
103+
104+
if( isPopStateEvent ) {
105+
popstateEvent = new $.Event( "popstate" );
106+
popstateEvent.originalEvent = {
107+
type: "popstate",
108+
state: null
109+
};
110+
111+
this.squash( url, state );
112+
113+
// Trigger a new faux popstate event to replace the one that we
114+
// caught that was triggered by the hash setting above.
115+
if( !noEvents ) {
116+
history.ignoreNextPopState = true;
117+
$( window ).trigger( popstateEvent );
118+
}
119+
}
120+
121+
// record the history entry so that the information can be included
122+
// in hashchange event driven navigate events in a similar fashion to
123+
// the state that's provided by popstate
124+
history.add( state.url, state );
125+
}
126+
});
127+
128+
$.History = function( stack, index, initialDestination ) {
129+
this.stack = stack || [];
130+
this.activeIndex = index || 0;
131+
this.initialDst = initialDestination || path.parseLocation().hash.replace( /^#/, "" );
132132
};
133133

134-
$.extend($.History.prototype, {
134+
$.extend($.History.prototype, {
135135
getActive: function() {
136136
return this.stack[ this.activeIndex ];
137137
},
@@ -253,7 +253,7 @@ define([
253253
// existing navigation functionalty that is tightly coupled to the history information
254254
$.navigate.history = history = new $.History();
255255

256-
// instantiate an instance of the navigator for use within the $.navigate method
256+
// instantiate an instance of the navigator for use within the $.navigate method
257257
$.navigate.navigator = new $.Navigator( history );
258258

259259
// This binding is intended to catch the popstate events that are fired

0 commit comments

Comments
 (0)