2015-08-05 05:23:05 +00:00
|
|
|
var groups = []
|
|
|
|
|
|
|
|
// Save attempts to POST or PUT to /groups/
|
2016-01-17 04:59:40 +00:00
|
|
|
function save(idx) {
|
2015-06-24 04:02:29 +00:00
|
|
|
var targets = []
|
2016-01-17 04:59:40 +00:00
|
|
|
$.each($("#targetsTable").DataTable().rows().data(), function(i, target) {
|
2015-06-24 04:02:29 +00:00
|
|
|
targets.push({
|
2016-05-25 23:35:47 +00:00
|
|
|
first_name: unescapeHtml(target[0]),
|
|
|
|
last_name: unescapeHtml(target[1]),
|
|
|
|
email: unescapeHtml(target[2]),
|
|
|
|
position: unescapeHtml(target[3])
|
2015-06-24 04:02:29 +00:00
|
|
|
})
|
|
|
|
})
|
2015-06-17 04:09:27 +00:00
|
|
|
var group = {
|
2016-01-17 04:59:40 +00:00
|
|
|
name: $("#name").val(),
|
|
|
|
targets: targets
|
|
|
|
}
|
|
|
|
// 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)
|
2016-01-17 04:59:40 +00:00
|
|
|
.success(function(data) {
|
|
|
|
successFlash("Group updated successfully!")
|
|
|
|
load()
|
|
|
|
dismiss()
|
|
|
|
$("#modal").modal('hide')
|
|
|
|
})
|
|
|
|
.error(function(data) {
|
|
|
|
modalError(data.responseJSON.message)
|
|
|
|
})
|
2015-08-05 05:23:05 +00:00
|
|
|
} else {
|
|
|
|
// Else, if this is a new group, POST it
|
|
|
|
// to /groups
|
|
|
|
api.groups.post(group)
|
2016-01-17 04:59:40 +00:00
|
|
|
.success(function(data) {
|
|
|
|
successFlash("Group added successfully!")
|
|
|
|
load()
|
|
|
|
dismiss()
|
|
|
|
$("#modal").modal('hide')
|
|
|
|
})
|
|
|
|
.error(function(data) {
|
|
|
|
modalError(data.responseJSON.message)
|
|
|
|
})
|
2015-08-05 05:23:05 +00:00
|
|
|
}
|
2015-06-17 04:09:27 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +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()
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function edit(idx) {
|
2016-01-17 07:31:46 +00:00
|
|
|
targets = $("#targetsTable").dataTable({
|
2016-01-21 03:53:12 +00:00
|
|
|
destroy: true, // Destroy any other instantiated table - http://datatables.net/manual/tech-notes/3#destroy
|
|
|
|
columnDefs: [{
|
|
|
|
orderable: false,
|
|
|
|
targets: "no-sort"
|
|
|
|
}]
|
|
|
|
})
|
2016-01-17 04:59:40 +00:00
|
|
|
$("#modalSubmit").unbind('click').click(function() {
|
|
|
|
save(idx)
|
|
|
|
})
|
2015-08-05 05:23:05 +00:00
|
|
|
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()
|
2016-01-17 04:59:40 +00:00
|
|
|
.row.add([
|
2016-05-25 23:35:47 +00:00
|
|
|
escapeHtml(record.first_name),
|
|
|
|
escapeHtml(record.last_name),
|
|
|
|
escapeHtml(record.email),
|
|
|
|
escapeHtml(record.position),
|
2016-01-17 04:59:40 +00:00
|
|
|
'<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>'
|
|
|
|
]).draw()
|
2015-08-05 05:23:05 +00:00
|
|
|
});
|
2015-06-18 03:44:05 +00:00
|
|
|
}
|
2015-06-28 00:21:46 +00:00
|
|
|
// Handle file uploads
|
|
|
|
$("#csvupload").fileupload({
|
2016-01-17 04:59:40 +00:00
|
|
|
dataType: "json",
|
|
|
|
add: function(e, data) {
|
2015-06-28 00:21:46 +00:00
|
|
|
$("#modal\\.flashes").empty()
|
2016-01-17 04:59:40 +00:00
|
|
|
var acceptFileTypes = /(csv|txt)$/i;
|
2015-06-28 00:21:46 +00:00
|
|
|
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();
|
|
|
|
},
|
2016-01-17 04:59:40 +00:00
|
|
|
done: function(e, data) {
|
2015-06-28 00:21:46 +00:00
|
|
|
$.each(data.result, function(i, record) {
|
|
|
|
targets.DataTable()
|
2016-01-17 04:59:40 +00:00
|
|
|
.row.add([
|
2016-05-25 23:35:47 +00:00
|
|
|
escapeHtml(record.first_name),
|
|
|
|
escapeHtml(record.last_name),
|
|
|
|
escapeHtml(record.email),
|
|
|
|
escapeHtml(record.position),
|
2016-01-17 04:59:40 +00:00
|
|
|
'<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>'
|
|
|
|
]).draw()
|
2015-06-28 00:21:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|
2015-08-05 05:23:05 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function deleteGroup(idx) {
|
|
|
|
if (confirm("Delete " + groups[idx].name + "?")) {
|
2015-08-05 05:23:05 +00:00
|
|
|
api.groupId.delete(groups[idx].id)
|
2016-01-17 04:59:40 +00:00
|
|
|
.success(function(data) {
|
|
|
|
successFlash(data.message)
|
|
|
|
load()
|
|
|
|
})
|
2015-08-05 05:23:05 +00:00
|
|
|
}
|
2015-06-18 03:44:05 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +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()
|
2016-01-17 04:59:40 +00:00
|
|
|
.success(function(gs) {
|
|
|
|
$("#loading").hide()
|
|
|
|
if (gs.length > 0) {
|
|
|
|
groups = gs
|
|
|
|
$("#emptyMessage").hide()
|
|
|
|
$("#groupTable").show()
|
2016-01-21 03:53:12 +00:00
|
|
|
groupTable = $("#groupTable").DataTable({
|
|
|
|
destroy: true,
|
|
|
|
columnDefs: [{
|
|
|
|
orderable: false,
|
|
|
|
targets: "no-sort"
|
|
|
|
}]
|
|
|
|
});
|
2016-01-17 04:59:40 +00:00
|
|
|
groupTable.clear();
|
|
|
|
$.each(groups, function(i, group) {
|
|
|
|
var targets = ""
|
|
|
|
$.each(group.targets, function(i, target) {
|
|
|
|
targets += target.email + ", "
|
|
|
|
if (targets.length > 50) {
|
|
|
|
targets = targets.slice(0, -3) + "..."
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
groupTable.row.add([
|
2016-05-25 23:35:47 +00:00
|
|
|
escapeHtml(group.name),
|
|
|
|
escapeHtml(targets),
|
2016-01-17 04:59:40 +00:00
|
|
|
moment(group.modified_date).format('MMMM Do YYYY, h:mm:ss a'),
|
|
|
|
"<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>"
|
2016-01-17 04:59:40 +00:00
|
|
|
]).draw()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
$("#emptyMessage").show()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.error(function() {
|
|
|
|
errorFlash("Error fetching groups")
|
|
|
|
})
|
2015-06-17 04:09:27 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
$(document).ready(function() {
|
2015-06-17 04:09:27 +00:00
|
|
|
load()
|
2016-07-09 13:06:30 +00:00
|
|
|
// Setup the event listeners
|
|
|
|
// Handle manual additions
|
2016-01-17 04:59:40 +00:00
|
|
|
$("#targetForm").submit(function() {
|
2016-07-09 19:46:06 +00:00
|
|
|
// Create new data row.
|
|
|
|
var emailInput = escapeHtml($("#email").val()).toLowerCase();
|
|
|
|
var newRow = [
|
|
|
|
escapeHtml($("#firstName").val()),
|
|
|
|
escapeHtml($("#lastName").val()),
|
|
|
|
emailInput,
|
|
|
|
escapeHtml($("#position").val()),
|
|
|
|
'<span style="cursor:pointer;"><i class="fa fa-trash-o"></i></span>'
|
|
|
|
];
|
|
|
|
|
|
|
|
// Check table to see if email already exists.
|
|
|
|
var targetsTable = targets.DataTable();
|
|
|
|
var existingRowIndex = targetsTable
|
|
|
|
.column(2, {order: "index"}) // Email column
|
|
|
|
.data()
|
|
|
|
.indexOf(emailInput);
|
|
|
|
|
|
|
|
// Update or add new row as necessary.
|
|
|
|
if (existingRowIndex >= 0) {
|
|
|
|
targetsTable
|
|
|
|
.row(existingRowIndex, {order: "index"})
|
|
|
|
.data(newRow);
|
|
|
|
} else {
|
|
|
|
targetsTable.row.add(newRow);
|
|
|
|
}
|
|
|
|
targetsTable.draw();
|
|
|
|
|
|
|
|
// Reset user input.
|
2016-07-09 13:06:30 +00:00
|
|
|
$("#targetForm>div>input").val('');
|
|
|
|
$("#firstName").focus();
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
// Handle Deletion
|
2016-01-17 04:59:40 +00:00
|
|
|
$("#targetsTable").on("click", "span>i.fa-trash-o", function() {
|
2015-08-05 05:23:05 +00:00
|
|
|
targets.DataTable()
|
2016-01-17 04:59:40 +00:00
|
|
|
.row($(this).parents('tr'))
|
|
|
|
.remove()
|
|
|
|
.draw();
|
2016-07-09 13:06:30 +00:00
|
|
|
});
|
2016-01-17 04:59:40 +00:00
|
|
|
$("#modal").on("hide.bs.modal", function() {
|
2016-07-09 13:06:30 +00:00
|
|
|
dismiss();
|
|
|
|
});
|
|
|
|
});
|