varlabels={"In progress":"label-primary",Queued:"label-info",Completed:"label-success","Emails Sent":"label-success",Error:"label-danger"};varcampaigns=[];varcampaign={};functionlaunch(){Swal.fire({title:"Are you sure?",text:"This will schedule the campaign to be launched.",type:"question",animation:false,showCancelButton:true,confirmButtonText:"Launch",confirmButtonColor:"#428bca",reverseButtons:true,allowOutsideClick:false,showLoaderOnConfirm:true,preConfirm:function(){returnnewPromise(function(resolve,reject){groups=[];$("#users").select2("data").forEach(function(group){groups.push({name:group.text})});varsend_by_date=$("#send_by_date").val();if(send_by_date!=""){send_by_date=moment(send_by_date,"MMMM Do YYYY, h:mm a").utc().format()}campaign={name:$("#name").val(),template:{name:$("#template").select2("data")[0].text},url:$("#url").val(),page:{name:$("#page").select2("data")[0].text},smtp:{name:$("#profile").select2("data")[0].text},launch_date:moment($("#launch_date").val(),"MMMM Do YYYY, h:mm a").utc().format(),send_by_date:send_by_date||null,groups:groups};api.campaigns.post(campaign).success(function(data){resolve();campaign=data}).error(function(data){$("#modal\\.flashes").empty().append('<div style="text-align:center" class="alert alert-danger"> <i class="fa fa-exclamation-circle"></i> '+data.responseJSON.message+"</div>");Swal.close()})})}}).then(function(result){if(result.value){Swal.fire("Campaign Scheduled!","This campaign has been scheduled for launch!","success")}$('button:contains("OK")').on("click",function(){window.location="/campaigns/"+campaign.id.toString()})})}functionsendTestEmail(){vartest_email_request={template:{name:$("#template").select2("data")[0].text},first_name:$("input[name=to_first_name]").val(),last_name:$("input[name=to_last_name]").val(),email:$("input[name=to_email]").val(),position:$("input[name=to_position]").val(),url:$("#url").val(),page:{name:$("#page").select2("data")[0].text},smtp:{name:$("#profile").select2("data")[0].text}};btnHtml=$("#sendTestModalSubmit").html();$("#sendTestModalSubmit").html('<i class="fa fa-spinner fa-spin"></i> Sending');api.send_test_email(test_email_request).success(function(data){$("#sendTestEmailModal\\.flashes").empty().append('<div style="text-align:center" class="alert alert-success"> <i class="fa fa-check-circle"></i> Email Sent!</div>');$("#sendTestModalSubmit").html(btnHtml)}).error(function(data){$("#sendTestEmailModal\\.flashes").empty().append('<div style="text-align:center" class="alert alert-danger"> <i class="fa fa-exclamation-circle"></i> '+data.responseJSON.message+"</div>");$("#sendTestModalSubmit").html(btnHtml)})}functiondismiss(){$("#modal\\.flashes").empty();$("#name").val("");$("#template").val("").change();$("#page").val("").change();$("#url").val("");$("#profile").val("").change();$("#users").val("").change();$("#modal").modal("hide")}functiondeleteCampaign(idx){Swal.fire({title:"Are you sure?",text:"This will delete the campaign. This can't be undone!",type:"warning",animation:false,showCancelButton:true,confirmButtonText:"Delete "+campaigns[idx].name,confirmButtonColor:"#428bca",reverseButtons:true,allowOutsideClick:false,preConfirm:function(){returnnewPromise(function(resolve,reject){api.campaignId.delete(campaigns[idx].id).success(function(msg){resolve()}).error(function(data){reject(data.responseJSON.message)})})}}).then(function(result){if(result.value){Swal.fire("Campaign Deleted!","This campaign has been deleted!","success")}$('button:contains("OK")').on("click",function(){location.reload()})})}functionsetupOptions(){api.groups.summary().success(function(summaries){groups=summaries.groups;if(groups.length==0){modalError("No groups found!");returnfalse}else{vargroup_s2=$.map(groups,function(obj){obj.text=obj.name;obj.title=obj.num_targets+" targets";returnobj});console.log(group_s2);$("#users.form-control").select2({placeholder:"Select Groups",data:group_s2})}});api.templates.get().success(function(templates){if(templates.length==0){modalError("No templates found!");ret