EditRecipCtrl - Don't round recipient count
[civicrm-core.git] / ang / crmMailing / EditRecipCtrl.js
1 (function(angular, $, _) {
2
3 // Controller for the edit-recipients fields (
4 // WISHLIST: Move most of this to a (cache-enabled) service
5 // Scope members:
6 // - [input] mailing: object
7 // - [output] recipients: array of recipient records
8 angular.module('crmMailing').controller('EditRecipCtrl', function EditRecipCtrl($scope, dialogService, crmApi, crmMailingMgr, $q, crmMetadata, crmStatus) {
9 // Time to wait before triggering AJAX update to recipients list
10 var RECIPIENTS_DEBOUNCE_MS = 100;
11 var RECIPIENTS_PREVIEW_LIMIT = 50;
12
13 var ts = $scope.ts = CRM.ts(null);
14
15 $scope.isMailingList = function isMailingList(group) {
16 var GROUP_TYPE_MAILING_LIST = '2';
17 return _.contains(group.group_type, GROUP_TYPE_MAILING_LIST);
18 };
19
20 $scope.recipients = null;
21 $scope.getRecipientsEstimate = function() {
22 var ts = $scope.ts;
23 if ($scope.recipients === null) {
24 return ts('(Estimating)');
25 }
26 if ($scope.recipients === 0) {
27 return ts('No recipients');
28 }
29 if ($scope.recipients === 1) {
30 return ts('~1 recipient');
31 }
32 return ts('~%1 recipients', {1: $scope.recipients});
33 };
34
35 // We monitor four fields -- use debounce so that changes across the
36 // four fields can settle-down before AJAX.
37 var refreshRecipients = _.debounce(function() {
38 $scope.$apply(function() {
39 $scope.recipients = null;
40 if (!$scope.mailing) {
41 return;
42 }
43 crmMailingMgr.previewRecipientCount($scope.mailing).then(function(recipients) {
44 $scope.recipients = recipients;
45 });
46 });
47 }, RECIPIENTS_DEBOUNCE_MS);
48 $scope.$watchCollection("mailing.recipients.groups.include", refreshRecipients);
49 $scope.$watchCollection("mailing.recipients.groups.exclude", refreshRecipients);
50 $scope.$watchCollection("mailing.recipients.mailings.include", refreshRecipients);
51 $scope.$watchCollection("mailing.recipients.mailings.exclude", refreshRecipients);
52
53 $scope.previewRecipients = function previewRecipients() {
54 return crmStatus({start: ts('Previewing...'), success: ''}, crmMailingMgr.previewRecipients($scope.mailing, RECIPIENTS_PREVIEW_LIMIT).then(function(recipients) {
55 var model = {
56 count: $scope.recipients,
57 sample: recipients,
58 sampleLimit: RECIPIENTS_PREVIEW_LIMIT
59 };
60 var options = CRM.utils.adjustDialogDefaults({
61 width: '40%',
62 autoOpen: false,
63 title: ts('Preview (%1)', {
64 1: $scope.getRecipientsEstimate()
65 })
66 });
67 dialogService.open('recipDialog', '~/crmMailing/PreviewRecipCtrl.html', model, options);
68 }));
69 };
70
71 // Open a dialog for editing the advanced recipient options.
72 $scope.editOptions = function editOptions(mailing) {
73 var options = CRM.utils.adjustDialogDefaults({
74 autoOpen: false,
75 width: '40%',
76 height: 'auto',
77 title: ts('Edit Options')
78 });
79 $q.when(crmMetadata.getFields('Mailing')).then(function(fields) {
80 var model = {
81 fields: fields,
82 mailing: mailing
83 };
84 dialogService.open('previewComponentDialog', '~/crmMailing/EditRecipOptionsDialogCtrl.html', model, options);
85 });
86 };
87 });
88
89 })(angular, CRM.$, CRM._);