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

Commit

Permalink
bstract backward movement in history, Fixes #4950
Browse files Browse the repository at this point in the history
`window.history.back` doesn't work in phonegap applications
after a page refresh, though it does work under hashchange/replacestate.
The solution is to use their `navigator.app.backHistory` method
along with a configuration option. The reasoning for the option
is to prevent any corner cases popping up with existing phonegap
applications. Forward history movement for the same usecase is _not_
addressed but remains an even lower priority.
  • Loading branch information
johnbender committed Sep 12, 2012
1 parent 6df2479 commit c3b89eb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
7 changes: 3 additions & 4 deletions js/jquery.mobile.dialog.js
Expand Up @@ -25,7 +25,7 @@ $.widget( "mobile.dialog", $.mobile.widget, {
});

$el.addClass( "ui-dialog ui-overlay-" + this.options.overlayTheme );

// Class the markup for dialog styling
// Set aria role
$el
Expand Down Expand Up @@ -88,9 +88,8 @@ $.widget( "mobile.dialog", $.mobile.widget, {
if ( !this._isClosed ) {
this._isClosed = true;
if ( $.mobile.hashListeningEnabled ) {
window.history.back();
}
else {
$.mobile.back();
} else {
dst = $.mobile.urlHistory.getPrev().url;
if ( !$.mobile.path.isPath( dst ) ) {
dst = $.mobile.path.makeUrlAbsolute( "#" + dst );
Expand Down
2 changes: 1 addition & 1 deletion js/jquery.mobile.forms.select.custom.js
Expand Up @@ -321,7 +321,7 @@ define( [
// doesn't solve the possible issue with calling change page
// where the objects don't define data urls which prevents dialog key
// stripping - changePage has incoming refactor
window.history.back();
$.mobile.back();
} else {
self.screen.addClass( "ui-screen-hidden" );
self.listbox.addClass( "ui-selectmenu-hidden" ).removeAttr( "style" ).removeClass( "in" );
Expand Down
31 changes: 24 additions & 7 deletions js/jquery.mobile.navigation.js
Expand Up @@ -410,10 +410,27 @@ define( [

} : undefined;

/*
internal utility functions
--------------------------------------*/

/* internal utility functions */

// NOTE Issue #4950 Android phonegap doesn't navigate back properly
// when a full page refresh has taken place. It appears that hashchange
// and replacestate history alterations work fine but we need to support
// both forms of history traversal in our code that uses backward history
// movement
$.mobile.back = function() {
var nav = window.navigator;

// if the setting is on and the navigator object is
// available use the phonegap navigation capability
if( this.phonegapNavigationEnabled &&
nav &&
nav.app &&
nav.app.backHistory ){
nav.app.backHistory();
} else {
window.history.back();
}
};

//direct focus to the page title, or otherwise first focusable element
$.mobile.focusPage = function ( page ) {
Expand Down Expand Up @@ -1319,8 +1336,8 @@ define( [
};

//if there's a data-rel=back attr, go back in history
if( $link.is( ":jqmData(rel='back')" ) ) {
window.history.back();
if ( $link.is( ":jqmData(rel='back')" ) ) {
$.mobile.back();
return false;
}

Expand Down Expand Up @@ -1445,7 +1462,7 @@ define( [
//the current dialog
urlHistory.directHashChange({
currentUrl: to,
isBack: function() { window.history.back(); },
isBack: function() { $.mobile.back(); },
isForward: function() { window.history.forward(); }
});

Expand Down
2 changes: 1 addition & 1 deletion js/jquery.mobile.navigation.pushstate.js
Expand Up @@ -95,7 +95,7 @@ define( [ "jquery", "./jquery.mobile.navigation", "../external/requirejs/depend!
// Note that in some cases we might be replacing an url with the
// same url. We do this anyways because we need to make sure that
// all of our history entries have a state object associated with
// them. This allows us to work around the case where window.history.back()
// them. This allows us to work around the case where $.mobile.back()
// is called to transition from an external page to an embedded page.
// In that particular case, a hashchange event is *NOT* generated by the browser.
// Ensuring each history entry has a state object means that onPopState()
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/navigation/navigation_helpers.js
Expand Up @@ -249,4 +249,22 @@

equal( $.mobile.path.getLocation( allUriParts ), allUriParts.replace( "jblas:password@", "") );
});

test( "calling mobile back uses phonegap's navigator object when present", function() {
var previous = $.mobile.phonegapNavigationEnabled;

expect( 1 );

$.mobile.phonegapNavigationEnabled = true;
window.navigator = {
app: {
backHistory: function() {
ok( true, "history back called" );
}
}
};

$.mobile.back();
$.mobile.phonegapNavigationEnabled = previous;
});
})(jQuery);

0 comments on commit c3b89eb

Please sign in to comment.