From 4aec0b7740bd57249d504616345ece90fb31c524 Mon Sep 17 00:00:00 2001 From: Christophe Matthieu Date: Wed, 10 Oct 2018 11:18:03 +0200 Subject: [PATCH] [FIX] calendar: notifications may appear Following the transformation into notification service, performed by commit 07cd24b1ecf3ab8edd3fffbf2fcd3a3e685f357c, calendar has been forgotten. --- .../calendar/static/src/js/base_calendar.js | 12 +++---- .../src/js/services/notification_service.js | 5 ++- .../services/notification_service_tests.js | 34 +++++++++++++++++++ 3 files changed, 43 insertions(+), 8 deletions(-) diff --git a/addons/calendar/static/src/js/base_calendar.js b/addons/calendar/static/src/js/base_calendar.js index e09222b6025..88a04077bc8 100644 --- a/addons/calendar/static/src/js/base_calendar.js +++ b/addons/calendar/static/src/js/base_calendar.js @@ -57,10 +57,8 @@ WebClient.include({ // Clear previously set timeouts and destroy currently displayed calendar notifications clearTimeout(this.get_next_calendar_notif_timeout); _.each(this.calendar_notif_timeouts, clearTimeout); - _.each(this.calendar_notif, function(notif) { - if (!notif.isDestroyed()) { - notif.destroy(); - } + _.each(this.calendar_notif, function (notificationID) { + self.call('notification', 'close', notificationID, true); }); this.calendar_notif_timeouts = {}; this.calendar_notif = {}; @@ -68,13 +66,13 @@ WebClient.include({ // For each notification, set a timeout to display it _.each(notifications, function(notif) { self.calendar_notif_timeouts[notif.event_id] = setTimeout(function() { - var notification = new CalendarNotification(self.notification_manager, { + var notificationID = self.call('notification', 'notify', { + Notification: CalendarNotification, title: notif.title, text: notif.message, eventID: notif.event_id, }); - self.notification_manager.display(notification); - self.calendar_notif[notif.event_id] = notification; + self.calendar_notif[notif.event_id] = notificationID; }, notif.timer * 1000); last_notif_timer = Math.max(last_notif_timer, notif.timer); }); diff --git a/addons/web/static/src/js/services/notification_service.js b/addons/web/static/src/js/services/notification_service.js index bbeeca51bb8..7575dcd8160 100644 --- a/addons/web/static/src/js/services/notification_service.js +++ b/addons/web/static/src/js/services/notification_service.js @@ -56,6 +56,8 @@ var NotificationService = AbstractService.extend({ * Note that this method does not wait for the appendTo method to complete. * * @param {Object} params + * @param {function} [params.Notification] javascript class of a notification + * to instantiate by default use 'web.Notification' * @param {string} params.title notification title * @param {string} params.message notification main message * @param {string} params.type 'notification' or 'warning' @@ -76,7 +78,8 @@ var NotificationService = AbstractService.extend({ this.$el = $('
'); this.$el.prependTo('body'); } - var notification = this.notifications[++id] = new Notification(this, params); + var NotificationWidget = params.Notification || Notification; + var notification = this.notifications[++id] = new NotificationWidget(this, params); notification.appendTo(this.$el); return id; }, diff --git a/addons/web/static/tests/services/notification_service_tests.js b/addons/web/static/tests/services/notification_service_tests.js index 68227093cea..e53af5b5699 100644 --- a/addons/web/static/tests/services/notification_service_tests.js +++ b/addons/web/static/tests/services/notification_service_tests.js @@ -244,4 +244,38 @@ QUnit.module('Services', { }); }); + QUnit.test('Display a custom notification', function (assert) { + var done = assert.async(); + assert.expect(3); + + var Custom = Notification.extend({ + init: function (parent, params) { + this._super.apply(this, arguments); + assert.ok(params.customParams, 'instantiate custom notification'); + }, + start: function () { + var self = this; + return this._super().then(function () { + self.$el.html('Custom'); + }); + }, + }); + + var view = createView(this.viewParams); + view.call('notification', 'notify', { + Notification: Custom, + customParams: true, + }); + assert.strictEqual( + $('body .o_notification_manager .o_notification:contains(Custom)').length, 1, + "should display the notification"); + view.destroy(); + setTimeout(function () { + assert.strictEqual( + $('body .o_notification_manager .o_notification').length, 0, + "should destroy the notification"); + done(); + }); + }); + });});