Sbisson has uploaded a new change for review. https://gerrit.wikimedia.org/r/282345
Change subject: [WIP] EchoController ...................................................................... [WIP] EchoController Change-Id: I819cf0b7d6a8246df2461c71e41cd14854038247 --- M Resources.php A modules/controller/mw.echo.controller.js M modules/ext.echo.init.js M modules/ooui/mw.echo.ui.BundledNotificationGroupWidget.js M modules/ooui/mw.echo.ui.NotificationBadgeWidget.js M modules/ooui/mw.echo.ui.NotificationGroupItemWidget.js M modules/ooui/mw.echo.ui.NotificationItemWidget.js M modules/ooui/mw.echo.ui.NotificationsWidget.js M modules/viewmodel/mw.echo.dm.NotificationsModel.js M modules/viewmodel/mw.echo.dm.UnreadNotificationCounter.js 10 files changed, 72 insertions(+), 17 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo refs/changes/45/282345/1 diff --git a/Resources.php b/Resources.php index feea1c2..f789434 100644 --- a/Resources.php +++ b/Resources.php @@ -51,6 +51,7 @@ ), 'dependencies' => array( 'ext.echo.ui', + 'ext.echo.controller', 'ext.echo.styles.badge', ), 'targets' => array( 'desktop' ), @@ -165,6 +166,15 @@ ), 'targets' => array( 'desktop', 'mobile' ), ), + 'ext.echo.controller' => $echoResourceTemplate + array( + 'scripts' => array( + 'controller/mw.echo.controller.js' + ), + 'dependencies' => array( + 'ext.echo.api' + ), + 'targets' => array( 'desktop', 'mobile' ), + ), 'ext.echo.api' => $echoResourceTemplate + array( 'scripts' => array( 'api/mw.echo.api.js', diff --git a/modules/controller/mw.echo.controller.js b/modules/controller/mw.echo.controller.js new file mode 100644 index 0000000..89d0226 --- /dev/null +++ b/modules/controller/mw.echo.controller.js @@ -0,0 +1,29 @@ +( function ( mw ) { + + mw.echo.Controller = function MwEchoController( echoApi, model, counter ) { + this.api = echoApi; + this.model = model; + this.counter = counter; + }; + + mw.echo.Controller.prototype.markNotificationAsRead = function ( id, source ) { + var controller = this, + notification = this.model.getNotification( id, source ); + + if ( !notification ) { + console.log( 'notification not found' ); + return; + } + + notification.toggleRead(); + this.api.markItemsRead( [ notification.getId() ], source, notification.isRead() ) + .then( function () { + return controller.refreshUnreadCount(); + } ); + }; + + mw.echo.Controller.prototype.refreshUnreadCount = function () { + return this.counter.update(); + }; + +} )( mediaWiki ); \ No newline at end of file diff --git a/modules/ext.echo.init.js b/modules/ext.echo.init.js index 3d08677..3c3e0d2 100644 --- a/modules/ext.echo.init.js +++ b/modules/ext.echo.init.js @@ -46,7 +46,8 @@ unreadMessageCounter, unreadAlertCounter, momentOrigLocale = moment.locale(), - maxNotificationCount = mw.config.get( 'wgEchoMaxNotificationCount' ); + maxNotificationCount = mw.config.get( 'wgEchoMaxNotificationCount' ), + messageController, alertController; // Set up new 'short relative time' locale strings for momentjs moment.defineLocale( 'echo-shortRelativeTime', { @@ -74,6 +75,7 @@ // Load message button and popup if messages exist if ( $existingMessageLink.length ) { + unreadMessageCounter = new mw.echo.dm.UnreadNotificationCounter( echoApi, 'message', maxNotificationCount ); messageNotificationsModel = new mw.echo.dm.NotificationsModel( echoApi, @@ -82,7 +84,8 @@ type: 'message' } ); - mw.echo.ui.messageWidget = new mw.echo.ui.NotificationBadgeWidget( messageNotificationsModel, unreadMessageCounter, { + messageController = new mw.echo.Controller( echoApi, messageNotificationsModel, unreadMessageCounter ); + mw.echo.ui.messageWidget = new mw.echo.ui.NotificationBadgeWidget( messageNotificationsModel, unreadMessageCounter, messageController, { markReadWhenSeen: false, $overlay: mw.echo.ui.$overlay, numItems: numMessages, @@ -112,7 +115,8 @@ type: 'alert' } ); - mw.echo.ui.alertWidget = new mw.echo.ui.NotificationBadgeWidget( alertNotificationsModel, unreadAlertCounter, { + alertController = new mw.echo.Controller( echoApi, alertNotificationsModel, unreadAlertCounter ); + mw.echo.ui.alertWidget = new mw.echo.ui.NotificationBadgeWidget( alertNotificationsModel, unreadAlertCounter, alertController, { markReadWhenSeen: true, numItems: numAlerts, hasUnseen: hasUnseenAlerts, diff --git a/modules/ooui/mw.echo.ui.BundledNotificationGroupWidget.js b/modules/ooui/mw.echo.ui.BundledNotificationGroupWidget.js index 1751bd9..87793b3 100644 --- a/modules/ooui/mw.echo.ui.BundledNotificationGroupWidget.js +++ b/modules/ooui/mw.echo.ui.BundledNotificationGroupWidget.js @@ -12,10 +12,11 @@ * @cfg {jQuery} [$overlay] A jQuery element functioning as an overlay * for popups. */ - mw.echo.ui.BundledNotificationGroupWidget = function MwEchoUiBundledNotificationGroupWidget( model, config ) { + mw.echo.ui.BundledNotificationGroupWidget = function MwEchoUiBundledNotificationGroupWidget( model, controller, config ) { config = config || {}; this.model = model; + this.controller = controller; this.id = this.model.getSource(); // Parent constructor @@ -25,7 +26,7 @@ this.$overlay = config.$overlay || this.$element; this.notifsWidget = new mw.echo.ui.NotificationsWidget( - model, + model, this.controller, { bundle: true, $overlay: this.$overlay, diff --git a/modules/ooui/mw.echo.ui.NotificationBadgeWidget.js b/modules/ooui/mw.echo.ui.NotificationBadgeWidget.js index d8810f8..11db66c 100644 --- a/modules/ooui/mw.echo.ui.NotificationBadgeWidget.js +++ b/modules/ooui/mw.echo.ui.NotificationBadgeWidget.js @@ -25,7 +25,7 @@ * @cfg {jQuery} [$overlay] A jQuery element functioning as an overlay * for popups. */ - mw.echo.ui.NotificationBadgeWidget = function MwEchoUiNotificationBadgeButtonPopupWidget( model, unreadCounter, config ) { + mw.echo.ui.NotificationBadgeWidget = function MwEchoUiNotificationBadgeButtonPopupWidget( model, unreadCounter, controller, config ) { var buttonFlags, allNotificationsButton, preferencesButton, footerButtonGroupWidget, $footer, initialNotifCount, notice; @@ -47,6 +47,7 @@ // View model this.notificationsModel = model; this.unreadCounter = unreadCounter; + this.controller = controller; this.type = this.notificationsModel.getType(); this.maxNotificationCount = mw.config.get( 'wgEchoMaxNotificationCount' ); @@ -73,7 +74,7 @@ // Notifications widget this.notificationsWidget = new mw.echo.ui.NotificationsWidget( - this.notificationsModel, + this.notificationsModel, this.controller, { type: this.type, $overlay: this.$menuOverlay, diff --git a/modules/ooui/mw.echo.ui.NotificationGroupItemWidget.js b/modules/ooui/mw.echo.ui.NotificationGroupItemWidget.js index b277ee2..6ae3feb 100644 --- a/modules/ooui/mw.echo.ui.NotificationGroupItemWidget.js +++ b/modules/ooui/mw.echo.ui.NotificationGroupItemWidget.js @@ -12,11 +12,11 @@ * @param {mw.echo.dm.NotificationGroupItem} model Item model * @param {Object} [config] Configuration object */ - mw.echo.ui.NotificationGroupItemWidget = function MwEchoUiNotificationGroupItemWidget( model, config ) { + mw.echo.ui.NotificationGroupItemWidget = function MwEchoUiNotificationGroupItemWidget( model, controller, config ) { config = config || {}; // Parent constructor - mw.echo.ui.NotificationGroupItemWidget.parent.call( this, model, config ); + mw.echo.ui.NotificationGroupItemWidget.parent.call( this, model, controller, config ); // Mixin constructor OO.ui.mixin.GroupWidget.call( this, config ); @@ -99,7 +99,7 @@ mw.echo.ui.NotificationGroupItemWidget.prototype.onModelAddGroup = function ( notificationsModel, index ) { this.addItems( [ - new mw.echo.ui.BundledNotificationGroupWidget( notificationsModel, { + new mw.echo.ui.BundledNotificationGroupWidget( notificationsModel, this.controller, { showTitle: this.showTitles, $overlay: this.$overlay } ) @@ -172,7 +172,7 @@ for ( i = 0; i < items.length; i++ ) { widgets.push( - new mw.echo.ui.BundledNotificationGroupWidget( items[ i ], { + new mw.echo.ui.BundledNotificationGroupWidget( items[ i ], this.controller, { showTitle: this.showTitles, $overlay: this.$overlay } ) diff --git a/modules/ooui/mw.echo.ui.NotificationItemWidget.js b/modules/ooui/mw.echo.ui.NotificationItemWidget.js index 81552ba..6a91cb0 100644 --- a/modules/ooui/mw.echo.ui.NotificationItemWidget.js +++ b/modules/ooui/mw.echo.ui.NotificationItemWidget.js @@ -14,7 +14,7 @@ * for popups. * @cfg {boolean} [bundle=false] This notification item is part of a bundle. */ - mw.echo.ui.NotificationItemWidget = function MwEchoUiNotificationItemWidget( model, config ) { + mw.echo.ui.NotificationItemWidget = function MwEchoUiNotificationItemWidget( model, controller, config ) { var i, secondaryUrls, urlObj, linkButton, $icon, isInsideMenu, echoMoment, $message = $( '<div>' ).addClass( 'mw-echo-ui-notificationItemWidget-content-message' ), widget = this; @@ -25,6 +25,7 @@ mw.echo.ui.NotificationItemWidget.parent.call( this, $.extend( { data: model.getId() }, config ) ); this.model = model; + this.controller = controller; this.$overlay = config.$overlay || this.$element; this.bundle = !!config.bundle; @@ -244,7 +245,7 @@ * Respond to mark as read button click */ mw.echo.ui.NotificationItemWidget.prototype.onMarkAsReadButtonClick = function () { - this.model.toggleRead( true ); + this.controller.markNotificationAsRead( this.model.getId(), this.model.getSource() ); }; /** diff --git a/modules/ooui/mw.echo.ui.NotificationsWidget.js b/modules/ooui/mw.echo.ui.NotificationsWidget.js index af0e9c8..9d4954c 100644 --- a/modules/ooui/mw.echo.ui.NotificationsWidget.js +++ b/modules/ooui/mw.echo.ui.NotificationsWidget.js @@ -15,13 +15,14 @@ * @cfg {boolean} [bundle=false] This notification is part of a bundled notification * group. This affects the rendering of the items. */ - mw.echo.ui.NotificationsWidget = function MwEchoUiNotificationsWidget( model, config ) { + mw.echo.ui.NotificationsWidget = function MwEchoUiNotificationsWidget( model, controller, config ) { config = config || {}; // Parent constructor mw.echo.ui.NotificationsWidget.parent.call( this, config ); this.model = model; + this.controller = controller; this.markReadWhenSeen = !!config.markReadWhenSeen; this.bundle = !!config.bundle; @@ -106,7 +107,7 @@ if ( notificationItem instanceof mw.echo.dm.NotificationGroupItem ) { widget = new mw.echo.ui.NotificationGroupItemWidget( - notificationItem, + notificationItem, this.controller, { bundle: this.bundle, $overlay: this.$overlay @@ -114,7 +115,7 @@ ); } else { widget = new mw.echo.ui.NotificationItemWidget( - notificationItem, + notificationItem, this.controller, { $overlay: this.$overlay, bundle: this.bundle, diff --git a/modules/viewmodel/mw.echo.dm.NotificationsModel.js b/modules/viewmodel/mw.echo.dm.NotificationsModel.js index dba2eaf..7bb3be9 100644 --- a/modules/viewmodel/mw.echo.dm.NotificationsModel.js +++ b/modules/viewmodel/mw.echo.dm.NotificationsModel.js @@ -145,6 +145,14 @@ /* Methods */ + mw.echo.dm.NotificationsModel.prototype.getNotification = function ( id, source ) { + if ( source === 'local' ) { + return this.getItems().find( function ( item ) { return item.getId() === id; } ); + } + + return null; + }; + /** * Respond to item seen state change * diff --git a/modules/viewmodel/mw.echo.dm.UnreadNotificationCounter.js b/modules/viewmodel/mw.echo.dm.UnreadNotificationCounter.js index bae7171..d4d72ce 100644 --- a/modules/viewmodel/mw.echo.dm.UnreadNotificationCounter.js +++ b/modules/viewmodel/mw.echo.dm.UnreadNotificationCounter.js @@ -84,7 +84,7 @@ */ mw.echo.dm.UnreadNotificationCounter.prototype.update = function () { var model = this; - this.api.fetchUnreadCount( this.source, this.type ).then( function ( actualCount ) { + return this.api.fetchUnreadCount( this.source, this.type ).then( function ( actualCount ) { model.setCount( actualCount, false ); } ); }; -- To view, visit https://gerrit.wikimedia.org/r/282345 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I819cf0b7d6a8246df2461c71e41cd14854038247 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/Echo Gerrit-Branch: master Gerrit-Owner: Sbisson <sbis...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits