attachments added
[civicrm-core.git] / js / angular-newMailing.js
1 (function(angular, $, _) {
2
3 var partialUrl = function(relPath) {
4 return CRM.resourceUrls['civicrm'] + '/partials/crmMailingType/' + relPath;
5 };
6
7 var crmMailing = angular.module('crmMailing', ['ngRoute', 'ui.utils']);
8
9
10 //-------------------------------------------------------------------------------------------------------
11 crmMailing.config(['$routeProvider',
12 function($routeProvider) {
13 $routeProvider.when('/mailing', {
14 templateUrl: partialUrl('mailingList.html'),
15 controller: 'mailingListCtrl',
16 resolve: {
17 mailingList: function($route, crmApi) {
18 return crmApi('Mailing', 'get', {});
19 }
20
21 }
22 });
23
24
25
26 $routeProvider.when('/mailing/:id', {
27 templateUrl: partialUrl('main.html'),
28 controller: 'mailingCtrl',
29 resolve: {
30 selectedMail: function($route, crmApi) {
31 if ( $route.current.params.id !== 'new') {
32 return crmApi('Mailing', 'getsingle', {id: $route.current.params.id});
33 }
34 else {
35 return {name: "New Mail", visibility: "Public Pages", url_tracking:"1", forward_replies:"0", auto_responder:"0", open_tracking:"1",
36 };
37 }
38 }
39 }
40 });
41 }
42 ]);
43 //-----------------------------------------
44 // Add a new record by name.
45 // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" />
46
47
48
49
50
51 crmMailing.controller('mailingCtrl', function($scope, crmApi, selectedMail) {
52 $scope.partialUrl = partialUrl;
53 $scope.campaignList = CRM.crmMailing.campNames;
54 $scope.mailNameList = _.pluck(CRM.crmCaseType.civiMails, 'name');
55 $scope.groupNamesList = CRM.crmMailing.groupNames;
56 $scope.incGroup = [];
57 $scope.excGroup = [];
58 $scope.currentMailing = selectedMail;
59 window.ct = $scope.currentMailing;
60 $scope.acttab=0;
61 $scope.composeS="1";
62 $scope.trackreplies="0";
63 ///changing upload on screen
64
65 $scope.upldChange= function(composeS){
66 if(composeS=="1"){
67 return true;
68 }
69 else
70 return false;
71 }
72
73 $scope.trackr= function(trackreplies){
74 if(trackreplies=="1"){
75 return true;
76 }
77 else
78 return false;
79 }
80
81 /// Add a new group to mailing
82 /* $scope.addGroup = function(grp, groupName) {
83 var names = _.pluck(CRM.crmMailing.groupNames, 'name');
84 if (!_.contains(names, groupName)) {
85 grp.push({
86 name: groupName
87 });
88 }
89 }; */
90
91
92
93
94 $scope.save = function() {
95 var result = crmApi('Mailing', 'create', $scope.currentMailing, true);
96 result.success(function(data) {
97 if (data.is_error == 0) {
98 $scope.currentMailing.id = data.id;
99 console.log("OK");
100 }
101 console.log("OK2");
102 });
103 };
104 });
105
106
107
108 crmMailing.directive('nexttab', function() {
109 return {
110
111 restrict: 'A',
112 link: function(scope, element, attrs) {
113
114 $(element).parent().parent().tabs();
115
116 $(element).on("click",function() {
117 scope.acttab=scope.acttab +1;
118 $(element).parent().parent().tabs({active:scope.acttab});
119 console.log("sid");
120 });
121 }
122 };
123 });
124
125 crmMailing.directive('prevtab', function() {
126 return {
127
128 restrict: 'A',
129 link: function(scope, element, attrs) {
130
131 $(element).parent().parent().tabs();
132
133 $(element).on("click",function() {
134 scope.acttab=scope.acttab -1;
135 $(element).parent().parent().tabs({active:scope.acttab});
136 console.log("sid");
137 });
138 }
139 };
140 });
141
142
143 crmMailing.directive('chsgroup',function(){
144 return {
145 restrict : 'AE',
146 link: function(scope,element, attrs){
147 $(element).select2(
148 {width:"400px",
149 placeholder: "Include Group",
150 });
151 }
152 };
153
154 });
155
156
157
158 crmMailing.controller('browse', function(){
159 FileUploadCtrl.$inject = ['$scope']
160 function FileUploadCtrl(scope) {
161 //============== DRAG & DROP =============
162 // source for drag&drop: http://www.webappers.com/2011/09/28/drag-drop-file-upload-with-html5-javascript/
163 var dropbox = document.getElementById("dropbox")
164 scope.dropText = 'Drop files here...'
165
166 // init event handlers
167 function dragEnterLeave(evt) {
168 evt.stopPropagation()
169 evt.preventDefault()
170 scope.$apply(function(){
171 scope.dropText = 'Drop files here...'
172 scope.dropClass = ''
173 })
174 }
175 dropbox.addEventListener("dragenter", dragEnterLeave, false)
176 dropbox.addEventListener("dragleave", dragEnterLeave, false)
177 dropbox.addEventListener("dragover", function(evt) {
178 evt.stopPropagation()
179 evt.preventDefault()
180 var clazz = 'not-available'
181 var ok = evt.dataTransfer && evt.dataTransfer.types && evt.dataTransfer.types.indexOf('Files') >= 0
182 scope.$apply(function(){
183 scope.dropText = ok ? 'Drop files here...' : 'Only files are allowed!'
184 scope.dropClass = ok ? 'over' : 'not-available'
185 })
186 }, false)
187 dropbox.addEventListener("drop", function(evt) {
188 console.log('drop evt:', JSON.parse(JSON.stringify(evt.dataTransfer)))
189 evt.stopPropagation()
190 evt.preventDefault()
191 scope.$apply(function(){
192 scope.dropText = 'Drop files here...'
193 scope.dropClass = ''
194 })
195 var files = evt.dataTransfer.files
196 if (files.length > 0) {
197 scope.$apply(function(){
198 scope.files = []
199 for (var i = 0; i < files.length; i++) {
200 scope.files.push(files[i])
201 }
202 })
203 }
204 }, false)
205 //============== DRAG & DROP =============
206
207 scope.setFiles = function(element) {
208 scope.$apply(function(scope) {
209 console.log('files:', element.files);
210 // Turn the FileList object into an Array
211 scope.files = []
212 for (var i = 0; i < element.files.length; i++) {
213 scope.files.push(element.files[i])
214 }
215 scope.progressVisible = false
216 });
217 };
218
219 scope.uploadFile = function() {
220 var fd = new FormData()
221 for (var i in scope.files) {
222 fd.append("uploadedFile", scope.files[i])
223 }
224 var xhr = new XMLHttpRequest()
225 xhr.upload.addEventListener("progress", uploadProgress, false)
226 xhr.addEventListener("load", uploadComplete, false)
227 xhr.addEventListener("error", uploadFailed, false)
228 xhr.addEventListener("abort", uploadCanceled, false)
229 xhr.open("POST", "/fileupload")
230 scope.progressVisible = true
231 xhr.send(fd)
232 }
233
234 function uploadProgress(evt) {
235 scope.$apply(function(){
236 if (evt.lengthComputable) {
237 scope.progress = Math.round(evt.loaded * 100 / evt.total)
238 } else {
239 scope.progress = 'unable to compute'
240 }
241 })
242 }
243
244 function uploadComplete(evt) {
245 /* This event is raised when the server send back a response */
246 alert(evt.target.responseText)
247 }
248
249 function uploadFailed(evt) {
250 alert("There was an error attempting to upload the file.")
251 }
252
253 function uploadCanceled(evt) {
254 scope.$apply(function(){
255 scope.progressVisible = false
256 })
257 alert("The upload has been canceled by the user or the browser dropped the connection.")
258 }
259 }
260 });
261
262
263
264
265 crmMailing.controller('mailingListCtrl', function($scope, crmApi, mailingList) {
266 $scope.mailingList = mailingList.values;
267 $scope.mailStatus = _.pluck(CRM.crmMailing.mailStatus, 'status');
268 });
269
270 })(angular, CRM.$, CRM._);
271
272
273