summaryrefslogtreecommitdiff
path: root/addons/web/static/src/js/widgets/signature.js
blob: 25cb16fdb45ee4632a14c78ebdaf2b56e061ffa4 (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
odoo.define('web.signature_widget', function (require) {
"use strict";

const framework = require('web.framework');
const SignatureDialog = require('web.signature_dialog');
const widgetRegistry = require('web.widget_registry');
const Widget = require('web.Widget');


const WidgetSignature = Widget.extend({
    custom_events: Object.assign({}, Widget.prototype.custom_events, {
        upload_signature: '_onUploadSignature',
    }),
    events: Object.assign({}, Widget.prototype.events, {
        'click .o_sign_label': '_onClickSignature',
    }),
    template: 'SignButton',
    /**
     * @constructor
     * @param {Widget} parent
     * @param {Object} record
     * @param {Object} nodeInfo
     */
    init: function (parent, record, nodeInfo) {
        this._super.apply(this, arguments);
        this.res_id = record.res_id;
        this.res_model = record.model;
        this.state = record;
        this.node = nodeInfo;
        // signature_field is the field on which the signature image will be
        // saved (`signature` by default).
        this.signature_field = this.node.attrs.signature_field || 'signature';
    },

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

    /**
     * Open a dialog to sign.
     *
     * @private
     */
    _onClickSignature: function () {
        const nameAndSignatureOptions = {
            displaySignatureRatio: 3,
            mode: 'draw',
            noInputName: true,
            signatureType: 'signature',
        };

        if (this.node.attrs.full_name) {
            let signName;
            const fieldFullName = this.state.data[this.node.attrs.full_name];
            if (fieldFullName && fieldFullName.type === 'record') {
                signName = fieldFullName.data.display_name;
            } else {
                signName = fieldFullName;
            }
            nameAndSignatureOptions.defaultName = signName || undefined;
        }

        nameAndSignatureOptions.defaultFont = this.node.attrs.default_font || '';
        this.signDialog = new SignatureDialog(this, {
            nameAndSignatureOptions: nameAndSignatureOptions,
        });
        this.signDialog.open();
    },
    /**
     * Upload the signature image (write it on the corresponding field) and
     * close the dialog.
     *
     * @returns {Promise}
     * @private
     */
    _onUploadSignature: function (ev) {
        const file = ev.data.signatureImage[1];
        const always = () => {
            this.trigger_up('reload');
            framework.unblockUI();
        };
        framework.blockUI();
        const rpcProm = this._rpc({
            model: this.res_model,
            method: 'write',
            args: [[this.res_id], {
                [this.signature_field]: file,
            }],
        });
        rpcProm.then(always).guardedCatch(always);
        return rpcProm;
    },
});

widgetRegistry.add('signature', WidgetSignature);

});