2015-08-14 23:24:02 +00:00
|
|
|
var templates = []
|
2015-06-25 07:53:03 +00:00
|
|
|
var icons = {
|
2016-01-17 04:59:40 +00:00
|
|
|
"application/vnd.ms-excel": "fa-file-excel-o",
|
|
|
|
"text/plain": "fa-file-text-o",
|
|
|
|
"image/gif": "fa-file-image-o",
|
|
|
|
"image/png": "fa-file-image-o",
|
|
|
|
"application/pdf": "fa-file-pdf-o",
|
|
|
|
"application/x-zip-compressed": "fa-file-archive-o",
|
|
|
|
"application/x-gzip": "fa-file-archive-o",
|
|
|
|
"application/vnd.openxmlformats-officedocument.presentationml.presentation": "fa-file-powerpoint-o",
|
|
|
|
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "fa-file-word-o",
|
|
|
|
"application/octet-stream": "fa-file-o",
|
|
|
|
"application/x-msdownload": "fa-file-o"
|
2015-06-25 07:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Save attempts to POST to /templates/
|
2016-01-17 04:59:40 +00:00
|
|
|
function save(idx) {
|
|
|
|
var template = {
|
|
|
|
attachments: []
|
|
|
|
}
|
2015-06-25 07:53:03 +00:00
|
|
|
template.name = $("#name").val()
|
2015-06-30 00:33:11 +00:00
|
|
|
template.subject = $("#subject").val()
|
|
|
|
template.html = CKEDITOR.instances["html_editor"].getData();
|
2015-10-23 00:54:01 +00:00
|
|
|
// Fix the URL Scheme added by CKEditor (until we can remove it from the plugin)
|
|
|
|
template.html = template.html.replace(/https?:\/\/{{\.URL}}/gi, "{{.URL}}")
|
2016-01-17 04:59:40 +00:00
|
|
|
// If the "Add Tracker Image" checkbox is checked, add the tracker
|
|
|
|
if ($("#use_tracker_checkbox").prop("checked") &&
|
|
|
|
template.html.indexOf("{{.Tracker}}") == -1 &&
|
|
|
|
template.html.indexOf("{{.TrackingUrl}}") == -1) {
|
|
|
|
template.html = template.html.replace("</body>", "{{.Tracker}}</body>")
|
2016-01-14 04:42:16 +00:00
|
|
|
}
|
2015-06-30 00:33:11 +00:00
|
|
|
template.text = $("#text_editor").val()
|
2016-01-17 04:59:40 +00:00
|
|
|
// Add the attachments
|
|
|
|
$.each($("#attachmentsTable").DataTable().rows().data(), function(i, target) {
|
2015-06-30 00:33:11 +00:00
|
|
|
template.attachments.push({
|
2016-01-17 04:59:40 +00:00
|
|
|
name: target[1],
|
2015-06-30 00:33:11 +00:00
|
|
|
content: target[3],
|
|
|
|
type: target[4],
|
|
|
|
})
|
|
|
|
})
|
2016-01-17 04:59:40 +00:00
|
|
|
if (idx != -1) {
|
2015-08-14 23:24:02 +00:00
|
|
|
template.id = templates[idx].id
|
|
|
|
api.templateId.put(template)
|
2016-01-17 04:59:40 +00:00
|
|
|
.success(function(data) {
|
|
|
|
successFlash("Template edited successfully!")
|
|
|
|
load()
|
|
|
|
dismiss()
|
|
|
|
})
|
2015-08-14 23:24:02 +00:00
|
|
|
} else {
|
|
|
|
// Submit the template
|
|
|
|
api.templates.post(template)
|
2016-01-17 04:59:40 +00:00
|
|
|
.success(function(data) {
|
|
|
|
successFlash("Template added successfully!")
|
|
|
|
load()
|
|
|
|
dismiss()
|
|
|
|
})
|
|
|
|
.error(function(data) {
|
|
|
|
modalError(data.responseJSON.message)
|
|
|
|
})
|
2015-08-14 23:24:02 +00:00
|
|
|
}
|
2015-06-24 04:02:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function dismiss() {
|
2015-06-24 04:02:29 +00:00
|
|
|
$("#modal\\.flashes").empty()
|
2015-08-14 23:24:02 +00:00
|
|
|
$("#attachmentsTable").dataTable().DataTable().clear().draw()
|
|
|
|
$("#name").val("")
|
2016-02-21 22:51:01 +00:00
|
|
|
$("#subject").val("")
|
2015-08-14 23:24:02 +00:00
|
|
|
$("#text_editor").val("")
|
|
|
|
$("#html_editor").val("")
|
2015-09-16 01:39:33 +00:00
|
|
|
$("#modal").modal('hide')
|
2015-06-25 07:53:03 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function deleteTemplate(idx) {
|
|
|
|
if (confirm("Delete " + templates[idx].name + "?")) {
|
2015-08-14 23:27:04 +00:00
|
|
|
api.templateId.delete(templates[idx].id)
|
2016-01-17 04:59:40 +00:00
|
|
|
.success(function(data) {
|
|
|
|
successFlash(data.message)
|
|
|
|
load()
|
|
|
|
})
|
2015-08-14 23:27:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function attach(files) {
|
2016-01-21 01:42:41 +00:00
|
|
|
attachmentsTable = $("#attachmentsTable").DataTable({
|
|
|
|
destroy: true,
|
2016-01-21 03:53:12 +00:00
|
|
|
"order": [
|
|
|
|
[1, "asc"]
|
|
|
|
],
|
|
|
|
columnDefs: [{
|
|
|
|
orderable: false,
|
|
|
|
targets: "no-sort"
|
|
|
|
}, {
|
|
|
|
sClass: "datatable_hidden",
|
|
|
|
targets: [3, 4]
|
|
|
|
}]
|
2016-01-21 01:42:41 +00:00
|
|
|
});
|
2016-01-17 04:59:40 +00:00
|
|
|
$.each(files, function(i, file) {
|
2015-06-25 07:53:03 +00:00
|
|
|
var reader = new FileReader();
|
|
|
|
/* Make this a datatable */
|
2016-01-17 04:59:40 +00:00
|
|
|
reader.onload = function(e) {
|
2015-06-25 07:53:03 +00:00
|
|
|
var icon = icons[file.type] || "fa-file-o"
|
2016-01-17 04:59:40 +00:00
|
|
|
// Add the record to the modal
|
2015-06-28 00:21:46 +00:00
|
|
|
attachmentsTable.row.add([
|
|
|
|
'<i class="fa ' + icon + '"></i>',
|
|
|
|
file.name,
|
2015-06-30 00:33:11 +00:00
|
|
|
'<span class="remove-row"><i class="fa fa-trash-o"></i></span>',
|
|
|
|
reader.result.split(",")[1],
|
|
|
|
file.type || "application/octet-stream"
|
2015-06-28 00:21:46 +00:00
|
|
|
]).draw()
|
2015-06-25 07:53:03 +00:00
|
|
|
}
|
|
|
|
reader.onerror = function(e) {
|
|
|
|
console.log(e)
|
|
|
|
}
|
|
|
|
reader.readAsDataURL(file)
|
|
|
|
})
|
2015-06-24 04:02:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function edit(idx) {
|
|
|
|
$("#modalSubmit").unbind('click').click(function() {
|
|
|
|
save(idx)
|
|
|
|
})
|
|
|
|
$("#attachmentUpload").unbind('click').click(function() {
|
|
|
|
this.value = null
|
|
|
|
})
|
2015-06-24 04:02:29 +00:00
|
|
|
$("#html_editor").ckeditor()
|
2015-06-28 00:21:46 +00:00
|
|
|
$("#attachmentsTable").show()
|
2016-01-21 02:33:59 +00:00
|
|
|
attachmentsTable = $('#attachmentsTable').DataTable({
|
|
|
|
destroy: true,
|
2016-01-21 03:53:12 +00:00
|
|
|
"order": [
|
|
|
|
[1, "asc"]
|
|
|
|
],
|
|
|
|
columnDefs: [{
|
|
|
|
orderable: false,
|
|
|
|
targets: "no-sort"
|
|
|
|
}, {
|
|
|
|
sClass: "datatable_hidden",
|
|
|
|
targets: [3, 4]
|
|
|
|
}]
|
2016-01-21 02:33:59 +00:00
|
|
|
});
|
2016-01-17 04:59:40 +00:00
|
|
|
var template = {
|
|
|
|
attachments: []
|
|
|
|
}
|
2015-08-14 23:24:02 +00:00
|
|
|
if (idx != -1) {
|
|
|
|
template = templates[idx]
|
|
|
|
$("#name").val(template.name)
|
2016-01-17 04:59:40 +00:00
|
|
|
$("#subject").val(template.subject)
|
2015-08-14 23:24:02 +00:00
|
|
|
$("#html_editor").val(template.html)
|
|
|
|
$("#text_editor").val(template.text)
|
2016-01-17 04:59:40 +00:00
|
|
|
$.each(template.attachments, function(i, file) {
|
2015-08-14 23:24:02 +00:00
|
|
|
var icon = icons[file.type] || "fa-file-o"
|
2016-01-17 04:59:40 +00:00
|
|
|
// Add the record to the modal
|
2015-08-14 23:24:02 +00:00
|
|
|
attachmentsTable.row.add([
|
|
|
|
'<i class="fa ' + icon + '"></i>',
|
|
|
|
file.name,
|
|
|
|
'<span class="remove-row"><i class="fa fa-trash-o"></i></span>',
|
|
|
|
file.content,
|
|
|
|
file.type || "application/octet-stream"
|
|
|
|
]).draw()
|
|
|
|
})
|
2015-06-24 04:02:29 +00:00
|
|
|
}
|
2015-08-14 23:24:02 +00:00
|
|
|
// Handle Deletion
|
2016-01-17 04:59:40 +00:00
|
|
|
$("#attachmentsTable").unbind('click').on("click", "span>i.fa-trash-o", function() {
|
|
|
|
attachmentsTable.row($(this).parents('tr'))
|
|
|
|
.remove()
|
|
|
|
.draw();
|
2015-08-14 23:24:02 +00:00
|
|
|
})
|
2015-06-24 04:02:29 +00:00
|
|
|
}
|
|
|
|
|
2016-02-16 02:41:07 +00:00
|
|
|
function copy(idx) {
|
|
|
|
$("#modalSubmit").unbind('click').click(function() {
|
|
|
|
save(-1)
|
|
|
|
})
|
|
|
|
$("#attachmentUpload").unbind('click').click(function() {
|
|
|
|
this.value = null
|
|
|
|
})
|
|
|
|
$("#html_editor").ckeditor()
|
|
|
|
$("#attachmentsTable").show()
|
|
|
|
attachmentsTable = $('#attachmentsTable').DataTable({
|
|
|
|
destroy: true,
|
|
|
|
"order": [
|
|
|
|
[1, "asc"]
|
|
|
|
],
|
|
|
|
columnDefs: [{
|
|
|
|
orderable: false,
|
|
|
|
targets: "no-sort"
|
|
|
|
}, {
|
|
|
|
sClass: "datatable_hidden",
|
|
|
|
targets: [3, 4]
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
var template = {
|
|
|
|
attachments: []
|
|
|
|
}
|
|
|
|
template = templates[idx]
|
|
|
|
$("#name").val("Copy of " + template.name)
|
|
|
|
$("#subject").val(template.subject)
|
|
|
|
$("#html_editor").val(template.html)
|
|
|
|
$("#text_editor").val(template.text)
|
|
|
|
$.each(template.attachments, function(i, file) {
|
|
|
|
var icon = icons[file.type] || "fa-file-o"
|
|
|
|
// Add the record to the modal
|
|
|
|
attachmentsTable.row.add([
|
|
|
|
'<i class="fa ' + icon + '"></i>',
|
|
|
|
file.name,
|
|
|
|
'<span class="remove-row"><i class="fa fa-trash-o"></i></span>',
|
|
|
|
file.content,
|
|
|
|
file.type || "application/octet-stream"
|
|
|
|
]).draw()
|
|
|
|
})
|
|
|
|
// Handle Deletion
|
|
|
|
$("#attachmentsTable").unbind('click').on("click", "span>i.fa-trash-o", function() {
|
|
|
|
attachmentsTable.row($(this).parents('tr'))
|
|
|
|
.remove()
|
|
|
|
.draw();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function importEmail() {
|
2015-09-15 04:42:29 +00:00
|
|
|
raw = $("#email_content").val()
|
2016-01-17 04:59:40 +00:00
|
|
|
if (!raw) {
|
2015-09-15 04:42:29 +00:00
|
|
|
modalError("No Content Specified!")
|
|
|
|
} else {
|
2016-01-17 04:59:40 +00:00
|
|
|
$.ajax({
|
|
|
|
type: "POST",
|
|
|
|
url: "/api/import/email",
|
|
|
|
data: raw,
|
|
|
|
dataType: "json",
|
|
|
|
contentType: "text/plain"
|
|
|
|
})
|
|
|
|
.success(function(data) {
|
|
|
|
$("#text_editor").val(data.text)
|
|
|
|
$("#html_editor").val(data.html)
|
|
|
|
$("#subject").val(data.subject)
|
|
|
|
$("#importEmailModal").modal("hide")
|
|
|
|
})
|
|
|
|
.error(function(data) {
|
|
|
|
modalError(data.responseJSON.message)
|
|
|
|
})
|
2015-09-15 04:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function load() {
|
2015-08-14 23:24:02 +00:00
|
|
|
$("#templateTable").hide()
|
|
|
|
$("#emptyMessage").hide()
|
|
|
|
$("#loading").show()
|
2015-06-24 04:02:29 +00:00
|
|
|
api.templates.get()
|
2016-01-17 04:59:40 +00:00
|
|
|
.success(function(ts) {
|
|
|
|
templates = ts
|
|
|
|
$("#loading").hide()
|
|
|
|
if (templates.length > 0) {
|
|
|
|
$("#templateTable").show()
|
2016-01-21 03:53:12 +00:00
|
|
|
templateTable = $("#templateTable").DataTable({
|
|
|
|
destroy: true,
|
|
|
|
columnDefs: [{
|
|
|
|
orderable: false,
|
|
|
|
targets: "no-sort"
|
|
|
|
}]
|
|
|
|
});
|
2016-01-17 04:59:40 +00:00
|
|
|
templateTable.clear()
|
|
|
|
$.each(templates, function(i, template) {
|
|
|
|
templateTable.row.add([
|
|
|
|
template.name,
|
|
|
|
moment(template.modified_date).format('MMMM Do YYYY, h:mm:ss a'),
|
2016-02-16 02:41:07 +00:00
|
|
|
"<div class='pull-right'><span data-toggle='modal' data-target='#modal'><button class='btn btn-primary' data-toggle='tooltip' data-placement='left' title='Edit Template' onclick='edit(" + i + ")'>\
|
2015-07-07 03:26:08 +00:00
|
|
|
<i class='fa fa-pencil'></i>\
|
2016-02-16 02:41:07 +00:00
|
|
|
</button></span>\
|
|
|
|
<span data-toggle='modal' data-target='#modal'><button class='btn btn-primary' data-toggle='tooltip' data-placement='left' title='Copy Template' onclick='copy(" + i + ")'>\
|
|
|
|
<i class='fa fa-copy'></i>\
|
|
|
|
</button></span>\
|
|
|
|
<button class='btn btn-danger' data-toggle='tooltip' data-placement='left' title='Delete Template' onclick='deleteTemplate(" + 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()
|
|
|
|
})
|
2016-02-16 02:41:07 +00:00
|
|
|
$('[data-toggle="tooltip"]').tooltip()
|
2016-01-17 04:59:40 +00:00
|
|
|
} else {
|
|
|
|
$("#emptyMessage").show()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.error(function() {
|
|
|
|
$("#loading").hide()
|
|
|
|
errorFlash("Error fetching templates")
|
|
|
|
})
|
2015-06-24 04:02:29 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
$(document).ready(function() {
|
2015-09-15 04:42:29 +00:00
|
|
|
// Setup multiple modals
|
|
|
|
// Code based on http://miles-by-motorcycle.com/static/bootstrap-modal/index.html
|
2016-01-17 04:59:40 +00:00
|
|
|
$('.modal').on('hidden.bs.modal', function(event) {
|
|
|
|
$(this).removeClass('fv-modal-stack');
|
|
|
|
$('body').data('fv_open_modals', $('body').data('fv_open_modals') - 1);
|
2015-09-15 04:42:29 +00:00
|
|
|
});
|
2016-01-17 04:59:40 +00:00
|
|
|
$('.modal').on('shown.bs.modal', function(event) {
|
2015-09-15 04:42:29 +00:00
|
|
|
// Keep track of the number of open modals
|
2016-01-17 04:59:40 +00:00
|
|
|
if (typeof($('body').data('fv_open_modals')) == 'undefined') {
|
|
|
|
$('body').data('fv_open_modals', 0);
|
2015-09-15 04:42:29 +00:00
|
|
|
}
|
|
|
|
// if the z-index of this modal has been set, ignore.
|
2016-01-17 04:59:40 +00:00
|
|
|
if ($(this).hasClass('fv-modal-stack')) {
|
2015-09-15 04:42:29 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-01-17 04:59:40 +00:00
|
|
|
$(this).addClass('fv-modal-stack');
|
|
|
|
// Increment the number of open modals
|
|
|
|
$('body').data('fv_open_modals', $('body').data('fv_open_modals') + 1);
|
|
|
|
// Setup the appropriate z-index
|
|
|
|
$(this).css('z-index', 1040 + (10 * $('body').data('fv_open_modals')));
|
|
|
|
$('.modal-backdrop').not('.fv-modal-stack').css('z-index', 1039 + (10 * $('body').data('fv_open_modals')));
|
|
|
|
$('.modal-backdrop').not('fv-modal-stack').addClass('fv-modal-stack');
|
2015-09-15 04:42:29 +00:00
|
|
|
});
|
|
|
|
$.fn.modal.Constructor.prototype.enforceFocus = function() {
|
2016-01-17 04:59:40 +00:00
|
|
|
$(document)
|
|
|
|
.off('focusin.bs.modal') // guard against infinite focus loop
|
|
|
|
.on('focusin.bs.modal', $.proxy(function(e) {
|
|
|
|
if (
|
|
|
|
this.$element[0] !== e.target && !this.$element.has(e.target).length
|
|
|
|
// CKEditor compatibility fix start.
|
|
|
|
&& !$(e.target).closest('.cke_dialog, .cke').length
|
|
|
|
// CKEditor compatibility fix end.
|
|
|
|
) {
|
|
|
|
this.$element.trigger('focus');
|
|
|
|
}
|
|
|
|
}, this));
|
|
|
|
};
|
2015-06-24 04:02:29 +00:00
|
|
|
load()
|
|
|
|
})
|