Lisitng Page Fixes
[civicrm-core.git] / js / angular-crmMailingAB.js
1 /**
2 * Created by aditya on 6/12/14.
3 */
4
5
6 (function (angular, $, _) {
7
8 var partialUrl = function (relPath) {
9 return CRM.resourceUrls['civicrm'] + '/partials/abtesting/' + relPath;
10 };
11
12 var crmMailingAB = angular.module('crmMailingAB', ['ngRoute', 'ui.utils','ngSanitize']);
13 var mltokens = [];
14 crmMailingAB.run(function ($rootScope, $templateCache) {
15 $rootScope.$on('$viewContentLoaded', function () {
16 $templateCache.removeAll();
17 });
18 });
19 crmMailingAB.config([
20 '$routeProvider',
21 function ($routeProvider) {
22 $routeProvider.when('/mailing/abtesting', {
23 templateUrl: partialUrl('list.html'),
24 controller: 'ABListingCtrl',
25 resolve: {
26 mailingList: function ($route, crmApi) {
27 return crmApi('Mailing', 'get', {});
28 }
29 }
30 });
31 $routeProvider.when('/mailing/abtesting/report/:id', {
32 templateUrl: partialUrl('report.html'),
33 controller: 'ReportCtrl',
34 resolve: {
35 selectedABTest: function($route, crmApi) {
36 return crmApi('MailingAB', 'getsingle', {id: $route.current.params.id});
37
38 }
39 }
40
41 });
42 $routeProvider.when('/mailing/abtesting/:id', {
43 templateUrl: partialUrl('main.html'),
44 controller: 'TabsDemoCtrl',
45 resolve: {
46 selectedABTest: function($route, crmApi) {
47 if ( $route.current.params.id !== 'new') {
48
49 return crmApi('MailingAB', 'getsingle', {id: $route.current.params.id});
50 }
51 else {
52 console.log("created");
53 //created_id has been set to my id. Does not save without created_id. Needs to made generic based on the user
54 return { just_created:"1"
55 };
56 }
57 }
58 }
59 });
60 }
61 ]);
62
63 //-----------------------------------------
64 // Add a new record by name.
65 // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" />
66 crmMailingAB.controller('ABListingCtrl', function ($scope, crmApi) {
67 $scope.abmailList = CRM.crmMailing.mailingabNames;
68 console.log($scope.abmailList);
69
70
71 });
72 crmMailingAB.controller('ReportCtrl', function ($scope, crmApi,selectedABTest) {
73
74
75 $scope.graph_data = [{
76 time: 1,
77 x: 3,
78 y: 4
79 }, {
80 time: 2,
81 x: 6,
82 y: 8,
83 }, {
84 time: 3,
85 x: 10,
86 y: 9,
87 }, {
88 time: 4,
89 x: 13,
90 y: 11,
91 }]
92 console.log(selectedABTest);
93 if(selectedABTest.winner_criteria_id==1){
94 $scope.winner_criteria = "Open";
95 }
96 else if(selectedABTest.winner_criteria_id ==2){
97 $scope.winner_criteria ="Total Unique Clicks";
98 }
99 else if(selectedABTest.winner_criteria_id ==3){
100 $scope.winner_criteria ="Total Clicks on a particular link";
101
102 }
103
104
105
106 })
107 crmMailingAB.controller('TabsDemoCtrl', function ($scope, crmApi,selectedABTest,$sce) {
108 $scope.abId="";
109 $scope.whatnext=2;
110 $scope.currentABTest=selectedABTest
111 $scope.groups = CRM.crmMailing.groupNames;
112 $scope.mailList = CRM.crmMailing.civiMails;
113 $scope.eMailing = CRM.crmMailing.emailAdd;
114 $scope.tmpList = CRM.crmMailing.mesTemplate;
115 $scope.headerfooter = CRM.crmMailing.headerfooterList;
116 if($scope.currentABTest.declare_winning_time != null){
117 $scope.ans= $scope.currentABTest.declare_winning_time.split(" ");
118 $scope.currentABTest.date=$scope.ans[0];
119 $scope.currentABTest.time=$scope.ans[1];
120
121 }
122
123 if($scope.currentABTest.just_created != 1){
124 console.log("Prithvi");
125 console.log($scope.currentABTest);
126 console.log($scope.currentABTest.mailing_id_a);
127
128 $scope.abId = $scope.currentABTest.id;
129 var abmailA = crmApi('Mailing','getsingle',{id:$scope.currentABTest.mailing_id_a});
130 var abmailB= crmApi('Mailing','getsingle',{id:$scope.currentABTest.mailing_id_b});
131 var abmailC = crmApi('Mailing','getsingle',{id:$scope.currentABTest.mailing_id_c});
132 abmailA.success(function (data) {
133 if (data.is_error == 0) {
134 $scope.mailA = data;
135
136 };
137 });
138 abmailB.success(function(data) {
139 if (data.is_error == 0) {
140 $scope.mailB = data;
141
142 };
143 });
144 abmailC.success(function(data) {
145 if (data.is_error == 0) {
146 $scope.mailC = data;
147
148 };
149 });
150 }
151 else{
152 console.log("Prithvila");
153 console.log($scope.currentABTest);
154 $scope.mailA = {};
155 $scope.mailB = {};
156 $scope.mailC = {};
157 }
158
159 $scope.sendtest=false;
160 if(typeof $scope.mailA == 'undefined')$scope.mailA={};
161 if(typeof $scope.mailB == 'undefined')$scope.mailB={};
162 if(typeof $scope.mailB == 'undefined')$scope.mailC={};
163
164 $scope.templates =
165 [
166 { name: 'Subject Lines', url: partialUrl('subject_lines.html'),val: 1},
167 { name: 'From Name', url: partialUrl('from_name.html'),val:2},
168 {name: 'Two different Emails', url: partialUrl('two_emails.html'),val:3}
169 ];
170 if($scope.currentABTest.just_created != 1){
171
172 console.log("adit "+$scope.currentABTest.testing_criteria_id);
173
174 $scope.template=$scope.templates[$scope.currentABTest.testing_criteria_id-1];
175 console.log($scope.template.val);
176 }
177 else{
178 console.log("adi");
179 $scope.template = $scope.templates[0];
180 }
181
182 mltokens = CRM.crmMailing.mailTokens;
183 $scope.deliberatelyTrustDangerousSnippeta = function() {
184 return $sce.trustAsHtml($scope.previewa);
185 };
186
187 $scope.deliberatelyTrustDangerousSnippetb = function() {
188 return $sce.trustAsHtml($scope.previewb);
189 };
190
191 $scope.tab_val = 0;
192 $scope.max_tab = 0;
193 $scope.campaign_clicked = function () {
194 if ($scope.max_tab >= 0) {
195 $scope.tab_val = 0;
196 }
197 };
198
199 $scope.winner_criteria="";
200 $scope.compose_clicked = function () {
201 if ($scope.max_tab >= 1) {
202 $scope.tab_val = 1;
203 }
204 };
205 $scope.rec_clicked = function () {
206 if ($scope.max_tab >= 2) {
207 $scope.tab_val = 2;
208 }
209 };
210 $scope.preview_clicked = function () {
211 if ($scope.max_tab >= 3) {
212 $scope.tab_val = 3;
213 }
214 };
215 $scope.preview = false;
216 $scope.slide_value = 0;
217
218 $scope.setifyes = function (val) {
219 if (val == 1) {
220 $scope.ifyes = true;
221 }
222 else {
223 $scope.ifyes = false;
224 }
225 };
226
227 $scope.reply = function(){
228 if($scope.trackreplies==0){
229 $scope.trackreplies=1;
230 }
231 else{
232 $scope.trackreplies=0;
233 $scope.currentMailing.forward_replies=0;
234 $scope.currentMailing.auto_responder=0;
235 }
236 }
237
238 $scope.isAuto= function(au){
239 return au.component_type == "Reply";
240 };
241
242 $scope.trackr= function(trackreplies){
243 if(trackreplies=="1"){
244 return true;
245 }
246 else
247 return false;
248 }
249
250 $scope.sen = function(){
251 $scope.sendtest=true;
252 }
253
254 $scope.isHeader= function(hf){
255 return hf.component_type == "Header";
256 };
257 //filter so we only get footers from mailing component
258 $scope.isFooter= function(f){
259 return f.component_type == "Footer";
260 };
261
262 $scope.send_date = "01/01/2000";
263
264 $scope.dt = "";
265
266
267 $scope.savea = function (dat) {
268
269 var result = crmApi('Mailing', 'create', dat, true);
270 console.log(result);
271 result.success(function(data) {
272 if (data.is_error == 0) {
273 $scope.mailA.id = data.id;
274 $scope.currentABTest.mailing_id_a=$scope.mailA.id;
275 console.log("Mail a Id "+ $scope.mailA.id);
276 }
277 });
278 };
279
280 $scope.append_mails = function(){
281 crmApi('MailingAB','create',{id:$scope.abId,mailing_id_a:$scope.mailA.id,mailing_id_b:$scope.mailB.id,mailing_id_c:$scope.mailC.id});
282 $scope.currentABTest.id= $scope.abId;
283 };
284
285 $scope.saveb = function (dat) {
286 var flag =0;
287 var result = crmApi('Mailing', 'create', dat, true);
288 console.log(result);
289 result.success(function(data) {
290 if (data.is_error == 0) {
291 $scope.mailB.id = data.id;
292 console.log("Mail b Id "+ $scope.mailB.id);
293 $scope.currentABTest.mailing_id_b=$scope.mailB.id;
294
295 $scope.append_mails();
296
297
298 }
299 });
300
301 };
302
303 $scope.savec = function (dat) {
304 var flag =0;
305 console.log("C is called");
306 var result = crmApi('Mailing', 'create', dat, true);
307 console.log(result);
308 result.success(function(data) {
309 if (data.is_error == 0) {
310 $scope.mailC.id = data.id;
311 console.log("Mail C Id "+ $scope.mailC.id);
312 $scope.currentABTest.mailing_id_c=$scope.mailC.id;
313
314 $scope.append_mails();
315
316
317 }
318 });
319
320 };
321 $scope.previewa="";
322 $scope.pre= function(){
323 $scope.preview=true;
324
325 }
326 $scope.init = function (par) {
327
328 $scope.whatnext = par.toString()
329 };
330
331 $scope.setdate = function (par) {
332 console.log("called")
333 console.log("av " + par)
334 $scope.send_date = par;
335 $scope.dt = par;
336 $scope.apply();
337 };
338
339 $scope.testmailid ="";
340 $scope.incGroup = [];
341 $scope.excGroup = [];
342 $scope.incGroupids =[];
343 $scope.excGroupids =[];
344 $scope.tp1 = {};
345 $scope.create_abtest = function(){
346 var result;
347 $scope.currentABTest.testing_criteria_id=$scope.template.val;
348
349 if($scope.abId =="" )
350 result= crmApi('MailingAB','create',{name: $scope.currentABTest.name,testing_criteria_id: $scope.template.val});
351 else{
352 if (typeof $scope.currentABTest.mailing_id_a == 'undefined')
353 result= crmApi('MailingAB','create',{id:$scope.abId,testing_criteria_id: $scope.template.val});
354 else{
355 result= crmApi('MailingAB','create',{id:$scope.abId,testing_criteria_id: $scope.template.val,mailing_id_a:$scope.currentABTest.mailing_id_a,mailing_id_b:$scope.currentABTest.mailing_id_b} );
356
357 }
358
359 }
360
361
362 result.success(function(data) {
363 if (data.is_error == 0) {
364 $scope.abId = data.id;
365 console.log("ID "+$scope.abId);
366 }
367 });
368 };
369
370
371
372 $scope.a_b_update = function(){
373
374 $scope.tp1.include =$scope.incGroupids;
375 $scope.tp1.exclude=$scope.excGroupids;
376
377 console.log($scope.tp1);
378 console.log($scope.currentABTest.id);
379
380 console.log("--------");
381 var result= crmApi('Mailing','a_b_recipients_update',{
382 id:$scope.currentABTest.id,
383 groups: $scope.tp1
384 });
385
386 var resulta =crmApi('Mailing','preview',{id:$scope.currentABTest.mailing_id_a});
387
388 resulta.success(function(data) {
389 if (data.is_error == 0) {
390 console.log("came");
391
392 $scope.previewa=data.values.html;
393
394
395
396 }
397 });
398
399 resulta =crmApi('Mailing','preview',{id:$scope.currentABTest.mailing_id_b});
400
401 resulta.success(function(data) {
402 if (data.is_error == 0) {
403 console.log("came");
404 console.log(data.values.html);
405 $scope.previewb=data.values.html;
406
407
408 }
409 });
410 }
411 $scope.update_abtest = function(){
412
413 $scope.currentABTest.declare_winning_time= $scope.currentABTest.date + " " + $scope.currentABTest.time ;
414
415 result= crmApi('MailingAB','create',{id:$scope.abId,
416 testing_criteria_id: $scope.template.val,
417 mailing_id_a:$scope.currentABTest.mailing_id_a,
418 mailing_id_b:$scope.currentABTest.mailing_id_b,
419 mailing_id_c:$scope.currentABTest.mailing_id_c,
420 winner_criteria_id : $scope.currentABTest.winner_criteria_id,
421 group_percentage: $scope.currentABTest.group_percentage,
422 declare_winning_time: $scope.currentABTest.declare_winning_time
423 } );
424
425
426
427 };
428
429
430
431
432
433
434
435 $scope.tmp = function (tst,aorb){
436 if(aorb==1){
437 $scope.mailA.msg_template_id=tst;
438 console.log($scope.mailA.msg_template_id+ "sasas");
439 if($scope.mailA.msg_template_id == null){
440 $scope.mailA.body_html="";
441 }
442 else{
443 for(var a in $scope.tmpList){
444
445 if($scope.tmpList[a].id==$scope.mailA.msg_template_id){
446 $scope.mailA.body_html=$scope.tmpList[a].msg_html;
447 }
448 }
449 }
450 }
451 else if(aorb==2){
452
453 $scope.mailB.msg_template_id=tst;
454 console.log($scope.mailB.msg_template_id+ "sasas");
455 if($scope.mailB.msg_template_id == null){
456 $scope.mailB.body_html="";
457 }
458 else{
459 for(var a in $scope.tmpList){
460
461 if($scope.tmpList[a].id==$scope.mailB.msg_template_id){
462 $scope.mailB.body_html=$scope.tmpList[a].msg_html;
463 }
464 }
465 }
466
467 }
468 else {
469
470 $scope.mailA.msg_template_id=tst;
471 console.log($scope.mailA.msg_template_id+ "sasas");
472 if($scope.mailA.msg_template_id == null){
473 $scope.mailA.body_html="";
474 }
475 else{
476 for(var a in $scope.tmpList){
477
478 if($scope.tmpList[a].id==$scope.mailA.msg_template_id){
479 $scope.mailA.body_html=$scope.tmpList[a].msg_html;
480 }
481 }
482 }
483
484 $scope.mailB.msg_template_id=tst;
485 console.log($scope.mailB.msg_template_id+ "sasas");
486 if($scope.mailB.msg_template_id == null){
487 $scope.mailB.body_html="";
488 }
489 else{
490 for(var a in $scope.tmpList){
491
492 if($scope.tmpList[a].id==$scope.mailB.msg_template_id){
493 $scope.mailB.body_html=$scope.tmpList[a].msg_html;
494 }
495 }
496 }
497
498
499 }
500 };
501
502
503 });
504
505 crmMailingAB.directive('nexttab', function () {
506 return {
507 // Restrict it to be an attribute in this case
508 restrict: 'A',
509
510 priority: 500,
511 // responsible for registering DOM listeners as well as updating the DOM
512 link: function (scope, element, attrs) {
513
514 $(element).parent().parent().parent().parent().tabs(scope.$eval(attrs.nexttab));
515 var myarr = new Array(1, 2, 3)
516 $(element).parent().parent().parent().parent().tabs({disabled: myarr});
517
518 $(element).on("click", function () {
519 if(scope.tab_val==0){
520 scope.create_abtest();
521 }
522 else if(scope.tab_val == 2){
523 scope.update_abtest();
524 if(scope.currentABTest.winner_criteria_id==1){
525 scope.winner_criteria="Open";
526 }
527 else if(scope.currentABTest.winner_criteria_id==2){
528 scope.winner_criteria=" Total Unique Clicks";
529 }
530 else if(scope.currentABTest.winner_criteria_id==3){
531 scope.winner_criteria="Total Clicks on a particular link";
532 }
533
534
535 scope.a_b_update();
536
537 }
538
539 scope.tab_val = scope.tab_val + 1;
540
541 scope.max_tab = Math.max(scope.tab_val, scope.max_tab);
542 var myArray1 = new Array();
543 for (var i = scope.max_tab + 1; i < 4; i++) {
544 myArray1.push(i);
545 }
546 $(element).parent().parent().parent().parent().parent().tabs("option", "disabled", myArray1);
547 $(element).parent().parent().parent().parent().parent().tabs("option", "active", scope.tab_val);
548 scope.$apply();
549 console.log("Adir");
550 });
551 }
552 };
553 });
554
555 crmMailingAB.directive('prevtab', function () {
556 return {
557 // Restrict it to be an attribute in this case
558 restrict: 'A',
559 priority: 500,
560 // responsible for registering DOM listeners as well as updating the DOM
561 link: function (scope, element, attrs) {
562
563
564 $(element).on("click", function () {
565 var temp = scope.tab_val - 1;
566 scope.tab_val = scope.tab_val - 1;
567
568 console.log(temp);
569 if (temp == 3) {
570
571 }
572 else {
573 $(element).parent().parent().parent().parent().parent().tabs("option", "active", temp);
574 }
575
576 scope.$apply();
577
578 });
579 }
580 };
581 });
582
583 crmMailingAB.directive('chsgroup', function () {
584 return {
585 restrict: 'AE',
586 link: function (scope, element, attrs) {
587 function format(item) {
588 if (!item.id) {
589 // return `text` for optgroup
590 return item.text;
591 }
592 // return item template
593 var a = item.id.split(" ");
594 if (a[1] == "group" && a[2] == "include") {
595 return "<img src='../../sites/all/modules/civicrm/i/include.jpeg' height=12 width=12/>" + " " + "<img src='../../sites/all/modules/civicrm/i/group.png' height=12 width=12/>" + item.text;
596 }
597 if (a[1] == "group" && a[2] == "exclude") {
598 return "<img src='../../sites/all/modules/civicrm/i/Error.gif' height=12 width=12/>" + " " + "<img src='../../sites/all/modules/civicrm/i/group.png' height=12 width=12/>" + item.text;
599 }
600 if (a[1] == "mail" && a[2] == "include") {
601 return "<img src='../../sites/all/modules/civicrm/i/include.jpeg' height=12 width=12/>" + " " + "<img src='../../sites/all/modules/civicrm/i/EnvelopeIn.gif' height=12 width=12/>" + item.text;
602 }
603 if (a[1] == "mail" && a[2] == "exclude") {
604 return "<img src='../../sites/all/modules/civicrm/i/Error.gif' height=12 width=12/>" + " " + "<img src='../../sites/all/modules/civicrm/i/EnvelopeIn.gif' height=12 width=12/>" + item.text;
605 }
606 }
607
608 $(element).select2({
609 width: "400px",
610 placeholder: "Select the groups you wish to include",
611 formatResult: format,
612 formatSelection: format,
613 escapeMarkup: function (m) {
614 return m;
615 }
616 });
617
618 $(element).on('select2-selecting', function (e) {
619 var a = e.val.split(" ");
620 var l = a.length;
621 if (a[2] == "include") {
622 var str = "";
623 for (i = 3; i < l; i++) {
624 str += a[i];
625 str += " ";
626 }
627 scope.incGroup.push(str);
628 scope.incGroupids.push(a[0]);
629 scope.$apply();
630 }
631
632 else {
633 var str = "";
634 for (i = 3; i < l; i++) {
635 str += a[i];
636 str += " ";
637 }
638
639 scope.excGroup.push(str);
640 scope.excGroupids.push(a[0]);
641 scope.$apply();
642 }
643
644 });
645 $(element).on("select2-removed", function (e) {
646 if (e.val.split(" ")[2] == "exclude") {
647 var index = scope.excGroup.indexOf(e.val.split(" ")[3]);
648 scope.excGroup.splice(index, 1);
649 scope.incGroup.splice(index,1);
650 scope.$apply();
651 }
652 else {
653 var index = scope.incGroup.indexOf(e.val.split(" ")[3]);
654 scope.incGroup.splice(index, 1);
655 scope.incGroupids.splice(index,1);
656 scope.$apply();
657 }
658 });
659 }
660 };
661
662 });
663
664 crmMailingAB.directive('groupselect',function(){
665 return {
666 restrict : 'AE',
667 link: function(scope,element, attrs){
668 $(element).select2({width:"200px", data: mltokens, placeholder:"Insert Token"});
669
670
671 $(element).on('select2-selecting', function(e) {
672 scope.$evalAsync('_resetSelection()');console.log(mltokens);
673 /* if(scope.currentMailing.body_html == null){
674 scope.currentMailing.body_html = e.val;
675 }
676 else
677 scope.currentMailing.body_html = scope.currentMailing.body_html + e.val;
678 */
679 var msg = document.getElementById("body_html").value;
680 var cursorlen = document.getElementById("body_html").selectionStart;
681 console.log(cursorlen);
682 var textlen = msg.length;
683 document.getElementById("body_html").value = msg.substring(0, cursorlen) + e.val + msg.substring(cursorlen, textlen);
684 scope.currentMailing.body_html = msg.substring(0, cursorlen) + e.val + msg.substring(cursorlen, textlen);
685 console.log(document.getElementById("body_html").value);
686 console.log(scope.currentMailing.body_html);
687 var cursorPos = (cursorlen + e.val.length);
688 document.getElementById("body_html").selectionStart = cursorPos;
689 document.getElementById("body_html").selectionEnd = cursorPos;
690 document.getElementById("body_html").focus();
691 scope.$apply();
692 e.preventDefault();
693 })
694
695 }
696 };
697
698 });
699
700 crmMailingAB.directive('sliderbar', function () {
701 return{
702 restrict: 'AE',
703 link: function (scope, element, attrs) {
704 if(typeof scope.currentABTest.group_percentage != 'undefined'){
705 console.log("Yay");
706 $(element).slider({value:scope.currentABTest.group_percentage});
707 }
708 $(element).slider({min: 1});
709 $(element).slider({
710 slide: function (event, ui) {
711 scope.currentABTest.group_percentage = ui.value;
712 scope.$apply();
713 }
714 });
715 }
716 };
717 });
718
719 crmMailingAB.directive('tpmax', function () {
720 return {
721 restrict: 'E',
722 link: function (scope, element, attr) {
723 scope.$watch('automated', function (val) {
724 if (val == "Yes") {
725 $(element).dialog({
726 title: 'Automated A/B Testing',
727 width: 800,
728 height: 600,
729 closed: false,
730 cache: false,
731 modal: true
732 });
733 }
734 });
735
736 $(element).find("#closebutton").on("click", function () {
737 $(element).dialog("close");
738 });
739 }
740 };
741 });
742
743 crmMailingAB.directive('previewmail', function () {
744 return {
745 // Restrict it to be an attribute in this case
746 restrict: 'AE',
747
748 priority: 500,
749 // responsible for registering DOM listeners as well as updating the DOM
750 link: function (scope, element, attrs) {
751
752 scope.$watch('preview', function () {
753 if(scope.preview == true){
754
755 $(element).dialog({
756 title: 'Preview Mailing',
757 width: 1080,
758 height: 800,
759 closed: false,
760 cache: false,
761 modal: true,
762
763 close :function(){console.log("close");
764 scope.preview = false;scope.$apply()}
765
766
767 });}
768
769 });
770
771
772 $(element).find("#closebutton").on("click", function () {
773 console.log("close");
774 scope.preview = false;
775 $(element).dialog("close");
776 });
777 }
778 };
779 });
780
781 crmMailingAB.directive('sendmailtest', function () {
782 return {
783 // Restrict it to be an attribute in this case
784 restrict: 'AE',
785
786 link: function (scope, element, attrs) {
787
788 scope.$watch('sendtest', function () {
789 if(scope.sendtest == true){
790
791 $(element).dialog({
792 title: 'Send Test Mails',
793 width: 1080,
794 height: 800,
795 closed: false,
796 cache: false,
797 modal: true,
798 button:{
799 'Send': function() {
800 /* Do stuff */
801 $(this).dialog('close');
802 }
803 },
804 close :function(){console.log("close");
805 scope.sendtest = false;scope.$apply()}
806
807
808 });
809 }
810
811 });
812
813
814 $(element).find("#closebutton").on("click", function () {
815 console.log("close");
816 scope.preview = false;
817 $(element).dialog("close");
818 });
819 }
820 };
821 });
822
823 crmMailingAB.directive('numbar', function () {
824 return{
825 restrict: 'AE',
826 link: function (scope, element, attrs) {
827 $(element).spinner({max: attrs.numbar, min: 0});
828 }
829 };
830 });
831
832 crmMailingAB.directive('datepick', function () {
833 return {
834
835
836 restrict: 'AE',
837 link: function (scope, element, attrs) {
838 $(element).datepicker({
839 dateFormat: "yy-mm-dd",
840 onSelect: function (date) {
841 $(".ui-datepicker a").removeAttr("href");
842
843 scope.scheddate.date = date.toString();
844 scope.$apply();
845 console.log(scope.scheddate.date);
846
847 }
848
849
850 });
851 }
852 };
853 });
854
855 crmMailingAB.directive('submitform', function () {
856 return {
857 restrict: 'A',
858 priority: 1000,
859 link: function (scope, element, attrs) {
860 $(element).on("click", function () {
861
862 console.log("clicked");
863 scope.savea({
864 id: scope.mailA.id,
865 name: "Aditya Nambiar",
866 visibility: scope.mailA.visibility,
867 created_id: 1,
868 subject: scope.mailA.subject,
869 msg_template_id: scope.mailA.msg_template_id==null ? "" : scope.mailA.msg_template_id,
870 open_tracking: scope.mailA.open_tracking,
871 url_tracking: scope.mailA.url_tracking,
872 forward_replies: scope.mailA.forward_replies,
873 auto_responder: scope.mailA.auto_responder,
874 from_name: scope.mailA.from_name,
875 from_email: scope.mailA.from_email,
876 replyto_email: scope.mailA.replyto_email,
877 unsubscribe_id: scope.mailA.unsubscribe_id,
878 resubscribe_id: scope.mailA.resubscribe_id,
879 body_html: scope.mailA.body_html,
880 body_text: scope.mailA.body_text,
881 scheduled_date: scope.mailA.scheduled_date,
882 scheduled_id: scope.mailA.scheduled_id,
883 campaign_id: scope.mailA.campaign_id==null ? "" : scope.mailA.campaign_id,
884 header_id: scope.mailA.header_id,
885 footer_id: scope.mailA.footer_id,
886
887 is_completed: scope.mailA.is_completed
888 });
889
890 console.log("Truth " + scope.whatnext)
891
892 if (scope.whatnext == "3") {
893 console.log("sdf");
894
895 scope.mailB.name= scope.mailA.name;
896 scope.mailB.visibility= scope.mailA.visibility;
897 scope.mailB.created_id= scope.mailA.created_id;
898 scope.mailB.subject= scope.mailA.subject;
899 scope.mailB.msg_template_id= scope.mailA.msg_template_id==null ? "" : scope.mailA.msg_template_id;
900 scope.mailB.open_tracking= scope.mailA.open_tracking;
901 scope.mailB.url_tracking= scope.mailA.url_tracking;
902 scope.mailB.forward_replies= scope.mailA.forward_replies;
903 scope.mailB.auto_responder= scope.mailA.auto_responder;
904 scope.mailB.from_name= scope.mailA.from_name;
905 scope.mailB.replyto_email= scope.mailA.replyto_email;
906 scope.mailB.unsubscribe_id= scope.mailA.unsubscribe_id;
907 scope.mailB.resubscribe_id= scope.mailA.resubscribe_id;
908 scope.mailB.body_html= scope.mailA.body_html;
909 scope.mailB.body_text= scope.mailA.body_text;
910 scope.mailB.scheduled_date= scope.mailA.scheduled_date;
911 scope.mailB.scheduled_id= scope.mailA.scheduled_id;
912 scope.mailB.campaign_id= scope.mailA.campaign_id==null ? "" : scope.mailA.campaign_id;
913 scope.mailB.header_id= scope.mailA.header_id;
914 scope.mailB.footer_id= scope.mailA.footer_id;
915
916 scope.mailB.is_completed= scope.mailA.is_completed;
917
918 }
919 else {
920 if (scope.whatnext == "2") {
921 scope.mailB.fromEmail = scope.mailA.fromEmail;
922 scope.mailB.name= scope.mailA.name;
923 scope.mailB.visibility= scope.mailA.visibility;
924 scope.mailB.created_id= scope.mailA.created_id;
925 scope.mailB.msg_template_id= scope.mailA.msg_template_id==null ? "" : scope.mailA.msg_template_id;
926 scope.mailB.open_tracking= scope.mailA.open_tracking;
927 scope.mailB.url_tracking= scope.mailA.url_tracking;
928 scope.mailB.forward_replies= scope.mailA.forward_replies;
929 scope.mailB.auto_responder= scope.mailA.auto_responder;
930 scope.mailB.from_name= scope.mailA.from_name;
931 scope.mailB.replyto_email= scope.mailA.replyto_email;
932 scope.mailB.unsubscribe_id= scope.mailA.unsubscribe_id;
933 scope.mailB.resubscribe_id= scope.mailA.resubscribe_id;
934 scope.mailB.body_html= scope.mailA.body_html;
935 scope.mailB.body_text= scope.mailA.body_text;
936 scope.mailB.scheduled_date= scope.mailA.scheduled_date;
937 scope.mailB.scheduled_id= scope.mailA.scheduled_id;
938 scope.mailB.campaign_id= scope.mailA.campaign_id==null ? "" : scope.mailA.campaign_id;
939 scope.mailB.header_id= scope.mailA.header_id;
940 scope.mailB.footer_id= scope.mailA.footer_id;
941
942 scope.mailB.is_completed= scope.mailA.is_completed;
943
944 }
945 }
946 console.log("call B");
947 scope.saveb({
948
949 id: scope.mailB.id,
950 name: "Aditya Nambiar",
951 visibility: scope.mailB.visibility,
952 created_id: 1,
953 subject: scope.mailB.subject,
954 msg_template_id: scope.mailB.msg_template_id==null ? "" : scope.mailB.msg_template_id,
955 open_tracking: scope.mailB.open_tracking,
956 url_tracking: scope.mailB.url_tracking,
957 forward_replies: scope.mailB.forward_replies,
958 auto_responder: scope.mailB.auto_responder,
959 from_name: scope.mailB.from_name,
960 from_email: scope.mailB.from_email,
961 replyto_email: scope.mailB.replyto_email,
962 unsubscribe_id: scope.mailB.unsubscribe_id,
963 resubscribe_id: scope.mailB.resubscribe_id,
964 body_html: scope.mailB.body_html,
965 body_text: scope.mailB.body_text,
966 scheduled_date: scope.mailB.scheduled_date,
967 scheduled_id: scope.mailB.scheduled_id,
968 campaign_id: scope.mailB.campaign_id==null ? "" : scope.mailB.campaign_id,
969 header_id: scope.mailB.header_id,
970 footer_id: scope.mailB.footer_id,
971
972 is_completed: scope.mailA.is_completed
973
974 });
975 console.log("call C");
976 scope.savec({
977 id: scope.mailC.id,
978 name: "Aditya Nambiar",
979 visibility: scope.mailB.visibility,
980 created_id: 1,
981 subject: scope.mailB.subject,
982 msg_template_id: scope.mailB.msg_template_id==null ? "" : scope.mailB.msg_template_id,
983 open_tracking: scope.mailB.open_tracking,
984 url_tracking: scope.mailB.url_tracking,
985 forward_replies: scope.mailB.forward_replies,
986 auto_responder: scope.mailB.auto_responder,
987 from_name: scope.mailB.from_name,
988 from_email: scope.mailB.from_email,
989 replyto_email: scope.mailB.replyto_email,
990 unsubscribe_id: scope.mailB.unsubscribe_id,
991 resubscribe_id: scope.mailB.resubscribe_id,
992 body_html: scope.mailB.body_html,
993 body_text: scope.mailB.body_text,
994 campaign_id: scope.mailB.campaign_id==null ? "" : scope.mailB.campaign_id,
995 header_id: scope.mailB.header_id,
996 footer_id: scope.mailB.footer_id,
997
998 is_completed: scope.mailA.is_completed
999 });
1000
1001
1002 });
1003 }
1004 };
1005
1006 });
1007
1008
1009
1010 crmMailingAB.directive('nextbutton', function () {
1011 return {
1012 restrict: 'AE',
1013 replace: 'true',
1014 template: '<div class="crm-submit-buttons" id="campaignbutton">' +
1015 '<div class = "crm-button crm-button-type-upload crm-button_qf_Contact_upload_view" >' +
1016 '<input type="submit" value="Next" id="campaignbutton _qf_Contact_upload_view-top" class="btn btn-primary" nexttab={{tab_val}}>' +
1017 '</div></div>'
1018
1019 };
1020 });
1021
1022 crmMailingAB.directive('cancelbutton', function () {
1023 return {
1024 restrict: 'AE',
1025 replace: 'true',
1026 template: '<div class="crm-submit-buttons" id="campaignbutton">' +
1027 '<div class = "crm-button crm-button-type-upload crm-button_qf_Contact_upload_view" >' +
1028 '<input type="submit" value="Cancel" id="campaignbutton _qf_Contact_upload_view-top" class="btn btn-primary" >' +
1029 '</div></div>'
1030
1031 };
1032 });
1033
1034 crmMailingAB.directive('chsdate',function(){
1035 return {
1036 restrict: 'AE',
1037 link: function(scope,element,attrs){
1038 $(element).datepicker({
1039 dateFormat: "yy-mm-dd",
1040 onSelect: function(date) {
1041 $(".ui-datepicker a").removeAttr("href");
1042 scope.currentABTest.date=date.toString();
1043 scope.$apply();
1044 console.log(scope.currentABTest.date);
1045 }
1046 });
1047 }
1048 };
1049 });
1050
1051 crmMailingAB.directive('prevbutton', function () {
1052 return {
1053 restrict: 'AE',
1054 replace: 'true',
1055 template: '<div class="crm-submit-buttons" >' +
1056 '<div class = "crm-button crm-button-type-upload crm-button_qf_Contact_upload_view" >' +
1057 '<input type="submit" value="Previous" id="campaignbutton _qf_Contact_upload_view-top" class="btn btn-primary" prevtab={{tab_val}}>' +
1058 '</div></div>'
1059
1060 };
1061 });
1062
1063 crmMailingAB.directive('linegraph',function(){
1064 return {
1065 restrict: 'AE',
1066 link: function(scope,element,attrs){
1067 console.log("try");
1068 var data= scope.graph_data;
1069
1070 // see wht it looks like in the console
1071 console.log("Data");
1072 console.log(data);
1073
1074 // set up a colour variable
1075 var color = d3.scale.category10();
1076
1077 // map one colour each to x, y and z
1078 // keys grabs the key value or heading of each key value pair in the json
1079 // but not time
1080 console.log("Key");
1081
1082 console.log(d3.keys(data[0]));
1083 color.domain(d3.keys(data[0]).filter(function(key) {
1084 return key !== "time";
1085 }));
1086
1087 // create a nested series for passing to the line generator
1088 // it's best understood by console logging the data
1089 var series = color.domain().map(function(name) {
1090 console.log(name);
1091
1092 return {
1093 name: name,
1094 values: data.map(function(d) {
1095 console.log("------");
1096 console.log(d);
1097 return {
1098 time: d.time,
1099 score: +d[name]
1100
1101 };
1102 })
1103 };
1104 });
1105 console.log("Series");
1106
1107 console.log(series);
1108
1109 // Set the dimensions of the canvas / graph
1110 var margin = {
1111 top: 30,
1112 right: 20,
1113 bottom: 40,
1114 left: 75
1115 },
1116 width = 375 - margin.left - margin.right,
1117 height = 250 - margin.top - margin.bottom;
1118
1119 // Set the ranges
1120 //var x = d3.time.scale().range([0, width]).domain([0,10]);
1121 var x = d3.scale.linear().range([0,width]);
1122 var y = d3.scale.linear().range([height, 0]);
1123
1124 // Define the axes
1125 var xAxis = d3.svg.axis().scale(x)
1126 .orient("bottom").ticks(10);
1127
1128 var yAxis = d3.svg.axis().scale(y)
1129 .orient("left").ticks(5);
1130
1131 // Define the line
1132 // Note you plot the time / score pair from each key you created ealier
1133 var valueline = d3.svg.line()
1134 .x(function(d) {
1135 return x(d.time);
1136 })
1137 .y(function(d) {
1138 return y(d.score);
1139 });
1140
1141
1142 // Adds the svg canvas
1143 var svg = d3.select(element[0])
1144 .append("svg")
1145 .attr("width", width + margin.left + margin.right)
1146 .attr("height", height + margin.top + margin.bottom)
1147 .append("g")
1148 .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
1149
1150 // Scale the range of the data
1151 x.domain(d3.extent(data, function(d) {
1152 return d.time;
1153 }));
1154
1155 // note the nested nature of this you need to dig an additional level
1156 y.domain([
1157 d3.min(series, function(c) {
1158 return d3.min(c.values, function(v) {
1159 return v.score;
1160 });
1161 }),
1162 d3.max(series, function(c) {
1163 return d3.max(c.values, function(v) {
1164 return v.score;
1165 });
1166 })
1167 ]);
1168 svg.append("text") // text label for the x axis
1169 .attr("x", width / 2 )
1170 .attr("y", height + margin.bottom )
1171 .style("text-anchor", "middle")
1172 .text("Time");
1173
1174 svg.append("text") // text label for the x axis
1175
1176 .style("text-anchor", "middle")
1177 .text(scope.winner_criteria).attr("transform", function(d) {
1178 return "rotate(-90)"
1179 }).attr("x", -height/2 )
1180 .attr("y", -30 );
1181 ;
1182 // create a variable called series and bind the date
1183 // for each series append a g element and class it as series for css styling
1184 var series = svg.selectAll(".series")
1185 .data(series)
1186 .enter().append("g")
1187 .attr("class", "series");
1188
1189 // create the path for each series in the variable series i.e. x, y and z
1190 // pass each object called x, y nad z to the lne generator
1191 series.append("path")
1192 .attr("class", "line")
1193 .attr("d", function(d) {
1194 console.log(d); // to see how d3 iterates through series
1195 return valueline(d.values);
1196 })
1197 .style("stroke", function(d) {
1198 return color(d.name);
1199 });
1200
1201 // Add the X Axis
1202 svg.append("g") // Add the X Axis
1203 .attr("class", "x axis")
1204 .attr("transform", "translate(0," + height + ")")
1205 .call(xAxis);
1206
1207 // Add the Y Axis
1208 svg.append("g") // Add the Y Axis
1209 .attr("class", "y axis")
1210 .call(yAxis);
1211
1212
1213 }
1214
1215 }
1216
1217 });
1218
1219 })(angular, CRM.$, CRM._);
1220