From d2361b5424f3fdbd446182267914de55e2432abe Mon Sep 17 00:00:00 2001 From: John Bender Date: Mon, 27 Aug 2012 09:28:30 -0700 Subject: [PATCH] enable close button when history is disabled --- js/widgets/popup.js | 27 +++++++++++++++++++++++---- tests/unit/popup/index.html | 1 + tests/unit/popup/popup_core.js | 12 ++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/js/widgets/popup.js b/js/widgets/popup.js index 3456bf49f34..ad84b1a8c75 100644 --- a/js/widgets/popup.js +++ b/js/widgets/popup.js @@ -49,7 +49,7 @@ define( [ "jquery", initSelector: ":jqmData(role='popup')", navigateEvents: "navigate.popup", closeEvents: "navigate.popup pagebeforechange.popup", - history: true + history: false }, _eatEventAndClose: function( e ) { @@ -552,16 +552,21 @@ define( [ "jquery", }, _closePrereqsDone: function() { - this._ui.container.removeAttr( "tabindex" ); + var self = this; + + self._ui.container.removeAttr( "tabindex" ); // remove nav bindings if they are still present - $.mobile.pageContainer.unbind( this.options.closeEvents ); + self.options.container.unbind( self.options.closeEvents ); + + // unbind click handlers added when history is disabled + self.options.container.undelegate( "a:jqmData(rel='back')", "click.popup" ); // remove the global mutex for popups $.mobile.popup.active = undefined; // alert users that the popup is closed - this._trigger( "afterclose" ); + self._trigger( "afterclose" ); }, _close: function() { @@ -632,6 +637,20 @@ define( [ "jquery", // and leave the url as is if( !self.options.history ) { self._bindContainerClose(); + + // When histoy is disabled we have to grab the data-rel + // back link clicks so we can close the popup instead of + // relying on history to do it for us + self.options.container + .delegate( "a:jqmData(rel='back')", "click.popup", function( e ) { + self._close(); + + // NOTE prevent the browser and navigation handlers from + // working with the link's rel=back. This may cause + // issues for developers expecting the event to bubble + return false; + }); + return; } diff --git a/tests/unit/popup/index.html b/tests/unit/popup/index.html index 410c091fd72..9bcebaed7c3 100644 --- a/tests/unit/popup/index.html +++ b/tests/unit/popup/index.html @@ -56,6 +56,7 @@

This is the test popup

+ Close
diff --git a/tests/unit/popup/popup_core.js b/tests/unit/popup/popup_core.js index cba9e2439d4..ba629bbca19 100644 --- a/tests/unit/popup/popup_core.js +++ b/tests/unit/popup/popup_core.js @@ -467,4 +467,16 @@ start ]); }); + + test( "Close links work on a history disabled popup", function() { + var $popup = $( "#test-history-popup" ); + + expect( 2 ); + + $popup.popup( 'open' ); + ok( $.mobile.popup.active, "popup is shown on link click" ); + + $popup.find( "a" ).click(); + ok( !$.mobile.popup.active, "popup is hidden on link click" ); + }); })( jQuery );