2015-08-23 20:20:28 +00:00
|
|
|
/*
|
|
|
|
landing_pages.js
|
|
|
|
Handles the creation, editing, and deletion of landing pages
|
|
|
|
Author: Jordan Wright <github.com/jordan-wright>
|
|
|
|
*/
|
|
|
|
var pages = []
|
2016-01-17 04:59:40 +00:00
|
|
|
|
2015-08-23 20:20:28 +00:00
|
|
|
// Save attempts to POST to /templates/
|
2016-01-17 04:59:40 +00:00
|
|
|
function save(idx) {
|
2015-08-23 20:20:28 +00:00
|
|
|
var page = {}
|
|
|
|
page.name = $("#name").val()
|
|
|
|
page.html = CKEDITOR.instances["html_editor"].getData();
|
2016-01-17 04:59:40 +00:00
|
|
|
if (idx != -1) {
|
2015-10-23 23:35:42 +00:00
|
|
|
page.id = pages[idx].id
|
|
|
|
api.pageId.put(page)
|
2016-01-17 04:59:40 +00:00
|
|
|
.success(function(data) {
|
|
|
|
successFlash("Page edited successfully!")
|
|
|
|
load()
|
|
|
|
dismiss()
|
|
|
|
})
|
2015-08-23 20:20:28 +00:00
|
|
|
} else {
|
|
|
|
// Submit the page
|
2015-08-26 02:03:12 +00:00
|
|
|
api.pages.post(page)
|
2016-01-17 04:59:40 +00:00
|
|
|
.success(function(data) {
|
|
|
|
successFlash("Page added successfully!")
|
|
|
|
load()
|
|
|
|
dismiss()
|
|
|
|
})
|
|
|
|
.error(function(data) {
|
|
|
|
modalError(data.responseJSON.message)
|
|
|
|
})
|
2015-08-23 20:20:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function dismiss() {
|
2015-08-23 20:20:28 +00:00
|
|
|
$("#modal\\.flashes").empty()
|
|
|
|
$("#name").val("")
|
|
|
|
$("#html_editor").val("")
|
2015-09-16 01:39:33 +00:00
|
|
|
$("#newLandingPageModal").modal('hide')
|
2015-08-23 20:20:28 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function deletePage(idx) {
|
|
|
|
if (confirm("Delete " + pages[idx].name + "?")) {
|
2015-08-26 02:03:12 +00:00
|
|
|
api.pageId.delete(pages[idx].id)
|
2016-01-17 04:59:40 +00:00
|
|
|
.success(function(data) {
|
|
|
|
successFlash(data.message)
|
|
|
|
load()
|
|
|
|
})
|
2015-08-23 20:20:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function importSite() {
|
2015-08-25 23:56:58 +00:00
|
|
|
url = $("#url").val()
|
2016-01-17 04:59:40 +00:00
|
|
|
if (!url) {
|
2015-08-25 23:56:58 +00:00
|
|
|
modalError("No URL Specified!")
|
|
|
|
} else {
|
|
|
|
api.clone_site({
|
2016-01-17 04:59:40 +00:00
|
|
|
url: url,
|
|
|
|
include_resources: false
|
|
|
|
})
|
|
|
|
.success(function(data) {
|
|
|
|
console.log($("#html_editor"))
|
|
|
|
$("#html_editor").val(data.html)
|
|
|
|
$("#importSiteModal").modal("hide")
|
|
|
|
})
|
|
|
|
.error(function(data) {
|
|
|
|
modalError(data.responseJSON.message)
|
|
|
|
})
|
2015-08-25 23:56:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function edit(idx) {
|
|
|
|
$("#modalSubmit").unbind('click').click(function() {
|
|
|
|
save(idx)
|
|
|
|
})
|
2015-08-23 20:20:28 +00:00
|
|
|
$("#html_editor").ckeditor()
|
|
|
|
var page = {}
|
|
|
|
if (idx != -1) {
|
|
|
|
page = pages[idx]
|
|
|
|
$("#name").val(page.name)
|
|
|
|
$("#html_editor").val(page.html)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
function load() {
|
|
|
|
/*
|
|
|
|
load() - Loads the current pages using the API
|
|
|
|
*/
|
2015-08-23 20:20:28 +00:00
|
|
|
$("#pagesTable").hide()
|
|
|
|
$("#emptyMessage").hide()
|
|
|
|
$("#loading").show()
|
2015-08-26 02:03:12 +00:00
|
|
|
api.pages.get()
|
2016-01-17 04:59:40 +00:00
|
|
|
.success(function(ps) {
|
|
|
|
pages = ps
|
|
|
|
$("#loading").hide()
|
|
|
|
if (pages.length > 0) {
|
|
|
|
$("#pagesTable").show()
|
2016-01-21 03:53:12 +00:00
|
|
|
pagesTable = $("#pagesTable").DataTable({
|
|
|
|
destroy: true,
|
|
|
|
columnDefs: [{
|
|
|
|
orderable: false,
|
|
|
|
targets: "no-sort"
|
|
|
|
}]
|
|
|
|
});
|
2016-01-17 04:59:40 +00:00
|
|
|
pagesTable.clear()
|
|
|
|
$.each(pages, function(i, page) {
|
|
|
|
pagesTable.row.add([
|
|
|
|
page.name,
|
|
|
|
moment(page.modified_date).format('MMMM Do YYYY, h:mm:ss a'),
|
|
|
|
"<div class='pull-right'><button class='btn btn-primary' data-toggle='modal' data-target='#newLandingPageModal' onclick='edit(" + i + ")'>\
|
2015-08-23 20:20:28 +00:00
|
|
|
<i class='fa fa-pencil'></i>\
|
|
|
|
</button>\
|
|
|
|
<button class='btn btn-danger' onclick='deletePage(" + i + ")'>\
|
|
|
|
<i class='fa fa-trash-o'></i>\
|
|
|
|
</button></div>"
|
2016-01-17 04:59:40 +00:00
|
|
|
]).draw()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
$("#emptyMessage").show()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.error(function() {
|
|
|
|
$("#loading").hide()
|
|
|
|
errorFlash("Error fetching pages")
|
|
|
|
})
|
2015-08-26 02:03:12 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 04:59:40 +00:00
|
|
|
$(document).ready(function() {
|
2015-08-25 23:56:58 +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-08-25 23:56:58 +00:00
|
|
|
});
|
2016-01-17 04:59:40 +00:00
|
|
|
$('.modal').on('shown.bs.modal', function(event) {
|
2015-08-25 23:56:58 +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-08-25 23:56:58 +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-08-25 23:56:58 +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-08-25 23:56:58 +00:00
|
|
|
});
|
2015-08-26 03:07:57 +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-08-26 02:03:12 +00:00
|
|
|
load()
|
2015-08-23 20:20:28 +00:00
|
|
|
})
|