summaryrefslogtreecommitdiff
path: root/addons/website_mail_channel/static/src/snippets/s_channel/options.js
blob: 5b51bb6afc29ecc100a30560ee040797cc0dc0b0 (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
odoo.define('website_mail_channel.s_channel_options', function (require) {
'use strict';

var core = require('web.core');
var options = require('web_editor.snippets.options');
var wUtils = require('website.utils');

var _t = core._t;

options.registry.Channel = options.Class.extend({
    /**
     * @override
     */
    async start() {
        await this._super(...arguments);
        this.publicChannels = await this._getPublicChannels();
    },
    /**
     * @override
     */
    cleanForSave: function () {
        this.$target.addClass('d-none');
    },
    /**
     * If we have already created channels => select the first one
     * else => modal prompt (create a new channel)
     *
     * @override
     */
    onBuilt() {
        if (this.publicChannels.length) {
            this.$target[0].dataset.id = this.publicChannels[0][0];
        } else {
            const widget = this._requestUserValueWidgets('create_mail_channel_opt')[0];
            widget.$el.click();
        }
    },

    //--------------------------------------------------------------------------
    // Options
    //--------------------------------------------------------------------------

    /**
     * Creates a new mail.channel through a modal prompt.
     *
     * @see this.selectClass for parameters
     */
    createChannel: function (previewMode, widgetValue, params) {
        var self = this;
        return wUtils.prompt({
            id: "editor_new_mail_channel_subscribe",
            window_title: _t("New Mail Channel"),
            input: _t("Name"),
        }).then(function (result) {
            var name = result.val;
            if (!name) {
                return;
            }
            return self._rpc({
                model: 'mail.channel',
                method: 'create',
                args: [{
                    name: name,
                    public: 'public',
                }],
            }).then(function (id) {
                self.$target.attr("data-id", id);
                return self._rerenderXML();
            });
        });
    },

    //--------------------------------------------------------------------------
    // Private
    //--------------------------------------------------------------------------

    /**
     * @override
     */
    _renderCustomXML(uiFragment) {
        // TODO remove this part in master 
        const createChannelEl = uiFragment.querySelector('we-button[data-create-channel]');
        createChannelEl.dataset.name = 'create_mail_channel_opt';

        return this._getPublicChannels().then(channels => {
            const menuEl = uiFragment.querySelector('.select_discussion_list');
            for (const channel of channels) {
                const el = document.createElement('we-button');
                el.dataset.selectDataAttribute = channel[0];
                el.textContent = channel[1];
                menuEl.appendChild(el);
            }
        });
    },
    /**
     * @private
     * @return {Promise}
     */
    _getPublicChannels() {
        return this._rpc({
            model: 'mail.channel',
            method: 'name_search',
            args: ['', [['public', '=', 'public']]],
        });
    },
});
});