admin.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. import {ModalEslDetails} from "./ModalEslDetails";
  2. import * as OMGUtils from "./OMGUtils";
  3. let modalEslDetails = null;
  4. function SaveConfig() {
  5. let config = $('#config_text').val();
  6. OMGUtils.AjaxReq(`${BASEURL}/admin/config`, {
  7. method: 'POST',
  8. body: JSON.stringify({"method": "saveconfig", "value": config})
  9. })
  10. .then(json => toastr.success(json.err))
  11. .catch(err => toastr.error(err.url, `${err.status} (${err.statusText})`));
  12. }
  13. function LoadConfig() {
  14. OMGUtils.AjaxReq(`${BASEURL}/admin/config`, {
  15. method: 'POST',
  16. body: JSON.stringify({"method": "loadconfig"})
  17. })
  18. .then(json => {
  19. $('#config_text').val(json.value);
  20. toastr.success(json.err);
  21. })
  22. .catch(err => toastr.error(err.url, `${err.status} (${err.statusText})`));
  23. }
  24. function OpenESLDetails(labelID) {
  25. modalEslDetails.OpenESLDetails(labelID);
  26. }
  27. function ESLMultiOperations(method) {
  28. let selectedLabels = $('#datatable').DataTable().rows({selected: true}).data().toArray();
  29. selectedLabels.forEach(labelID => {
  30. OMGUtils.AjaxReq(`${BASEURL}/admin/config`, {
  31. method: 'POST',
  32. body: JSON.stringify({"method": method, "labelid": labelID[1]})
  33. })
  34. .then(json => {
  35. if (json.err == 0)
  36. toastr.success(json.value);
  37. else
  38. toastr.error(json.value);
  39. })
  40. .catch(err => toastr.error(err.url, `${err.status} (${err.statusText})`));
  41. });
  42. }
  43. /**
  44. * Load html for template selector
  45. *
  46. * @param bool clearCache true clear cache
  47. */
  48. function TemplateSelector(clearCache) {
  49. $('.templateselector').on('hide.bs.modal', function () {
  50. $('.templateselector .modal-body').html('');
  51. });
  52. $('.templateselector').on('show.bs.modal', function (e) {
  53. let selectedRows = $('#datatable').DataTable().rows({selected: true}).data().toArray();
  54. let dt_columns = selectedRows.map(v => v[5]);
  55. let jsonData = JSON.stringify(dt_columns);
  56. console.log(selectedRows.length);
  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. });