Merge pull request #11772 from michaelmcandrew/CRM-21821-respect-nav-item-weight
[civicrm-core.git] / ang / crmMailing / BlockPreview.js
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)
4
5 angular.module('crmMailing').directive('crmMailingBlockPreview', function(crmUiHelp) {
6 return {
7 templateUrl: '~/crmMailing/BlockPreview.html',
8 link: function(scope, elm, attr) {
9 scope.$watch(attr.crmMailing, function(newValue) {
10 scope.mailing = newValue;
11 });
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};
17
18 scope.doPreview = function(mode) {
19 scope.$eval(attr.onPreview, {
20 preview: {mode: mode}
21 });
22 };
23 scope.doSend = function doSend(recipient) {
24 scope.$eval(attr.onSend, {
25 preview: {recipient: recipient}
26 });
27 };
28
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 CRM.api3({
33 contact: ['contact', 'get', {group: scope.testGroup.gid, options: {limit: 0}, return: 'display_name,email'}],
34 group: ['group', 'getsingle', {id: scope.testGroup.gid, return: 'title'}]
35 }).done(function(data) {
36 $dialog.dialog('option', 'title', ts('Send to %1', {1: data.group.title}));
37 var count = 0,
38 // Fixme: should this be in a template?
39 markup = '<ol>';
40 _.each(data.contact.values, function(row) {
41 // Fixme: contact api doesn't seem capable of filtering out contacts with no email, so we're doing it client-side
42 if (row.email) {
43 count++;
44 markup += '<li>' + row.display_name + ' - ' + row.email + '</li>';
45 }
46 });
47 markup += '</ol>';
48 markup = '<h4>' + ts('A test message will be sent to %1 people:', {1: count}) + '</h4>' + markup;
49 if (!count) {
50 markup = '<div class="messages status"><i class="crm-i fa-exclamation-triangle"></i> ' +
51 (data.contact.count ? ts('None of the contacts in this group have an email address.') : ts('Group is empty.')) +
52 '</div>';
53 }
54 $dialog
55 .html(markup)
56 .trigger('crmLoad')
57 .parent().find('button[data-op=yes]').prop('disabled', !count);
58 });
59 };
60 }
61 };
62 });
63
64 })(angular, CRM.$, CRM._);