Bmansurov has uploaded a new change for review.

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

Change subject: Preload next page when WikiGrok C is shown
......................................................................

Preload next page when WikiGrok C is shown

Change-Id: I08acb66c9c8de470958d05c193ac252f3f8098c9
---
M includes/Resources.php
A javascripts/modules/wikiGrokRoulette/WikiGrokRoulette.js
M javascripts/modules/wikiGrokRoulette/init.js
M javascripts/modules/wikigrok/WikiGrokDialogC.js
4 files changed, 140 insertions(+), 80 deletions(-)


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

diff --git a/includes/Resources.php b/includes/Resources.php
index 6ecaed9..e5a422c 100644
--- a/includes/Resources.php
+++ b/includes/Resources.php
@@ -1021,6 +1021,7 @@
                ),
                'scripts' => array(
                        'javascripts/modules/wikiGrokRoulette/ErrorDrawer.js',
+                       
'javascripts/modules/wikiGrokRoulette/WikiGrokRoulette.js',
                        'javascripts/modules/wikiGrokRoulette/init.js',
                ),
                'templates' => array(
@@ -1030,6 +1031,7 @@
 
        'mobile.wikigrok.dialog.c' => $wgMFResourceFileModuleBoilerplate + 
array(
                'dependencies' => array(
+                       'mobile.wikigrok.roulette',
                        'mobile.wikigrok.dialog.b',
                ),
                'scripts' => array(
diff --git a/javascripts/modules/wikiGrokRoulette/WikiGrokRoulette.js 
b/javascripts/modules/wikiGrokRoulette/WikiGrokRoulette.js
new file mode 100644
index 0000000..ea1612d
--- /dev/null
+++ b/javascripts/modules/wikiGrokRoulette/WikiGrokRoulette.js
@@ -0,0 +1,100 @@
+( function ( M ) {
+       var api = M.require( 'api' ),
+               util = M.require( 'util' ),
+               query = util.query,
+               mainMenu = M.require( 'mainmenu' ),
+               LoadingOverlay = M.require( 'LoadingOverlay' ),
+               ErrorDrawer = M.require( 'modules/wikiGrokRoulette/ErrorDrawer' 
),
+               nextPage,
+               WikiGrokRoulette;
+
+       /**
+        * Make the browser to go to url
+        * @param {String} url Address of the page to go to
+        * @param {Boolean} reload Whether to force reload (useful when only 
hash
+        * is different from the current page url)
+        * @private
+        * @ignore
+        */
+       function navigate( url, reload ) {
+               // navigated to the url
+               window.location.href = url;
+               // FIXME: expose wikigrok/init.js so that we can just show 
wikigrok without reloading in such cases
+               // force reload if page titles match
+               if ( reload ) {
+                       window.location.reload();
+               }
+       }
+
+       /**
+        * Load and navigate to the next page
+        * @class modules/wikiGrokRoulette/WikiGrokRoulette
+        * @singleton
+        * @uses LoadingOverlay
+        * @uses modules/wikiGrokRoulette/ErrorDrawer
+        */
+       WikiGrokRoulette = {
+               /**
+                * Get the next page and cache it
+                * Valid response from the server is transformed to the 
following form:
+                * { url: 'page url', title: 'page title'}
+                * @return {jQuery.Deferred}
+                */
+               getNextPage: function () {
+                       return api.ajax( {
+                               action: 'query',
+                               list: 'wikigrokrandom',
+                               categories: this.categories
+                       } ).then( function ( response ) {
+                               if ( response.query &&
+                                       response.query.wikigrokrandom &&
+                                       response.query.wikigrokrandom.length > 0
+                               ) {
+                                       // Remove title from query because it's 
already used in constructing the URL.
+                                       // Leave everything else unchanged for 
testing purposes.
+                                       delete query.title;
+                                       nextPage = {
+                                               url: mw.util.getUrl(
+                                                       
response.query.wikigrokrandom[0].title,
+                                                       query
+                                               ) + '#wikigrokversion=c',
+                                               title: 
response.query.wikigrokrandom[0].title
+                                       };
+                                       return nextPage;
+                               }
+                       } );
+               },
+
+               /**
+                * Navigate to the next page. Use cached page URL if available
+                */
+               navigateToNextPage: function () {
+                       var loadingOverlay = new LoadingOverlay();
+
+                       loadingOverlay.show();
+
+                       if ( nextPage ) {
+                               // force reload if the current and next page 
titles match
+                               navigate( nextPage.url, 
M.getCurrentPage().title === nextPage.title );
+                       } else {
+                               this.getNextPage().done( function ( page ) {
+                                       if ( page.url && page.title ) {
+                                               // force reload if only the 
hash is different
+                                               navigate( page.url, 
M.getCurrentPage().title === page.title );
+                                       } else {
+                                               loadingOverlay.hide( false );
+                                               new ErrorDrawer();
+                                       }
+                               } ).fail( function () {
+                                       loadingOverlay.hide( false );
+                                       new ErrorDrawer();
+                               } ).always( function () {
+                                       mainMenu.closeNavigationDrawers();
+                               } );
+                       }
+               }
+       };
+
+       M.define( 'modules/wikiGrokRoulette/WikiGrokRoulette', WikiGrokRoulette 
);
+
+}( mw.mobileFrontend ) );
diff --git a/javascripts/modules/wikiGrokRoulette/init.js 
b/javascripts/modules/wikiGrokRoulette/init.js
index 6abd44a..b21c7d1 100644
--- a/javascripts/modules/wikiGrokRoulette/init.js
+++ b/javascripts/modules/wikiGrokRoulette/init.js
@@ -1,54 +1,13 @@
 ( function ( M, $ ) {
        M.assertMode( [ 'alpha' ] );
        var $wikiGrokMenuItem =  $( '#mw-mf-page-left' ).find( 
'.wikigrok-roulette' ),
-               mainMenu = M.require( 'mainmenu' ),
-               LoadingOverlay = M.require( 'LoadingOverlay' ),
-               util = M.require( 'util' ),
-               query = util.query,
-               ErrorDrawer = M.require( 'modules/wikiGrokRoulette/ErrorDrawer' 
);
+               WikiGrokRoulette = M.require( 
'modules/wikiGrokRoulette/WikiGrokRoulette' );
 
        // Handle Random WikiGrok menu item
        if ( $wikiGrokMenuItem.length ) {
-               $wikiGrokMenuItem.on( 'click', function ( ev ) {
-                       var api = M.require( 'api' ),
-                               loadingOverlay = new LoadingOverlay();
-
+               $wikiGrokMenuItem.one( 'click', function ( ev ) {
                        ev.preventDefault();
-
-                       loadingOverlay.show();
-
-                       api.ajax( {
-                               action: 'query',
-                               list: 'wikigrokrandom',
-                               categories: this.categories
-                       } ).done( function ( response ) {
-                               if ( response.query &&
-                                       response.query.wikigrokrandom &&
-                                       response.query.wikigrokrandom.length > 0
-                               ) {
-                                       // Remove title from query because it's 
already used in constructing the URL.
-                                       // Leave everything else unchanged for 
testing purposes.
-                                       delete query.title;
-                                       // navigated to the random page
-                                       window.location.href = mw.util.getUrl(
-                                               
response.query.wikigrokrandom[0].title,
-                                               query
-                                       ) + '#wikigrokversion=c';
-                                       // FIXME: expose wikigrok/init.js so 
that we can just show wikigrok without reloading in such cases
-                                       // force reload if page titles match
-                                       if ( M.getCurrentPage().title === 
response.query.wikigrokrandom[0].title ) {
-                                               window.location.reload();
-                                       }
-                               } else {
-                                       loadingOverlay.hide( false );
-                                       new ErrorDrawer();
-                               }
-                       } ).fail( function () {
-                               loadingOverlay.hide( false );
-                               new ErrorDrawer();
-                       } ).always( function () {
-                               mainMenu.closeNavigationDrawers();
-                       } );
+                       WikiGrokRoulette.navigateToNextPage();
                } );
        }
 }( mw.mobileFrontend, jQuery ) );
diff --git a/javascripts/modules/wikigrok/WikiGrokDialogC.js 
b/javascripts/modules/wikigrok/WikiGrokDialogC.js
index 0089ffd..da6fc9b 100644
--- a/javascripts/modules/wikigrok/WikiGrokDialogC.js
+++ b/javascripts/modules/wikigrok/WikiGrokDialogC.js
@@ -1,5 +1,6 @@
 ( function ( M, $ ) {
        var WikiGrokDialogB = M.require( 'modules/wikigrok/WikiGrokDialogB' ),
+               WikiGrokRoulette = M.require( 
'modules/wikiGrokRoulette/WikiGrokRoulette' ),
                Drawer = M.require( 'Drawer' ),
                browser = M.require( 'browser' ),
                WikiGrokDialogC;
@@ -25,7 +26,6 @@
                 */
                postRecordClaims: function () {
                        var self = this,
-                               wikiGrokMenuItem = $( '#mw-mf-page-left' 
).find( '.wikigrok-roulette' ),
                                badgeLevels = [ 1, 3, 5, 10, 20, 50, 100 ],
                                showNext = true,
                                responseText,
@@ -34,44 +34,40 @@
                        self.$( '.wg-content, .wg-thanks-content, .wg-link, 
.footer' ).hide();
                        self.$( '.spinner' ).show();
 
-                       if ( wikiGrokMenuItem.length ) {
-                               // Count responses if local storage supported
-                               if ( browser.supportsLocalStorage ) {
-                                       responseCount = localStorage.getItem( 
'wikiGrokResponseCount' );
-                                       // Increment claim response count, null 
if no responses
-                                       if ( responseCount !== null ) {
-                                               responseCount++;
-                                       } else {
-                                               responseCount = 1;
-                                       }
-                                       // Save response count
-                                       localStorage.setItem( 
'wikiGrokResponseCount', responseCount );
+                       // Count responses if local storage supported
+                       if ( browser.supportsLocalStorage ) {
+                               responseCount = localStorage.getItem( 
'wikiGrokResponseCount' );
+                               // Increment claim response count, null if no 
responses
+                               if ( responseCount !== null ) {
+                                       responseCount++;
+                               } else {
+                                       responseCount = 1;
+                               }
+                               // Save response count
+                               localStorage.setItem( 'wikiGrokResponseCount', 
responseCount );
 
-                                       // Add badge if responseCount is at a 
badge level
-                                       if ( $.inArray( responseCount, 
badgeLevels ) !== -1 ) {
-                                               showNext = false;
-                                               responseText = 'Good going! 
<br> You just completed ' + responseCount + ' task';
-                                               if ( responseCount === 1 ) {
-                                                       responseText += '.';
-                                               } else {
-                                                       responseText += 's.';
-                                               }
-                                               self.$( '.spinner' ).hide();
-                                               self.$( '.wg-link' 
).empty().addClass( 'wg-badge-' + responseCount ).show();
-                                               self.$( '.wg-content' )
-                                                       .html( responseText )
-                                                       .show();
-                                               // let the user enjoy the badge 
for 2 seconds
-                                               setTimeout( function () {
-                                                       
wikiGrokMenuItem.trigger( 'click' );
-                                               }, 2000 );
+                               // Add badge if responseCount is at a badge 
level
+                               if ( $.inArray( responseCount, badgeLevels ) 
!== -1 ) {
+                                       showNext = false;
+                                       responseText = 'Good going! <br> You 
just completed ' + responseCount + ' task';
+                                       if ( responseCount === 1 ) {
+                                               responseText += '.';
+                                       } else {
+                                               responseText += 's.';
                                        }
+                                       self.$( '.spinner' ).hide();
+                                       self.$( '.wg-link' ).empty().addClass( 
'wg-badge-' + responseCount ).show();
+                                       self.$( '.wg-content' )
+                                               .html( responseText )
+                                               .show();
+                                       // let the user enjoy the badge for 2 
seconds
+                                       setTimeout( function () {
+                                               
WikiGrokRoulette.navigateToNextPage();
+                                       }, 2000 );
                                }
-                               if ( showNext ) {
-                                       wikiGrokMenuItem.trigger( 'click' );
-                               }
-                       } else {
-                               
WikiGrokDialogB.prototype.postRecordClaims.apply( this, arguments );
+                       }
+                       if ( showNext ) {
+                               WikiGrokRoulette.navigateToNextPage();
                        }
                },
 
@@ -88,6 +84,9 @@
 
                        self.askWikidataQuestion( options );
                        this.show();
+
+                       // Silently fetch the next page
+                       WikiGrokRoulette.getNextPage();
                }
        } );
 

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

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

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

Reply via email to