diff --git a/addons/portal/static/src/js/portal_name_and_signature.js b/addons/portal/static/src/js/portal_name_and_signature.js index 20ce8d5ae22..18e5a6ee2a8 100644 --- a/addons/portal/static/src/js/portal_name_and_signature.js +++ b/addons/portal/static/src/js/portal_name_and_signature.js @@ -19,6 +19,7 @@ var NameAndSignature = Widget.extend({ 'input .o_portal_sign_name_input': '_onInputSignName', // signature 'click .o_portal_sign_signature': '_onClickSignature', + 'change .o_portal_sign_signature': '_onChangeSignature', // draw 'click .o_portal_sign_draw_button': '_onClickSignDrawButton', 'click .o_portal_sign_draw_clear a': '_onClickSignDrawClear', @@ -180,7 +181,7 @@ var NameAndSignature = Widget.extend({ */ isSignatureEmpty: function () { var signature = this.$signatureField.jSignature('getData'); - return signature ? this.emptySignature === signature : true; + return signature && this.emptySignature ? this.emptySignature === signature : true; }, /** * (Re)initializes the signature area: @@ -390,6 +391,7 @@ var NameAndSignature = Widget.extend({ height ); _.extend(context, ignoredContext); + self.trigger_up('signature_changed'); }, 0); }; image.src = imgSrc; @@ -432,6 +434,29 @@ var NameAndSignature = Widget.extend({ } }, 0); }, + /** + * Waits for the signature to be not empty and triggers up the event + * `signature_changed`. + * This is necessary because some methods of jSignature are async but + * they don't return a promise and don't trigger any event. + * + * @private + * @param {Deferred} [def=Deferred] - Deferred that will be returned by + * the method and resolved when the signature is not empty anymore. + * @returns {Deferred} + */ + _waitForSignatureNotEmpty: function (def) { + def = def || $.Deferred(); + if (!this.isSignatureEmpty()) { + this.trigger_up('signature_changed'); + def.resolve(); + } else { + // Use the existing def to prevent the method from creating a new + // one at every loop. + setTimeout(this._waitForSignatureNotEmpty.bind(this, def), 10); + } + return def; + }, //---------------------------------------------------------------------- // Handlers @@ -521,6 +546,15 @@ var NameAndSignature = Widget.extend({ this.$loadFile.find('input').click(); this.setMode('load'); }, + /** + * Triggers up the signature change event. + * + * @private + * @param {Event} ev + */ + _onChangeSignature: function (ev) { + this.trigger_up('signature_changed'); + }, /** * Handles change on load file input: displays the loaded image if the * format is correct, or diplays an error otherwise. diff --git a/addons/portal/static/src/js/portal_signature.js b/addons/portal/static/src/js/portal_signature.js index f2741c7d741..f9a83caa224 100644 --- a/addons/portal/static/src/js/portal_signature.js +++ b/addons/portal/static/src/js/portal_signature.js @@ -20,6 +20,9 @@ odoo.define('portal.signature_form', function (require) { events: { 'click .o_portal_sign_submit': '_onClickSignSubmit', }, + custom_events: { + 'signature_changed': '_onChangeSignature', + }, /** * Overridden to allow options. @@ -134,6 +137,15 @@ odoo.define('portal.signature_form', function (require) { } }); }, + /** + * Toggles the submit button depending on the signature state. + * + * @private + */ + _onChangeSignature: function () { + var isEmpty = this.nameAndSignature.isSignatureEmpty(); + this.$confirm_btn.prop('disabled', isEmpty); + }, }); return { diff --git a/addons/portal/static/src/xml/portal_signature.xml b/addons/portal/static/src/xml/portal_signature.xml index 9904ce88407..57fd067b4a9 100644 --- a/addons/portal/static/src/xml/portal_signature.xml +++ b/addons/portal/static/src/xml/portal_signature.xml @@ -7,7 +7,7 @@