1 (function(angular
, $, _
) {
2 // example: <div crm-mailing-block-preview crm-mailing="myMailing" on-preview="openPreview(myMailing, preview.mode)" on-send="sendEmail(myMailing,preview.recipient)">
3 // note: the directive defines a variable called "preview" with any inputs supplied by the user (e.g. the target recipient for an example mailing)
5 angular
.module('crmMailing').directive('crmMailingBlockPreview', function(crmUiHelp
) {
7 templateUrl
: '~/crmMailing/BlockPreview.html',
8 link: function(scope
, elm
, attr
) {
9 scope
.$watch(attr
.crmMailing
, function(newValue
) {
10 scope
.mailing
= newValue
;
12 scope
.crmMailingConst
= CRM
.crmMailing
;
13 scope
.ts
= CRM
.ts(null);
14 scope
.hs
= crmUiHelp({file
: 'CRM/Mailing/MailingUI'});
15 scope
.testContact
= {email
: CRM
.crmMailing
.defaultTestEmail
};
16 scope
.testGroup
= {gid
: null};
18 scope
.doPreview = function(mode
) {
19 scope
.$eval(attr
.onPreview
, {
23 scope
.doSend
= function doSend(recipient
) {
24 scope
.$eval(attr
.onSend
, {
25 preview
: {recipient
: recipient
}
29 scope
.previewTestGroup = function(e
) {
30 var $dialog
= $(this);
31 $dialog
.html('<div class="crm-loading-element"></div>').parent().find('button[data-op=yes]').prop('disabled', true);
32 $dialog
.dialog('option', 'title', ts('Send to %1', {1: _
.pluck(_
.where(scope
.crmMailingConst
.groupNames
, {id
: scope
.testGroup
.gid
}), 'title')[0]}));
33 CRM
.api3('contact', 'get', {
34 group
: scope
.testGroup
.gid
,
36 return: 'display_name,email'
37 }).done(function(data
) {
39 // Fixme: should this be in a template?
41 _
.each(data
.values
, function(row
) {
42 // Fixme: contact api doesn't seem capable of filtering out contacts with no email, so we're doing it client-side
45 markup
+= '<li>' + row
.display_name
+ ' - ' + row
.email
+ '</li>';
49 markup
= '<h4>' + ts('A test message will be sent to %1 people:', {1: count
}) + '</h4>' + markup
;
51 markup
= '<div class="messages status"><i class="crm-i fa-exclamation-triangle"></i> ' +
52 (data
.count
? ts('None of the contacts in this group have an email address.') : ts('Group is empty.')) +
58 .parent().find('button[data-op=yes]').prop('disabled', !count
);
65 })(angular
, CRM
.$, CRM
._
);