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 `${name}`; + } }, { - 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($( + "", + {text: mutator.name, value: mutator.id} + )); + }); + + $inputs.prop("disabled", false); +} diff --git a/apps/home/templates/home/modals/editStyle.html b/apps/home/templates/home/modals/editStyle.html new file mode 100644 index 0000000..0ec1ac8 --- /dev/null +++ b/apps/home/templates/home/modals/editStyle.html @@ -0,0 +1,98 @@ + + + + + + + Add + Edit + Message Style + + + + + + + Name + + Human-readable name for this entry. + + + + + + Use an Embed + + Display in an Embed, instead of plain text. + + + + + Hyperlink the Title + + Click the title to go to the url. + + + + + Show the Author + + Show the content author if possible. + + + + + Show the Publish Date + + Show when the content was published. + + + + + Show any Images + + Show any found images. + + + + + Fetch missing Images + + If images aren't found, try to fetch them. + + + + + Title Mutator + + ----- + + Modify the title in fun ways. + + + + + Description Mutator + + ----- + + Modify the description in fun ways. + + + + + + + + + \ No newline at end of file