From ffafa78e628780fb68df863d0597ee5bbc7e8c1d Mon Sep 17 00:00:00 2001 From: John Bender Date: Thu, 20 Jan 2011 22:20:07 -0800 Subject: [PATCH] added tests for external links in dialogs, small refactor for dialogClickHandler function --- js/jquery.mobile.dialog.js | 32 ++++++------ tests/unit/dialog/dialog_events.js | 39 +++++++++++++++ tests/unit/dialog/index.html | 78 ++++++++++++++++++++++++++++++ 3 files changed, 133 insertions(+), 16 deletions(-) create mode 100644 tests/unit/dialog/dialog_events.js create mode 100644 tests/unit/dialog/index.html diff --git a/js/jquery.mobile.dialog.js b/js/jquery.mobile.dialog.js index c288c4bc..09921f6b 100644 --- a/js/jquery.mobile.dialog.js +++ b/js/jquery.mobile.dialog.js @@ -11,25 +11,25 @@ $.widget( "mobile.dialog", $.mobile.widget, { var self = this, $el = self.element, $prevPage = $.mobile.activePage, - $closeBtn = $('Close'); + $closeBtn = $('Close'), + + dialogClickHandler = function(e){ + var $target = $(e.target); - var dialogClickHandler = function(e){ - var $target = $(e.target); + // fixes issues with target links in dialogs breaking + // page transitions by reseting the active page below + if( $target.attr('target') || $.mobile.isExternalLink($target) ) { + return; + } - // fixes issues with target links in dialogs breaking - // page transitions by reseting the active page below - if( $target.attr('target') || $.mobile.isExternalLink($target) ) { - return; - } + if( e.type == "click" && ( $(e.target).closest('[data-back]')[0] || this==$closeBtn[0] ) ){ + self.close(); + return false; + } - if( e.type == "click" && ( $(e.target).closest('[data-back]')[0] || this==$closeBtn[0] ) ){ - self.close(); - return false; - } - - //otherwise, assume we're headed somewhere new. set activepage to dialog so the transition will work - $.mobile.activePage = self.element; - }; + //otherwise, assume we're headed somewhere new. set activepage to dialog so the transition will work + $.mobile.activePage = self.element; + }; // NOTE avoid click handler in the case of an external resource // TODO add function in navigation to handle external check diff --git a/tests/unit/dialog/dialog_events.js b/tests/unit/dialog/dialog_events.js new file mode 100644 index 00000000..1915d17e --- /dev/null +++ b/tests/unit/dialog/dialog_events.js @@ -0,0 +1,39 @@ +/* + * mobile dialog unit tests + */ +(function($){ + var testValue = "a value we can test!"; + + module('jquery.mobile.dialog.js', { + setup: function(){ + //bring up the dialog + $("a[href='#foo-dialog']").click(); + + // We prefer the external links don't send the browser elsewhere + $("#foo-dialog").delegate("a", "click", function(e){ + return false; + }); + + // set a test value to verify against change + $.mobile.activePage = testValue; + } + }); + + test( "external and targeted links do not set the active page", function(){ + var verifyActivePage = function(selector){ + $(selector).click(); + same( $.mobile.activePage, testValue, "mobile active page remains untouched"); + }; + + verifyActivePage("a#http-link"); + verifyActivePage("a#mailto-link"); + verifyActivePage("a#rel-link"); + verifyActivePage("a#target-link"); + }); + + test( "non external links set the active page", function(){ + $.mobile.activePage = testValue; + $("a#internal-link").click(); + ok( $.mobile.activePage !== testValue, "mobile active page remains untouched"); + }); +})(jQuery); diff --git a/tests/unit/dialog/index.html b/tests/unit/dialog/index.html new file mode 100644 index 00000000..7ef1114e --- /dev/null +++ b/tests/unit/dialog/index.html @@ -0,0 +1,78 @@ + + + + + jQuery Mobile Dialog Test Suite + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +

jQuery Mobile Dialog Test Suite

+

+

+
    +
+ + + + + +
+ +
+ +
+ foo + foo + foo + foo + foo +
+ + +