From 6c4bf99da8816d1aa3368d669b601df6120977b6 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Thu, 23 Jun 2016 15:51:05 +0200 Subject: [PATCH] ApiUnreadNotificationPages: Output pages as an array rather than an object Make the frontend code based on page titles rather than page IDs. Change-Id: I79c6a0e3a7178acdb14039a0c5208e90a7341044 --- .../api/ApiEchoUnreadNotificationPages.php | 5 +- modules/controller/mw.echo.Controller.js | 2 +- modules/model/mw.echo.dm.SourcePagesModel.js | 66 ++----------------- .../mw.echo.ui.CrossWikiUnreadFilterWidget.js | 4 +- .../ui/mw.echo.ui.NotificationsInboxWidget.js | 6 +- modules/ui/mw.echo.ui.PageFilterWidget.js | 19 +++--- 6 files changed, 21 insertions(+), 81 deletions(-) diff --git a/includes/api/ApiEchoUnreadNotificationPages.php b/includes/api/ApiEchoUnreadNotificationPages.php index a0fa9181e..7bf6a9154 100644 --- a/includes/api/ApiEchoUnreadNotificationPages.php +++ b/includes/api/ApiEchoUnreadNotificationPages.php @@ -39,8 +39,7 @@ class ApiEchoUnreadNotificationPages extends ApiCrossWikiBase { $apis = $this->foreignNotifications->getApiEndpoints( $this->getRequestedWikis() ); foreach ( $result as $wiki => $data ) { $result[$wiki]['source'] = $apis[$wiki]; - // StdClass to ensure empty data is json_encoded to `{}` instead of `[]` - $result[$wiki]['pages'] = $data['pages'] ?: new StdClass; + $result[$wiki]['pages'] = $data['pages'] ?: array(); } $this->getResult()->addValue( 'query', $this->getModuleName(), $result ); @@ -82,7 +81,7 @@ class ApiEchoUnreadNotificationPages extends ApiCrossWikiBase { $result = array(); $titles = Title::newFromIDs( array_keys( $pages ) ); foreach ( $titles as $title ) { - $result[$title->getArticleID()] = array( + $result[] = array( 'title' => $title->getPrefixedText(), 'count' => $pages[$title->getArticleID()], ); diff --git a/modules/controller/mw.echo.Controller.js b/modules/controller/mw.echo.Controller.js index 6122d5202..9c77b3c3e 100644 --- a/modules/controller/mw.echo.Controller.js +++ b/modules/controller/mw.echo.Controller.js @@ -131,7 +131,7 @@ { continue: continueValue, readState: filters.getReadState(), - titles: filters.getSourcePagesModel().getCurrentPageTitle() + titles: filters.getSourcePagesModel().getCurrentPage() } ) .then( function ( data ) { diff --git a/modules/model/mw.echo.dm.SourcePagesModel.js b/modules/model/mw.echo.dm.SourcePagesModel.js index d71927e52..385a94ac6 100644 --- a/modules/model/mw.echo.dm.SourcePagesModel.js +++ b/modules/model/mw.echo.dm.SourcePagesModel.js @@ -63,34 +63,13 @@ return this.currentSource; }; - /** - * Get the current page or pages' id. - * Returns null if no page is selected. - * - * @return {number|number[]} Current page id - */ - mw.echo.dm.SourcePagesModel.prototype.getCurrentPage = function () { - return this.currentPage; - }; - /** - * Get the current source - * - * @return {string} Current source - */ - mw.echo.dm.SourcePagesModel.prototype.getCurrentSource = function () { - return this.currentSource; - }; - /** * Get the title of the currently selected page * * @return {string} Page title */ - mw.echo.dm.SourcePagesModel.prototype.getCurrentPageTitle = function () { - return this.getPageTitle( - this.getCurrentSource(), - this.getCurrentPage() - ); + mw.echo.dm.SourcePagesModel.prototype.getCurrentPage = function () { + return this.currentPage; }; /** @@ -144,36 +123,12 @@ * Get all pages in a source * * @param {string} source Symbolic name of the source - * @return {Object} Page definitions in this source + * @return {Object[]} Page definitions in this source */ mw.echo.dm.SourcePagesModel.prototype.getSourcePages = function ( source ) { return this.sources[ source ] && this.sources[ source ].pages; }; - /** - * Get a specific page's title - * - * @param {string} source Symbolic name for source - * @param {number} pageId Page ID - * @return {string} Page title - */ - mw.echo.dm.SourcePagesModel.prototype.getPageTitle = function ( source, pageId ) { - return this.getPageTitleById( source, pageId ); - }; - - /** - * Get page title by the source and page ID - * - * @param {string} source Symbolic name of the source - * @param {number} pageId Page ID - * @return {string} Page title - */ - mw.echo.dm.SourcePagesModel.prototype.getPageTitleById = function ( source, pageId ) { - return this.sources[ source ] && - this.sources[ source ].pages[ pageId ] && - this.sources[ source ].pages[ pageId ].title; - }; - /** * Reset the data */ @@ -189,24 +144,11 @@ * @param {Object} details Details object */ mw.echo.dm.SourcePagesModel.prototype.setSourcePagesDetails = function ( source, details ) { - var id, pageDetails; - - // Source information this.sources[ source ] = { title: details.source.title, base: details.source.base, totalCount: details.totalCount, - pages: {} + pages: details.pages }; - - // Fill in pages - for ( id in details.pages ) { - pageDetails = details.pages[ id ]; - this.sources[ source ].pages[ id ] = { - title: pageDetails.title, - count: pageDetails.count, - id: id - }; - } }; } )( mediaWiki ); diff --git a/modules/ui/mw.echo.ui.CrossWikiUnreadFilterWidget.js b/modules/ui/mw.echo.ui.CrossWikiUnreadFilterWidget.js index ed2cf788f..a3d6bc28d 100644 --- a/modules/ui/mw.echo.ui.CrossWikiUnreadFilterWidget.js +++ b/modules/ui/mw.echo.ui.CrossWikiUnreadFilterWidget.js @@ -102,12 +102,12 @@ */ mw.echo.ui.CrossWikiUnreadFilterWidget.prototype.onPageFilterChoose = function ( widget, item ) { var source = widget.getSource(), - pageId = item && item.getData(); + page = item && item.getData(); if ( item ) { this.setItemSelected( item ); // Emit a choice - this.emit( 'filter', source, pageId ); + this.emit( 'filter', source, page ); } }; diff --git a/modules/ui/mw.echo.ui.NotificationsInboxWidget.js b/modules/ui/mw.echo.ui.NotificationsInboxWidget.js index a36d9d495..f8230db3a 100644 --- a/modules/ui/mw.echo.ui.NotificationsInboxWidget.js +++ b/modules/ui/mw.echo.ui.NotificationsInboxWidget.js @@ -158,10 +158,10 @@ * Respond to unread page filter * * @param {string} source Source symbolic name - * @param {number} pageId Page Id + * @param {string} page Page name */ - mw.echo.ui.NotificationsInboxWidget.prototype.onSourcePageFilter = function ( source, pageId ) { - this.controller.setFilter( 'sourcePage', source, pageId ); + mw.echo.ui.NotificationsInboxWidget.prototype.onSourcePageFilter = function ( source, page ) { + this.controller.setFilter( 'sourcePage', source, page ); this.populateNotifications(); }; diff --git a/modules/ui/mw.echo.ui.PageFilterWidget.js b/modules/ui/mw.echo.ui.PageFilterWidget.js index c8a0379c0..c28ad7b62 100644 --- a/modules/ui/mw.echo.ui.PageFilterWidget.js +++ b/modules/ui/mw.echo.ui.PageFilterWidget.js @@ -71,24 +71,23 @@ * Populate the widget from the model */ mw.echo.ui.PageFilterWidget.prototype.populateDataFromModel = function () { - var id, title, widget, + var i, title, widget, optionWidgets = [], sourcePages = this.model.getSourcePages( this.source ); - for ( id in sourcePages ) { - title = this.model.getPageTitle( this.source, id ); - if ( !title ) { - continue; - } + if ( !sourcePages ) { + return; + } + for ( i = 0; i < sourcePages.length; i++ ) { widget = new mw.echo.ui.PageNotificationsOptionWidget( { - label: title, + label: sourcePages[ i ].title, // TODO: Pages that are a user page should // have a user icon icon: 'article', - unreadCount: sourcePages[ id ].count, + unreadCount: sourcePages[ i ].count, // TODO: When we group pages, this should be - // an array of IDs - data: id, + // an array of titles + data: sourcePages[ i ].title, classes: [ 'mw-echo-ui-pageFilterWidget-page' ] } ); optionWidgets.push( widget );