Jdlrobson has uploaded a new change for review.

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

Change subject: Save changes to members of collection only when done clicked
......................................................................

Save changes to members of collection only when done clicked

* Throw confirm message when you try and exit.
* Add methods for adding/removing multiple titles

Bug: T95776
Change-Id: I333466cdc4ba0c838c9585739d311e6b4a35e170
---
M i18n/en.json
M i18n/qqq.json
M resources/ext.gather.api/CollectionsApi.js
M resources/ext.gather.collection.editor/CollectionEditOverlay.js
M resources/ext.gather.page.search/CollectionPageList.js
M resources/ext.gather.page.search/CollectionSearchPanel.js
6 files changed, 126 insertions(+), 38 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Gather 
refs/changes/59/204659/1

diff --git a/i18n/en.json b/i18n/en.json
index b6db092..9a2e684 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -63,6 +63,7 @@
        "gather-add-to-collection-confirm": "Add to collection",
        "gather-add-to-collection-cancel": "No thanks",
        "gather-update-collection-success": "Collection was successfully 
updated.",
+       "gather-edit-collection-confirm": "You have made changes to your 
collection. Do you want to leave without saving them?",
        "gather-remove-toast": "The page has been removed from your \"$1\" 
collection.",
        "gather-no-such-action": "Sorry, the requested action doesn't exist.",
        "gather-unknown-error": "Sorry, there was an unknown error while 
processing your request.",
diff --git a/i18n/qqq.json b/i18n/qqq.json
index d30890d..ce41e56 100644
--- a/i18n/qqq.json
+++ b/i18n/qqq.json
@@ -68,6 +68,7 @@
        "gather-add-to-collection-confirm": "Label for button that user can 
click if they would like to add the current page to a collection",
        "gather-add-to-collection-cancel": "Label for button that user can 
click if they do not want to add to a collection.\n{{Identical|No thanks}}",
        "gather-update-collection-success": "Message that shows when collection 
description and/or title changed.",
+       "gather-edit-collection-confirm": "Confirm message prompting you to 
save changes before exiting.",
        "gather-remove-toast": "Message displayed when you remove an item from 
a collection. Parameters:\n* $1 - Name of collection.",
        "gather-no-such-action": "Message used, if thse user tried to request a 
non-existent action on a collection.",
        "gather-unknown-error": "Used as an error message, if there was an 
