import { Grid } from "gridjs/dist/gridjs.umd.js"; import gridjs from "gridjs/dist/gridjs.umd.js"; import "gridjs/dist/gridjs.umd.js"; import GlobalConfig, { addThousandSeparators } from "../global-config.js"; import Swal from "sweetalert2"; class DataSettings { constructor() { this.toastMessage = document.getElementById("toast-message"); this.toastElement = document.getElementById("toastNotification"); this.toast = new bootstrap.Toast(this.toastElement); this.table = null; // Initialize immediately this.init(); } /** * Initialize the DataSettings class */ init() { this.initTableDataSettings(); this.initEvents(); } /** * Get API token from meta tag * @returns {string|null} */ getApiToken() { const tokenMeta = document.querySelector('meta[name="api-token"]'); return tokenMeta ? tokenMeta.getAttribute("content") : null; } /** * Get authentication headers for API requests * @returns {object} */ getAuthHeaders() { const token = this.getApiToken(); const csrfToken = document .querySelector('meta[name="csrf-token"]') ?.getAttribute("content"); const headers = { "Content-Type": "application/json", Accept: "application/json", }; if (token) { headers["Authorization"] = `Bearer ${token}`; } if (csrfToken) { headers["X-CSRF-TOKEN"] = csrfToken; } return headers; } /** * Make API request with authentication * @param {string} url * @param {object} options * @returns {Promise} */ async makeApiRequest(url, options = {}) { const defaultOptions = { headers: this.getAuthHeaders(), ...options, }; try { const response = await fetch(url, defaultOptions); if (!response.ok) { throw new Error( `HTTP ${response.status}: ${response.statusText}` ); } return response; } catch (error) { console.error("API Request failed:", error); throw error; } } initEvents() { document.body.addEventListener("click", async (event) => { const deleteButton = event.target.closest( ".btn-delete-data-settings" ); if (deleteButton) { event.preventDefault(); await this.handleDelete(deleteButton); } }); } initTableDataSettings() { let tableContainer = document.getElementById("table-data-settings"); tableContainer.innerHTML = ""; let canUpdate = tableContainer.getAttribute("data-updater") === "1"; let canDelete = tableContainer.getAttribute("data-destroyer") === "1"; let menuId = tableContainer.getAttribute("data-menuId"); // Create a new Grid.js instance this.table = new Grid({ columns: [ "ID", "Key", "Value", "Created", { name: "Actions", width: "120px", formatter: function (cell) { let buttons = ""; if (canUpdate) { buttons += ` `; } if (canDelete) { buttons += ` `; } if (!canUpdate && !canDelete) { buttons = `No Privilege`; } return gridjs.html( `