|
| 1 | +jQuery(document).ready(function ($) { |
| 2 | + var sectionTemplate = $("#api_sections .api-section").first().clone(); |
| 3 | + var sectionCount = $("#api_sections .api-section").length; |
| 4 | + |
| 5 | + // Add new section |
| 6 | + $("#add_section").on("click", function () { |
| 7 | + var newSection = sectionTemplate.clone(); |
| 8 | + sectionCount++; |
| 9 | + |
| 10 | + newSection.find("h4").text("Section " + sectionCount); |
| 11 | + newSection.find("select, input").each(function () { |
| 12 | + var name = $(this).attr("name"); |
| 13 | + if (name) { |
| 14 | + $(this).attr( |
| 15 | + "name", |
| 16 | + name.replace("[0]", "[" + (sectionCount - 1) + "]") |
| 17 | + ); |
| 18 | + } |
| 19 | + }); |
| 20 | + |
| 21 | + // Clear the section name field |
| 22 | + newSection.find('input[name$="[name]"]').val(sectionCount); |
| 23 | + |
| 24 | + newSection.attr("data-index", sectionCount - 1); |
| 25 | + newSection.find(".remove-section").show(); |
| 26 | + |
| 27 | + $("#api_sections").append(newSection); |
| 28 | + resetRemoveButtons(); |
| 29 | + }); |
| 30 | + |
| 31 | + // Remove section |
| 32 | + $("#api_sections").on("click", ".remove-section", function () { |
| 33 | + $(this).closest(".api-section").remove(); |
| 34 | + resetSectionIndexes(); |
| 35 | + resetRemoveButtons(); |
| 36 | + }); |
| 37 | + |
| 38 | + // Reset section indexes |
| 39 | + function resetSectionIndexes() { |
| 40 | + $("#api_sections .api-section").each(function (index) { |
| 41 | + $(this) |
| 42 | + .find("h4") |
| 43 | + .text("Section " + (index + 1)); |
| 44 | + $(this).attr("data-index", index); |
| 45 | + $(this) |
| 46 | + .find("select, input") |
| 47 | + .each(function () { |
| 48 | + var name = $(this).attr("name"); |
| 49 | + if (name) { |
| 50 | + $(this).attr("name", name.replace(/\[\d+\]/, "[" + index + "]")); |
| 51 | + } |
| 52 | + }); |
| 53 | + }); |
| 54 | + sectionCount = $("#api_sections .api-section").length; |
| 55 | + } |
| 56 | + |
| 57 | + // Reset remove buttons |
| 58 | + function resetRemoveButtons() { |
| 59 | + var sections = $("#api_sections .api-section"); |
| 60 | + sections.find(".remove-section").show(); |
| 61 | + if (sections.length === 1) { |
| 62 | + sections.first().find(".remove-section").hide(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // Handle access type switch |
| 67 | + $('input[name="custom_api_access_type"]').on("change", function () { |
| 68 | + if ($(this).val() === "private") { |
| 69 | + $("#custom_api_roles_row").show(); |
| 70 | + } else { |
| 71 | + $("#custom_api_roles_row").hide(); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + // Initialize |
| 76 | + resetRemoveButtons(); |
| 77 | +}); |
0 commit comments