Added functional preliminary support for browser back button, so that popups can be dismissed by clicking back (particularly important on mobile devices like BlackBerry).

This commit is contained in:
scottjehl 2011-12-05 22:30:07 +07:00
parent 44cfedbe83
commit 4b74d9d738

View file

@ -188,6 +188,17 @@ $.widget("mobile.popup", $.mobile.widget, {
return { x : newleft, y : newtop };
},
_bindHashChange: function(){
var self = this;
$( window ).one( "hashchange.popup", function(){
self.close( true );
});
},
_unbindHashChange: function(){
$( window ).unbind( "hashchange.popup" );
},
open: function(x, y) {
if (!this._isOpen) {
@ -213,12 +224,20 @@ $.widget("mobile.popup", $.mobile.widget, {
.animationComplete(function() {
self._ui.screen.height($(document).height());
});
// listen for hashchange that will occur when we set it to null dialog hash
$( window ).one( "hashchange", function(){
self._bindHashChange();
});
// set hash to non-linkable dialog url
$.mobile.path.set( "&ui-state=dialog" );
this._isOpen = true;
}
},
close: function() {
close: function( fromHash ) {
if (this._isOpen) {
var self = this,
hideScreen = function() {
@ -241,6 +260,14 @@ $.widget("mobile.popup", $.mobile.widget, {
this._ui.screen.animate({opacity: 0.0}, "fast", hideScreen);
else
hideScreen();
// unbind listener that comes with opening popup
this._unbindHashChange();
// if the close event did not come from an internal hash listener, reset URL back
if( !fromHash ){
window.history.back();
}
}
}
});