JGonera has uploaded a new change for review.
https://gerrit.wikimedia.org/r/73900
Change subject: Make editor button work on Android 2.x
......................................................................
Make editor button work on Android 2.x
Apparently, the event object passed to hashchange callbacks in Android
Browser 2.x does not contain some properties available in newer browsers.
This patch changes the way the hash is fetched making the Router more
reliable on older browsers.
Change-Id: I25716393e7c8526c709f39375b06453a6dec26ef
---
M javascripts/common/Router.js
M tests/javascripts/common/test_Router.js
2 files changed, 42 insertions(+), 27 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend
refs/changes/00/73900/1
diff --git a/javascripts/common/Router.js b/javascripts/common/Router.js
index bfbd310..ce8c1b7 100644
--- a/javascripts/common/Router.js
+++ b/javascripts/common/Router.js
@@ -11,9 +11,8 @@
return false;
}
- function extractHash( url ) {
- var match = url.match( /#(.*)$/ );
- return ( match && match[1] ) || '';
+ function getHash() {
+ return window.location.hash.slice( 1 );
}
function Router() {
@@ -22,28 +21,30 @@
// duplicate entries that already exist
this.routes = {};
this._enabled = true;
+ this._oldHash = getHash();
- $( window ).on( 'hashchange', function( ev ) {
- // hashchange is async and location.hash might not
contain the right hash anymore
- var hash = extractHash( ev.originalEvent.newURL ),
routeEv = $.Event();
+ $( window ).on( 'hashchange', function() {
+ // ev.originalEvent.newURL is undefined on Android 2.x
+ var hash = getHash(), routeEv = $.Event();
- if ( !self._enabled ) {
- self._enabled = true;
- return;
- }
+ if ( self._enabled ) {
+ self.emit( 'route', routeEv );
- self.emit( 'route', routeEv );
-
- if ( !routeEv.isDefaultPrevented() ) {
- $.each( self.routes, function( id, entry ) {
- return !matchRoute( hash, entry );
- } );
+ if ( !routeEv.isDefaultPrevented() ) {
+ $.each( self.routes, function( id,
entry ) {
+ return !matchRoute( hash, entry
);
+ } );
+ } else {
+ // if route was prevented, ignore the
next hash change and revert the
+ // hash to its old value
+ self._enabled = false;
+ window.location.hash = self._oldHash;
+ }
} else {
- // if route was prevented, ignore the next hash
change and revert the
- // hash to its old value
- self._enabled = false;
- window.location.hash = extractHash(
ev.originalEvent.oldURL );
+ self._enabled = true;
}
+
+ self._oldHash = hash;
} );
}
diff --git a/tests/javascripts/common/test_Router.js
b/tests/javascripts/common/test_Router.js
index 09ba082..3c97496 100644
--- a/tests/javascripts/common/test_Router.js
+++ b/tests/javascripts/common/test_Router.js
@@ -1,18 +1,32 @@
( function( M, $ ) {
- var Router = M.require( 'Router' ), router;
+ var Router = M.require( 'Router' ), hashQueue = [], interval, router;
+
+ // we can't change hash too quickly because hashchange callbacks are
async
+ // (don't fire immediately after the hash is changed) and all the
callbacks
+ // would get the same (latest) hash; see setup and teardown too
+ function setHash( hash ) {
+ hashQueue.push( hash );
+ }
QUnit.module( 'MobileFrontend Router', {
setup: function() {
router = new Router();
+ interval = setInterval( function() {
+ var hash = hashQueue.pop();
+ if ( hash !== undefined ) {
+ window.location.hash = hash;
+ }
+ }, 10 );
},
teardown: function() {
// hashchange is async, we need to wait
$( window ).one( 'hashchange.test', function() {
$( window ).off( 'hashchange.test' );
+ clearInterval( interval );
QUnit.start();
} );
- window.location.hash = '';
+ setHash( '' );
QUnit.stop();
}
} );
@@ -22,7 +36,7 @@
assert.ok( true, 'run callback for route' );
QUnit.start();
} );
- window.location.hash = '#teststring';
+ setHash( '#teststring' );
} );
QUnit.asyncTest( '#route, RegExp', 1, function( assert ) {
@@ -30,8 +44,8 @@
assert.strictEqual( param, '123', 'run callback for
route with correct params' );
QUnit.start();
} );
- window.location.hash = '#testre-abc';
- window.location.hash = '#testre-123';
+ setHash( '#testre-abc' );
+ setHash( '#testre-123' );
} );
QUnit.asyncTest( 'on route', 2, function( assert ) {
@@ -41,12 +55,12 @@
// try preventing second route (#testprevent)
router.one( 'route', function() {
+ setHash( '#testprevent' );
router.one( 'route', function( ev ) {
ev.preventDefault();
} );
} );
- window.location.hash = '#initial';
- window.location.hash = '#testprevent';
+ setHash( '#initial' );
$( window ).on( 'hashchange.test', function() {
++count;
--
To view, visit https://gerrit.wikimedia.org/r/73900
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I25716393e7c8526c709f39375b06453a6dec26ef
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: JGonera <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits