2015-08-14 23:24:02 +00:00
|
|
|
var templates = []
|
2015-06-25 07:53:03 +00:00
|
|
|
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-08-14 23:24:02 +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}}")
|
2015-06-30 00:33:11 +00:00
|
|
|
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-08-14 23:24:02 +00:00
|
|
|
if (idx != -1){
|
|
|
|
template.id = templates[idx].id
|
|
|
|
api.templateId.put(template)
|
|
|
|
.success(function(data){
|
|
|
|
successFlash("Template edited successfully!")
|
|
|
|
load()
|
|
|
|
dismiss()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// Submit the template
|
|
|
|
api.templates.post(template)
|
|
|
|
.success(function(data){
|
|
|
|
successFlash("Template added successfully!")
|
|
|
|
load()
|
|
|
|
dismiss()
|
|
|
|
})
|
|
|
|
.error(function(data){
|
|
|
|
modalError(data.responseJSON.message)
|
|
|
|
})
|
|
|
|
}
|
2015-06-24 04:02:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function dismiss(){
|
|
|
|
$("#modal\\.flashes").empty()
|
2015-08-14 23:24:02 +00:00
|
|
|
$("#attachmentsTable").dataTable().DataTable().clear().draw()
|
|
|
|
$("#name").val("")
|
|
|
|
$("#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
|
|
|
}
|
|
|
|
|
2015-08-14 23:27:04 +00:00
|
|
|
function deleteTemplate(idx){
|
|
|
|
if (confirm("Delete " + templates[idx].name + "?")){
|
|
|
|
api.templateId.delete(templates[idx].id)
|
|
|
|
.success(function(data){
|
|
|
|
successFlash(data.message)
|
|
|
|
load()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 07:53:03 +00:00
|
|
|
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-08-14 23:24:02 +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()
|
2015-08-14 23:24:02 +00:00
|
|
|
attachmentsTable = null
|
|
|
|
if ( $.fn.dataTable.isDataTable('#attachmentsTable') ) {
|
|
|
|
attachmentsTable = $('#attachmentsTable').DataTable();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
attachmentsTable = $("#attachmentsTable").DataTable({
|
|
|
|
"aoColumnDefs" : [{
|
|
|
|
"targets" : [3,4],
|
|
|
|
"sClass" : "datatable_hidden"
|
|
|
|
}]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
var template = {attachments:[]}
|
|
|
|
if (idx != -1) {
|
|
|
|
template = templates[idx]
|
|
|
|
$("#name").val(template.name)
|
2015-09-15 04:42:29 +00:00
|
|
|
$("#subject").val(template.subject)
|
2015-08-14 23:24:02 +00:00
|
|
|
$("#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()
|
|
|
|
})
|
2015-06-24 04:02:29 +00:00
|
|
|
}
|
2015-08-14 23:24:02 +00:00
|
|
|
// Handle Deletion
|
|
|
|
$("#attachmentsTable").unbind('click').on("click", "span>i.fa-trash-o", function(){
|
|
|
|
attachmentsTable.row( $(this).parents('tr') )
|
|
|
|
.remove()
|
|
|
|
.draw();
|
|
|
|
})
|
2015-06-24 04:02:29 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 04:42:29 +00:00
|
|
|
function importEmail(){
|
|
|
|
raw = $("#email_content").val()
|
|
|
|
if (!raw){
|
|
|
|
modalError("No Content Specified!")
|
|
|
|
} else {
|
|
|
|
$.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-06-24 04:02:29 +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()
|
2015-08-14 23:24:02 +00:00
|
|
|
.success(function(ts){
|
|
|
|
templates = ts
|
|
|
|
$("#loading").hide()
|
2015-06-24 04:02:29 +00:00
|
|
|
if (templates.length > 0){
|
|
|
|
$("#templateTable").show()
|
|
|
|
templateTable = $("#templateTable").DataTable();
|
2015-08-14 23:24:02 +00:00
|
|
|
templateTable.clear()
|
2015-06-24 04:02:29 +00:00
|
|
|
$.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-08-14 23:24:02 +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-14 23:27:04 +00:00
|
|
|
<button class='btn btn-danger' onclick='deleteTemplate(" + i + ")'>\
|
2015-07-07 03:26:08 +00:00
|
|
|
<i class='fa fa-trash-o'></i>\
|
|
|
|
</button></div>"
|
2015-06-24 04:02:29 +00:00
|
|
|
]).draw()
|
|
|
|
})
|
2015-08-14 23:24:02 +00:00
|
|
|
} else {
|
|
|
|
$("#emptyMessage").show()
|
2015-06-24 04:02:29 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.error(function(){
|
2015-08-14 23:24:02 +00:00
|
|
|
$("#loading").hide()
|
2015-06-24 04:02:29 +00:00
|
|
|
errorFlash("Error fetching templates")
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
$(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
|
|
|
|
$('.modal').on('hidden.bs.modal', function( event ) {
|
|
|
|
$(this).removeClass( 'fv-modal-stack' );
|
|
|
|
$('body').data( 'fv_open_modals', $('body').data( 'fv_open_modals' ) - 1 );
|
|
|
|
});
|
|
|
|
$( '.modal' ).on( 'shown.bs.modal', function ( event ) {
|
|
|
|
// Keep track of the number of open modals
|
|
|
|
if ( typeof( $('body').data( 'fv_open_modals' ) ) == 'undefined' )
|
|
|
|
{
|
|
|
|
$('body').data( 'fv_open_modals', 0 );
|
|
|
|
}
|
|
|
|
// if the z-index of this modal has been set, ignore.
|
|
|
|
if ( $(this).hasClass( 'fv-modal-stack' ) )
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
$(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' );
|
|
|
|
});
|
|
|
|
$.fn.modal.Constructor.prototype.enforceFocus = function() {
|
2015-08-26 03:07:57 +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()
|
|
|
|
})
|