Jdlrobson has uploaded a new change for review.

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

Change subject: Split buildOverlay into smaller functions
......................................................................

Split buildOverlay into smaller functions

Start with a getNotificationLimit and markAllAsRead function

Change-Id: Ice53af40a152f6c3b5a92683c574ef7dc2d01047
---
M modules/overlay/ext.echo.overlay.js
M tests/qunit/overlay/test_ext.echo.overlay.js
2 files changed, 53 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/81/151981/1

diff --git a/modules/overlay/ext.echo.overlay.js 
b/modules/overlay/ext.echo.overlay.js
index 96fbd4c..c0f7cc9 100644
--- a/modules/overlay/ext.echo.overlay.js
+++ b/modules/overlay/ext.echo.overlay.js
@@ -3,9 +3,14 @@
        'use strict';
 
        // backwards compatibility <= MW 1.21
-       var getUrl = mw.util.getUrl || mw.util.wikiGetlink;
+       var getUrl = mw.util.getUrl || mw.util.wikiGetlink,
+               api = new mw.Api( { ajax: { cache: false } } );
 
        mw.echo.overlay = {
+               /**
+                * @var mw.Api
+                */
+               api: api,
 
                /**
                 * @param newCount formatted count
@@ -30,22 +35,34 @@
                        );
                },
 
-               buildOverlay: function ( callback ) {
-                       var notificationLimit,
-                               $overlay = $( '<div>' ).addClass( 
'mw-echo-overlay' ),
-                               $prefLink = $( '#pt-preferences a' ),
-                               count = 0,
-                               apiData,
-                               api = new mw.Api( { ajax: { cache: false } } );
-
-                       // Set notification limit based on height of the window
-                       notificationLimit = Math.floor( ( $( window ).height() 
- 134 ) / 90 );
+               /**
+                * Set notification limit based on height of the window
+                * @method
+                * @return int
+                */
+               getNotificationLimit: function () {
+                       var notificationLimit = Math.floor( ( $( window 
).height() - 134 ) / 90 );
 
                        if ( notificationLimit < 1 ) {
                                notificationLimit = 1;
                        } else if ( notificationLimit > 8 ) {
                                notificationLimit = 8;
                        }
+                       return notificationLimit;
+               },
+
+               /**
+                * Builds an overlay element
+                * @method
+                * @param callback a callback which passes the newly created 
overlay as a parameter
+                */
+               buildOverlay: function ( callback ) {
+                       var notificationLimit = this.getNotificationLimit(),
+                               $overlay = $( '<div>' ).addClass( 
'mw-echo-overlay' ),
+                               $prefLink = $( '#pt-preferences a' ),
+                               count = 0,
+                               self = this,
+                               apiData;
 
                        apiData = {
                                'action' : 'query',
@@ -55,7 +72,7 @@
                                'notprop' : 'index|list|count'
                        };
 
-                       api.get( mw.echo.desktop.appendUseLang( apiData ) 
).done( function ( result ) {
+                       this.api.get( mw.echo.desktop.appendUseLang( apiData ) 
).done( function ( result ) {
                                var notifications = result.query.notifications,
                                        unread = [],
                                        unreadTotalCount = 
result.query.notifications.count,
@@ -165,7 +182,7 @@
                                                .text( mw.msg( 
'echo-mark-all-as-read' ) )
                                                .click( function ( e ) {
                                                        e.preventDefault();
-                                                       api.post( 
mw.echo.desktop.appendUseLang( {
+                                                       self.api.post( 
mw.echo.desktop.appendUseLang( {
                                                                'action' : 
'echomarkread',
                                                                'all' : true,
                                                                'token': 
mw.user.tokens.get( 'editToken' )
@@ -251,23 +268,31 @@
                                $overlay.append( $overlayFooter );
 
                                callback( $overlay );
-
-                               // only need to mark as read if there is unread 
item
-                               if ( unread.length > 0 ) {
-                                       api.post( 
mw.echo.desktop.appendUseLang( {
-                                               'action' : 'echomarkread',
-                                               'list' : unread.join( '|' ),
-                                               'token': mw.user.tokens.get( 
'editToken' )
-                                       } ) ).done( function ( result ) {
-                                               if ( 
result.query.echomarkread.count !== undefined ) {
-                                                       count = 
result.query.echomarkread.count;
-                                                       
mw.echo.overlay.updateCount( count, result.query.echomarkread.rawcount );
-                                               }
-                                       } );
-                               }
+                               self.markAllAsRead( unread );
                        } ).fail( function () {
                                window.location.href = $( '#pt-notifications a' 
).attr( 'href' );
                        } );
+               },
+               /**
+                * Mark a list of notifications as read
+                * @method
+                * @param {array} unread a list of unread ids
+                */
+               markAllAsRead: function( unread ) {
+                       // only need to mark as read if there is unread item
+                       if ( unread.length > 0 ) {
+                               this.api.post( mw.echo.desktop.appendUseLang( {
+                                       'action' : 'echomarkread',
+                                       'list' : unread.join( '|' ),
+                                       'token': mw.user.tokens.get( 
'editToken' )
+                               } ) ).done( function ( result ) {
+                                       var count;
+                                       if ( result.query.echomarkread.count 
!== undefined ) {
+                                               count = 
result.query.echomarkread.count;
+                                               mw.echo.overlay.updateCount( 
count, result.query.echomarkread.rawcount );
+                                       }
+                               } );
+                       }
                }
        };
 
diff --git a/tests/qunit/overlay/test_ext.echo.overlay.js 
b/tests/qunit/overlay/test_ext.echo.overlay.js
index 55bebdd..b6747ed 100644
--- a/tests/qunit/overlay/test_ext.echo.overlay.js
+++ b/tests/qunit/overlay/test_ext.echo.overlay.js
@@ -47,7 +47,7 @@
                                        } );
                                }
                        };
-                       this.sandbox.stub( mw, 'Api', ApiStub );
+                       this.sandbox.stub( mw.echo.overlay, 'api', new 
ApiStub() );
                }
        } );
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ice53af40a152f6c3b5a92683c574ef7dc2d01047
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
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