summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/services/notification_service.js
blob: 295b49d760c231d04e82c22c501dcf32d690b0a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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({
    custom_events: {
        close: '_onCloseNotification',
    },

    /**
     * @override
     */
    start: 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 warning the user about some
     * condition (connection lost), but the condition does 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 {function} [params.Notification] javascript class of a notification
     *   to instantiate by default use 'web.Notification'
     * @param {string} params.title notification title
     * @param {string} params.subtitle notification subtitle
     * @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 (params) {
        if (!this.$el) {
            this.$el = $('<div class="o_notification_manager"/>');
            this.$el.prependTo('body');
        }
        var NotificationWidget = params.Notification || Notification;
        var notification = this.notifications[++id] = new NotificationWidget(this, params);
        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;
});