summaryrefslogtreecommitdiff
path: root/addons/note/static/src/js/systray_activity_menu.js
blob: ac4054c0bc0ca2b2a18f180b4124b1184086674f (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
odoo.define('note.systray.ActivityMenu', function (require) {
"use strict";

var ActivityMenu = require('mail.systray.ActivityMenu');

var core = require('web.core');
var datepicker = require('web.datepicker');

var _t = core._t;

ActivityMenu.include({
    events: _.extend({}, ActivityMenu.prototype.events, {
        'click .o_note_show': '_onAddNoteClick',
        'click .o_note_save': '_onNoteSaveClick',
        'click .o_note_set_datetime': '_onNoteDateTimeSetClick',
        'keydown input.o_note_input': '_onNoteInputKeyDown',
        'click .o_note': '_onNewNoteClick',
    }),
    //--------------------------------------------------
    // Private
    //--------------------------------------------------
    /**
     * Moving notes at first place
     * @override
     */
    _getActivityData: function () {
        var self = this;
        return this._super.apply(this, arguments).then(function () {
            var reminderIndex = _.findIndex(self.activities, function (val) {
                return val.model === 'note.note';
            });
            if (reminderIndex > 0) {
                self.activities.splice(0, 0, self.activities.splice(reminderIndex, 1)[0]);
            }
        });
    },
    /**
     * Save the note to database using datepicker date and field as note
     * By default, when no datetime is set, it uses the current datetime.
     *
     * @private
     */
    _saveNote: function () {
        var note = this.$('.o_note_input').val().trim();
        if (! note) {
            return;
        }
        var params = {'note': note};
        var noteDateTime = this.noteDateTimeWidget.getValue();
        if (noteDateTime) {
            params = _.extend(params, {'date_deadline': noteDateTime});
        } else {
            params = _.extend(params, {'date_deadline': moment()});
        }
        this.$('.o_note_show').removeClass('d-none');
        this.$('.o_note').addClass('d-none');
        this._rpc({
            route: '/note/new',
            params: params,
        }).then(this._updateActivityPreview.bind(this));
    },
    //-----------------------------------------
    // Handlers
    //-----------------------------------------
    /**
     * @override
     */
    _onActivityFilterClick: function (ev) {
        var $el = $(ev.currentTarget);
        if (!$el.hasClass("o_note")) {
            var data = _.extend({}, $el.data(), $(ev.target).data());
            if (data.res_model === "note.note" && data.filter === "my") {
                this.do_action({
                    type: 'ir.actions.act_window',
                    name: data.model_name,
                    res_model:  data.res_model,
                    views: [[false, 'kanban'], [false, 'form'], [false, 'list']]
                }, {
                    clear_breadcrumbs: true,
                });
            } else {
                this._super.apply(this, arguments);
            }
        }
    },
    /**
     * When add new note button clicked, toggling quick note create view inside
     * Systray activity view
     *
     * @private
     * @param {MouseEvent} ev
     */
    _onAddNoteClick: function (ev) {
        var self = this;
        ev.stopPropagation();
        if (!this.noteDateTimeWidget){
            this.noteDateTimeWidget = new datepicker.DateWidget(this, {useCurrent: true});
        }
        this.noteDateTimeWidget.appendTo(this.$('.o_note_datetime')).then(function() {
            self.noteDateTimeWidget.$input.attr('placeholder', _t("Today"));
            self.noteDateTimeWidget.setValue(false);
            self.$('.o_note_show, .o_note').toggleClass('d-none');
            self.$('.o_note_input').val('').focus();
        });
    },
    /**
     * When focusing on input for new quick note systerm tray must be open.
     * Preventing to close
     *
     * @private
     * @param {MouseEvent} ev
     */
    _onNewNoteClick: function (ev) {
        ev.stopPropagation();
    },
    /**
     * Opens datetime picker for note.
     * Quick FIX due to no option for set custom icon instead of caret in datepicker.
     *
     * @private
     * @param {MouseEvent} ev
     */
    _onNoteDateTimeSetClick: function (ev) {
        ev.preventDefault();
        ev.stopPropagation();
        this.noteDateTimeWidget.$input.click();
    },
    /**
     * Saving note (quick create) and updating activity preview
     *
     * @private
     * @param {MouseEvent} ev
     */
    _onNoteSaveClick: function (ev) {
        this._saveNote();
    },
    /**
     * Handling Enter key for quick create note.
     *
     * @private
     * @param {KeyboardEvent} ev
     */
    _onNoteInputKeyDown: function (ev) {
        if (ev.which === $.ui.keyCode.ENTER) {
            this._saveNote();
        }
    },
});
});