@@ -5103,8 +5103,49 @@ async function editServer(serverId) {
51035103 );
51045104 }
51055105
5106+ // Set associated resources data attribute on the container
5107+ const editResourcesContainer = document.getElementById(
5108+ "edit-server-resources",
5109+ );
5110+ if (editResourcesContainer && server.associatedResources) {
5111+ editResourcesContainer.setAttribute(
5112+ "data-server-resources",
5113+ JSON.stringify(server.associatedResources),
5114+ );
5115+ }
5116+
5117+ // Set associated prompts data attribute on the container
5118+ const editPromptsContainer = document.getElementById(
5119+ "edit-server-prompts",
5120+ );
5121+ if (editPromptsContainer && server.associatedPrompts) {
5122+ editPromptsContainer.setAttribute(
5123+ "data-server-prompts",
5124+ JSON.stringify(server.associatedPrompts),
5125+ );
5126+ }
5127+
51065128 openModal("server-edit-modal");
51075129
5130+ // Initialize the select handlers for resources and prompts in the edit modal
5131+ initResourceSelect(
5132+ "edit-server-resources",
5133+ "selectedEditResourcesPills",
5134+ "selectedEditResourcesWarning",
5135+ 6,
5136+ "selectAllEditResourcesBtn",
5137+ "clearAllEditResourcesBtn",
5138+ );
5139+
5140+ initPromptSelect(
5141+ "edit-server-prompts",
5142+ "selectedEditPromptsPills",
5143+ "selectedEditPromptsWarning",
5144+ 6,
5145+ "selectAllEditPromptsBtn",
5146+ "clearAllEditPromptsBtn",
5147+ );
5148+
51085149 // Use multiple approaches to ensure checkboxes get set
51095150 setEditServerAssociations(server);
51105151 setTimeout(() => setEditServerAssociations(server), 100);
@@ -5452,6 +5493,213 @@ if (window.htmx && !window._toolsHtmxHandlerAttached) {
54525493 });
54535494}
54545495
5496+ // Set up HTMX handler for auto-checking newly loaded resources when Select All is active
5497+ if (window.htmx && !window._resourcesHtmxHandlerAttached) {
5498+ window._resourcesHtmxHandlerAttached = true;
5499+
5500+ window.htmx.on("htmx:afterSettle", function (evt) {
5501+ // Only handle resource pagination requests
5502+ if (
5503+ evt.detail.pathInfo &&
5504+ evt.detail.pathInfo.requestPath &&
5505+ evt.detail.pathInfo.requestPath.includes("/admin/resources/partial")
5506+ ) {
5507+ setTimeout(() => {
5508+ // Find the container
5509+ let container = null;
5510+ const target = evt.detail.target;
5511+
5512+ if (target && target.id === "edit-server-resources") {
5513+ container = target;
5514+ } else if (target && target.id === "associatedResources") {
5515+ container = target;
5516+ } else if (target) {
5517+ container =
5518+ target.closest("#associatedResources") ||
5519+ target.closest("#edit-server-resources");
5520+ }
5521+
5522+ if (!container) {
5523+ const editModal =
5524+ document.getElementById("server-edit-modal");
5525+ const isEditModalOpen =
5526+ editModal && !editModal.classList.contains("hidden");
5527+
5528+ if (isEditModalOpen) {
5529+ container = document.getElementById(
5530+ "edit-server-resources",
5531+ );
5532+ } else {
5533+ container = document.getElementById(
5534+ "associatedResources",
5535+ );
5536+ }
5537+ }
5538+
5539+ if (container) {
5540+ const newCheckboxes = container.querySelectorAll(
5541+ "input[data-auto-check=true]",
5542+ );
5543+
5544+ const selectAllInput = container.querySelector(
5545+ 'input[name="selectAllResources"]',
5546+ );
5547+
5548+ // Check if Select All is active
5549+ if (selectAllInput && selectAllInput.value === "true") {
5550+ newCheckboxes.forEach((cb) => {
5551+ cb.checked = true;
5552+ cb.removeAttribute("data-auto-check");
5553+ });
5554+
5555+ if (newCheckboxes.length > 0) {
5556+ const event = new Event("change", {
5557+ bubbles: true,
5558+ });
5559+ container.dispatchEvent(event);
5560+ }
5561+ }
5562+
5563+ // Also check for edit mode: pre-select items based on server's associated resources
5564+ const dataAttr = container.getAttribute(
5565+ "data-server-resources",
5566+ );
5567+ if (dataAttr) {
5568+ try {
5569+ const associatedResourceIds = JSON.parse(dataAttr);
5570+ newCheckboxes.forEach((cb) => {
5571+ const checkboxValue = parseInt(cb.value);
5572+ if (
5573+ associatedResourceIds.includes(
5574+ checkboxValue,
5575+ )
5576+ ) {
5577+ cb.checked = true;
5578+ }
5579+ cb.removeAttribute("data-auto-check");
5580+ });
5581+
5582+ if (newCheckboxes.length > 0) {
5583+ const event = new Event("change", {
5584+ bubbles: true,
5585+ });
5586+ container.dispatchEvent(event);
5587+ }
5588+ } catch (e) {
5589+ console.error(
5590+ "Error parsing data-server-resources:",
5591+ e,
5592+ );
5593+ }
5594+ }
5595+ }
5596+ }, 10);
5597+ }
5598+ });
5599+ }
5600+
5601+ // Set up HTMX handler for auto-checking newly loaded prompts when Select All is active
5602+ if (window.htmx && !window._promptsHtmxHandlerAttached) {
5603+ window._promptsHtmxHandlerAttached = true;
5604+
5605+ window.htmx.on("htmx:afterSettle", function (evt) {
5606+ // Only handle prompt pagination requests
5607+ if (
5608+ evt.detail.pathInfo &&
5609+ evt.detail.pathInfo.requestPath &&
5610+ evt.detail.pathInfo.requestPath.includes("/admin/prompts/partial")
5611+ ) {
5612+ setTimeout(() => {
5613+ // Find the container
5614+ let container = null;
5615+ const target = evt.detail.target;
5616+
5617+ if (target && target.id === "edit-server-prompts") {
5618+ container = target;
5619+ } else if (target && target.id === "associatedPrompts") {
5620+ container = target;
5621+ } else if (target) {
5622+ container =
5623+ target.closest("#associatedPrompts") ||
5624+ target.closest("#edit-server-prompts");
5625+ }
5626+
5627+ if (!container) {
5628+ const editModal =
5629+ document.getElementById("server-edit-modal");
5630+ const isEditModalOpen =
5631+ editModal && !editModal.classList.contains("hidden");
5632+
5633+ if (isEditModalOpen) {
5634+ container = document.getElementById(
5635+ "edit-server-prompts",
5636+ );
5637+ } else {
5638+ container =
5639+ document.getElementById("associatedPrompts");
5640+ }
5641+ }
5642+
5643+ if (container) {
5644+ const newCheckboxes = container.querySelectorAll(
5645+ "input[data-auto-check=true]",
5646+ );
5647+
5648+ const selectAllInput = container.querySelector(
5649+ 'input[name="selectAllPrompts"]',
5650+ );
5651+
5652+ // Check if Select All is active
5653+ if (selectAllInput && selectAllInput.value === "true") {
5654+ newCheckboxes.forEach((cb) => {
5655+ cb.checked = true;
5656+ cb.removeAttribute("data-auto-check");
5657+ });
5658+
5659+ if (newCheckboxes.length > 0) {
5660+ const event = new Event("change", {
5661+ bubbles: true,
5662+ });
5663+ container.dispatchEvent(event);
5664+ }
5665+ }
5666+
5667+ // Also check for edit mode: pre-select items based on server's associated prompts
5668+ const dataAttr = container.getAttribute(
5669+ "data-server-prompts",
5670+ );
5671+ if (dataAttr) {
5672+ try {
5673+ const associatedPromptIds = JSON.parse(dataAttr);
5674+ newCheckboxes.forEach((cb) => {
5675+ const checkboxValue = parseInt(cb.value);
5676+ if (
5677+ associatedPromptIds.includes(checkboxValue)
5678+ ) {
5679+ cb.checked = true;
5680+ }
5681+ cb.removeAttribute("data-auto-check");
5682+ });
5683+
5684+ if (newCheckboxes.length > 0) {
5685+ const event = new Event("change", {
5686+ bubbles: true,
5687+ });
5688+ container.dispatchEvent(event);
5689+ }
5690+ } catch (e) {
5691+ console.error(
5692+ "Error parsing data-server-prompts:",
5693+ e,
5694+ );
5695+ }
5696+ }
5697+ }
5698+ }, 10);
5699+ }
5700+ });
5701+ }
5702+
54555703// ===================================================================
54565704// ENHANCED TAB HANDLING with Better Error Management
54575705// ===================================================================
0 commit comments