You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
6.0 KiB

  1. odoo.define('web_widget_digitized_signature.web_digital_sign', function(require) {
  2. "use strict";
  3. var core = require('web.core');
  4. var BasicFields= require('web.basic_fields');
  5. var FormController = require('web.FormController');
  6. var Registry = require('web.field_registry');
  7. var utils = require('web.utils');
  8. var session = require('web.session');
  9. var field_utils = require('web.field_utils');
  10. var _t = core._t;
  11. var QWeb = core.qweb;
  12. var FieldSignature = BasicFields.FieldBinaryImage.extend({
  13. template: 'FieldSignature',
  14. events: _.extend({}, BasicFields.FieldBinaryImage.prototype.events, {
  15. 'click .save_sign': '_on_save_sign',
  16. 'click #sign_clean': '_on_clear_sign'
  17. }),
  18. jsLibs: ['/web_widget_digitized_signature/static/lib/jSignature/jSignatureCustom.js'],
  19. placeholder: "/web/static/src/img/placeholder.png",
  20. init: function() {
  21. this._super.apply(this, arguments);
  22. // this.$('> img').remove();
  23. // this.$('.signature > canvas').remove();
  24. this.sign_options = {
  25. 'decor-color': '#D1D0CE',
  26. 'color': '#000',
  27. 'background-color': '#fff',
  28. 'height': '150',
  29. 'width': '550'
  30. };
  31. this.empty_sign = [];
  32. },
  33. start: function() {
  34. var self = this;
  35. this.$(".signature").jSignature("init", this.sign_options);
  36. this.$(".signature").attr({
  37. "tabindex": "0",
  38. 'height': "100"
  39. });
  40. this.empty_sign = this.$(".signature").jSignature("getData", 'image');
  41. self._render();
  42. },
  43. _on_clear_sign: function() {
  44. this.$(".signature > canvas").remove();
  45. this.$('> img').remove();
  46. this.$(".signature").attr("tabindex", "0");
  47. var sign_options = {
  48. 'decor-color': '#D1D0CE',
  49. 'color': '#000',
  50. 'background-color': '#fff',
  51. 'height': '150',
  52. 'width': '550',
  53. 'clear': true
  54. };
  55. this.$(".signature").jSignature(sign_options);
  56. this.$(".signature").focus();
  57. this._setValue(false);
  58. },
  59. _on_save_sign: function(value_) {
  60. var self = this;
  61. this.$('> img').remove();
  62. var signature = this.$(".signature").jSignature("getData", 'image');
  63. var is_empty = signature ?
  64. self.empty_sign[1] === signature[1] :
  65. false;
  66. if (!is_empty && typeof signature !== "undefined" && signature[1]) {
  67. this._setValue(signature[1]);
  68. }
  69. },
  70. _render: function() {
  71. var self = this;
  72. var url = this.placeholder;
  73. if (this.value && !utils.is_bin_size(this.value)) {
  74. url = 'data:image/png;base64,' + this.value;
  75. } else if (this.value) {
  76. url = session.url('/web/image', {
  77. model: this.model,
  78. id: JSON.stringify(this.res_id),
  79. field: this.nodeOptions.preview_image || this.name,
  80. unique: field_utils.format.datetime(this.recordData.__last_update).replace(/[^0-9]/g, ''),
  81. });
  82. } else {
  83. url = this.placeholder;
  84. }
  85. if (this.mode === "readonly") {
  86. var $img = $(QWeb.render("FieldBinaryImage-img", {
  87. widget: self,
  88. url: url
  89. }));
  90. this.$('> img').remove();
  91. this.$(".signature").hide();
  92. this.$el.prepend($img);
  93. $img.on('error', function() {
  94. self.on_clear();
  95. $img.attr('src', self.placeholder);
  96. self.do_warn(_t("Image"), _t("Could not display the selected image."));
  97. });
  98. } else if (this.mode === "edit") {
  99. this.$('> img').remove();
  100. if (this.value) {
  101. var field_name = this.nodeOptions.preview_image ?
  102. this.nodeOptions.preview_image :
  103. this.name;
  104. self._rpc({
  105. model: this.model,
  106. method: 'read',
  107. args: [this.res_id, [field_name]]
  108. }).done(function(data) {
  109. if (data) {
  110. var field_desc = _.values(_.pick(data[0], field_name));
  111. self.$(".signature").jSignature("clear");
  112. self.$(".signature").jSignature("setData", 'data:image/png;base64,' + field_desc[0]);
  113. }
  114. });
  115. } else {
  116. this.$('> img').remove();
  117. this.$('.signature > canvas').remove();
  118. var sign_options = {
  119. 'decor-color': '#D1D0CE',
  120. 'color': '#000',
  121. 'background-color': '#fff',
  122. 'height': '150',
  123. 'width': '550'
  124. };
  125. this.$(".signature").jSignature("init", sign_options);
  126. }
  127. } else if (this.mode === 'create') {
  128. this.$('> img').remove();
  129. this.$('> canvas').remove();
  130. if (!this.value) {
  131. this.$(".signature").empty().jSignature("init", {
  132. 'decor-color': '#D1D0CE',
  133. 'color': '#000',
  134. 'background-color': '#fff',
  135. 'height': '150',
  136. 'width': '550'
  137. });
  138. }
  139. }
  140. }
  141. });
  142. FormController.include({
  143. saveRecord: function() {
  144. this.$('.save_sign').click();
  145. return this._super.apply(this, arguments);
  146. }
  147. });
  148. Registry.add('signature', FieldSignature);
  149. });