summaryrefslogtreecommitdiff
path: root/addons/website_event/static/src/js/website_event_set_customize_options.js
blob: a6d9f749152abbeec18819bfc5c6c4363efd41e7 (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
112
113
114
115
116
117
118
119
120
121
122
odoo.define('website_event.set_customize_options', function (require) {
"use strict";

var CustomizeMenu = require('website.customizeMenu');
var publicWidget = require('web.public.widget');

var EventSpecificOptions = publicWidget.Widget.extend({
    template: 'website_event.customize_options',
    xmlDependencies: ['/website_event/static/src/xml/customize_options.xml'],
    events: {
        'change #display-website-menu': '_onDisplaySubmenuChange',
    },

    /**
     * @override
     */
    start: function () {
        this.$submenuInput = this.$('#display-website-menu');
        this.modelName = this._getEventObject().model;
        this.eventId = this._getEventObject().id;
        this._initCheckbox();
    },

    //--------------------------------------------------------------------------
    // Handlers
    //--------------------------------------------------------------------------

    _onDisplaySubmenuChange: function (ev) {
        var checkboxValue = this.$submenuInput.is(':checked');
        this._toggleSubmenuDisplay(checkboxValue);
    },

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

    _getCheckboxFields: function () {
        return ['website_menu', 'website_url'];
    },

    _getCheckboxFieldMatch: function (checkboxField) {
        if (checkboxField === 'website_menu') {
            return this.$submenuInput;
        }
    },

    _getEventObject: function() {
        var repr = $('html').data('main-object');
        var m = repr.match(/(.+)\((\d+),(.*)\)/);
        return {
            model: m[1],
            id: m[2] | 0,
        };
    },

    _initCheckbox: function () {
        var self = this;
        this._rpc({
            model: this.modelName,
            method: 'read',
            args: [
                [this.eventId],
                this._getCheckboxFields()
            ],
        }).then((data) => {
            self._initCheckboxCallback(data);
        });
    },

    _initCheckboxCallback: function (rpcData) {
        if (rpcData[0]['website_menu']) {
            var submenuInput = this._getCheckboxFieldMatch('website_menu');
            submenuInput.attr('checked', 'checked');
        }
        this.eventUrl = rpcData[0]['website_url'];
    },

    _reloadEventPage: function () {
        window.location = this.eventUrl;
    },

    _toggleSubmenuDisplay: function (val) {
        var self = this;
        this._rpc({
            model: this.modelName,
            method: 'toggle_website_menu',
            args: [[this.eventId], val],
        }).then(function () {
            self._reloadEventPage();
        });
    },

});

CustomizeMenu.include({
    _getEventObject: function() {
        var repr = $('html').data('main-object');
        var m = repr.match(/(.+)\((\d+),(.*)\)/);
        return {
            model: m[1],
            id: m[2] | 0,
        };
    },

    _loadCustomizeOptions: function () {
        var self = this;
        var def = this._super.apply(this, arguments);
        return def.then(function () {
            if (!self.__eventOptionsLoaded && self._getEventObject().model === 'event.event') {
                self.__eventOptionsLoaded = true;
                self.eventOptions = new EventSpecificOptions(self);
                self.eventOptions.insertAfter(self.$el.find('.dropdown-divider:first()'));
            }
        });
    },
});

return {
    EventSpecificOptions: EventSpecificOptions,
};

});