From 252d31ffacbf9df98bcc229e4decbb198b235f34 Mon Sep 17 00:00:00 2001 From: yuri Date: Tue, 20 Mar 2018 14:00:05 +0200 Subject: [PATCH] email template for reply --- .../crm/src/views/record/panels/history.js | 4 +- client/src/views/email/detail.js | 8 +-- client/src/views/email/record/compose.js | 54 ++++++++++++++++++- 3 files changed, 56 insertions(+), 10 deletions(-) diff --git a/client/modules/crm/src/views/record/panels/history.js b/client/modules/crm/src/views/record/panels/history.js index a45ba481ef..0a1d30591d 100644 --- a/client/modules/crm/src/views/record/panels/history.js +++ b/client/modules/crm/src/views/record/panels/history.js @@ -172,9 +172,7 @@ Espo.define('crm:views/record/panels/history', 'crm:views/record/panels/activiti this.createView('quickCreate', viewName, { attributes: attributes, }, function (view) { - view.render(function () { - view.getView('edit').hideField('selectTemplate'); - }); + view.render(); this.listenToOnce(view, 'after:save', function () { this.collection.fetch(); diff --git a/client/src/views/email/detail.js b/client/src/views/email/detail.js index 7e553252d5..7224ff3801 100644 --- a/client/src/views/email/detail.js +++ b/client/src/views/email/detail.js @@ -303,9 +303,7 @@ Espo.define('views/email/detail', ['views/detail', 'email-helper'], function (De this.createView('quickCreate', viewName, { attributes: attributes, }, function (view) { - view.render(function () { - view.getView('edit').hideField('selectTemplate'); - }); + view.render(); view.notify(false); @@ -340,9 +338,7 @@ Espo.define('views/email/detail', ['views/detail', 'email-helper'], function (De this.createView('quickCreate', viewName, { attributes: attributes, }, function (view) { - view.render(function () { - view.getView('edit').hideField('selectTemplate'); - }); + view.render(); view.notify(false); }); diff --git a/client/src/views/email/record/compose.js b/client/src/views/email/record/compose.js index 31349dacb7..b9091794c0 100644 --- a/client/src/views/email/record/compose.js +++ b/client/src/views/email/record/compose.js @@ -37,17 +37,31 @@ Espo.define('views/email/record/compose', ['views/record/edit', 'views/email/rec setup: function () { Dep.prototype.setup.call(this); + this.initialBody = null; + this.initialIsHtml = null; + if (!this.model.get('isHtml') && this.getPreferences().get('emailReplyForceHtml')) { var body = (this.model.get('body') || '').replace(/\n/g, '
'); this.model.set('body', body); this.model.set('isHtml', true); } + if (this.model.get('body')) { + this.initialBody = this.model.get('body'); + this.initialIsHtml = this.model.get('isHtml'); + } + if (!this.options.signatureDisabled && this.hasSignature()) { var body = this.prependSignature(this.model.get('body') || '', this.model.get('isHtml')); this.model.set('body', body); } + this.isBodyChanged = false; + + this.listenTo(this.model, 'change:body', function () { + this.isBodyChanged = true; + }, this); + if (this.options.keepAttachmentsOnSelectTemplate) { this.initialAttachmentsIds = this.model.get('attachmentsIds') || []; this.initialAttachmentsNames = this.model.get('attachmentsNames') || {}; @@ -64,7 +78,10 @@ Espo.define('views/email/record/compose', ['views/record/edit', 'views/email/rec var $div = $('
').html(bodyPlain); bodyPlain = $div.text(); - if (bodyPlain !== '' && this.model.get('body') !== this.attributes.body) { + if ( + bodyPlain !== '' && + this.isBodyChanged + ) { this.confirm({ message: this.translate('confirmInsertTemplate', 'messages', 'Email'), confirmText: this.translate('Yes') @@ -87,6 +104,20 @@ Espo.define('views/email/record/compose', ['views/record/edit', 'views/email/rec if (this.hasSignature()) { body = this.appendSignature(body || '', data.isHtml); } + + if (this.initialBody) { + var initialBody = this.initialBody; + if (data.isHtml !== this.initialIsHtml) { + if (data.isHtml) { + initialBody = this.plainToHtml(initialBody); + } else { + initialBody = this.htmlToPlain(initialBody); + } + } + + body += initialBody; + } + this.model.set('isHtml', data.isHtml); this.model.set('name', data.subject); this.model.set('body', ''); @@ -160,6 +191,27 @@ Espo.define('views/email/record/compose', ['views/record/edit', 'views/email/rec model.set('status', 'Draft'); this.save(); + }, + + htmlToPlain: function (text) { + text = text || ''; + var value = text.replace(//mg, '\n'); + + value = value.replace(/<\/p\s*\/?>/mg, '\n\n'); + + var $div = $('
').html(value); + $div.find('style').remove(); + $div.find('link[ref="stylesheet"]').remove(); + + value = $div.text(); + + return value; + }, + + plainToHtml: function (html) { + html = html || ''; + var value = html.replace(/\n/g, '
'); + return value; } });