Content changes in compose tab
[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 //console.log(CRM.resourceUrls['civicrm']);
10 return CRM.resourceUrls['civicrm'] + '/partials/abtesting/' + relPath;
11 //return '/drupal-7.28/sites/all/modules/civicrm/partials/abtesting/' + relPath;
12
13
14 };
15
16 var crmMailingAB = angular.module('crmMailingAB', ['ngRoute', 'ui.utils']);
17
18 crmMailingAB.run(function($rootScope, $templateCache) {
19 $rootScope.$on('$viewContentLoaded', function() {
20 $templateCache.removeAll();
21 });
22 });
23
24 crmMailingAB.config(['$routeProvider',
25 function($routeProvider) {
26 $routeProvider.when('/mailing', {
27 template: '<h1>sdfs</h1>',
28 controller: 'mailingListCtrl',
29 resolve: {
30 mailingList: function($route, crmApi) {
31 return crmApi('Mailing', 'get', {});
32 }
33 }
34 });
35
36 $routeProvider.when('/mailing/abtesting', {
37 templateUrl: partialUrl('main.html'),
38 controller: 'TabsDemoCtrl',
39 resolve: {
40
41 mailingList: function($route, crmApi) {
42 return crmApi('Mailing', 'get', {});
43 }
44 }
45 });
46 }
47 ]);
48 //-----------------------------------------
49 // Add a new record by name.
50 // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" />
51
52 crmMailingAB.controller('TabsDemoCtrl', function($scope, crmApi) {
53
54 $scope.groups= CRM.crmMailing.groupNames;
55 $scope.mailList = CRM.crmMailing.civiMails;
56
57 $scope.tab_val=0;
58 $scope.max_tab=0;
59 $scope.campaign_clicked= function(){
60 if($scope.max_tab >= 0 ){
61 $scope.tab_val =0;
62 }
63 };
64 $scope.compose_clicked=function(){
65 if($scope.max_tab >=1){
66 $scope.tab_val =1;
67 }
68 };
69 $scope.rec_clicked=function(){
70 if($scope.max_tab >=2){
71 $scope.tab_val =2;
72 }
73 };
74 $scope.preview_clicked=function(){
75 if($scope.max_tab>=3){
76 $scope.tab_val=3;
77 }
78 };
79 $scope.templates =
80 [ { name: 'Subject Lines', url: partialUrl('subject_lines.html')},
81 { name: 'From Name', url: partialUrl('from_name.html')},
82 {name:'Two different Emails',url: partialUrl('two_emails.html')} ];
83 $scope.template = $scope.templates[0];
84
85 $scope.slide_value = 0;
86
87 $scope.setifyes= function(val){
88 if(val ==1) {
89 $scope.ifyes = true;
90 }
91 else
92 $scope.ifyes=false;
93 };
94
95 $scope.send_date ="01/01/2000";
96
97 $scope.dt="";
98
99 $scope.mailA={};
100
101 $scope.mailB={};
102 $scope.save=function(dat){
103
104 var result= crmApi('Mailing', 'create',dat, true);
105 console.log("Ac "+result);
106 };
107
108 $scope.init=function(par){
109
110 $scope.whatnext=par.toString()
111 };
112
113 $scope.setdate= function(par){
114 console.log("called")
115 console.log("av "+par)
116 $scope.send_date =par;
117 $scope.dt=par;
118 $scope.apply();
119 };
120
121 $scope.scheddate={};
122 $scope.scheddate.date = "6";
123 $scope.scheddate.time = "";
124 $scope.incGroup =[];
125 $scope.excGroup=[];
126
127
128 });
129
130 crmMailingAB.directive('nexttab', function() {
131 return {
132 // Restrict it to be an attribute in this case
133 restrict: 'A',
134
135 priority: 500,
136 // responsible for registering DOM listeners as well as updating the DOM
137 link: function(scope, element, attrs) {
138
139 $(element).parent().parent().parent().parent().tabs(scope.$eval(attrs.nexttab));
140 var myarr = new Array(1,2,3)
141 $(element).parent().parent().parent().parent().tabs({disabled:myarr});
142
143 $(element).on("click",function() {
144 scope.tab_val=scope.tab_val +1;
145
146 scope.max_tab= Math.max(scope.tab_val,scope.max_tab);
147 var myArray1 = new Array( );
148 for ( var i = scope.max_tab+1; i < 4; i++ ) {
149 myArray1.push(i);
150 }
151 $(element).parent().parent().parent().parent().parent().tabs( "option", "disabled", myArray1 );
152 $(element).parent().parent().parent().parent().parent().tabs("option", "active", scope.tab_val);
153 scope.$apply();
154 console.log("Adir");
155 });
156 }
157 };
158 });
159
160 crmMailingAB.directive('prevtab', function() {
161 return {
162 // Restrict it to be an attribute in this case
163 restrict: 'A',
164 priority: 500,
165 // responsible for registering DOM listeners as well as updating the DOM
166 link: function(scope, element, attrs) {
167
168
169
170 $(element).on("click",function() {
171 var temp= scope.tab_val -1 ;
172 scope.tab_val=scope.tab_val -1;
173
174 console.log(temp);
175 if(temp==3){
176
177 }
178 else {
179 $(element).parent().parent().parent().parent().parent().tabs("option", "active", temp);
180 }
181
182 scope.$apply();
183
184 });
185 }
186 };
187 });
188
189 crmMailingAB.directive('groupselect',function(){
190 return {
191 restrict : 'AE',
192 link: function(scope,element, attrs){
193 function format(item) {
194 if(!item.id) {
195 // return `text` for optgroup
196 return item.text;
197 }
198 // return item template
199 var a = item.id.split(" ");
200 if(a[1]=="group" && a[2]=="include")
201 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;
202 if(a[1]=="group" && a[2]=="exclude")
203 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;
204 if(a[1]=="mail" && a[2]=="include")
205 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;
206 if(a[1]=="mail" && a[2]=="exclude")
207 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;
208 }
209
210 $(element).select2({
211 width:"400px",
212 placeholder: "Select the groups you wish to include",
213 formatResult: format,
214 formatSelection: format,
215 escapeMarkup: function(m) { return m; }
216 });
217
218 $(element).on('select2-selecting', function(e) {
219 var a = e.val.split(" ");
220 var l = a.length;
221 if(a[2]=="include")
222 { var str="";
223 for(i=3; i< l; i++){
224 str+=a[i];
225 str+=" ";
226 }
227 scope.incGroup.push(str);scope.$apply();}
228
229 else
230 { var str="";
231 for(i=3; i< l; i++){
232 str+=a[i];
233 str+=" ";
234 }
235
236 scope.excGroup.push(str);scope.$apply();}
237
238 });
239 $(element).on("select2-removed", function(e) {
240 if(e.val.split(" ")[2]=="exclude") {
241 var index = scope.excGroup.indexOf(e.val.split(" ")[3]);
242 scope.excGroup.splice(index, 1);
243 scope.$apply();
244 }
245 else{
246 var index = scope.incGroup.indexOf(e.val.split(" ")[3]);
247 scope.incGroup.splice(index, 1);scope.$apply();
248 }
249 });
250 }
251 };
252
253 });
254
255 crmMailingAB.directive('sliderbar',function(){
256 return{
257 restrict: 'AE',
258 link: function(scope,element, attrs){
259 $(element).slider({min:1});
260 $(element).slider({
261 slide: function( event, ui ) {
262 scope.slide_value = ui.value;
263 scope.$apply();
264 }
265 });
266 }
267 };
268 });
269
270 crmMailingAB.directive('tpmax',function(){
271 return {
272 restrict: 'E',
273 link: function(scope,element,attr){
274 scope.$watch('automated', function(val) {
275 if(val=="Yes") {
276 $(element).dialog({
277 title: 'Automated A/B Testing',
278 width: 800,
279 height: 600,
280 closed: false,
281 cache: false,
282 modal: true
283 });
284 }
285 });
286
287 $(element).find("#closebutton").on("click",function(){
288 $(element).dialog("close");
289 });
290 }
291 };
292 });
293
294 crmMailingAB.directive('numbar',function(){
295 return{
296 restrict: 'AE',
297 link: function(scope,element, attrs){
298 $(element).spinner({max:attrs.numbar,min:0});
299 }
300 };
301 });
302
303 crmMailingAB.directive('datepick',function(){
304 return {
305
306
307 restrict: 'AE',
308 link: function(scope,element,attrs){
309 $(element).datepicker({
310 dateFormat: "yy-mm-dd",
311 onSelect: function(date) {
312 $(".ui-datepicker a").removeAttr("href");
313
314 scope.scheddate.date=date.toString();
315 scope.$apply();
316 console.log(scope.scheddate.date);
317
318 }
319
320
321 });
322 }
323 };
324 });
325
326 crmMailingAB.directive('submitform',function(){
327 return {
328 restrict:'A',
329 priority: 1000,
330 link: function(scope,element,attrs){
331 $(element).on("click",function() {
332
333 console.log("clicked");
334 scope.save({
335 "sequential": 1,
336 "name": "Aditya Nambiar",
337 "subject": scope.mailA.subj,
338 "created_id": "2",
339 "from_email": scope.mailA.fromEmail,
340 "body_text": scope.mailA.body
341
342 });
343 console.log("Truth "+ scope.whatnext)
344
345 if(scope.whatnext=="3"){
346 console.log("sdf");
347 scope.mailB.subj=scope.mailA.subj;
348 scope.mailB.body=scope.mailA.body;
349
350 }
351 else if(scope.whatnext=="2"){
352 scope.mailB.fromEmail=scope.mailA.fromEmail;
353 scope.mailB.body=scope.mailA.body;
354
355 }
356
357
358 scope.save({
359 "sequential": 1,
360 "name": "Aditya Nambiar",
361 "subject": scope.mailB.subj,
362 "created_id": "2",
363 "from_email": scope.mailB.fromEmail,
364 "body_text": scope.mailB.body
365
366 });
367
368 });
369 }
370 };
371
372 });
373
374 crmMailingAB.directive('nextbutton',function(){
375 return {
376 restrict: 'AE',
377 replace:'true',
378 template:'<div class="crm-submit-buttons" id="campaignbutton">'+
379 '<div class = "crm-button crm-button-type-upload crm-button_qf_Contact_upload_view" >' +
380 '<input type="submit" value="Next" id="campaignbutton _qf_Contact_upload_view-top" class="btn btn-primary" nexttab={{tab_val}}>'+
381 '</div></div>'
382
383 };
384 });
385
386 crmMailingAB.directive('cancelbutton',function(){
387 return {
388 restrict: 'AE',
389 replace:'true',
390 template:'<div class="crm-submit-buttons" id="campaignbutton">'+
391 '<div class = "crm-button crm-button-type-upload crm-button_qf_Contact_upload_view" >' +
392 '<input type="submit" value="Cancel" id="campaignbutton _qf_Contact_upload_view-top" class="btn btn-primary" >'+
393 '</div></div>'
394
395 };
396 });
397
398 crmMailingAB.directive('prevbutton',function(){
399 return {
400 restrict: 'AE',
401 replace:'true',
402 template:'<div class="crm-submit-buttons" >'+
403 '<div class = "crm-button crm-button-type-upload crm-button_qf_Contact_upload_view" >' +
404 '<input type="submit" value="Previous" id="campaignbutton _qf_Contact_upload_view-top" class="btn btn-primary" prevtab={{tab_val}}>'+
405 '</div></div>'
406
407 };
408 });
409
410
411
412
413 })(angular, CRM.$, CRM._);
414