2015-08-05 05:23:05 +00:00
|
|
|
var groups = []
|
|
|
|
|
|
|
|
// Save attempts to POST or PUT to /groups/
|
|
|
|
function save(idx){
|
2015-06-24 04:02:29 +00:00
|
|
|
var targets = []
|
|
|
|
$.each($("#targetsTable").DataTable().rows().data(), function(i, target){
|
|
|
|
targets.push({
|
|
|
|
first_name : target[0],
|
|
|
|
last_name: target[1],
|
|
|
|
email: target[2],
|
|
|
|
position: target[3]
|
|
|
|
})
|
|
|
|
})
|
2015-06-17 04:09:27 +00:00
|
|
|
var group = {
|
|
|
|
name: $("#name").val(),
|
|
|
|
targets: targets
|
|
|
|
}
|
2015-06-24 04:02:29 +00:00
|
|
|
// Submit the group
|
2015-08-05 05:23:05 +00:00
|
|
|
if (idx != -1) {
|
|
|
|
// If we're just editing an existing group,
|
|
|
|
// we need to PUT /groups/:id
|
|
|
|
group.id = groups[idx].id
|
|
|
|
api.groupId.put(group)
|
|
|
|
.success(function(data){
|
|
|
|
successFlash("Group updated successfully!")
|
|
|
|
load()
|
|
|
|
dismiss()
|
|
|
|
})
|
|
|
|
.error(function(data){
|
|
|
|
modalError(data.responseJSON.message)
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Else, if this is a new group, POST it
|
|
|
|
// to /groups
|
|
|
|
api.groups.post(group)
|
|
|
|
.success(function(data){
|
|
|
|
successFlash("Group added successfully!")
|
|
|
|
load()
|
|
|
|
dismiss()
|
|
|
|
})
|
|
|
|
.error(function(data){
|
|
|
|
modalError(data.responseJSON.message)
|
|
|
|
})
|
|
|
|
}
|
2015-06-17 04:09:27 +00:00
|
|
|
}
|
|
|
|
|
2015-06-18 03:44:05 +00:00
|
|
|
function dismiss(){
|
2015-06-28 00:21:46 +00:00
|
|
|
$("#targetsTable").dataTable().DataTable().clear().draw()
|
2015-08-05 05:23:05 +00:00
|
|
|
$("#name").val("")
|
2015-06-18 03:44:05 +00:00
|
|
|
$("#modal\\.flashes").empty()
|
|
|
|
$("#modal").modal('hide')
|
|
|
|
}
|
|
|
|
|
2015-08-05 05:23:05 +00:00
|
|
|
function edit(idx){
|
2016-01-17 07:31:46 +00:00
|
|
|
targets = $("#targetsTable").dataTable({
|
|
|
|
columnDefs: [
|
|
|
|
{ orderable: false, targets: -1 }
|
|
|
|
]
|
|
|
|
})
|
2015-08-05 05:23:05 +00:00
|
|
|
$("#modalSubmit").unbind('click').click(function(){save(idx)})
|
|
|
|
if (idx == -1) {
|
2015-06-18 03:44:05 +00:00
|
|
|
group = {}
|
2015-08-05 05:23:05 +00:00
|
|
|
} else {
|
|
|
|
group = groups[idx]
|
|
|
|
$("#name").val(group.name)
|
|
|
|
$.each(group.targets, function(i, record) {
|
|
|
|
targets.DataTable()
|
|
|
|
.row.add([
|
|
|
|
record.first_name,
|
|
|
|
record.last_name,
|
|
|
|
record.email,
|
|
|
|
record.position,
|
|
|
|
'<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>'
|
|
|
|
]).draw()
|
|
|
|
});
|
2015-06-18 03:44:05 +00:00
|
|
|
}
|
2015-06-28 00:21:46 +00:00
|
|
|
// Handle file uploads
|
|
|
|
$("#csvupload").fileupload({
|
|
|
|
dataType:"json",
|
|
|
|
add: function(e, data){
|
|
|
|
$("#modal\\.flashes").empty()
|
|
|
|
var acceptFileTypes= /(csv|txt)$/i;
|
|
|
|
var filename = data.originalFiles[0]['name']
|
|
|
|
if (filename && !acceptFileTypes.test(filename.split(".").pop())) {
|
|
|
|
modalError("Unsupported file extension (use .csv or .txt)")
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
data.submit();
|
|
|
|
},
|
|
|
|
done: function(e, data){
|
|
|
|
$.each(data.result, function(i, record) {
|
|
|
|
targets.DataTable()
|
|
|
|
.row.add([
|
|
|
|
record.first_name,
|
|
|
|
record.last_name,
|
|
|
|
record.email,
|
|
|
|
record.position,
|
|
|
|
'<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>'
|
|
|
|
]).draw()
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
2015-08-05 05:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function deleteGroup(idx){
|
|
|
|
if (confirm("Delete " + groups[idx].name + "?")){
|
|
|
|
api.groupId.delete(groups[idx].id)
|
|
|
|
.success(function(data){
|
|
|
|
successFlash(data.message)
|
|
|
|
load()
|
|
|
|
})
|
|
|
|
}
|
2015-06-18 03:44:05 +00:00
|
|
|
}
|
|
|
|
|
2015-06-17 04:09:27 +00:00
|
|
|
function load(){
|
2015-08-12 02:12:09 +00:00
|
|
|
$("#groupTable").hide()
|
|
|
|
$("#emptyMessage").hide()
|
|
|
|
$("#loading").show()
|
2015-06-17 04:09:27 +00:00
|
|
|
api.groups.get()
|
2015-08-05 05:23:05 +00:00
|
|
|
.success(function(gs){
|
2015-08-26 02:03:12 +00:00
|
|
|
$("#loading").hide()
|
2015-08-05 05:23:05 +00:00
|
|
|
if (gs.length > 0){
|
|
|
|
groups = gs
|
2015-08-12 02:12:09 +00:00
|
|
|
$("#emptyMessage").hide()
|
2015-06-17 04:09:27 +00:00
|
|
|
$("#groupTable").show()
|
|
|
|
groupTable = $("#groupTable").DataTable();
|
2015-08-05 05:23:05 +00:00
|
|
|
groupTable.clear();
|
2015-06-17 04:09:27 +00:00
|
|
|
$.each(groups, function(i, group){
|
2015-06-28 00:21:46 +00:00
|
|
|
var targets = ""
|
|
|
|
$.each(group.targets, function(i, target){
|
|
|
|
targets += target.email + ", "
|
|
|
|
if (targets.length > 50) {
|
|
|
|
targets = targets.slice(0,-3) + "..."
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})
|
2015-06-17 04:09:27 +00:00
|
|
|
groupTable.row.add([
|
2015-06-24 04:02:29 +00:00
|
|
|
group.name,
|
2015-06-28 00:21:46 +00:00
|
|
|
targets,
|
2015-07-07 03:26:08 +00:00
|
|
|
moment(group.modified_date).format('MMMM Do YYYY, h:mm:ss a'),
|
2015-08-05 05:23:05 +00:00
|
|
|
"<div class='pull-right'><button class='btn btn-primary' data-toggle='modal' data-target='#modal' onclick='edit(" + i + ")'>\
|
2015-07-07 03:26:08 +00:00
|
|
|
<i class='fa fa-pencil'></i>\
|
|
|
|
</button>\
|
2015-08-05 05:23:05 +00:00
|
|
|
<button class='btn btn-danger' onclick='deleteGroup(" + i + ")'>\
|
2015-07-07 03:26:08 +00:00
|
|
|
<i class='fa fa-trash-o'></i>\
|
|
|
|
</button></div>"
|
2015-06-17 04:09:27 +00:00
|
|
|
]).draw()
|
|
|
|
})
|
2015-08-12 02:12:09 +00:00
|
|
|
} else {
|
|
|
|
$("#emptyMessage").show()
|
2015-06-17 04:09:27 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.error(function(){
|
|
|
|
errorFlash("Error fetching groups")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
$(document).ready(function(){
|
|
|
|
load()
|
2015-08-05 05:23:05 +00:00
|
|
|
// Setup the event listeners
|
|
|
|
// Handle manual additions
|
|
|
|
$("#targetForm").submit(function(){
|
|
|
|
targets.DataTable()
|
|
|
|
.row.add([
|
|
|
|
$("#firstName").val(),
|
|
|
|
$("#lastName").val(),
|
|
|
|
$("#email").val(),
|
|
|
|
$("#position").val(),
|
|
|
|
'<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>'
|
|
|
|
])
|
|
|
|
.draw()
|
|
|
|
$("#targetForm>div>input").val('')
|
|
|
|
$("#firstName").focus()
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
// Handle Deletion
|
|
|
|
$("#targetsTable").on("click", "span>i.fa-trash-o", function(){
|
|
|
|
targets.DataTable()
|
|
|
|
.row( $(this).parents('tr') )
|
|
|
|
.remove()
|
|
|
|
.draw();
|
|
|
|
})
|
2015-06-17 04:09:27 +00:00
|
|
|
})
|