From 27f0d3115e2dc2b6a4e4a8486efbfe44072b78b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Mon, 28 Aug 2017 12:12:06 +0200 Subject: [PATCH] [IMP] portal: add a generic signature widget to be used in customer portal This generic widget allow to confirm and sign documents on the customer portal. Purpose is to reuse it in sale customer portal and online quotes notably. It is made generic to call a route using parameters given to the widget. It also supports access token to ease its use in various customer portal pages where the customer is not always connected but is granted access through an access token. --- addons/portal/__manifest__.py | 1 + .../portal/static/src/js/portal_signature.js | 118 ++++++++++++++++++ .../static/src/xml/portal_signature.xml | 30 +++++ addons/portal/views/assets.xml | 2 + addons/portal/views/portal_templates.xml | 17 +++ .../views/website_quote_templates.xml | 1 - 6 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 addons/portal/static/src/js/portal_signature.js create mode 100644 addons/portal/static/src/xml/portal_signature.xml diff --git a/addons/portal/__manifest__.py b/addons/portal/__manifest__.py index d932cb2f025..6f1c0186a86 100644 --- a/addons/portal/__manifest__.py +++ b/addons/portal/__manifest__.py @@ -24,5 +24,6 @@ a dependency towards website edition and customization capabilities.""", ], 'qweb': [ 'static/src/xml/portal_chatter.xml', + 'static/src/xml/portal_signature.xml', ], } diff --git a/addons/portal/static/src/js/portal_signature.js b/addons/portal/static/src/js/portal_signature.js new file mode 100644 index 00000000000..eb58234907c --- /dev/null +++ b/addons/portal/static/src/js/portal_signature.js @@ -0,0 +1,118 @@ +odoo.define('portal.signature_form', function (require){ + "use strict"; + + require('web_editor.ready'); + + var ajax = require('web.ajax'); + var base = require('web_editor.base'); + var core = require('web.core'); + var Widget = require("web.Widget"); + var rpc = require("web.rpc"); + + var qweb = core.qweb; + + var SignatureForm = Widget.extend({ + template: 'portal.portal_signature', + events: { + 'click #o_portal_sign_clear': 'clearSign', + 'click .o_portal_sign_submit': 'submitSign', + 'init #o_portal_sign_accept': 'initSign', + }, + + init: function(parent, options) { + this._super.apply(this, arguments); + this.options = _.extend(options || {}, { + csrf_token: odoo.csrf_token, + }); + }, + + willStart: function() { + return this._loadTemplates(); + }, + + start: function() { + this.initSign(); + }, + + // Signature + initSign: function () { + this.$("#o_portal_signature").empty().jSignature({ + 'decor-color': '#D1D0CE', + 'color': '#000', + 'background-color': '#fff', + }); + this.empty_sign = this.$("#o_portal_signature").jSignature('getData', 'image'); + }, + + clearSign: function () { + this.$("#o_portal_signature").jSignature('reset'); + }, + + submitSign: function (ev) { + ev.preventDefault(); + + // extract data + var self = this; + var $confirm_btn = self.$el.find('button[type="submit"]'); + + // process : display errors, or submit + var partner_name = self.$("#o_portal_sign_name").val(); + var signature = self.$("#o_portal_signature").jSignature('getData', 'image'); + var is_empty = signature ? this.empty_sign[1] === signature[1] : true; + + this.$('#o_portal_sign_name').toggleClass('has-error', !partner_name); + this.$('#o_portal_sign_draw').toggleClass('panel-danger', is_empty).toggleClass('panel-default', !is_empty); + if (is_empty || ! partner_name) { + return false; + } + + $confirm_btn.prepend(' '); + $confirm_btn.attr('disabled', true); + + return rpc.query({ + route: this.options.callUrl, + params: { + 'res_id': this.options.resId, + 'access_token': this.options.accessToken, + 'partner_name': partner_name, + 'signature': signature ? signature[1] : false, + }, + }).then(function (data) { + self.$('.fa-spinner').remove(); + self.$('#o_portal_sign_accept').prepend('
PROUT' + data + '
'); + if (data.error) { + $confirm_btn.attr('disabled', false); + } + else if (data.success) { + $confirm_btn.remove(); + var $prout = qweb.render("portal.portal_signature_success", {widget: data}); + self.$('#o_portal_sign_draw').parent().replaceWith($prout); + } + }); + }, + + //-------------------------------------------------------------------------- + // Private + //-------------------------------------------------------------------------- + + /** + * @private + * @returns {Deferred} + */ + _loadTemplates: function () { + return ajax.loadXML('/portal/static/src/xml/portal_signature.xml', qweb); + }, + }); + + base.ready().then(function () { + $('.o_portal_signature_form').each(function () { + var $elem = $(this); + var form = new SignatureForm(null, $elem.data()); + form.appendTo($elem); + }); + }); + + return { + SignatureForm: SignatureForm, + }; +}); diff --git a/addons/portal/static/src/xml/portal_signature.xml b/addons/portal/static/src/xml/portal_signature.xml new file mode 100644 index 00000000000..a82d7ec89aa --- /dev/null +++ b/addons/portal/static/src/xml/portal_signature.xml @@ -0,0 +1,30 @@ + + +
+ +
+ + +
+
+
+
+ Clear +
+ Draw your signature +
+
+
+
+ +
+ + + + + + \ No newline at end of file diff --git a/addons/portal/views/assets.xml b/addons/portal/views/assets.xml index 9101cba2124..aaf2f6ec3e8 100644 --- a/addons/portal/views/assets.xml +++ b/addons/portal/views/assets.xml @@ -11,6 +11,8 @@ + + diff --git a/addons/portal/views/portal_templates.xml b/addons/portal/views/portal_templates.xml index 0c5bcf6c1c6..ce5ca4c2951 100644 --- a/addons/portal/views/portal_templates.xml +++ b/addons/portal/views/portal_templates.xml @@ -354,4 +354,21 @@
+ + + diff --git a/addons/website_quote/views/website_quote_templates.xml b/addons/website_quote/views/website_quote_templates.xml index 6931631528c..f7c9ef5a58b 100644 --- a/addons/website_quote/views/website_quote_templates.xml +++ b/addons/website_quote/views/website_quote_templates.xml @@ -3,7 +3,6 @@