2015-06-25 07:53:03 +00:00
|
|
|
var template = {attachments:[]}
|
|
|
|
var icons = {
|
|
|
|
"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"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save attempts to POST to /templates/
|
2015-06-24 04:02:29 +00:00
|
|
|
function save(){
|
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();
|
|
|
|
template.text = $("#text_editor").val()
|
|
|
|
// Add the attachments
|
|
|
|
$.each($("#attachmentsTable").DataTable().rows().data(), function(i, target){
|
|
|
|
template.attachments.push({
|
|
|
|
name : target[1],
|
|
|
|
content: target[3],
|
|
|
|
type: target[4],
|
|
|
|
})
|
|
|
|
})
|
2015-06-25 07:53:03 +00:00
|
|
|
// Submit the template
|
|
|
|
api.templates.post(template)
|
2015-06-24 04:02:29 +00:00
|
|
|
.success(function(data){
|
2015-06-25 07:53:03 +00:00
|
|
|
successFlash("Template added successfully!")
|
2015-06-24 04:02:29 +00:00
|
|
|
load()
|
|
|
|
dismiss()
|
|
|
|
})
|
|
|
|
.error(function(data){
|
2015-06-30 00:33:11 +00:00
|
|
|
modalError(data.responseJSON.message)
|
2015-06-24 04:02:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function dismiss(){
|
|
|
|
$("#modal\\.flashes").empty()
|
|
|
|
$("#modal").modal('hide')
|
2015-06-25 07:53:03 +00:00
|
|
|
template = {attachments:[]}
|
|
|
|
}
|
|
|
|
|
|
|
|
function attach(files){
|
2015-06-28 00:21:46 +00:00
|
|
|
attachmentsTable = $("#attachmentsTable").DataTable();
|
2015-06-25 07:53:03 +00:00
|
|
|
$.each(files, function(i, file){
|
|
|
|
var reader = new FileReader();
|
|
|
|
/* Make this a datatable */
|
|
|
|
reader.onload = function(e){
|
|
|
|
var icon = icons[file.type] || "fa-file-o"
|
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2015-06-25 07:53:03 +00:00
|
|
|
function edit(t){
|
2015-06-24 04:02:29 +00:00
|
|
|
$("#html_editor").ckeditor()
|
2015-06-28 00:21:46 +00:00
|
|
|
$("#attachmentsTable").show()
|
2015-06-30 00:33:11 +00:00
|
|
|
$("#attachmentsTable").DataTable({
|
|
|
|
"aoColumnDefs" : [{
|
|
|
|
"targets" : [3,4],
|
|
|
|
"sClass" : "datatable_hidden"
|
|
|
|
}]
|
|
|
|
});
|
2015-06-25 07:53:03 +00:00
|
|
|
if (t == "new") {
|
|
|
|
template = {attachments:[]}
|
2015-06-24 04:02:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function load(){
|
|
|
|
api.templates.get()
|
|
|
|
.success(function(templates){
|
|
|
|
if (templates.length > 0){
|
|
|
|
$("#emptyMessage").hide()
|
|
|
|
$("#templateTable").show()
|
|
|
|
templateTable = $("#templateTable").DataTable();
|
|
|
|
$.each(templates, function(i, template){
|
|
|
|
templateTable.row.add([
|
|
|
|
template.name,
|
2015-07-07 03:26:08 +00:00
|
|
|
moment(template.modified_date).format('MMMM Do YYYY, h:mm:ss a'),
|
2015-07-08 03:31:21 +00:00
|
|
|
"<div class='pull-right'><button class='btn btn-primary' onclick='alert(\"test\")'>\
|
2015-07-07 03:26:08 +00:00
|
|
|
<i class='fa fa-pencil'></i>\
|
|
|
|
</button>\
|
|
|
|
<button class='btn btn-danger' onclick='alert(\"test\")'>\
|
|
|
|
<i class='fa fa-trash-o'></i>\
|
|
|
|
</button></div>"
|
2015-06-24 04:02:29 +00:00
|
|
|
]).draw()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.error(function(){
|
|
|
|
errorFlash("Error fetching templates")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
$(document).ready(function(){
|
|
|
|
load()
|
|
|
|
})
|