summaryrefslogtreecommitdiff
path: root/web_notify/static/src/js/web_client.js
blob: a576dcdd1a82919ecdd26dab28b9459d3a7f9aea (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
odoo.define("web_notify.WebClient", function (require) {
    "use strict";

    var WebClient = require("web.WebClient");
    var session = require("web.session");
    require("bus.BusService");

    WebClient.include({
        show_application: function () {
            var res = this._super();
            this.start_polling();
            return res;
        },
        start_polling: function () {
            this.channel_success = "notify_success_" + session.uid;
            this.channel_danger = "notify_danger_" + session.uid;
            this.channel_warning = "notify_warning_" + session.uid;
            this.channel_info = "notify_info_" + session.uid;
            this.channel_default = "notify_default_" + session.uid;
            this.all_channels = [
                this.channel_success,
                this.channel_danger,
                this.channel_warning,
                this.channel_info,
                this.channel_default,
            ];
            this.call("bus_service", "startPolling");

            if (this.call("bus_service", "isMasterTab")) {
                this.call("bus_service", "addChannel", this.channel_success);
                this.call("bus_service", "addChannel", this.channel_danger);
                this.call("bus_service", "addChannel", this.channel_warning);
                this.call("bus_service", "addChannel", this.channel_info);
                this.call("bus_service", "addChannel", this.channel_default);
            }
            this.call("bus_service", "on", "notification", this, this.bus_notification);
        },
        bus_notification: function (notifications) {
            var self = this;
            _.each(notifications, function (notification) {
                var channel = notification[0];
                var message = notification[1];
                if (
                    self.all_channels !== null &&
                    self.all_channels.indexOf(channel) > -1
                ) {
                    self.on_message(message);
                }
            });
        },
        on_message: function (message) {
            return this.call("notification", "notify", {
                type: message.type,
                title: message.title,
                message: message.message,
                sticky: message.sticky,
                className: message.className,
            });
        },
    });
});