Jdlrobson has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/288005

Change subject: Hygiene: Switch to using mediawiki.router
......................................................................

Hygiene: Switch to using mediawiki.router

Bug: T134908
Change-Id: I6517b7c4f6c9d69ebdd0d1fc1cd3c390dcaaa75f
---
M .jshintrc
M extension.json
M includes/MobileFrontend.hooks.php
M resources/mobile.startup/OverlayManager.js
D resources/mobile.startup/Router.js
M resources/skins.minerva.categories/init.js
M resources/skins.minerva.editor/init.js
M resources/skins.minerva.notifications/init.js
M resources/skins.minerva.scripts/cleanuptemplates.js
M resources/skins.minerva.scripts/init.js
M resources/skins.minerva.scripts/preInit.js
M resources/skins.minerva.talk/init.js
D tests/qunit/mobile.startup/test_Router.js
13 files changed, 15 insertions(+), 289 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend 
refs/changes/05/288005/1

diff --git a/.jshintrc b/.jshintrc
index 6ebf058..48a8b0c 100644
--- a/.jshintrc
+++ b/.jshintrc
@@ -1,5 +1,6 @@
 {
        "globals": {
+               "require": false,
                "module": false,
                "console": true,
                "jQuery": true,
diff --git a/extension.json b/extension.json
index b5ba64b..b496685 100644
--- a/extension.json
+++ b/extension.json
@@ -623,7 +623,6 @@
                        ],
                        "scripts": [
                                "resources/mobile.startup/util.js",
-                               "resources/mobile.startup/Router.js",
                                "resources/mobile.startup/OverlayManager.js",
                                "resources/mobile.startup/PageGateway.js",
                                "resources/mobile.startup/Anchor.js",
@@ -1613,6 +1612,7 @@
                                "desktop"
                        ],
                        "dependencies": [
+                               "mediawiki.router",
                                "mobile.startup",
                                "mobile.mainMenu",
                                
"mobile.loggingSchemas.mobileWebLanguageSwitcher",
@@ -1730,6 +1730,8 @@
                                "desktop"
                        ],
                        "dependencies": [
+                               "mediawiki.router",
+                               "skins.minerva.scripts",
                                "mobile.overlays"
                        ],
                        "scripts": [
diff --git a/includes/MobileFrontend.hooks.php 
b/includes/MobileFrontend.hooks.php
index 84ccd7b..4d7301a 100644
--- a/includes/MobileFrontend.hooks.php
+++ b/includes/MobileFrontend.hooks.php
@@ -1094,6 +1094,7 @@
                                'skins.minerva.notifications' => 
$resourceBoilerplate + [
                                        'dependencies' => [
                                                'mobile.overlays',
+                                               'mediawiki.router',
                                                'skins.minerva.scripts',
                                                'mediawiki.ui.anchor'
                                        ],
diff --git a/resources/mobile.startup/OverlayManager.js 
b/resources/mobile.startup/OverlayManager.js
index 70a36ff..c0a9a6b 100644
--- a/resources/mobile.startup/OverlayManager.js
+++ b/resources/mobile.startup/OverlayManager.js
@@ -1,8 +1,4 @@
 ( function ( M, $ ) {
-
-       var overlayManager,
-               router = M.require( 'mobile.startup/router' );
-
        /**
         * Manages opening and closing overlays when the URL hash changes to one
         * of the registered values (see OverlayManager.add()).
@@ -246,9 +242,6 @@
                }
        } );
 
-       overlayManager = new OverlayManager( router );
-
        M.define( 'mobile.startup/OverlayManager', OverlayManager );
-       M.define( 'mobile.startup/overlayManager', overlayManager );
 
 }( mw.mobileFrontend, jQuery ) );
diff --git a/resources/mobile.startup/Router.js 
b/resources/mobile.startup/Router.js
deleted file mode 100644
index 0860906..0000000
--- a/resources/mobile.startup/Router.js
+++ /dev/null
@@ -1,179 +0,0 @@
-// FIXME: Merge this code with OverlayManager
-( function ( M, $ ) {
-
-       var router;
-
-       /**
-        * Does hash match entry.path?
-        * @method
-        * @private
-        * @ignore
-        * @param {String} hash String to match
-        * @param {Object} entry Entry object
-        * @returns {Boolean} Whether hash matches entry.path
-        */
-       function matchRoute( hash, entry ) {
-               var match = hash.match( entry.path );
-               if ( match ) {
-                       entry.callback.apply( this, match.slice( 1 ) );
-                       return true;
-               }
-               return false;
-       }
-
-       /**
-        * Provides navigation routing and location information
-        * @class Router
-        * @mixins OO.EventEmitter
-        */
-       function Router() {
-               var self = this;
-               OO.EventEmitter.call( this );
-               // use an object instead of an array for routes so that we don't
-               // duplicate entries that already exist
-               this.routes = {};
-               this._enabled = true;
-               this._oldHash = this.getPath();
-
-               $( window ).on( 'popstate', function () {
-                       self.emit( 'popstate' );
-               } );
-
-               $( window ).on( 'hashchange', function () {
-                       self.emit( 'hashchange' );
-               } );
-
-               this.on( 'hashchange', function () {
-                       // ev.originalEvent.newURL is undefined on Android 2.x
-                       var routeEv;
-
-                       if ( self._enabled ) {
-                               routeEv = $.Event( 'route', {
-                                       path: self.getPath()
-                               } );
-                               self.emit( 'route', routeEv );
-
-                               if ( !routeEv.isDefaultPrevented() ) {
-                                       self.checkRoute();
-                               } else {
-                                       // if route was prevented, ignore the 
next hash change and revert the
-                                       // hash to its old value
-                                       self._enabled = false;
-                                       self.navigate( self._oldHash );
-                               }
-                       } else {
-                               self._enabled = true;
-                       }
-
-                       self._oldHash = self.getPath();
-               } );
-       }
-       OO.mixinClass( Router, OO.EventEmitter );
-
-       /**
-        * Check the current route and run appropriate callback if it matches.
-        * @method
-        */
-       Router.prototype.checkRoute = function () {
-               var hash = this.getPath();
-
-               $.each( this.routes, function ( id, entry ) {
-                       return !matchRoute( hash, entry );
-               } );
-       };
-
-       /**
-        * Bind a specific callback to a hash-based route, e.g.
-        *
-        *     @example
-        *     route( 'alert', function () { alert( 'something' ); } );
-        *     route( /hi-(.*)/, function ( name ) { alert( 'Hi ' + name ) } );
-        *
-        * @method
-        * @param {Object} path String or RegExp to match.
-        * @param {Function} callback Callback to be run when hash changes to 
one
-        * that matches.
-        */
-       Router.prototype.route = function ( path, callback ) {
-               var entry = {
-                       path: typeof path === 'string' ? new RegExp( '^' + path 
+ '$' ) : path,
-                       callback: callback
-               };
-               this.routes[entry.path] = entry;
-               matchRoute( this.getPath(), entry );
-       };
-
-       /**
-        * Navigate to a specific route. This is only a wrapper for changing the
-        * hash now.
-        *
-        * @method
-        * @param {String} path String with a route (hash without #).
-        */
-       Router.prototype.navigate = function ( path ) {
-               window.location.hash = path;
-       };
-
-       /**
-        * Triggers back on the window
-        */
-       Router.prototype.goBack = function () {
-               window.history.back();
-       };
-
-       /**
-        * Navigate to the previous route. This is a wrapper for 
window.history.back
-        * @method
-        * @return {jQuery.Deferred}
-        */
-       Router.prototype.back = function () {
-               var deferredRequest = $.Deferred(),
-                       self = this,
-                       timeoutID;
-
-               this.once( 'popstate', function () {
-                       clearTimeout( timeoutID );
-                       deferredRequest.resolve();
-               } );
-
-               this.goBack();
-
-               // If for some reason (old browser, bug in IE/windows 8.1, etc) 
popstate doesn't fire,
-               // resolve manually. Since we don't know for sure which 
browsers besides IE10/11 have
-               // this problem, it's better to fall back this way rather than 
singling out browsers
-               // and resolving the deferred request for them individually.
-               // See 
https://connect.microsoft.com/IE/feedback/details/793618/history-back-popstate-not-working-as-expected-in-webview-control
-               // Give browser a few ms to update its history.
-               timeoutID = setTimeout( function () {
-                       self.off( 'popstate' );
-                       deferredRequest.resolve();
-               }, 50 );
-
-               return deferredRequest;
-       };
-
-       /**
-        * Get current path (hash).
-        *
-        * @method
-        * @return {String} Current path.
-        */
-       Router.prototype.getPath = function () {
-               return window.location.hash.slice( 1 );
-       };
-
-       /**
-        * Determine if current browser supports onhashchange event
-        * @method
-        * @return {Boolean}
-        */
-       Router.prototype.isSupported = function () {
-               return 'onhashchange' in window;
-       };
-
-       router = new Router();
-
-       M.define( 'mobile.startup/Router', Router );
-       M.define( 'mobile.startup/router', router );
-
-}( mw.mobileFrontend, jQuery ) );
diff --git a/resources/skins.minerva.categories/init.js 
b/resources/skins.minerva.categories/init.js
index 1d14f4a..9fff9fd 100644
--- a/resources/skins.minerva.categories/init.js
+++ b/resources/skins.minerva.categories/init.js
@@ -1,7 +1,7 @@
 ( function ( M, $ ) {
 
        var loader = M.require( 'mobile.overlays/moduleLoader' ),
-               overlayManager = M.require( 'mobile.startup/overlayManager' ),
+               overlayManager = require( 'skins.minerva.scripts' 
).overlayManager,
                user = M.require( 'mobile.user/user' );
 
        // categories overlay
diff --git a/resources/skins.minerva.editor/init.js 
b/resources/skins.minerva.editor/init.js
index 4dbf82e..c1b1f71 100644
--- a/resources/skins.minerva.editor/init.js
+++ b/resources/skins.minerva.editor/init.js
@@ -6,7 +6,7 @@
                blockInfo =  mw.config.get( 'wgMinervaUserBlockInfo', false ),
                settings = M.require( 'mobile.settings/settings' ),
                router = M.require( 'mobile.startup/router' ),
-               overlayManager = M.require( 'mobile.startup/overlayManager' ),
+               overlayManager = require( 'skins.minerva.scripts' 
).overlayManager,
                loader = M.require( 'mobile.overlays/moduleLoader' ),
                Icon = M.require( 'mobile.startup/Icon' ),
                Button = M.require( 'mobile.startup/Button' ),
diff --git a/resources/skins.minerva.notifications/init.js 
b/resources/skins.minerva.notifications/init.js
index 7b65dec..dfe61e0 100644
--- a/resources/skins.minerva.notifications/init.js
+++ b/resources/skins.minerva.notifications/init.js
@@ -5,8 +5,8 @@
 ( function ( M, $, mw ) {
        var mainMenu = M.require( 'skins.minerva.scripts/skin' ).getMainMenu(),
                $btn = $( '#secondary-button.user-button' ).parent(),
-               router = M.require( 'mobile.startup/router' ),
-               overlayManager = M.require( 'mobile.startup/overlayManager' ),
+               router = M.require( 'mediawiki.router' ),
+               overlayManager = require( 'skins.minerva.scripts' 
).overlayManager,
                icons = M.require( 'mobile.startup/icons' );
 
        /**
diff --git a/resources/skins.minerva.scripts/cleanuptemplates.js 
b/resources/skins.minerva.scripts/cleanuptemplates.js
index 09ffb17..7978341 100644
--- a/resources/skins.minerva.scripts/cleanuptemplates.js
+++ b/resources/skins.minerva.scripts/cleanuptemplates.js
@@ -1,7 +1,7 @@
 ( function ( M, $ ) {
 
        ( function () {
-               var overlayManager = M.require( 'mobile.startup/overlayManager' 
),
+               var overlayManager = require( 'skins.minerva.scripts' 
).overlayManager,
                        CleanupOverlay = M.require( 
'mobile.issues/CleanupOverlay' );
 
                /**
diff --git a/resources/skins.minerva.scripts/init.js 
b/resources/skins.minerva.scripts/init.js
index a62364b..730db2d 100644
--- a/resources/skins.minerva.scripts/init.js
+++ b/resources/skins.minerva.scripts/init.js
@@ -8,7 +8,7 @@
                router = M.require( 'mobile.startup/router' ),
                context = M.require( 'mobile.context/context' ),
                useNewMediaViewer = context.isBetaGroupMember(),
-               overlayManager = M.require( 'mobile.startup/overlayManager' ),
+               overlayManager = module.exports.overlayManager,
                page = M.getCurrentPage(),
                thumbs = page.getThumbnails(),
                experiments = mw.config.get( 'wgMFExperiments' ) || {},
diff --git a/resources/skins.minerva.scripts/preInit.js 
b/resources/skins.minerva.scripts/preInit.js
index 1190168..25ea1c3 100644
--- a/resources/skins.minerva.scripts/preInit.js
+++ b/resources/skins.minerva.scripts/preInit.js
@@ -7,6 +7,7 @@
  */
 ( function ( M, $ ) {
        var currentPage, skin,
+               overlayManager = new OverlayManager( require( 
'mediawiki.router' ) ),
                PageGateway = M.require( 'mobile.startup/PageGateway' ),
                gateway = new PageGateway( new mw.Api() ),
                Page = M.require( 'mobile.startup/Page' ),
@@ -77,4 +78,6 @@
                        mw.config.get( 'wgMFEnableJSConsoleRecruitment' ) ) {
                console.log( mw.msg( 'mobile-frontend-console-recruit' ) );
        }
+
+       module.exports.overlayManager = overlayManager;
 }( mw.mobileFrontend, jQuery ) );
diff --git a/resources/skins.minerva.talk/init.js 
b/resources/skins.minerva.talk/init.js
index 0d75b05..5bfce6b 100644
--- a/resources/skins.minerva.talk/init.js
+++ b/resources/skins.minerva.talk/init.js
@@ -7,7 +7,7 @@
                // use the plain return value here - T128273
                title = $talk.attr( 'data-title' ),
                page = M.getCurrentPage(),
-               overlayManager = M.require( 'mobile.startup/overlayManager' ),
+               overlayManager = require( 'skins.minerva.scripts' 
).overlayManager,
                skin = M.require( 'skins.minerva.scripts/skin' ),
                pageTitle, talkTitle;
 
diff --git a/tests/qunit/mobile.startup/test_Router.js 
b/tests/qunit/mobile.startup/test_Router.js
deleted file mode 100644
index fedb94b..0000000
--- a/tests/qunit/mobile.startup/test_Router.js
+++ /dev/null
@@ -1,95 +0,0 @@
-( function ( M ) {
-       var Router = M.require( 'mobile.startup/Router' ),
-               router;
-
-       QUnit.module( 'MobileFrontend Router', {
-               setup: function () {
-                       router = new Router();
-                       this.stub( router, 'getPath', function () {
-                               return router.testHash.slice( 1 );
-                       } );
-                       this.stub( router, 'navigate', function ( path ) {
-                               router.testHash = path;
-                       } );
-               },
-
-               teardown: function () {
-                       router.testHash = '';
-               }
-       } );
-
-       QUnit.test( '#route, string', 1, function ( assert ) {
-               router.testHash = '';
-               router.route( 'teststring', function () {
-                       assert.ok( true, 'run callback for route' );
-               } );
-               router.testHash = '#teststring';
-               router.emit( 'hashchange' );
-       } );
-
-       QUnit.test( '#route, RegExp', 1, function ( assert ) {
-               router.testHash = '';
-               router.route( /^testre-(\d+)$/, function ( param ) {
-                       assert.strictEqual( param, '123', 'run callback for 
route with correct params' );
-               } );
-               router.testHash = '#testre-abc';
-               router.emit( 'hashchange' );
-               router.testHash = '#testre-123';
-               router.emit( 'hashchange' );
-       } );
-
-       QUnit.test( 'on route', 2, function ( assert ) {
-               var count = 0,
-                       spy = this.sandbox.spy();
-
-               router.testHash = '';
-               router.route( 'testprevent', spy );
-
-               // try preventing second route (#testprevent)
-               router.once( 'route', function () {
-                       router.testHash = '#testprevent';
-                       router.once( 'route', function ( ev ) {
-                               ev.preventDefault();
-                       } );
-               } );
-               router.testHash = '#initial';
-
-               router.on( 'hashchange.test', function () {
-                       ++count;
-                       if ( count === 3 ) {
-                               assert.strictEqual( router.testHash, 
'#initial', 'reset hash' );
-                               assert.ok( !spy.called, 'don\'t run callback 
for prevented route' );
-                       }
-               } );
-               // emit a hashchange thrice to check if the hash has changed or 
not
-               router.emit( 'hashchange.test' );
-               router.emit( 'hashchange.test' );
-               router.emit( 'hashchange.test' );
-       } );
-
-       QUnit.test( 'on back', 2, function ( assert ) {
-               this.sandbox.stub( router, 'goBack' );
-               router.back().done( function () {
-                       assert.ok( true, 'back 1 complete' );
-               } );
-               router.back().done( function () {
-                       assert.ok( true, 'back 2 complete' );
-               } );
-               router.emit( 'popstate' );
-       } );
-
-       QUnit.test( 'on back without popstate', 2, function ( assert ) {
-               var historyStub = this.sandbox.stub( router, 'goBack' ), // do 
not emit popstate
-                       done = assert.async();
-               router.on( 'popstate', function () {
-                       assert.ok( false, 'this assertion is not supposed to 
get called' );
-               } );
-
-               router.back().done( function () {
-                       assert.ok( historyStub.called, 'history back has been 
called' );
-                       assert.ok( true, 'back without popstate complete' );
-                       done();
-               } );
-       } );
-
-}( mw.mobileFrontend ) );

-- 
To view, visit https://gerrit.wikimedia.org/r/288005
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6517b7c4f6c9d69ebdd0d1fc1cd3c390dcaaa75f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to