admin.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import {ModalEslDetails} from "./ModalEslDetails";
  2. import * as OMGUtils from "./OMGUtils";
  3. const DT_COLUMN = 5;
  4. let modalEslDetails = null;
  5. function SaveConfig() {
  6. let config = $('#config_text').val();
  7. OMGUtils.AjaxReq(`${BASEURL}/admin/config`, {
  8. method: 'POST',
  9. body: JSON.stringify({"method": "saveconfig", "value": config})
  10. })
  11. .then(json => toastr.success(json.err))
  12. .catch(err => toastr.error(err.url, `${err.status} (${err.statusText})`));
  13. }
  14. function LoadConfig() {
  15. OMGUtils.AjaxReq(`${BASEURL}/admin/config`, {
  16. method: 'POST',
  17. body: JSON.stringify({"method": "loadconfig"})
  18. })
  19. .then(json => {
  20. $('#config_text').val(json.value);
  21. toastr.success(json.err);
  22. })
  23. .catch(err => toastr.error(err.url, `${err.status} (${err.statusText})`));
  24. }
  25. function OpenESLDetails(labelID) {
  26. modalEslDetails.OpenESLDetails(labelID);
  27. }
  28. function ESLMultiOperations(method) {
  29. let selectedLabels = $('#datatable').DataTable().rows({selected: true}).data().toArray();
  30. selectedLabels.forEach(labelID => {
  31. OMGUtils.AjaxReq(`${BASEURL}/admin/config`, {
  32. method: 'POST',
  33. body: JSON.stringify({"method": method, "labelid": labelID[1]})
  34. })
  35. .then(json => {
  36. if (json.err == 0)
  37. toastr.success(json.value);
  38. else
  39. toastr.error(json.value);
  40. })
  41. .catch(err => toastr.error(err.url, `${err.status} (${err.statusText})`));
  42. });
  43. }
  44. /**
  45. * Load html for template selector
  46. *
  47. * @param bool clearCache true clear cache
  48. */
  49. function TemplateSelector(clearCache) {
  50. $('.templateselector').on('hide.bs.modal', function () {
  51. $('.templateselector .modal-body').html('');
  52. });
  53. $('.templateselector').on('show.bs.modal', function (e) {
  54. let selectedRows = $('#datatable').DataTable().rows({selected: true}).data().toArray();
  55. let dt_columns = selectedRows.map(v => v[DT_COLUMN]);
  56. let jsonData = JSON.stringify(dt_columns);
  57. if (selectedRows.length == 0) {
  58. toastr.warning('Nessuna label selezionata');
  59. e.preventDefault();
  60. e.stopImmediatePropagation();
  61. return true;
  62. }
  63. $('#spinnerloader').removeClass('d-none');
  64. OMGUtils.AjaxReq(`${BASEURL}/admin/commands`, {
  65. method: 'POST',
  66. body: JSON.stringify({"method": 'TemplateSelector', "clearCache": clearCache, "dt": jsonData})
  67. })
  68. .then(json => {
  69. if (json.err == 0)
  70. $('.templateselector .modal-body').html(json.value);
  71. else
  72. toastr.error(json.value);
  73. })
  74. .catch(err => toastr.error(err.url, `${err.status} (${err.statusText})`))
  75. .finally(() => $('#spinnerloader').addClass('d-none'));
  76. });
  77. $('.templateselector').modal('show');
  78. }
  79. // MAIN JS
  80. document.addEventListener('DOMContentLoaded', function () {
  81. modalEslDetails = new ModalEslDetails('.statusmodal');
  82. Waves.attach('.btn', ['waves-effect', 'waves-light']);
  83. Waves.init();
  84. $.fn.dataTable.ext.order['dom-checkbox'] = function (settings, col) {
  85. return this.api().column(col, {order: 'index'}).nodes().map(function (td, i) {
  86. return $(td).closest('tr').hasClass('selected') ? '1' : '0';
  87. });
  88. };
  89. // DataTable fix
  90. $("#datatable").DataTable({
  91. paging: false,
  92. scrollY: true,
  93. responsive: true,
  94. fnInitComplete: function () {
  95. $(".dataTables_scrollBody").slimscroll({
  96. color: "#DDDDDD",
  97. railVisible: true,
  98. railColor: "black",
  99. railOpacity: .3,
  100. height: "50vh"
  101. })
  102. },
  103. columnDefs: [
  104. {
  105. targets: 0,
  106. className: 'select-checkbox',
  107. orderDataType: "dom-checkbox"
  108. },
  109. {
  110. targets: -1,
  111. orderable: false
  112. }
  113. ],
  114. select: {
  115. style: 'multi',
  116. selector: 'td:first-child',
  117. }
  118. });
  119. window.OpenESLDetails = OpenESLDetails;
  120. window.LoadConfig = LoadConfig;
  121. window.SaveConfig = SaveConfig;
  122. window.ESLMultiOperations = ESLMultiOperations;
  123. window.TemplateSelector = TemplateSelector;
  124. });