admin.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import $ from '../../../shared/jquery.js';
  2. import toastr from 'toastr';
  3. import Waves from 'node-waves';
  4. import * as OMGUtils from './OMGUtils';
  5. window.toastr = toastr;
  6. window.LoadConfig = loadConfig;
  7. window.SaveConfig = saveConfig;
  8. window.ToggleSelectAll = toggleSelectAll;
  9. const CONFIG_ENDPOINT = `${BASEURL}/admin/config`;
  10. const LOAD_CONFIGURATION_METHOD = 'loadconfig';
  11. const SAVE_CONFIGURATION_METHOD = 'saveconfig';
  12. /**
  13. * Displays a request failure in the admin interface.
  14. *
  15. * @param {unknown} error Request error.
  16. * @returns {void}
  17. * @sideEffects Displays a toast notification.
  18. */
  19. function showRequestError(error) {
  20. const message = error instanceof Response
  21. ? `${error.status} (${error.statusText})`
  22. : 'Unexpected request error';
  23. console.error('Admin configuration request failed.', error);
  24. toastr.error(message);
  25. }
  26. /**
  27. * Saves the configuration currently displayed in the editor.
  28. *
  29. * @returns {void}
  30. * @sideEffects Performs a network request and displays a toast notification.
  31. */
  32. function saveConfig() {
  33. OMGUtils.AjaxReq(CONFIG_ENDPOINT, {
  34. method: 'POST',
  35. body: JSON.stringify({ method: SAVE_CONFIGURATION_METHOD, value: $('#config_text').val() }),
  36. }).fetchPromise()
  37. .then(response => toastr.success(response.err))
  38. .catch(showRequestError);
  39. }
  40. /**
  41. * Loads the current configuration into the editor.
  42. *
  43. * @returns {void}
  44. * @sideEffects Performs a network request and updates the configuration editor.
  45. */
  46. function loadConfig() {
  47. OMGUtils.AjaxReq(CONFIG_ENDPOINT, {
  48. method: 'POST',
  49. body: JSON.stringify({ method: LOAD_CONFIGURATION_METHOD }),
  50. }).fetchPromise()
  51. .then(response => {
  52. $('#config_text').val(response.value);
  53. toastr.success(response.err);
  54. })
  55. .catch(showRequestError);
  56. }
  57. /**
  58. * Selects or deselects all rows in a DataTable.
  59. *
  60. * @param {string} selector DataTable selector.
  61. * @param {boolean} shouldSelect Whether rows should be selected.
  62. * @returns {void}
  63. * @sideEffects Changes the DataTable selection.
  64. */
  65. function toggleSelectAll(selector, shouldSelect) {
  66. const rows = $(selector).DataTable().rows({ page: 'current' });
  67. if (shouldSelect) {
  68. rows.select();
  69. return;
  70. }
  71. rows.deselect();
  72. }
  73. /**
  74. * Initializes the admin interface once the document is ready.
  75. *
  76. * @returns {void}
  77. * @sideEffects Initializes third-party UI widgets and exposes legacy window functions.
  78. */
  79. function initializeAdmin() {
  80. Waves.attach('.btn', ['waves-effect', 'waves-light']);
  81. Waves.init();
  82. if (!$.fn.dataTable || !$('#datatable').length) {
  83. return;
  84. }
  85. $.fn.dataTable.ext.order['dom-checkbox'] = function (settings, column) {
  86. return this.api().column(column, { order: 'index' }).nodes().map(function (cell) {
  87. return $(cell).closest('tr').hasClass('selected') ? '1' : '0';
  88. });
  89. };
  90. $('#datatable').DataTable({
  91. paging: false,
  92. scrollY: true,
  93. responsive: true,
  94. initComplete: function () {
  95. $('.dt-scroll-body').slimscroll({
  96. color: '#DDDDDD',
  97. railVisible: true,
  98. railColor: 'black',
  99. railOpacity: 0.3,
  100. height: '50vh',
  101. });
  102. },
  103. columnDefs: [
  104. { targets: 0, className: 'select-checkbox', orderDataType: 'dom-checkbox' },
  105. { targets: -1, orderable: false },
  106. ],
  107. select: { style: 'multi', selector: 'td:first-child' },
  108. });
  109. }
  110. document.addEventListener('DOMContentLoaded', initializeAdmin);