diff --git a/static/js/app/templates.js b/static/js/app/templates.js index fb461714..486b9c91 100644 --- a/static/js/app/templates.js +++ b/static/js/app/templates.js @@ -1,4 +1,4 @@ -var template = {attachments:[]} +var templates = [] var icons = { "application/vnd.ms-excel" : "fa-file-excel-o", "text/plain" : "fa-file-text-o", @@ -14,7 +14,8 @@ var icons = { } // Save attempts to POST to /templates/ -function save(){ +function save(idx){ + var template = {attachments:[]} template.name = $("#name").val() template.subject = $("#subject").val() template.html = CKEDITOR.instances["html_editor"].getData(); @@ -27,22 +28,35 @@ function save(){ type: target[4], }) }) - // Submit the template - api.templates.post(template) - .success(function(data){ - successFlash("Template added successfully!") - load() - dismiss() - }) - .error(function(data){ - modalError(data.responseJSON.message) - }) + 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) + }) + } } function dismiss(){ $("#modal\\.flashes").empty() $("#modal").modal('hide') - template = {attachments:[]} + $("#attachmentsTable").dataTable().DataTable().clear().draw() + $("#name").val("") + $("#text_editor").val("") + $("#html_editor").val("") } function attach(files){ @@ -68,32 +82,66 @@ function attach(files){ }) } -function edit(t){ +function edit(idx){ + $("#modalSubmit").unbind('click').click(function(){save(idx)}) + $("#attachmentUpload").unbind('click').click(function(){this.value=null}) $("#html_editor").ckeditor() $("#attachmentsTable").show() - $("#attachmentsTable").DataTable({ - "aoColumnDefs" : [{ - "targets" : [3,4], - "sClass" : "datatable_hidden" - }] - }); - if (t == "new") { - template = {attachments:[]} + 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) + $("#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([ + '', + file.name, + '', + 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(); + }) } function load(){ + $("#templateTable").hide() + $("#emptyMessage").hide() + $("#loading").show() api.templates.get() - .success(function(templates){ + .success(function(ts){ + templates = ts + $("#loading").hide() if (templates.length > 0){ - $("#emptyMessage").hide() $("#templateTable").show() templateTable = $("#templateTable").DataTable(); + templateTable.clear() $.each(templates, function(i, template){ templateTable.row.add([ template.name, moment(template.modified_date).format('MMMM Do YYYY, h:mm:ss a'), - "
\
" ]).draw() }) + } else { + $("#emptyMessage").show() } }) .error(function(){ + $("#loading").hide() errorFlash("Error fetching templates") }) } diff --git a/templates/templates.html b/templates/templates.html index 0ecfcbbc..ef3091f8 100644 --- a/templates/templates.html +++ b/templates/templates.html @@ -9,7 +9,7 @@
  • Users & Groups
  • -
  • Email Templates +
  • Email Templates
  • Landing Pages
  • @@ -29,12 +29,15 @@
    - +
      -
    +
    + +
    +
    @@ -50,30 +53,6 @@
    -

    Add Files - +

    @@ -133,7 +112,7 @@