Skip to content
This repository
Browse code

first pass at disabling history with an option in popup

  • Loading branch information...
commit 5af122fa8532ee34177af7813ddaba6824ece795 1 parent 8416315
John Bender authored August 24, 2012
32  js/widgets/popup.js
@@ -46,7 +46,8 @@ define( [ "jquery",
46 46
 			transition: "none",
47 47
 			positionTo: "origin",
48 48
 			tolerance: null,
49  
-			initSelector: ":jqmData(role='popup')"
  49
+			initSelector: ":jqmData(role='popup')",
  50
+			history: true
50 51
 		},
51 52
 
52 53
 		_eatEventAndClose: function( e ) {
@@ -547,6 +548,9 @@ define( [ "jquery",
547 548
 		_closePrereqsDone: function() {
548 549
 			this._ui.container.removeAttr( "tabindex" );
549 550
 
  551
+			// remove nav bindings if they are still present
  552
+			$.mobile.pageContainer.unbind( "navigate.popup pagebeforechange.popup" );
  553
+
550 554
 			// remove the global mutex for popups
551 555
 			$.mobile.popup.active = undefined;
552 556
 
@@ -595,19 +599,31 @@ define( [ "jquery",
595 599
 			});
596 600
 		},
597 601
 
598  
-
599 602
 		// TODO no clear deliniation of what should be here and
600 603
 		// what should be in _open. Seems to be "visual" vs "history" for now
601 604
 		open: function( options ) {
602 605
 			var hashkey = $.mobile.dialogHashKey,
603  
-				activePage = $.mobile.activePage;
  606
+				activePage = $.mobile.activePage,
  607
+				self = this;
604 608
 
605 609
 			// make sure open is idempotent
606 610
 			if( $.mobile.popup.active ) {
607 611
 				return;
608 612
 			}
609 613
 
610  
-			var self = this, url = activePage.jqmData( "url" );
  614
+			// if history alteration is disabled close on navigate events
  615
+			// and don't modify the url
  616
+			if( !this.options.history ) {
  617
+				this._open( options );
  618
+
  619
+				$.mobile.pageContainer.one( "navigate.popup pagebeforechange.popup", function() {
  620
+					self._close();
  621
+				});
  622
+
  623
+				return;
  624
+			}
  625
+
  626
+			var url = activePage.jqmData( "url" );
611 627
 
612 628
 			// if the current url has no dialog hash key proceed as normal
613 629
 			// otherwise, if the page is a dialog simply tack on the hash key
@@ -641,8 +657,14 @@ define( [ "jquery",
641 657
 
642 658
 		close: function() {
643 659
 			// make sure close is idempotent
644  
-			if( $.mobile.popup.active ){
  660
+			if( !$.mobile.popup.active ){
  661
+				return;
  662
+			}
  663
+
  664
+			if( this.options.history ) {
645 665
 				window.history.back();
  666
+			} else {
  667
+				this._close();
646 668
 			}
647 669
 		}
648 670
 	});
8  tests/unit/popup/index.html
@@ -53,7 +53,15 @@ <h2 id="qunit-userAgent"></h2>
53 53
 			<div data-nstest-role="popup" id="popup-sequence-test">
54 54
 				<a id="popup-sequence-test-open-dialog" href="popup-sequence-test-dialog.html">Dialog</a>
55 55
 			</div>
  56
+
  57
+      <div data-nstest-role="popup" id="test-history-popup" data-nstest-history="false">
  58
+        <p>This is the test popup</p>
  59
+      </div>
56 60
     </div>
57 61
   </div>
  62
+
  63
+  <div data-nstest-role="page" id="no-popups">
  64
+    Page that is not the popup page
  65
+  </div>
58 66
 </body>
59 67
 </html>
36  tests/unit/popup/popup_core.js
@@ -417,7 +417,7 @@
417 417
 	});
418 418
 
419 419
 	asyncTest( "Popup focused after open", function() {
420  
-		var $link = $( "#open-test-popup" ), $popup = $( "#test-popup" ), eventCount = 0;
  420
+		var $link = $( "#open-test-popup" ), $popup = $( "#test-popup" );
421 421
 
422 422
 		expect( 2 );
423 423
 
@@ -433,4 +433,38 @@
433 433
 			});
434 434
 		});
435 435
 	});
  436
+
  437
+	test( "Popup doesn't alter the url when the history option is disabled", function() {
  438
+		var $popup = $( "#test-history-popup" ), hash = $.mobile.path.parseLocation().hash;
  439
+
  440
+		$popup.popup( "open" );
  441
+
  442
+		equal( hash, $.mobile.path.parseLocation().hash, "the hash remains the same" );
  443
+
  444
+		ok( $popup.is( ":visible" ), "popup is indeed visible" );
  445
+
  446
+		$popup.popup( "close" );
  447
+	});
  448
+
  449
+	asyncTest( "Navigating away from the popup page closes the popup without history enabled", function() {
  450
+		var $popup = $( "#test-history-popup" );
  451
+
  452
+		expect( 2 );
  453
+
  454
+		$popup.bind( "popupafterclose", function() {
  455
+			// TODO would be nice to verify that it happens
  456
+			//      right after the first page goes away
  457
+			ok( true, "popup was closed" );
  458
+		});
  459
+
  460
+		$.testHelper.pageSequence([
  461
+			function() {
  462
+				$popup.popup( "open" );
  463
+				ok( $popup.is( ":visible" ), "popup is indeed visible" );
  464
+				$.mobile.changePage( "#no-popups" );
  465
+			},
  466
+
  467
+			start
  468
+		]);
  469
+	});
436 470
 })( jQuery );

0 notes on commit 5af122f

Please sign in to comment.
Something went wrong with that request. Please try again.