Jdlrobson has uploaded a new change for review.

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

Change subject: Hygiene: Move logic for rendering WikiGrokPanel out of Panel 
itself
......................................................................

Hygiene: Move logic for rendering WikiGrokPanel out of Panel itself

* We should avoid the code being entered at all if the user has opted out
of Wikidata games
* Also switch to using M.settings to avoid any dangers of localStorage usage
breaking things

Note: commit looks scary but is simply moving code out and into an
if statement

Change-Id: If66b4faf3a240a7943b1088c320c20d8be348f89
---
M javascripts/modules/wikigrok/WikiGrokDialog.js
M javascripts/modules/wikigrok/wikigrokeval.js
2 files changed, 88 insertions(+), 94 deletions(-)


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

diff --git a/javascripts/modules/wikigrok/WikiGrokDialog.js 
b/javascripts/modules/wikigrok/WikiGrokDialog.js
index 41dd297..298cf0e 100644
--- a/javascripts/modules/wikigrok/WikiGrokDialog.js
+++ b/javascripts/modules/wikigrok/WikiGrokDialog.js
@@ -152,71 +152,63 @@
                postRender: function( options ) {
                        var self = this;
 
-                       // If the user hasn't opted-out of WikiGrok, load the 
interface.
-                       if ( !localStorage.getItem( 'mfHideWikiGrok' ) ) {
-
-                               // Insert the dialog into the page
-                               $( function() {
-                                       // If there is a table of contents, 
insert before it.
-                                       if ( $( '.toc-mobile' ).length ) {
-                                               self.insertBefore( 
'.toc-mobile' );
-                                       } else {
-                                               self.appendTo( 
M.getLeadSection() );
-                                       }
-                               } );
-
-                               // Initialize all the buttons and links
-                               // ...for final 'Thanks' step
-                               if ( options.thankUser ) {
-                                       this.$( '.wg-buttons .quit' ).on( 
'click', function() {
-                                               self.hide();
-                                       } );
-                               // ...for intermediate 'Question' step
-                               } else if ( options.beginQuestions ) {
-                                       this.$( '.wg-buttons .yes' ).on( 
'click', function() {
-                                               self.log( 'success' );
-                                               options.claimIsCorrect = 1;
-                                               self.recordClaim( options );
-                                       } );
-                                       this.$( '.wg-buttons .not-sure' ).on( 
'click', function() {
-                                               self.log( 'notsure' );
-                                               self.thankUser( options, false 
);
-                                       } );
-                                       this.$( '.wg-buttons .no' ).on( 
'click', function() {
-                                               self.log( 'success' );
-                                               options.claimIsCorrect = 0;
-                                               self.recordClaim( options );
-                                       } );
-                               // ...for initial 'Intro' step
+                       // Insert the dialog into the page
+                       $( function() {
+                               // If there is a table of contents, insert 
before it.
+                               if ( $( '.toc-mobile' ).length ) {
+                                       self.insertBefore( '.toc-mobile' );
                                } else {
-                                       this.log( 'view' );
-                                       this.$( '.wg-buttons .cancel' ).on( 
'click', function() {
-                                               self.hide();
-                                               self.log( 'nothanks' );
-                                               // Set a localStorage value to 
keep WikiGrok hidden for this user.
-                                               // We test for locaStorage 
support in wikigrok.js.
-                                               // Older browsers can only 
store strings in localStorage (not
-                                               // booleans).
-                                               localStorage.setItem( 
'mfHideWikiGrok', 'true' );
-                                       } );
-                                       this.$( '.wg-buttons .proceed' ).on( 
'click', function() {
-                                               self.log( 'attempt' );
-                                               // Proceed with asking the user 
a metadata question.
-                                               self.askWikidataQuestion( 
options );
-                                       } );
-                                       // Log more info clicks
-                                       $( '.wg-notice-link' ).on( 'click', 
function() {
-                                               self.log( 'moreinfo' );
-                                       } );
+                                       self.appendTo( M.getLeadSection() );
                                }
+                       } );
 
-                               // render() does a "deep copy" $.extend() on 
the template data, so we need
-                               // to reset the buttons after each step (since 
some steps have fewer
-                               // buttons than the initial default).
-                               self.options.buttons = [];
-
-                               this.show();
+                       // Initialize all the buttons and links
+                       // ...for final 'Thanks' step
+                       if ( options.thankUser ) {
+                               this.$( '.wg-buttons .quit' ).on( 'click', 
function() {
+                                       self.hide();
+                               } );
+                       // ...for intermediate 'Question' step
+                       } else if ( options.beginQuestions ) {
+                               this.$( '.wg-buttons .yes' ).on( 'click', 
function() {
+                                       self.log( 'success' );
+                                       options.claimIsCorrect = 1;
+                                       self.recordClaim( options );
+                               } );
+                               this.$( '.wg-buttons .not-sure' ).on( 'click', 
function() {
+                                       self.log( 'notsure' );
+                                       self.thankUser( options, false );
+                               } );
+                               this.$( '.wg-buttons .no' ).on( 'click', 
function() {
+                                       self.log( 'success' );
+                                       options.claimIsCorrect = 0;
+                                       self.recordClaim( options );
+                               } );
+                       // ...for initial 'Intro' step
+                       } else {
+                               this.log( 'view' );
+                               this.$( '.wg-buttons .cancel' ).on( 'click', 
function() {
+                                       self.hide();
+                                       self.log( 'nothanks' );
+                                       M.setting.saveUserSetting( 
'mfHideWikiGrok', 'true' );
+                               } );
+                               this.$( '.wg-buttons .proceed' ).on( 'click', 
function() {
+                                       self.log( 'attempt' );
+                                       // Proceed with asking the user a 
metadata question.
+                                       self.askWikidataQuestion( options );
+                               } );
+                               // Log more info clicks
+                               $( '.wg-notice-link' ).on( 'click', function() {
+                                       self.log( 'moreinfo' );
+                               } );
                        }
+
+                       // render() does a "deep copy" $.extend() on the 
template data, so we need
+                       // to reset the buttons after each step (since some 
steps have fewer
+                       // buttons than the initial default).
+                       self.options.buttons = [];
+
+                       this.show();
                }
        } );
 
diff --git a/javascripts/modules/wikigrok/wikigrokeval.js 
b/javascripts/modules/wikigrok/wikigrokeval.js
index 8c225e4..0bde32c 100644
--- a/javascripts/modules/wikigrok/wikigrokeval.js
+++ b/javascripts/modules/wikigrok/wikigrokeval.js
@@ -5,44 +5,46 @@
 
        // Get existing Wikidata claims about this page so we can decide if 
it's appropriate
        // to display the WikiGrok interface.
-       $.ajax( {
-               type: 'get',
-               url: 'https://www.wikidata.org/w/api.php',
-               data: {
-                       'action': 'wbgetentities',
-                       'ids': wikidataID,
-                       'props': 'claims',
-                       'format': 'json'
-               },
-               // Using JSONP so we aren't restricted by cross-site rules. 
This isn't
-               // strictly needed on the Wikimedia cluster since it has CORS 
exceptions
-               // for requests from other Wikimedia sites, but this makes it 
easy to
-               // test locally.
-               dataType: 'jsonp',
-               success: function( data ) {
-                       var instanceClaims,
-                               loadWikiGrokDialog = false;
+       if ( !M.settings.getUserSetting( 'mfHideWikiGrok' ) ) {
+               $.ajax( {
+                       type: 'get',
+                       url: 'https://www.wikidata.org/w/api.php',
+                       data: {
+                               'action': 'wbgetentities',
+                               'ids': wikidataID,
+                               'props': 'claims',
+                               'format': 'json'
+                       },
+                       // Using JSONP so we aren't restricted by cross-site 
rules. This isn't
+                       // strictly needed on the Wikimedia cluster since it 
has CORS exceptions
+                       // for requests from other Wikimedia sites, but this 
makes it easy to
+                       // test locally.
+                       dataType: 'jsonp',
+                       success: function( data ) {
+                               var instanceClaims,
+                                       loadWikiGrokDialog = false;
 
-                       // See if the page has any 'instance of' claims.
-                       if ( data.entities !== undefined && 
data.entities[wikidataID].claims.P31 !== undefined ) {
-                               instanceClaims = 
data.entities[wikidataID].claims.P31;
-                               $.each( instanceClaims, function( id, claim ) {
-                                       // See if any of the claims state that 
the topic is a human.
-                                       if ( 
claim.mainsnak.datavalue.value['numeric-id'] === 5 ) {
-                                               // Make sure there are no 
existing occupation claims.
-                                               if ( 
data.entities[wikidataID].claims.P106 === undefined ) {
-                                                       loadWikiGrokDialog = 
true;
+                               // See if the page has any 'instance of' claims.
+                               if ( data.entities !== undefined && 
data.entities[wikidataID].claims.P31 !== undefined ) {
+                                       instanceClaims = 
data.entities[wikidataID].claims.P31;
+                                       $.each( instanceClaims, function( id, 
claim ) {
+                                               // See if any of the claims 
state that the topic is a human.
+                                               if ( 
claim.mainsnak.datavalue.value['numeric-id'] === 5 ) {
+                                                       // Make sure there are 
no existing occupation claims.
+                                                       if ( 
data.entities[wikidataID].claims.P106 === undefined ) {
+                                                               
loadWikiGrokDialog = true;
+                                                       }
+                                                       // Break each loop.
+                                                       return false;
                                                }
-                                               // Break each loop.
-                                               return false;
+                                       } );
+                                       if ( loadWikiGrokDialog ) {
+                                               new WikiGrokDialog( { itemId: 
wikidataID } );
                                        }
-                               } );
-                               if ( loadWikiGrokDialog ) {
-                                       new WikiGrokDialog( { itemId: 
wikidataID } );
                                }
                        }
-               }
-       } );
+               } );
+       }
 
        // Make OverlayManager handle '#/wikigrok/moreinfo' links. We only need 
to do
        // this once.

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: If66b4faf3a240a7943b1088c320c20d8be348f89
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