From 9b5049700825d6f334201c9d826ad322a8fa0302 Mon Sep 17 00:00:00 2001 From: nbrettell Date: Fri, 10 Nov 2017 17:20:50 +0000 Subject: [PATCH] CRM-21383 - Inclusion of a directive to replace the existing select field for loading Message templates on demand in the CiviMail compose UI --- ang/crmMailing/BlockMailing.html | 15 +--- ang/crmMailing/BlockTemplates.html | 9 ++ ang/crmMailing/BlockTemplates.js | 5 ++ ang/crmMailing/MsgTemplateCtrl.js | 1 + ang/crmMailing/Templates.js | 134 +++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+), 14 deletions(-) create mode 100644 ang/crmMailing/BlockTemplates.html create mode 100644 ang/crmMailing/BlockTemplates.js create mode 100644 ang/crmMailing/Templates.js diff --git a/ang/crmMailing/BlockMailing.html b/ang/crmMailing/BlockMailing.html index 2e37083c1b..6a63bb56f7 100644 --- a/ang/crmMailing/BlockMailing.html +++ b/ang/crmMailing/BlockMailing.html @@ -7,20 +7,7 @@ It could perhaps be thinned by 30-60% by making more directives.
-
- - -
+
diff --git a/ang/crmMailing/BlockTemplates.html b/ang/crmMailing/BlockTemplates.html new file mode 100644 index 0000000000..38a37a91fd --- /dev/null +++ b/ang/crmMailing/BlockTemplates.html @@ -0,0 +1,9 @@ +
+ + +
\ No newline at end of file diff --git a/ang/crmMailing/BlockTemplates.js b/ang/crmMailing/BlockTemplates.js new file mode 100644 index 0000000000..9ee2efbfee --- /dev/null +++ b/ang/crmMailing/BlockTemplates.js @@ -0,0 +1,5 @@ +(function(angular, $, _) { + angular.module('crmMailing').directive('crmMailingBlockTemplates', function(crmMailingSimpleDirective) { + return crmMailingSimpleDirective('crmMailingBlockTemplates', '~/crmMailing/BlockTemplates.html'); + }); +})(angular, CRM.$, CRM._); \ No newline at end of file diff --git a/ang/crmMailing/MsgTemplateCtrl.js b/ang/crmMailing/MsgTemplateCtrl.js index b635f13fc5..5d200c3ffd 100644 --- a/ang/crmMailing/MsgTemplateCtrl.js +++ b/ang/crmMailing/MsgTemplateCtrl.js @@ -33,6 +33,7 @@ // @return Promise $scope.loadTemplate = function loadTemplate(mailing, id) { return crmMsgTemplates.get(id).then(function(tpl) { + mailing.msg_template_id = tpl.id; mailing.subject = tpl.msg_subject; mailing.body_text = tpl.msg_text; mailing.body_html = tpl.msg_html; diff --git a/ang/crmMailing/Templates.js b/ang/crmMailing/Templates.js new file mode 100644 index 0000000000..44c855a68b --- /dev/null +++ b/ang/crmMailing/Templates.js @@ -0,0 +1,134 @@ +(function(angular, $, _) { + // example + angular.module('crmMailing').directive('crmMailingTemplates', function(crmUiAlert) { + return { + restrict: 'AE', + require: 'ngModel', + scope: { + ngRequired: '@' + }, + templateUrl: '~/crmMailing/Templates.html', + link: function(scope, element, attrs, ngModel) { + scope.template = ngModel.$viewValue; + + var refreshUI = ngModel.$render = function refresuhUI() { + scope.template = ngModel.$viewValue; + if (ngModel.$viewValue) { + $(element).select2('val', ngModel.$viewValue); + } + }; + + // @return string HTML representing an option + function formatItem(item) { + if (!item.id) { + // return `text` for optgroup + return item.text; + } + return '' + item.text + ''; + } + + var rcpAjaxState = { + input: '', + entity: 'civicrm_msg_templates', + type: 'include', + page_n: 0, + page_i: 0, + }; + + $(element).select2({ + width: '36em', + placeholder: " Mailing Templates", + formatResult: formatItem, + escapeMarkup: function(m) { + return m; + }, + multiple: false, + initSelection: function(el, cb) { + + var value = el.val(); + + CRM.api3('MessageTemplate', 'getlist', { params: { id: value }, label_field: 'msg_title' }).then(function(tlist) { + + var template = {}; + + if (tlist.count) { + $(tlist.values).each(function(id, val) { + template.id = val.id; + template.text = val.label; + }) + } + + cb(template); + }); + }, + ajax: { + url: CRM.url('civicrm/ajax/rest'), + quietMillis: 300, + data: function(input, page_num) { + if (page_num <= 1) { + rcpAjaxState = { + input: input, + entity: 'civicrm_msg_templates', + type: 'include', + page_n: 0, + }; + } + + rcpAjaxState.page_i = page_num - rcpAjaxState.page_n; + var filterParams = { is_active: 1, workflow_id: { "IS NULL": 1 } }; + + var params = { + input: input, + page_num: rcpAjaxState.page_i, + label_field: 'msg_title', + search_field: 'msg_title', + params: filterParams, + }; + return params; + }, + transport: function(params) { + CRM.api3('MessageTemplate', 'getlist', params.data).then(params.success, params.error); + }, + results: function(data) { + + results = { + children: $.map(data.values, function(obj) { + return { id: obj.id, text: obj.label }; + }) + }; + + if (rcpAjaxState.page_i == 1 && data.count) { + results.text = ts('Message Templates'); + } + + more = data.more_results; + + if (more && !data.more_results) { + rcpAjaxState.page_n += rcpAjaxState.page_i; + } + + return { more: more, results: [ results ] }; + }, + } + }); + + $(element).on('select2-selecting', function(e) { + // in here is where the template HTML should be loaded + var entity_id = parseInt(e.val); + ngModel.$viewValue = entity_id; + + scope.$parent.loadTemplate(scope.$parent.$parent.mailing, entity_id); + scope.$apply(); + $(element).select2('close'); + e.preventDefault(); + }); + + + scope.$watchCollection("template", refreshUI); + setTimeout(refreshUI, 50); + } + }; + + + }); +})(angular, CRM.$, CRM._); \ No newline at end of file -- 2.25.1