unknown error.",
diff --git a/resources/ext.gather.api/CollectionsApi.js 
b/resources/ext.gather.api/CollectionsApi.js
index 9a2b8d7..fb6d575 100644
--- a/resources/ext.gather.api/CollectionsApi.js
+++ b/resources/ext.gather.api/CollectionsApi.js
@@ -23,6 +23,20 @@
                        public: false
                },
                /**
+                * Add a list of titles to existing collection.
+                * @method
+                * @param {Number} id Identifier of collection
+                * @param {String[]} titles array
+                * @return {jQuery.Deferred}
+                */
+               addPagesToCollection: function ( id, titles ) {
+                       return this.postWithToken( 'watch', {
+                               action: 'editlist',
+                               id: id,
+                               titles: titles
+                       } );
+               },
+               /**
                 * Add page to existing collection.
                 * @method
                 * @param {Number} id Identifier of collection
@@ -30,10 +44,21 @@
                 * @return {jQuery.Deferred}
                 */
                addPageToCollection: function ( id, page ) {
+                       return this.addPagesToCollection( id, [ page.getTitle() 
] );
+               },
+               /**
+                * Remove a list of pages from existing collection.
+                * @method
+                * @param {Number} id Identifier of collection
+                * @param {String[]} titles
+                * @return {jQuery.Deferred}
+                */
+               removePagesFromCollection: function ( id, titles ) {
                        return this.postWithToken( 'watch', {
                                action: 'editlist',
                                id: id,
-                               titles: [ page.getTitle() ]
+                               mode: 'remove',
+                               titles: titles
                        } );
                },
                /**
@@ -44,12 +69,7 @@
                 * @return {jQuery.Deferred}
                 */
                removePageFromCollection: function ( id, page ) {
-                       return this.postWithToken( 'watch', {
-                               action: 'editlist',
-                               id: id,
-                               mode: 'remove',
-                               titles: [ page.getTitle() ]
-                       } );
+                       return this.removePagesFromCollection( id, [ 
page.getTitle() ] );
                },
                /**
                 * Create a new collection
diff --git a/resources/ext.gather.collection.editor/CollectionEditOverlay.js 
b/resources/ext.gather.collection.editor/CollectionEditOverlay.js
index e27539d..b1a4e59 100644
--- a/resources/ext.gather.collection.editor/CollectionEditOverlay.js
+++ b/resources/ext.gather.collection.editor/CollectionEditOverlay.js
@@ -40,6 +40,7 @@
                                additionalClassNames: 'cancel',
                                label: mw.msg( 'mobile-frontend-overlay-close' )
                        } ).options,
+                       confirmExitMessage: mw.msg( 
'gather-edit-collection-confirm' ),
                        editSuccessMsg: mw.msg( 
'gather-update-collection-success' ),
                        editFailedError: mw.msg( 
'gather-edit-collection-failed-error' ),
                        unknownCollectionError: mw.msg( 
'gather-error-unknown-collection' ),
@@ -185,15 +186,28 @@
                        this._switchToFirstPane();
                },
                /**
+                * Refresh the page
+                * @private
+                */
+               _reloadCollection: function () {
+                       window.setTimeout( function () {
+                               router.navigate( '/' );
+                               window.location.reload();
+                       }, 100 );
+               },
+               /**
                 * Event handler when the continue button is clicked in the 
title/edit description pane.
                 */
                onFirstPaneSaveClick: function () {
-                       this.hide();
+                       var self = this;
                        if ( this._stateChanged ) {
-                               window.setTimeout( function () {
-                                       router.navigate( '/' );
-                                       window.location.reload();
-                               }, 100 );
+                               this.hide();
+                               this.reloadCollection();
+                       } else {
+                               this.$( '.save' ).prop( 'disabled', true );
+                               this.searchPanel.saveChanges().done( function 
() {
+                                       self._reloadCollection();
+                               } );
                        }
                },
                /**
@@ -235,6 +249,15 @@
                        }
 
                },
+               /** @inheritdoc */
+               hide: function () {
+                       if ( this.searchPanel.hasChanges() ) {
+                               if ( !window.confirm( 
this.options.confirmExitMessage ) ) {
+                                       return;
+                               }
+                       }
+                       return Overlay.prototype.hide.apply( this, arguments );
+               },
                /**
                 * Tests if title is valid
                 * @param {[type]} title Proposed collection title
diff --git a/resources/ext.gather.page.search/CollectionPageList.js 
b/resources/ext.gather.page.search/CollectionPageList.js
index 8c537a9..d760272 100644
--- a/resources/ext.gather.page.search/CollectionPageList.js
+++ b/resources/ext.gather.page.search/CollectionPageList.js
@@ -5,7 +5,6 @@
                CollectionsApi = M.require( 
'ext.gather.watchstar/CollectionsApi' ),
                View = M.require( 'View' ),
                Icon = M.require( 'Icon' ),
-               toast = M.require( 'toast' ),
                CollectionPageList;
 
        /**
@@ -44,6 +43,8 @@
                },
                /** @inheritdoc */
                initialize: function () {
+                       this._removals = [];
+                       this._additions = [];
                        // FIXME: PageList in MobileFrontend should be 
rewritten as PageListWatchstar.
                        View.prototype.initialize.apply( this, arguments );
                        this.api = new CollectionsApi();
@@ -61,8 +62,8 @@
                 * @param {jQuery.Event} ev
                 */
                onChangeMemberStatus: function ( ev ) {
-                       var $target = $( ev.currentTarget ),
-                               collection = this.options.collection,
+                       var index,
+                               $target = $( ev.currentTarget ),
                                $listThumb = $target.find( '.list-thumb' ),
                                self = this,
                                title = $target.data( 'title' ),
@@ -77,32 +78,58 @@
                        page.listThumbStyleAttribute = $listThumb.attr( 'style' 
);
 
                        if ( inCollection ) {
-                               this.api.removePageFromCollection( 
collection.id, page ).done( function () {
-                                       $target.find( '.status' ).replaceWith( 
self.options.iconDisabledButton );
-                                       $target.data( 'is-member', false );
-                                       toast.show( mw.msg( 
'gather-remove-toast', collection.title ), 'toast' );
-                                       /**
-                                        * @event member-removed
-                                        * @param {Page} page
-                                        * Fired when member is removed from 
collection
-                                        */
-                                       self.emit( 'member-removed', page );
-                               } );
+                               this._removals.push( title );
+                               index = this._additions.indexOf( title );
+                               if ( index > -1 ) {
+                                       this._additions.splice( index, 1 );
+                               }
+                               $target.find( '.status' ).replaceWith( 
self.options.iconDisabledButton );
+                               $target.data( 'is-member', false );
+                               /**
+                                * @event member-removed
+                                * @param {Page} page
+                                * Fired when member is removed from collection
+                                */
+                               self.emit( 'member-removed', page );
                        } else {
-                               this.api.addPageToCollection( collection.id, 
page ).done( function () {
-                                       $target.find( '.status' ).replaceWith( 
self.options.iconButton );
-                                       $target.data( 'is-member', true );
-                                       toast.show( mw.msg( 'gather-add-toast', 
collection.title ), 'toast' );
-                                       page.isMember = true;
-                                       /**
-                                        * @event member-added
-                                        * @param {Page} page
-                                        * Fired when member is removed from 
collection
-                                        */
-                                       self.emit( 'member-added', page );
-                               } );
+                               this._additions.push( title );
+                               index = this._removals.indexOf( title );
+                               if ( index > -1 ) {
+                                       this._removals.splice( index, 1 );
+                               }
+                               $target.find( '.status' ).replaceWith( 
self.options.iconButton );
+                               $target.data( 'is-member', true );
+                               page.isMember = true;
+                               /**
+                                * @event member-added
+                                * @param {Page} page
+                                * Fired when member is removed from collection
+                                */
+                               self.emit( 'member-added', page );
                        }
                        return false;
+               },
+               /**
+                * Save any changes made to the collection.
+                */
+               saveChanges: function () {
+                       var self = this,
+                               d = $.Deferred(),
+                               additions = this._additions,
+                               removals = self._removals,
+                               collection = this.options.collection;
+
+                       if ( additions.length || removals.length ) {
+                               this.api.addPagesToCollection( collection.id, 
additions ).done( function () {
+                                       self.api.removePagesFromCollection( 
collection.id, removals )
+                                               .done( $.proxy( d, 'resolve' ) )
+                                               .fail( $.proxy( d, 'reject' ) );
+                               } ).fail( $.proxy( d, 'reject' ) );
+                       } else {
+                               // no visit to the api necessary
+                               d.resolve();
+                       }
+                       return d;
                }
        } );
 
diff --git a/resources/ext.gather.page.search/CollectionSearchPanel.js 
b/resources/ext.gather.page.search/CollectionSearchPanel.js
index d3972e7..130a649 100644
--- a/resources/ext.gather.page.search/CollectionSearchPanel.js
+++ b/resources/ext.gather.page.search/CollectionSearchPanel.js
@@ -76,6 +76,7 @@
                        } else {
                                this.options.pages.push( page );
                        }
+                       this._hasChanged = true;
                },
                /**
                 * Updates the rendering of the internal CollectionPageList
@@ -154,6 +155,21 @@
                                // keep track of last query to take into 
account backspace usage
                                this.lastQuery = query;
                        }
+               },
+               /**
+                * Check if collection has changed.
+                * @return {Boolean}
+                */
+               hasChanges: function () {
+                       return this._hasChanged;
+               },
+               /**
+                * Save any changes made to the collection.
+                * @return {jQuery.Deferred}
+                */
+               saveChanges: function () {
+                       this._hasChanged = false;
+                       return this.pageList.saveChanges();
                }
        } );
 

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

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