diff options
Diffstat (limited to 'addons/web/static/src/js/services')
| -rw-r--r-- | addons/web/static/src/js/services/notification_manager.js | 90 | ||||
| -rw-r--r-- | addons/web/static/src/js/services/notification_service.js | 108 |
2 files changed, 108 insertions, 90 deletions
diff --git a/addons/web/static/src/js/services/notification_manager.js b/addons/web/static/src/js/services/notification_manager.js deleted file mode 100644 index b9b128be78d..00000000000 --- a/addons/web/static/src/js/services/notification_manager.js +++ /dev/null @@ -1,90 +0,0 @@ -odoo.define('web.NotificationManager', function (require) { -"use strict"; - -/** - * NotificationManager - * - * The NotificationManager is simply a widget that will be instantiated by the - * web client to display notifications in the top/right part of the screen. - * - * If you want to display such a notification, you probably do not want to do it - * by using this file. The proper way is to use the do_warn or do_notify - * methods on the Widget class. - * - * @todo This class should be converted into a proper service. Need the right - * service API to do it. - */ - -var Notification = require('web.Notification'); -var Widget = require('web.Widget'); - -var NotificationManager = Widget.extend({ - className: 'o_notification_manager', - - //-------------------------------------------------------------------------- - // Public - //-------------------------------------------------------------------------- - - /** - * Display a notification at the appropriate location, and returns the - * reference to the same widget (useful to get back a reference). - * - * Note that this method does not wait for the appendTo method to complete. - * - * @param {Notification} notification - * @returns {Notification} - */ - display: function (notification) { - notification.appendTo(this.$el); - return notification; - }, - /** - * Will display a notification of type 'notification' - * - * @param {string} title - * @param {string} text - * @param {boolean} sticky - * @returns {Notification} - */ - notify: function (title, text, sticky) { - return this._display(title, text, sticky, 'notification'); - }, - /** - * Will display a notification of type 'warning' - * - * @param {string} title - * @param {string} text - * @param {boolean} sticky - * @returns {Notification} - */ - warn: function (title, text, sticky) { - return this._display(title, text, sticky, 'warning'); - }, - - //-------------------------------------------------------------------------- - // Private - //-------------------------------------------------------------------------- - - /** - * Display a notification - * - * @private - * @param {string} title - * @param {string} text - * @param {boolean} sticky - * @param {string} type either 'notification' or 'warning' - * @returns {Notification} - */ - _display: function (title, text, sticky, type) { - return this.display(new Notification(this, { - title: title, - text: text, - sticky: sticky, - type: type, - })); - } -}); - -return NotificationManager; - -}); diff --git a/addons/web/static/src/js/services/notification_service.js b/addons/web/static/src/js/services/notification_service.js new file mode 100644 index 00000000000..7d6ad473fe4 --- /dev/null +++ b/addons/web/static/src/js/services/notification_service.js @@ -0,0 +1,108 @@ +odoo.define('web.NotificationService', function (require) { +"use strict"; + +var AbstractService = require('web.AbstractService'); +var Notification = require('web.Notification'); +var core = require('web.core'); +var id = 0; + +/** + * Notification Service + * + * The Notification Service is simply a service used to display notifications in + * the top/right part of the screen. + * + * If you want to display such a notification, you probably do not want to do it + * by using this file. The proper way is to use the do_warn or do_notify + * methods on the Widget class. + */ + +var NotificationService = AbstractService.extend({ + name: 'notification', + + custom_events: { + close: '_onCloseNotification', + }, + + init: function () { + this._super.apply(this, arguments); + this.notifications = {}; + }, + + //-------------------------------------------------------------------------- + // Public + //-------------------------------------------------------------------------- + + /** + * It may sometimes be useful to close programmatically a notification. For + * example, when there is a sticky notification that warns the user about + * some condition (connection lost), but the condition do not apply anymore. + * + * @param {number} notificationId + * @param {boolean} [silent=false] if true, the notification does not call + * onClose callback + */ + close: function (notificationId, silent) { + var notification = this.notifications[notificationId]; + if (!notification) { + return; + } + notification.close(silent); + }, + /** + * Display a notification at the appropriate location, and returns the + * reference id to the same widget. + * + * Note that this method does not wait for the appendTo method to complete. + * + * @param {Object} params + * @param {string} params.title notification title + * @param {string} params.message notification main message + * @param {string} params.type 'notification' or 'warning' + * @param {boolean} [params.sticky=false] if true, the notification will stay + * visible until the user clicks on it. + * @param {string} [params.className] className to add on the dom + * @param {function} [params.onClose] callback when the user click on the x + * or when the notification is auto close (no sticky) + * @param {Object[]} params.buttons + * @param {function} params.buttons[0].click callback on click + * @param {Boolean} [params.buttons[0].primary] display the button as primary + * @param {string} [params.buttons[0].text] button label + * @param {string} [params.buttons[0].icon] font-awsome className or image src + * @returns {Number} notification id + */ + notify: function (options) { + if (!this.$el) { + this.$el = $('<div class="o_notification_manager"/>'); + this.$el.prependTo('body'); + } + var notification = this.notifications[++id] = new Notification(this, options); + notification.appendTo(this.$el); + return id; + }, + + //-------------------------------------------------------------------------- + // Handlers + //-------------------------------------------------------------------------- + + /** + * @private + * @param {OdooEvent} ev + */ + _onCloseNotification: function (ev) { + ev.stopPropagation(); + for (var notificationId in this.notifications) { + if (this.notifications[notificationId] === ev.target) { + delete this.notifications[notificationId]; + break; + } + } + }, +}); + + +core.serviceRegistry.add('notification', NotificationService); + + +return NotificationService; +}); |
