diff --git a/apps/home/static/home/js/tabs/subs.js b/apps/home/static/home/js/tabs/subs.js index 5640632..b0bc943 100644 --- a/apps/home/static/home/js/tabs/subs.js +++ b/apps/home/static/home/js/tabs/subs.js @@ -1,11 +1,13 @@ const subTableId = "#subTable"; +const subModalId = "#subFormModal"; + // region Init Module function initSubscriptionsModule() { initializeDataTable( - "#subTable", + subTableId, [ { title: "Name", @@ -132,11 +134,26 @@ $(subTableId).on("click", ".edit-modal", async function() { }) async function openSubModal(id) { - $modal = $("#subFormModal"); + $modal = $(subModalId); 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 ($(this).is("select")) { + $(this).val(defaultVal).change(); + } + else { + $(this).val(defaultVal); + } + }); } else { $modal.find(".form-create").hide(); @@ -145,18 +162,53 @@ async function openSubModal(id) { let data = await ajaxRequest(`/api/subscriptions/${id}/`, "GET"); $modal.find("[data-field]").each(function() { - let key = $(this).attr("data-field"); - let value = data[key]; + 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); } - }); } $modal.modal("show"); } + +// region Load Modal Options + +$(document).on("selectedServerChange", async function() { + await loadMessageStyleOptions(); +}); + +async function loadMessageStyleOptions() { + let = $input = $(subModalId).find('[data-field="message_style"]'); + + // Disable and clear input + $input.val("").change(); + $input.prop("disabled", true); + + // Delete existing options + $input.find("option").each(function() { + if ($(this).val()) { + $(this).remove(); + } + }); + + // Load new values + const data = await ajaxRequest(`/api/message-styles/?server=${selectedServer.id}`, "GET"); + data.results.forEach(style => { + $input.append($( + "