1 (function (angular
, $, _
) {
3 var partialUrl = function (relPath
, module
) {
5 module
= 'crmMailingAB';
7 return CRM
.resourceUrls
['civicrm'] + '/partials/' + module
+ '/' + relPath
;
10 angular
.module('crmMailingAB', ['ngRoute', 'ui.utils', 'ngSanitize', 'crmUi', 'crmAttachment', 'crmMailing']);
11 angular
.module('crmMailingAB').config([
13 function ($routeProvider
) {
14 $routeProvider
.when('/abtest', {
15 templateUrl
: partialUrl('list.html'),
16 controller
: 'CrmMailingABListCtrl',
18 mailingABList: function ($route
, crmApi
) {
19 return crmApi('MailingAB', 'get', {rowCount
: 0});
23 $routeProvider
.when('/abtest/:id', {
24 templateUrl
: partialUrl('edit.html'),
25 controller
: 'CrmMailingABEditCtrl',
27 abtest: function ($route
, CrmMailingAB
) {
28 var abtest
= new CrmMailingAB($route
.current
.params
.id
== 'new' ? null : $route
.current
.params
.id
);
36 angular
.module('crmMailingAB').controller('CrmMailingABListCtrl', function ($scope
, mailingABList
, crmMailingABCriteria
) {
37 $scope
.mailingABList
= mailingABList
.values
;
38 $scope
.testing_criteria
= crmMailingABCriteria
.getAll();
41 angular
.module('crmMailingAB').controller('CrmMailingABEditCtrl', function ($scope
, abtest
, crmMailingABCriteria
, crmMailingMgr
, crmMailingPreviewMgr
, crmStatus
, $q
, $location
) {
42 $scope
.abtest
= abtest
;
43 var ts
= $scope
.ts
= CRM
.ts('CiviMail');
44 $scope
.crmMailingABCriteria
= crmMailingABCriteria
;
45 $scope
.crmMailingConst
= CRM
.crmMailing
;
46 $scope
.partialUrl
= partialUrl
;
48 $scope
.isSubmitted
= function isSubmitted() {
49 return _
.size(abtest
.mailings
.a
.jobs
) > 0 || _
.size(abtest
.mailings
.b
.jobs
) > 0;
51 $scope
.sync
= function sync() {
52 abtest
.mailings
.a
.name
= ts('Test A (%1)', {1: abtest
.ab
.name
});
53 abtest
.mailings
.b
.name
= ts('Test B (%1)', {1: abtest
.ab
.name
});
54 abtest
.mailings
.c
.name
= ts('Winner (%1)', {1: abtest
.ab
.name
});
56 var criteria
= crmMailingABCriteria
.get(abtest
.ab
.testing_criteria_id
);
58 // TODO review fields exposed in UI and make sure the sync rules match
59 switch (criteria
.name
) {
61 crmMailingMgr
.mergeInto(abtest
.mailings
.b
, abtest
.mailings
.a
, ['name', 'subject']);
64 crmMailingMgr
.mergeInto(abtest
.mailings
.b
, abtest
.mailings
.a
, ['name', 'from_name', 'from_email']);
66 case 'Two different emails':
67 crmMailingMgr
.mergeInto(abtest
.mailings
.b
, abtest
.mailings
.a
, [
77 throw "Unrecognized testing_criteria";
80 crmMailingMgr
.mergeInto(abtest
.mailings
.c
, abtest
.mailings
.a
, ['name']);
84 $scope
.save
= function save() {
86 return crmStatus({start
: ts('Saving...'), success
: ts('Saved')}, abtest
.save().then(updateUrl
));
90 $scope
.previewMailing
= function previewMailing(mailingName
, mode
) {
91 return crmMailingPreviewMgr
.preview(abtest
.mailings
[mailingName
], mode
);
95 $scope
.sendTest
= function sendTest(mailingName
, recipient
) {
96 return crmStatus({start
: ts('Saving...'), success
: ''}, abtest
.save().then(updateUrl
))
98 crmMailingPreviewMgr
.sendTest(abtest
.mailings
[mailingName
], recipient
);
103 $scope
.delete = function () {
104 return crmStatus({start
: ts('Deleting...'), success
: ts('Deleted')}, abtest
.delete());
108 $scope
.submit
= function submit() {
109 return crmStatus({start
: ts('Saving...'), success
: ''}, abtest
.save().then(updateUrl
))
111 return crmStatus({start
: ts('Submitting...'), success
: ts('Submitted')}, $q
.all([
112 crmMailingMgr
.submit(abtest
.mailings
.a
),
113 crmMailingMgr
.submit(abtest
.mailings
.b
)
118 function updateCriteriaName() {
119 var criteria
= crmMailingABCriteria
.get($scope
.abtest
.ab
.testing_criteria_id
)
120 $scope
.criteriaName
= criteria
? criteria
.name
: null;
123 // Transition URL "/abtest/new" => "/abtest/123"
124 function updateUrl() {
125 var parts
= $location
.path().split('/'); // e.g. "/abtest/new" or "/abtest/123/wizard"
126 if (parts
[2] != $scope
.abtest
.ab
.id
) {
127 parts
[2] = $scope
.abtest
.ab
.id
;
128 $location
.path(parts
.join('/'));
130 // FIXME: Angular unnecessarily refreshes UI
131 // WARNING: Changing the URL triggers a full reload. Any pending AJAX operations
132 // could be inconsistently applied. Run updateUrl() after other changes complete.
137 updateCriteriaName();
139 $scope
.$watch('abtest.ab.testing_criteria_id', updateCriteriaName
);
142 })(angular
, CRM
.$, CRM
._
);