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 | ||
fba44730 | 48 | CRM.api3('MessageTemplate', 'getlist', {params: {id: value}, label_field: 'msg_title'}).then(function(tlist) { |
9b504970 | 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; | |
fba44730 | 75 | var filterParams = {is_active: 1, workflow_name: {"IS NULL": 1}}; |
6932587c | 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 | ||
7cc12cc3 | 91 | var results = { |
9b504970 | 92 | children: $.map(data.values, function(obj) { |
fba44730 | 93 | return {id: obj.id, text: obj.label}; |
9b504970 | 94 | }) |
95 | }; | |
96 | ||
97 | if (rcpAjaxState.page_i == 1 && data.count) { | |
98 | results.text = ts('Message Templates'); | |
99 | } | |
100 | ||
fba44730 CW |
101 | return { |
102 | more: data.more_results, | |
103 | results: [results] | |
104 | }; | |
9b504970 | 105 | }, |
106 | } | |
107 | }); | |
108 | ||
109 | $(element).on('select2-selecting', function(e) { | |
110 | // in here is where the template HTML should be loaded | |
111 | var entity_id = parseInt(e.val); | |
112 | ngModel.$viewValue = entity_id; | |
113 | ||
114 | scope.$parent.loadTemplate(scope.$parent.$parent.mailing, entity_id); | |
115 | scope.$apply(); | |
116 | $(element).select2('close'); | |
117 | e.preventDefault(); | |
118 | }); | |
119 | ||
120 | ||
121 | scope.$watchCollection("template", refreshUI); | |
122 | setTimeout(refreshUI, 50); | |
123 | } | |
124 | }; | |
125 | ||
126 | ||
127 | }); | |
af92a69e | 128 | })(angular, CRM.$, CRM._); |