From 8ef9c9d9ddd8f6772739e5fcf66e5701281d7ca8 Mon Sep 17 00:00:00 2001 From: Corban-Lee Jones Date: Sun, 29 Sep 2024 19:06:55 +0100 Subject: [PATCH] style form & table layout --- apps/home/static/home/js/tabs/styles.js | 150 ++++++++++++++++-- .../home/templates/home/modals/editStyle.html | 98 ++++++++++++ 2 files changed, 235 insertions(+), 13 deletions(-) create mode 100644 apps/home/templates/home/modals/editStyle.html diff --git a/apps/home/static/home/js/tabs/styles.js b/apps/home/static/home/js/tabs/styles.js index be64ca8..2ca5d97 100644 --- a/apps/home/static/home/js/tabs/styles.js +++ b/apps/home/static/home/js/tabs/styles.js @@ -1,4 +1,5 @@ const styleTableId = "#styleTable"; +const styleModalId = "#styleFormModal"; // region Init Module @@ -9,31 +10,65 @@ function initMessageStylesModule() { [ { title: "Name", - data: "name" + data: "name", + render: function(data) { + const name = sanitise(data); + return ``; + } }, { - title: "Is Embed?", - data: "is_embed" + title: "Is Embed", + data: "is_embed", + className: "text-center", + render: function(data) { + const iconClass = data ? "bi-check-circle-fill text-success" : "bi-x-circle-fill text-danger"; + return ``; + } }, { - title: "Is Hyperlinked?", - data: "is_hyperlinked" + title: "Is Hyperlinked", + data: "is_hyperlinked", + className: "text-center", + render: function(data) { + const iconClass = data ? "bi-check-circle-fill text-success" : "bi-x-circle-fill text-danger"; + return ``; + } }, { - title: "Show Author?", - data: "show_author" + title: "Show Author", + data: "show_author", + className: "text-center", + render: function(data) { + const iconClass = data ? "bi-check-circle-fill text-success" : "bi-x-circle-fill text-danger"; + return ``; + } }, { - title: "Show Timestamp?", - data: "show_timestamp" + title: "Show Timestamp", + data: "show_timestamp", + className: "text-center", + render: function(data) { + const iconClass = data ? "bi-check-circle-fill text-success" : "bi-x-circle-fill text-danger"; + return ``; + } }, { - title: "Show Images?", - data: "show_images" + title: "Show Images", + data: "show_images", + className: "text-center", + render: function(data) { + const iconClass = data ? "bi-check-circle-fill text-success" : "bi-x-circle-fill text-danger"; + return ``; + } }, { - title: "Fetch Images?", - data: "fetch_images" + title: "Fetch Images", + data: "fetch_images", + className: "text-center", + render: function(data) { + const iconClass = data ? "bi-check-circle-fill text-success" : "bi-x-circle-fill text-danger"; + return ``; + } }, { title: "Title Mutator", @@ -66,3 +101,92 @@ async function loadMessageStyleData() { setTableFilter(styleTableId, "server", selectedServer.id); await loadTableData(styleTableId, "/api/message-styles/", "GET"); } + + +// region New/Edit Modal + +$(styleTableId).closest(".js-tableBody").siblings(".js-tableFilters").on("click", ".table-new-btn", async function() { + await openStyleModal(-1); +}); + +$(styleTableId).on("click", ".edit-modal", async function() { + let id = $(styleTableId).DataTable().row($(this).closest("tr")).data().id; + await openStyleModal(id); +}); + +async function openStyleModal(id) { + $modal = $(styleModalId); + + if (parseInt(id) === -1) { + $modal.find(".form-create").show(); + $modal.find(".form-edit").hide(); + + $modal.find("[data-field]").each(function() { + const type = $(this).attr("type"); + const defaultVal = $(this).attr("data-default") || ""; + + if (type === "checkbox") { + $(this).prop("checked", defaultVal === "true"); + } + else if (type === "datetime-local") { + $(this).val(getCurrentDateTime()); + } + else { + $(this).val(defaultVal).change(); + } + }); + } + else { + $modal.find(".form-create").hide(); + $modal.find(".form-edit").show(); + + const data = await ajaxRequest(`/api/message-styles/${id}/`, "GET"); + + $modal.find("[data-field]").each(function() { + const key = $(this).attr("data-field"); + const value = data[key]; + + if (typeof value === "boolean") { + $(this).prop("checked", value); + } + else if (isISODateTimeString(value)) { + $(this).val(value.split('+')[0].substring(0, 16)); + } + else { + $(this).val(value).change(); + } + }); + } + + $modal.modal("show"); +} + + +// region Load Mutator Options + +$(document).ready(async function() { + await loadMutatorOptions(); +}); + +async function loadMutatorOptions() { + let $inputs = $(styleModalId).find('[data-field="title_mutator"], [data-field="description_mutator"]'); + + $inputs.val("").change(); + $inputs.prop("disabled", true); + + $inputs.find("option").each(function() { + if ($(this).val()) { + $(this).remove(); + } + }); + + const data = await ajaxRequest("/api/message-mutators/?page_size=25", "GET"); + data.results.forEach(mutator => { + $inputs.append($( + "