Updated templates page to handle PUT vs POST - can now edit templates

pull/24/head
unknown 2015-08-14 18:24:02 -05:00
parent a78e92a436
commit d79de6263d
2 changed files with 85 additions and 55 deletions

View File

@ -1,4 +1,4 @@
var template = {attachments:[]} var templates = []
var icons = { var icons = {
"application/vnd.ms-excel" : "fa-file-excel-o", "application/vnd.ms-excel" : "fa-file-excel-o",
"text/plain" : "fa-file-text-o", "text/plain" : "fa-file-text-o",
@ -14,7 +14,8 @@ var icons = {
} }
// Save attempts to POST to /templates/ // Save attempts to POST to /templates/
function save(){ function save(idx){
var template = {attachments:[]}
template.name = $("#name").val() template.name = $("#name").val()
template.subject = $("#subject").val() template.subject = $("#subject").val()
template.html = CKEDITOR.instances["html_editor"].getData(); template.html = CKEDITOR.instances["html_editor"].getData();
@ -27,22 +28,35 @@ function save(){
type: target[4], type: target[4],
}) })
}) })
// Submit the template if (idx != -1){
api.templates.post(template) template.id = templates[idx].id
.success(function(data){ api.templateId.put(template)
successFlash("Template added successfully!") .success(function(data){
load() successFlash("Template edited successfully!")
dismiss() load()
}) dismiss()
.error(function(data){ })
modalError(data.responseJSON.message) } 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(){ function dismiss(){
$("#modal\\.flashes").empty() $("#modal\\.flashes").empty()
$("#modal").modal('hide') $("#modal").modal('hide')
template = {attachments:[]} $("#attachmentsTable").dataTable().DataTable().clear().draw()
$("#name").val("")
$("#text_editor").val("")
$("#html_editor").val("")
} }
function attach(files){ 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() $("#html_editor").ckeditor()
$("#attachmentsTable").show() $("#attachmentsTable").show()
$("#attachmentsTable").DataTable({ attachmentsTable = null
"aoColumnDefs" : [{ if ( $.fn.dataTable.isDataTable('#attachmentsTable') ) {
"targets" : [3,4], attachmentsTable = $('#attachmentsTable').DataTable();
"sClass" : "datatable_hidden"
}]
});
if (t == "new") {
template = {attachments:[]}
} }
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([
'<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();
})
} }
function load(){ function load(){
$("#templateTable").hide()
$("#emptyMessage").hide()
$("#loading").show()
api.templates.get() api.templates.get()
.success(function(templates){ .success(function(ts){
templates = ts
$("#loading").hide()
if (templates.length > 0){ if (templates.length > 0){
$("#emptyMessage").hide()
$("#templateTable").show() $("#templateTable").show()
templateTable = $("#templateTable").DataTable(); templateTable = $("#templateTable").DataTable();
templateTable.clear()
$.each(templates, function(i, template){ $.each(templates, function(i, template){
templateTable.row.add([ templateTable.row.add([
template.name, template.name,
moment(template.modified_date).format('MMMM Do YYYY, h:mm:ss a'), moment(template.modified_date).format('MMMM Do YYYY, h:mm:ss a'),
"<div class='pull-right'><button class='btn btn-primary' onclick='alert(\"test\")'>\ "<div class='pull-right'><button class='btn btn-primary' data-toggle='modal' data-target='#modal' onclick='edit(" + i + ")'>\
<i class='fa fa-pencil'></i>\ <i class='fa fa-pencil'></i>\
</button>\ </button>\
<button class='btn btn-danger' onclick='alert(\"test\")'>\ <button class='btn btn-danger' onclick='alert(\"test\")'>\
@ -101,9 +149,12 @@ function load(){
</button></div>" </button></div>"
]).draw() ]).draw()
}) })
} else {
$("#emptyMessage").show()
} }
}) })
.error(function(){ .error(function(){
$("#loading").hide()
errorFlash("Error fetching templates") errorFlash("Error fetching templates")
}) })
} }

View File

@ -9,7 +9,7 @@
</li> </li>
<li><a href="/users">Users &amp; Groups</a> <li><a href="/users">Users &amp; Groups</a>
</li> </li>
<li class="active"><a href="#/templates">Email Templates</a> <li class="active"><a href="/templates">Email Templates</a>
</li> </li>
<li><a href="/landing_pages">Landing Pages</a> <li><a href="/landing_pages">Landing Pages</a>
</li> </li>
@ -29,12 +29,15 @@
</div> </div>
<div id="flashes" class="row"></div> <div id="flashes" class="row"></div>
<div class="row"> <div class="row">
<button type="button" class="btn btn-primary" onclick="edit('new')" data-toggle="modal" data-target="#modal"><i class="fa fa-plus"></i> New Template</button> <button type="button" class="btn btn-primary" onclick="edit(-1)" data-toggle="modal" data-target="#modal"><i class="fa fa-plus"></i> New Template</button>
</div> </div>
&nbsp; &nbsp;
<div id="emptyMessage" class="row"> <div id="loading">
<i class="fa fa-spinner fa-spin fa-4x"></i>
</div>
<div id="emptyMessage" class="row" style="display:none;">
<div class="alert alert-info"> <div class="alert alert-info">
No templates created yet. Let's create one! No templates yet. Let's create one!
</div> </div>
</div> </div>
<div class="row"> <div class="row">
@ -50,30 +53,6 @@
</tbody> </tbody>
</table> </table>
</div> </div>
<!-- <div ng-show="templates.length" class="row">
<table ng-table="mainTableParams" class="table table-hover table-striped table-bordered">
<tbody>
<tr ng-repeat="template in $data | orderBy: '-modified_date'" class="editable-row">
<td data-title="'Name'" sortable="'name'" class="col-sm-2">template.name
<div class="btn-group" style="float: right;">
<button type="button" class="btn btn-primary dropdown-toggle edit-button" data-toggle="dropdown">
<span class="caret" style="border-top-color:#FFFFFF"></span>
<span class="sr-only">Toggle Dropdown</span>
</button>
<ul class="dropdown-menu" style="left:auto; right:0;" role="menu">
<li><a ng-click="editTemplate(template)">Edit</a>
</li>
<li class="divider"></li>
<li><a ng-click="deleteTemplate(template)">Delete</a>
</li>
</ul>
</div>
</td>
<td data-title="'Modified Date'" class="col-sm-1">template.modified_date | date:'medium'</td>
</tr>
</tbody>
</table>
</div> -->
</div> </div>
<!-- Modal --> <!-- Modal -->
<div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modalLabel"> <div class="modal fade" id="modal" tabindex="-1" role="dialog" aria-labelledby="modalLabel">
@ -113,7 +92,7 @@
</div> </div>
<br /> <br />
<span class="btn btn-danger btn-file"><i class="fa fa-plus"></i> Add Files <span class="btn btn-danger btn-file"><i class="fa fa-plus"></i> Add Files
<input type="file" onchange="attach(this.files)" multiple> <input id="attachmentUpload" type="file" onchange="attach(this.files)" multiple>
</span> </span>
<br /> <br />
<br /> <br />
@ -133,7 +112,7 @@
</div> </div>
<div class="modal-footer"> <div class="modal-footer">
<button type="button" class="btn btn-default" onclick="dismiss()">Cancel</button> <button type="button" class="btn btn-default" onclick="dismiss()">Cancel</button>
<button type="button" class="btn btn-primary" onclick="save()">Save Template</button> <button type="button" class="btn btn-primary" id="modalSubmit">Save Template</button>
</div> </div>
</div> </div>
</div> </div>