jenkins-bot has submitted this change and it was merged.

Change subject: Introduce WatchList class
......................................................................


Introduce WatchList class

Introduce new class that extends PageList
Move logic over there.

Also:
* Fix event logging for Watchlist schema watch and unwatch actions.
* Correctly identify watchlist A-Z actions as being on watchlist page

Change-Id: I1839d19de55633f1a99f7bfd06a4e8e2ceaba93c
---
M includes/Resources.php
M javascripts/modules/PageList.js
A javascripts/modules/watchlist/Watchlist.js
M javascripts/modules/watchstar/Watchstar.js
M javascripts/specials/watchlist.js
M tests/qunit/modules/test_PageList.js
A tests/qunit/modules/watchlist/test_Watchlist.js
7 files changed, 126 insertions(+), 29 deletions(-)

Approvals:
  Phuedx: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/includes/Resources.php b/includes/Resources.php
index 19289a4..4cccb5e 100644
--- a/includes/Resources.php
+++ b/includes/Resources.php
@@ -1130,6 +1130,7 @@
                        'mobile.pagelist.scripts',
                ),
                'scripts' => array(
+                       'javascripts/modules/watchlist/WatchList.js',
                        'javascripts/specials/watchlist.js',
                ),
        ),
diff --git a/javascripts/modules/PageList.js b/javascripts/modules/PageList.js
index 73afecd..bc8ad04 100644
--- a/javascripts/modules/PageList.js
+++ b/javascripts/modules/PageList.js
@@ -72,11 +72,32 @@
                        this.api = new WatchstarApi( options );
                        View.prototype.initialize.apply( this, arguments );
                },
+               /**
+                * @inheritdoc
+                */
                template: M.template.get( 'modules/PageList.hogan' ),
