1 (function(angular
, $, _
) {
2 // example <select crm-mailing-templates crm-mailing="mymailing"></select>
3 angular
.module('crmMailing').directive('crmMailingTemplates', function(crmUiAlert
) {
10 link: function(scope
, element
, attrs
, ngModel
) {
11 scope
.template
= ngModel
.$viewValue
;
13 var refreshUI
= ngModel
.$render
= function refresuhUI() {
14 scope
.template
= ngModel
.$viewValue
;
15 if (ngModel
.$viewValue
) {
16 $(element
).select2('val', ngModel
.$viewValue
);
20 // @return string HTML representing an option
21 function formatItem(item
) {
23 // return `text` for optgroup
26 return '<span class="crmMailing-template">' + item
.text
+ '</span>';
31 entity
: 'civicrm_msg_templates',
38 placeholder
: "<i class='fa fa-clipboard'></i> Mailing Templates",
39 formatResult
: formatItem
,
40 escapeMarkup: function(m
) {
44 initSelection: function(el
, cb
) {
48 CRM
.api3('MessageTemplate', 'getlist', {params
: {id
: value
}, label_field
: 'msg_title'}).then(function(tlist
) {
53 $(tlist
.values
).each(function(id
, val
) {
55 template
.text
= val
.label
;
63 url
: CRM
.url('civicrm/ajax/rest'),
65 data: function(input
, page_num
) {
69 entity
: 'civicrm_msg_templates',
74 rcpAjaxState
.page_i
= page_num
- rcpAjaxState
.page_n
;
75 var filterParams
= {is_active
: 1, workflow_name
: {"IS NULL": 1}};
79 page_num
: rcpAjaxState
.page_i
,
80 label_field
: 'msg_title',
81 search_field
: 'msg_title',
86 transport: function(params
) {
87 CRM
.api3('MessageTemplate', 'getlist', params
.data
).then(params
.success
, params
.error
);
89 results: function(data
) {
92 children
: $.map(data
.values
, function(obj
) {
93 return {id
: obj
.id
, text
: obj
.label
};
97 if (rcpAjaxState
.page_i
== 1 && data
.count
) {
98 results
.text
= ts('Message Templates');
102 more
: data
.more_results
,
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
;
114 scope
.$parent
.loadTemplate(scope
.$parent
.$parent
.mailing
, entity_id
);
116 $(element
).select2('close');
121 scope
.$watchCollection("template", refreshUI
);
122 setTimeout(refreshUI
, 50);
128 })(angular
, CRM
.$, CRM
._
);