function deleteCampaign(e){confirm("Delete "+campaigns[e].name+"?")&&api.campaignId.delete(campaigns[e].id).success(function(e){successFlash(e.message),location.reload()})}function generateStatsPieChart(e){var t={donut:!0,donutWidth:40,chartPadding:0,showLabel:!1},a={},i={series:[]},n=0;$.each(e,function(e,t){$.each(t.stats,function(e,t){return"total"==e?(n+=t,!0):void(a[e]?a[e]+=t:a[e]=t)})}),$.each(a,function(e,t){status_label=statsMapping[e],i.series.push({meta:status_label,value:Math.floor(t/n*100)}),$("#stats_chart_legend").append('
  • '+status_label+"
  • ")});new Chartist.Pie("#stats_chart",i,t);$piechart=$("#stats_chart");var s=$piechart.append('
    ').find(".chartist-tooltip").hide();$piechart.get(0).__chartist__.on("draw",function(e){e.element.addClass(statuses[e.meta].slice)}),$piechart.get(0).__chartist__.update(i),$piechart.on("mouseenter",".ct-slice-donut",function(){var e=$(this);value=e.attr("ct:value"),label=e.attr("ct:meta"),s.html(label+": "+value.toString()+"%").show()}),$piechart.on("mouseleave",".ct-slice-donut",function(){s.hide()}),$piechart.on("mousemove",function(e){s.css({left:(e.offsetX||e.originalEvent.layerX)-s.width()/2-10,top:(e.offsetY+40||e.originalEvent.layerY)-s.height()-80})})}var campaigns=[],statuses={"Email Sent":{slice:"ct-slice-donut-sent",legend:"ct-legend-sent",label:"label-success",icon:"fa-envelope",point:"ct-point-sent"},"Email Sent":{slice:"ct-slice-donut-sent",legend:"ct-legend-sent",label:"label-success",icon:"fa-envelope",point:"ct-point-sent"},"Email Opened":{slice:"ct-slice-donut-opened",legend:"ct-legend-opened",label:"label-warning",icon:"fa-envelope",point:"ct-point-opened"},"Clicked Link":{slice:"ct-slice-donut-clicked",legend:"ct-legend-clicked",label:"label-clicked",icon:"fa-mouse-pointer",point:"ct-point-clicked"},Success:{slice:"ct-slice-donut-success",legend:"ct-legend-success",label:"label-danger",icon:"fa-exclamation",point:"ct-point-clicked"},Error:{slice:"ct-slice-donut-error",legend:"ct-legend-error",label:"label-default",icon:"fa-times",point:"ct-point-error"},"Error Sending Email":{slice:"ct-slice-donut-error",legend:"ct-legend-error",label:"label-default",icon:"fa-times",point:"ct-point-error"},"Submitted Data":{slice:"ct-slice-donut-success",legend:"ct-legend-success",label:"label-danger",icon:"fa-exclamation",point:"ct-point-clicked"},Unknown:{slice:"ct-slice-donut-error",legend:"ct-legend-error",label:"label-default",icon:"fa-question",point:"ct-point-error"},Sending:{slice:"ct-slice-donut-sending",legend:"ct-legend-sending",label:"label-primary",icon:"fa-spinner",point:"ct-point-sending"},"Campaign Created":{label:"label-success",icon:"fa-rocket"}},statsMapping={sent:"Email Sent",opened:"Email Opened",clicked:"Clicked Link",submitted_data:"Submitted Data",error:"Error"};$(document).ready(function(){api.campaigns.summary().success(function(e){if($("#loading").hide(),campaigns=e.campaigns,campaigns.length>0){$("#dashboard").show();var t={labels:[],series:[[]]},a={axisX:{showGrid:!1},showArea:!0,plugins:[],low:0,high:100};campaignTable=$("#campaignTable").DataTable({columnDefs:[{orderable:!1,targets:"no-sort"}],order:[[1,"desc"]]}),$.each(campaigns,function(e,a){var i,n=moment(a.created_date).format("MMMM Do YYYY, h:mm:ss a"),s=statuses[a.status]||"label-default";if(moment(a.launch_date).isAfter(moment())){i="Scheduled to start: "+moment(a.launch_date).format("MMMM Do YYYY, h:mm:ss a");var l=i+"

    Number of recipients: "+a.stats.total}else{i="Launch Date: "+moment(a.launch_date).format("MMMM Do YYYY, h:mm:ss a");var l=i+"

    Number of recipients: "+a.stats.total+"

    Emails opened: "+a.stats.opened+"

    Emails clicked: "+a.stats.clicked+"

    Submitted Credentials: "+a.stats.submitted_data+"

    Errors : "+a.stats.error}campaignTable.row.add([escapeHtml(a.name),n,''+a.status+"","
    "]).draw(),$('[data-toggle="tooltip"]').tooltip(),a.y=0,a.y+=a.stats.clicked+a.stats.submitted_data,a.y=Math.floor(a.y/a.stats.total*100),t.labels.push(n),t.series[0].push({meta:e,value:a.y})}),generateStatsPieChart(campaigns);new Chartist.Line("#overview_chart",t,a);$chart=$("#overview_chart");var i=$chart.append('
    ').find(".chartist-tooltip").hide();$chart.on("mouseenter",".ct-point",function(){var e=$(this);value=e.attr("ct:value")||0,cidx=e.attr("ct:meta"),i.html(campaigns[cidx].name+"
    Successes: "+value.toString()+"%").show()}),$chart.on("mouseleave",".ct-point",function(){i.hide()}),$chart.on("mousemove",function(e){i.css({left:(e.offsetX||e.originalEvent.layerX)-i.width()/2-10,top:(e.offsetY+40||e.originalEvent.layerY)-i.height()-40})}),$("#overview_chart").on("click",".ct-point",function(e){$(this).attr("ct:meta");window.location.href="/campaigns/"+campaigns[cidx].id})}else $("#emptyMessage").show()}).error(function(){errorFlash("Error fetching campaigns")})});