summaryrefslogtreecommitdiff
path: root/addons/mail/static/src/js/field_char.js
blob: 1a1b90ec875e0ad3696e2d12054a13035e23ca33 (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
odoo.define('sms.onchange_in_keyup', function (require) {
"use strict";

var FieldChar = require('web.basic_fields').FieldChar;
FieldChar.include({

    //--------------------------------------------------------------------------
    // Public
    //-------------------------------------------------------------------------

    /**
     * Support a key-based onchange in text field. In order to avoid too much
     * rpc to the server _triggerOnchange is throttled (once every second max)
     *
     */
    init: function () {
        this._super.apply(this, arguments);
        this._triggerOnchange = _.throttle(this._triggerOnchange, 1000, {leading: false});
    },


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

    /**
     * Trigger the 'change' event at key down. It allows to trigger an onchange
     * while typing which may be interesting in some cases. Otherwise onchange
     * is triggered only on blur.
     *
     * @override
     * @private
     */
    _onKeydown: function () {
        this._super.apply(this, arguments);
        if (this.nodeOptions.onchange_on_keydown) {
            this._triggerOnchange();
        }
    },

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

    /**
     * Triggers the 'change' event to refresh the value. Throttled at init to
     * avoid spaming server.
     *
     * @private
     */
    _triggerOnchange: function () {
        this.$input.trigger('change');
    },
});

});