-               postRender: function ( options ) {
-                       View.prototype.postRender.apply( this, arguments );
-                       var pages = [], $li = this.$( 'li' ),
+               /**
+                * Retrieve pages
+                *
+                * @method
+                * @param {Array} ids a list of page ids
+                * @return jQuery.deferred
+                */
+               getPages: function( ids ) {
+                       return this.api.load( ids );
+               },
+               /**
+                * @inheritdoc
+                * Loads watch stars for each page.
+                */
+               postRender: function () {
+                       var $li,
+                               self = this,
+                               pages = [],
                                api = this.api;
+
+                       View.prototype.postRender.apply( this, arguments );
+                       $li = this.$( 'li' );
 
                        // Check what we have in the page list
                        $li.each( function () {
@@ -85,21 +106,32 @@
 
                        // Create watch stars for each entry in list
                        if ( !user.isAnon() && pages.length > 0 ) {
-                               api.load( pages, options.isWatchList ).done( 
function () {
+                               self.getPages( pages ).done( function () {
                                        $li.each( function () {
-                                               var page = new Page( {
-                                                       // FIXME: Set sections 
so we don't hit the api (hacky)
-                                                       sections: [],
-                                                       title: $( this ).attr( 
'title' ),
-                                                       id: $( this ).data( 
'id' )
-                                               } );
+                                               var watchstar,
+                                                       page = new Page( {
+                                                               // FIXME: Set 
sections so we don't hit the api (hacky)
+                                                               sections: [],
+                                                               title: $( this 
).attr( 'title' ),
+                                                               id: $( this 
).data( 'id' )
+                                                       } );
 
-                                               new Watchstar( {
+                                               watchstar = new Watchstar( {
                                                        isAnon: false,
                                                        isWatched: 
api.isWatchedPage( page ),
                                                        page: page,
                                                        el: $( '<div>' 
).appendTo( this )
                                                } );
+                                               /**
+                                                * @event watch
+                                                * Fired when an article in the 
PageList is watched.
+                                                */
+                                               watchstar.on( 'watch', $.proxy( 
self, 'emit', 'watch' ) );
+                                               /**
+                                                * @event unwatch
+                                                * Fired when an article in the 
PageList is watched.
+                                                */
+                                               watchstar.on( 'unwatch', 
$.proxy( self, 'emit', 'unwatch' ) );
                                        } );
                                } );
                        }
diff --git a/javascripts/modules/watchlist/Watchlist.js 
b/javascripts/modules/watchlist/Watchlist.js
new file mode 100644
index 0000000..73d013d
--- /dev/null
+++ b/javascripts/modules/watchlist/Watchlist.js
@@ -0,0 +1,35 @@
+( function ( M, $ ) {
+       var WatchList,
+               PageList = M.require( 'modules/PageList' );
+
+       /**
+        * @extends PageList
+        * @class WatchList
+        */
+       WatchList = PageList.extend( {
+               /**
+                * Retrieve pages where all pages are watched.
+                *
+                * @method
+                * @param {Array} ids a list of page ids
+                * @return jQuery.deferred
+                */
+               getPages: function( ids ) {
+                       return this.api.load( ids, true );
+               },
+               /**
+                * Also sets a watch uploads funnel.
+                * @inheritdoc
+                */
+               postRender: function () {
+                       PageList.prototype.postRender.apply( this, arguments );
+                       this.$el.find( 'a.title' ).on( 'mousedown', function () 
{
+                               // name funnel for watchlists to catch 
subsequent uploads
+                               $.cookie( 'mwUploadsFunnel', 'watchlist', { 
expires: new Date( new Date().getTime() + 60000 ) } );
+                       } );
+               }
+       } );
+
+       M.define( 'modules/watchlist/WatchList', WatchList );
+
+}( mw.mobileFrontend, jQuery ) );
diff --git a/javascripts/modules/watchstar/Watchstar.js 
b/javascripts/modules/watchstar/Watchstar.js
index 5b4b4f7..d1df115 100644
--- a/javascripts/modules/watchstar/Watchstar.js
+++ b/javascripts/modules/watchstar/Watchstar.js
@@ -73,9 +73,19 @@
                                                if ( api.isWatchedPage( page ) 
) {
                                                        options.isWatched = 
true;
                                                        self.render( options );
+                                                       /**
+                                                        * @event watch
+                                                        * Fired when the watch 
star is changed to watched status
+                                                        */
+                                                       self.emit( 'watch' );
                                                        toast.show( mw.msg( 
'mobile-frontend-watchlist-add', page.title ) );
                                                } else {
                                                        options.isWatched = 
false;
+                                                       /**
+                                                        * @event unwatch
+                                                        * Fired when the watch 
star is changed to unwatched status
+                                                        */
+                                                       self.emit( 'unwatch' );
                                                        self.render( options );
                                                        toast.show( mw.msg( 
'mobile-frontend-watchlist-removed', page.title ) );
                                                }
diff --git a/javascripts/specials/watchlist.js 
b/javascripts/specials/watchlist.js
index 844058b..5dac597 100644
--- a/javascripts/specials/watchlist.js
+++ b/javascripts/specials/watchlist.js
@@ -1,7 +1,10 @@
 ( function ( M, $ ) {
-       var PageList = M.require( 'modules/PageList' ),
+       var watchlist,
+               WatchList = M.require( 'modules/watchlist/WatchList' ),
                schema = M.require( 'loggingSchemas/MobileWebClickTracking' ),
-               pageName = mw.config.get( 'wgCanonicalSpecialPageName' ) === 
'Watchlist' ? 'watchlist' : 'diff',
+               canonicalName = mw.config.get( 'wgCanonicalSpecialPageName' ),
+               pageName = canonicalName === 'EditWatchlist' || canonicalName 
=== 'Watchlist' ?
+                       'watchlist' : 'diff',
                subPageName = M.query.watchlistview || 'a-z';
 
        function init() {
@@ -10,10 +13,12 @@
 
                // FIXME: find more elegant way to not show watchlist stars on 
recent changes
                if ( $( '.mw-mf-watchlist-selector' ).length === 0 ) {
-                       new PageList( { el: $watchlist, enhance: true, 
isWatchList: true } );
-                       $watchlist.find( 'a.title' ).on( 'mousedown', function 
() {
-                               // name funnel for watchlists to catch 
subsequent uploads
-                               $.cookie( 'mwUploadsFunnel', 'watchlist', { 
expires: new Date( new Date().getTime() + 60000 ) } );
+                       watchlist = new WatchList( { el: $watchlist, enhance: 
true } );
+                       watchlist.on( 'unwatch', function () {
+                               schema.log( actionNamePrefix + 'unwatch' );
+                       } );
+                       watchlist.on( 'watch', function () {
+                               schema.log( actionNamePrefix + 'watch' );
                        } );
                }
 
@@ -22,11 +27,6 @@
                schema.hijackLink( '.mw-mf-watchlist-selector a', 
actionNamePrefix + 'filter' );
                schema.hijackLink( '.page-list .title', actionNamePrefix + 
'view' );
                schema.hijackLink( '.more', actionNamePrefix + 'more' );
-
-               M.on( 'watched', function ( page, isWatched ) {
-                       var action = isWatched ? 'watch' : 'unwatch';
-                       schema.log( actionNamePrefix + action );
-               } );
        }
 
        $( function () {
diff --git a/tests/qunit/modules/test_PageList.js 
b/tests/qunit/modules/test_PageList.js
index 2ea359f..c85569e 100644
--- a/tests/qunit/modules/test_PageList.js
+++ b/tests/qunit/modules/test_PageList.js
@@ -29,11 +29,4 @@
                assert.strictEqual( pl.$el.find( '.' + 
watchIcon.getGlyphClassName() ).length, 1, "1 of articles is marked as watched" 
);
        } );
 
-       QUnit.test( 'In watched mode', 3, function( assert ) {
-               var pl = new PageList( { pages: [ { id: 30 }, { id: 50 }, { id: 
60 } ], isWatchList: true } );
-               assert.ok( this.spy.notCalled, 'Callback avoided' );
-               assert.strictEqual( pl.$el.find( '.watch-this-article' 
).length, 3, "3 articles have watch stars..." );
-               assert.strictEqual( pl.$el.find( '.' + 
watchIcon.getGlyphClassName() ).length, 3, "...and all are marked as watched." 
);
-       } );
-
 }( jQuery, mw.mobileFrontend ) );
diff --git a/tests/qunit/modules/watchlist/test_Watchlist.js 
b/tests/qunit/modules/watchlist/test_Watchlist.js
new file mode 100644
index 0000000..3cc5c2a
--- /dev/null
+++ b/tests/qunit/modules/watchlist/test_Watchlist.js
@@ -0,0 +1,26 @@
+( function ( $, M ) {
+
+       var WatchList = M.require( 'modules/watchlist/WatchList' ),
+               user = M.require( 'user' ),
+               Icon = M.require( 'Icon' ),
+               watchIcon = new Icon( { name: 'watched' } ),
+               WatchstarApi = M.require( 'modules/watchstar/WatchstarApi' );
+
+       QUnit.module( 'MobileFrontend modules/WatchList', {
+               setup: function() {
+                       var resp = { query: { pages: { 30: { watched: "" }, 50: 
{} } } };
+
+                       this.spy = this.sandbox.stub( WatchstarApi.prototype, 
'get' ).
+                               returns( $.Deferred().resolve( resp ) );
+                       this.sandbox.stub( user, 'isAnon' ).returns( false );
+               }
+       } );
+
+       QUnit.test( 'In watched mode', 3, function( assert ) {
+               var pl = new WatchList( { pages: [ { id: 30 }, { id: 50 }, { 
id: 60 } ] } );
+               assert.ok( this.spy.notCalled, 'Callback avoided' );
+               assert.strictEqual( pl.$el.find( '.watch-this-article' 
).length, 3, "3 articles have watch stars..." );
+               assert.strictEqual( pl.$el.find( '.' + 
watchIcon.getGlyphClassName() ).length, 3, "...and all are marked as watched." 
);
+       } );
+
+}( jQuery, mw.mobileFrontend ) );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I1839d19de55633f1a99f7bfd06a4e8e2ceaba93c
Gerrit-PatchSet: 3
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: Jdlrobson <[email protected]>
Gerrit-Reviewer: Awjrichards <[email protected]>
Gerrit-Reviewer: Phuedx <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

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

Reply via email to