9b504970 |
1 | (function(angular, $, _) { |
2 | // example <select crm-mailing-templates crm-mailing="mymailing"></select> |
3 | angular.module('crmMailing').directive('crmMailingTemplates', function(crmUiAlert) { |
4 | return { |
5 | restrict: 'AE', |
6 | require: 'ngModel', |
7 | scope: { |
8 | ngRequired: '@' |
9 | }, |
10 | templateUrl: '~/crmMailing/Templates.html', |
11 | link: function(scope, element, attrs, ngModel) { |
12 | scope.template = ngModel.$viewValue; |
13 | |
14 | var refreshUI = ngModel.$render = function refresuhUI() { |
15 | scope.template = ngModel.$viewValue; |
16 | if (ngModel.$viewValue) { |
17 | $(element).select2('val', ngModel.$viewValue); |
18 | } |
19 | }; |
20 | |
21 | // @return string HTML representing an option |
22 | function formatItem(item) { |
23 | if (!item.id) { |
24 | // return `text` for optgroup |
25 | return item.text; |
26 | } |
27 | return '<span class="crmMailing-template">' + item.text + '</span>'; |
28 | } |
29 | |
30 | var rcpAjaxState = { |
31 | input: '', |
32 | entity: 'civicrm_msg_templates', |
9b504970 |
33 | page_n: 0, |
34 | page_i: 0, |
35 | }; |
36 | |
37 | $(element).select2({ |
38 | width: '36em', |
39 | placeholder: "<i class='fa fa-clipboard'></i> Mailing Templates", |
40 | formatResult: formatItem, |
41 | escapeMarkup: function(m) { |
42 | return m; |
43 | }, |
44 | multiple: false, |
45 | initSelection: function(el, cb) { |
46 | |
47 | var value = el.val(); |
48 | |
49 | CRM.api3('MessageTemplate', 'getlist', { params: { id: value }, label_field: 'msg_title' }).then(function(tlist) { |
50 | |
51 | var template = {}; |
52 | |
53 | if (tlist.count) { |
54 | $(tlist.values).each(function(id, val) { |
55 | template.id = val.id; |
56 | template.text = val.label; |
dc9066b8 |
57 | }); |
9b504970 |
58 | } |
59 | |
60 | cb(template); |
61 | }); |
62 | }, |
63 | ajax: { |
64 | url: CRM.url('civicrm/ajax/rest'), |
65 | quietMillis: 300, |
66 | data: function(input, page_num) { |
67 | if (page_num <= 1) { |
68 | rcpAjaxState = { |
69 | input: input, |
70 | entity: 'civicrm_msg_templates', |
9b504970 |
71 | page_n: 0, |
72 | }; |
73 | } |
74 | |
75 | rcpAjaxState.page_i = page_num - rcpAjaxState.page_n; |
76 | var filterParams = { is_active: 1, workflow_id: { "IS NULL": 1 } }; |
77 | |
78 | var params = { |
79 | input: input, |
80 | page_num: rcpAjaxState.page_i, |
81 | label_field: 'msg_title', |
82 | search_field: 'msg_title', |
83 | params: filterParams, |
84 | }; |
85 | return params; |
86 | }, |
87 | transport: function(params) { |
88 | CRM.api3('MessageTemplate', 'getlist', params.data).then(params.success, params.error); |
89 | }, |
90 | results: function(data) { |
91 | |
92 | results = { |
93 | children: $.map(data.values, function(obj) { |
94 | return { id: obj.id, text: obj.label }; |
95 | }) |
96 | }; |
97 | |
98 | if (rcpAjaxState.page_i == 1 && data.count) { |
99 | results.text = ts('Message Templates'); |
100 | } |
101 | |
102 | more = data.more_results; |
103 | |
104 | if (more && !data.more_results) { |
105 | rcpAjaxState.page_n += rcpAjaxState.page_i; |
106 | } |
107 | |
108 | return { more: more, results: [ results ] }; |
109 | }, |
110 | } |
111 | }); |
112 | |
113 | $(element).on('select2-selecting', function(e) { |
114 | // in here is where the template HTML should be loaded |
115 | var entity_id = parseInt(e.val); |
116 | ngModel.$viewValue = entity_id; |
117 | |
118 | scope.$parent.loadTemplate(scope.$parent.$parent.mailing, entity_id); |
119 | scope.$apply(); |
120 | $(element).select2('close'); |
121 | e.preventDefault(); |
122 | }); |
123 | |
124 | |
125 | scope.$watchCollection("template", refreshUI); |
126 | setTimeout(refreshUI, 50); |
127 | } |
128 | }; |
129 | |
130 | |
131 | }); |
132 | })(angular, CRM.$, CRM._); |