From 416b666ca873bd4ef84f6703d418c0bae9b3e260 Mon Sep 17 00:00:00 2001 From: John Bender Date: Thu, 15 Sep 2011 10:16:40 -0700 Subject: [PATCH 1/8] prevent clicks on anything but the left mouse button, also adds the which value to the events when its undefined in the vmouse plugin --- js/jquery.mobile.navigation.js | 11 ++++++++++- js/jquery.mobile.vmouse.js | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/js/jquery.mobile.navigation.js b/js/jquery.mobile.navigation.js index 69068730..590d0bd2 100755 --- a/js/jquery.mobile.navigation.js +++ b/js/jquery.mobile.navigation.js @@ -1177,6 +1177,12 @@ //add active state on vclick $( document ).bind( "vclick", function( event ) { + // if this isn't a left click we don't care. Its important to note + // that when the virtual event is generated it will create + if ( event.which > 1 ){ + return; + } + var link = findClosestLink( event.target ); if ( link ) { if ( path.parseUrl( link.getAttribute( "href" ) || "#" ).hash !== "#" ) { @@ -1191,7 +1197,10 @@ // click routing - direct to HTTP or Ajax, accordingly $( document ).bind( "click", function( event ) { var link = findClosestLink( event.target ); - if ( !link ) { + + // If there is no link associated with the click or its not a left + // click we want to ignore the click + if ( !link || event.which > 1) { return; } diff --git a/js/jquery.mobile.vmouse.js b/js/jquery.mobile.vmouse.js index 47e9823a..a4d38128 100644 --- a/js/jquery.mobile.vmouse.js +++ b/js/jquery.mobile.vmouse.js @@ -74,6 +74,12 @@ function createVirtualEvent( event, eventType ) { } } + // make sure that if the mouse and click virtual events are generated + // without a .which one is defined + if ( t.search(/mouse(down|up)|click/) > -1 && !event.which ){ + event.which = 1; + } + if ( t.search(/^touch/) !== -1 ) { ne = getNativeEvent( oe ); t = ne.touches; From 9cc8daa91a432c692d426c662c03b193191a4520 Mon Sep 17 00:00:00 2001 From: John Bender Date: Thu, 15 Sep 2011 10:25:04 -0700 Subject: [PATCH 2/8] test that middle click doesn't trigger a page change --- tests/unit/navigation/index.html | 8 ++++++ tests/unit/navigation/navigation_core.js | 32 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/tests/unit/navigation/index.html b/tests/unit/navigation/index.html index feb2f278..583cf494 100644 --- a/tests/unit/navigation/index.html +++ b/tests/unit/navigation/index.html @@ -263,5 +263,13 @@ back button + + + +
+ foo +
+ +
diff --git a/tests/unit/navigation/navigation_core.js b/tests/unit/navigation/navigation_core.js index 421816f8..9373c500 100644 --- a/tests/unit/navigation/navigation_core.js +++ b/tests/unit/navigation/navigation_core.js @@ -787,6 +787,38 @@ ]); }); + asyncTest( "clicks with middle mouse button are ignored", function() { + $.testHelper.pageSequence([ + function() { + $.testHelper.openPage( "#odd-clicks-page" ); + }, + + function() { + $( "#right-or-middle-click" ).click(); + }, + + // make sure the page is opening first without the mocked button click value + // only necessary to prevent issues with test specific fixtures + function() { + same($.mobile.activePage[0], $("#odd-clicks-page-dest")[0]); + $.testHelper.openPage( "#odd-clicks-page" ); + + // mock the which value to simulate a middle click + $.Event.prototype.which = 2; + }, + + function() { + $( "#right-or-middle-click" ).click(); + }, + + function( timeout ) { + ok( timeout, "page event handler timed out due to ignored click" ); + ok($.mobile.activePage[0] !== $("#odd-clicks-page-dest")[0], "pages are not the same"); + start(); + } + ]); + }); + asyncTest( "handling of button active state when navigating by clicking back button", 1, function(){ $.testHelper.pageSequence([ // open our test page From 5346a91595ce2b39a155ba53b3aa2b6a8b301bd7 Mon Sep 17 00:00:00 2001 From: John Bender Date: Thu, 15 Sep 2011 10:36:12 -0700 Subject: [PATCH 3/8] added event test to make sure the which was being defined on triggered events --- tests/unit/event/event_core.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/unit/event/event_core.js b/tests/unit/event/event_core.js index a42489d9..453245d0 100644 --- a/tests/unit/event/event_core.js +++ b/tests/unit/event/event_core.js @@ -528,4 +528,23 @@ .trigger( "resize" ) .trigger( "resize" ); }); + + asyncTest( "mousedown mouseup and click events should add a which when its not defined", function() { + var whichDefined = function( event ){ + same(event.which, 1); + }; + + $( document ).bind( "vclick", whichDefined); + $( document ).trigger( "click" ); + + $( document ).bind( "vmousedown", whichDefined); + $( document ).trigger( "mousedown" ); + + $( document ).bind( "vmouseup", function( event ){ + same(event.which, 1); + start(); + }); + + $( document ).trigger( "mouseup" ); + }); })(jQuery); From 15cb663e526f75969e1d63f88cf94c298e4cb4d1 Mon Sep 17 00:00:00 2001 From: John Bender Date: Thu, 15 Sep 2011 10:45:27 -0700 Subject: [PATCH 4/8] suggestion from @jblas to save a bit of execution --- js/jquery.mobile.navigation.pushstate.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/js/jquery.mobile.navigation.pushstate.js b/js/jquery.mobile.navigation.pushstate.js index 8b5a126e..da0bb9db 100644 --- a/js/jquery.mobile.navigation.pushstate.js +++ b/js/jquery.mobile.navigation.pushstate.js @@ -58,15 +58,15 @@ // NOTE this takes place *after* the vanilla navigation hash change // handling has taken place and set the state of the DOM onHashChange: function( e ) { - var href, state, - hash = location.hash, - isPath = $.mobile.path.isPath( hash ); - hash = isPath ? hash.replace( "#", "" ) : hash; - // disable this hash change if( self.onHashChangeDisabled ){ return; } + + var href, state, + hash = location.hash, + isPath = $.mobile.path.isPath( hash ); + hash = isPath ? hash.replace( "#", "" ) : hash; // propulate the hash when its not available state = self.state(); From 56c0b9de367f4264ad7d00c55472303e5b9d231d Mon Sep 17 00:00:00 2001 From: John Bender Date: Thu, 15 Sep 2011 11:10:15 -0700 Subject: [PATCH 5/8] unmock which value for other tests --- tests/unit/navigation/navigation_core.js | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/navigation/navigation_core.js b/tests/unit/navigation/navigation_core.js index 9373c500..0b50b120 100644 --- a/tests/unit/navigation/navigation_core.js +++ b/tests/unit/navigation/navigation_core.js @@ -40,6 +40,7 @@ $.mobile.urlHistory.stack = []; $.mobile.urlHistory.activeIndex = 0; + $.Event.prototype.which = undefined; } }); From 150697c8d26409dac9d3c7785e85abc569c00108 Mon Sep 17 00:00:00 2001 From: John Bender Date: Thu, 15 Sep 2011 15:12:52 -0700 Subject: [PATCH 6/8] whitespace fix for nav core tests --- tests/unit/navigation/navigation_core.js | 36 ++++++++++++------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/tests/unit/navigation/navigation_core.js b/tests/unit/navigation/navigation_core.js index 0b50b120..d53e2df5 100644 --- a/tests/unit/navigation/navigation_core.js +++ b/tests/unit/navigation/navigation_core.js @@ -789,36 +789,36 @@ }); asyncTest( "clicks with middle mouse button are ignored", function() { - $.testHelper.pageSequence([ + $.testHelper.pageSequence([ function() { $.testHelper.openPage( "#odd-clicks-page" ); }, - function() { - $( "#right-or-middle-click" ).click(); - }, + function() { + $( "#right-or-middle-click" ).click(); + }, - // make sure the page is opening first without the mocked button click value - // only necessary to prevent issues with test specific fixtures - function() { - same($.mobile.activePage[0], $("#odd-clicks-page-dest")[0]); + // make sure the page is opening first without the mocked button click value + // only necessary to prevent issues with test specific fixtures + function() { + same($.mobile.activePage[0], $("#odd-clicks-page-dest")[0]); $.testHelper.openPage( "#odd-clicks-page" ); - // mock the which value to simulate a middle click - $.Event.prototype.which = 2; - }, + // mock the which value to simulate a middle click + $.Event.prototype.which = 2; + }, - function() { - $( "#right-or-middle-click" ).click(); - }, + function() { + $( "#right-or-middle-click" ).click(); + }, - function( timeout ) { + function( timeout ) { ok( timeout, "page event handler timed out due to ignored click" ); - ok($.mobile.activePage[0] !== $("#odd-clicks-page-dest")[0], "pages are not the same"); + ok($.mobile.activePage[0] !== $("#odd-clicks-page-dest")[0], "pages are not the same"); start(); - } + } ]); - }); + }); asyncTest( "handling of button active state when navigating by clicking back button", 1, function(){ $.testHelper.pageSequence([ From ae2e7f0dc4348c53dd8945f5489185118f2ae9e4 Mon Sep 17 00:00:00 2001 From: John Bender Date: Thu, 15 Sep 2011 15:14:34 -0700 Subject: [PATCH 7/8] add 'widgetinit' event for users to enhance widgets and markup post 'widgetcreate' --- js/jquery.mobile.widget.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/js/jquery.mobile.widget.js b/js/jquery.mobile.widget.js index 091faa01..919b1e15 100644 --- a/js/jquery.mobile.widget.js +++ b/js/jquery.mobile.widget.js @@ -8,6 +8,16 @@ (function( $, undefined ) { $.widget( "mobile.widget", { + // decorate the parent _createWidget to trigger `widgetinit` for users + // who wish to do post post `widgetcreate` alterations/additions + // + // TODO create a pull request for jquery ui to trigger this event + // in the original _createWidget + _createWidget: function() { + $.Widget.prototype._createWidget.apply( this, arguments ); + this._trigger( 'init' ); + }, + _getCreateOptions: function() { var elem = this.element, From f932c9b13d8103c2b1395533ee0057f6ef138998 Mon Sep 17 00:00:00 2001 From: John Bender Date: Thu, 15 Sep 2011 15:35:48 -0700 Subject: [PATCH 8/8] init test for jqm using page widget --- tests/unit/widget/index.html | 4 ++-- tests/unit/widget/widget_init.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 tests/unit/widget/widget_init.js diff --git a/tests/unit/widget/index.html b/tests/unit/widget/index.html index d33f51ad..9dfa476b 100644 --- a/tests/unit/widget/index.html +++ b/tests/unit/widget/index.html @@ -5,11 +5,12 @@ jQuery Mobile Widget Test Suite + + - @@ -34,6 +35,5 @@
- diff --git a/tests/unit/widget/widget_init.js b/tests/unit/widget/widget_init.js new file mode 100644 index 00000000..2cd47073 --- /dev/null +++ b/tests/unit/widget/widget_init.js @@ -0,0 +1,16 @@ +/* + * mobile widget unit tests + */ +(function($){ + var initFired = false; + + module( 'jquery.mobile.widget.js' ); + + $( "#foo" ).live( 'pageinit', function(){ + initFired = true; + }); + + test( "widget init event is fired after markup enhancement has taken place", function() { + ok( initFired ); + }); +})( jQuery ); \ No newline at end of file