import $ from '../../../shared/jquery.js'; import toastr from 'toastr'; import Waves from 'node-waves'; import * as OMGUtils from './OMGUtils'; window.toastr = toastr; window.LoadConfig = loadConfig; window.SaveConfig = saveConfig; window.ToggleSelectAll = toggleSelectAll; const CONFIG_ENDPOINT = `${BASEURL}/admin/config`; const LOAD_CONFIGURATION_METHOD = 'loadconfig'; const SAVE_CONFIGURATION_METHOD = 'saveconfig'; /** * Displays a request failure in the admin interface. * * @param {unknown} error Request error. * @returns {void} * @sideEffects Displays a toast notification. */ function showRequestError(error) { const message = error instanceof Response ? `${error.status} (${error.statusText})` : 'Unexpected request error'; console.error('Admin configuration request failed.', error); toastr.error(message); } /** * Saves the configuration currently displayed in the editor. * * @returns {void} * @sideEffects Performs a network request and displays a toast notification. */ function saveConfig() { OMGUtils.AjaxReq(CONFIG_ENDPOINT, { method: 'POST', body: JSON.stringify({ method: SAVE_CONFIGURATION_METHOD, value: $('#config_text').val() }), }).fetchPromise() .then(response => toastr.success(response.err)) .catch(showRequestError); } /** * Loads the current configuration into the editor. * * @returns {void} * @sideEffects Performs a network request and updates the configuration editor. */ function loadConfig() { OMGUtils.AjaxReq(CONFIG_ENDPOINT, { method: 'POST', body: JSON.stringify({ method: LOAD_CONFIGURATION_METHOD }), }).fetchPromise() .then(response => { $('#config_text').val(response.value); toastr.success(response.err); }) .catch(showRequestError); } /** * Selects or deselects all rows in a DataTable. * * @param {string} selector DataTable selector. * @param {boolean} shouldSelect Whether rows should be selected. * @returns {void} * @sideEffects Changes the DataTable selection. */ function toggleSelectAll(selector, shouldSelect) { const rows = $(selector).DataTable().rows({ page: 'current' }); if (shouldSelect) { rows.select(); return; } rows.deselect(); } /** * Initializes the admin interface once the document is ready. * * @returns {void} * @sideEffects Initializes third-party UI widgets and exposes legacy window functions. */ function initializeAdmin() { Waves.attach('.btn', ['waves-effect', 'waves-light']); Waves.init(); if (!$.fn.dataTable || !$('#datatable').length) { return; } $.fn.dataTable.ext.order['dom-checkbox'] = function (settings, column) { return this.api().column(column, { order: 'index' }).nodes().map(function (cell) { return $(cell).closest('tr').hasClass('selected') ? '1' : '0'; }); }; $('#datatable').DataTable({ paging: false, scrollY: true, responsive: true, initComplete: function () { $('.dt-scroll-body').slimscroll({ color: '#DDDDDD', railVisible: true, railColor: 'black', railOpacity: 0.3, height: '50vh', }); }, columnDefs: [ { targets: 0, className: 'select-checkbox', orderDataType: 'dom-checkbox' }, { targets: -1, orderable: false }, ], select: { style: 'multi', selector: 'td:first-child' }, }); } document.addEventListener('DOMContentLoaded', initializeAdmin);