1 (function(angular
, $, _
) {
3 var partialUrl = function(relPath
) {
4 return CRM
.resourceUrls
['civicrm'] + '/partials/crmMailingType/' + relPath
;
7 var crmMailing
= angular
.module('crmMailing', ['ngRoute', 'ui.utils']);
10 //-------------------------------------------------------------------------------------------------------
11 crmMailing
.config(['$routeProvider',
12 function($routeProvider
) {
13 $routeProvider
.when('/mailing', {
14 templateUrl
: partialUrl('mailingList.html'),
15 controller
: 'mailingListCtrl',
17 mailingList: function($route
, crmApi
) {
18 return crmApi('Mailing', 'get', {});
26 $routeProvider
.when('/mailing/:id', {
27 templateUrl
: partialUrl('main.html'),
28 controller
: 'mailingCtrl',
30 selectedMail: function($route
, crmApi
) {
31 if ( $route
.current
.params
.id
!== 'new') {
32 return crmApi('Mailing', 'getsingle', {id
: $route
.current
.params
.id
});
35 return {name
: "New Mail", visibility
: "Public Pages", url_tracking
:"1", forward_replies
:"0", auto_responder
:"0", open_tracking
:"1",
43 //-----------------------------------------
44 // Add a new record by name.
45 // Ex: <crmAddName crm-options="['Alpha','Beta','Gamma']" crm-var="newItem" crm-on-add="callMyCreateFunction(newItem)" />
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
;
58 $scope
.currentMailing
= selectedMail
;
59 window
.ct
= $scope
.currentMailing
;
62 $scope
.trackreplies
="0";
63 ///changing upload on screen
65 $scope
.upldChange= function(composeS
){
73 $scope
.trackr= function(trackreplies
){
74 if(trackreplies
=="1"){
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)) {
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
;
108 crmMailing
.directive('nexttab', function() {
112 link: function(scope
, element
, attrs
) {
114 $(element
).parent().parent().tabs();
116 $(element
).on("click",function() {
117 scope
.acttab
=scope
.acttab
+1;
118 $(element
).parent().parent().tabs({active
:scope
.acttab
});
125 crmMailing
.directive('prevtab', function() {
129 link: function(scope
, element
, attrs
) {
131 $(element
).parent().parent().tabs();
133 $(element
).on("click",function() {
134 scope
.acttab
=scope
.acttab
-1;
135 $(element
).parent().parent().tabs({active
:scope
.acttab
});
143 crmMailing
.directive('chsgroup',function(){
146 link: function(scope
,element
, attrs
){
149 placeholder
: "Include Group",
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...'
166 // init event handlers
167 function dragEnterLeave(evt
) {
168 evt
.stopPropagation()
170 scope
.$apply(function(){
171 scope
.dropText
= 'Drop files here...'
175 dropbox
.addEventListener("dragenter", dragEnterLeave
, false)
176 dropbox
.addEventListener("dragleave", dragEnterLeave
, false)
177 dropbox
.addEventListener("dragover", function(evt
) {
178 evt
.stopPropagation()
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'
187 dropbox
.addEventListener("drop", function(evt
) {
188 console
.log('drop evt:', JSON
.parse(JSON
.stringify(evt
.dataTransfer
)))
189 evt
.stopPropagation()
191 scope
.$apply(function(){
192 scope
.dropText
= 'Drop files here...'
195 var files
= evt
.dataTransfer
.files
196 if (files
.length
> 0) {
197 scope
.$apply(function(){
199 for (var i
= 0; i
< files
.length
; i
++) {
200 scope
.files
.push(files
[i
])
205 //============== DRAG & DROP =============
207 scope
.setFiles = function(element
) {
208 scope
.$apply(function(scope
) {
209 console
.log('files:', element
.files
);
210 // Turn the FileList object into an Array
212 for (var i
= 0; i
< element
.files
.length
; i
++) {
213 scope
.files
.push(element
.files
[i
])
215 scope
.progressVisible
= false
219 scope
.uploadFile = function() {
220 var fd
= new FormData()
221 for (var i
in scope
.files
) {
222 fd
.append("uploadedFile", scope
.files
[i
])
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
234 function uploadProgress(evt
) {
235 scope
.$apply(function(){
236 if (evt
.lengthComputable
) {
237 scope
.progress
= Math
.round(evt
.loaded
* 100 / evt
.total
)
239 scope
.progress
= 'unable to compute'
244 function uploadComplete(evt
) {
245 /* This event is raised when the server send back a response */
246 alert(evt
.target
.responseText
)
249 function uploadFailed(evt
) {
250 alert("There was an error attempting to upload the file.")
253 function uploadCanceled(evt
) {
254 scope
.$apply(function(){
255 scope
.progressVisible
= false
257 alert("The upload has been canceled by the user or the browser dropped the connection.")
265 crmMailing
.controller('mailingListCtrl', function($scope
, crmApi
, mailingList
) {
266 $scope
.mailingList
= mailingList
.values
;
267 $scope
.mailStatus
= _
.pluck(CRM
.crmMailing
.mailStatus
, 'status');
270 })(angular
, CRM
.$, CRM
._
);