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