ModalTemplateSelector.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. import {AjaxReq} from "./OMGUtils";
  2. // Private Methods
  3. /**
  4. * Change display of ESL labels
  5. *
  6. * @param arrLabels
  7. * @param idTemplate
  8. */
  9. function SetTemplate(arrLabels, idTemplate) {
  10. AjaxReq(`${BASEURL}/admin/config`, {
  11. method: 'POST',
  12. body: JSON.stringify({
  13. "method": "SetDisplay",
  14. "labels": arrLabels,
  15. "id": idTemplate
  16. })
  17. }).fetchPromise()
  18. .then(json => {
  19. $('.templateselector').modal('hide');
  20. toastr.success(json.err);
  21. })
  22. .catch(err => toastr.error(err.url, `${err.status} (${err.statusText})`));
  23. }
  24. /**
  25. * Load html for template selector
  26. *
  27. * @param bool clearCache true clear cache
  28. */
  29. function TemplateSelector(clearCache) {
  30. let AbortLoadTemplate;
  31. $('.templateselector').on('hide.bs.modal', function () {
  32. $('#spinnerloader').addClass('d-none');
  33. AbortLoadTemplate();
  34. $('.templateselector .modal-body').html('');
  35. });
  36. $('.templateselector').on('show.bs.modal', function (e) {
  37. let selectedRows = $('#datatable').DataTable().rows({selected: true}).data().toArray();
  38. let selectedLabels = selectedRows.map(v => v[COL_ID_IDX]);
  39. let resolutions = selectedRows.map(v => v[COL_RESOLUTION_IDX]);
  40. if (selectedRows.length == 0) {
  41. toastr.warning('Nessuna label selezionata');
  42. e.preventDefault();
  43. e.stopImmediatePropagation();
  44. return true;
  45. }
  46. $('#spinnerloader').removeClass('d-none');
  47. let req = AjaxReq(`${BASEURL}/admin/config`, {
  48. method: 'POST',
  49. body: JSON.stringify({
  50. "method": 'TemplateSelector',
  51. "clearCache": clearCache,
  52. "resolution": resolutions
  53. })
  54. });
  55. AbortLoadTemplate = req.abort;
  56. req.fetchPromise()
  57. .then(json => {
  58. if (json.err == 0) {
  59. $('.templateselector .modal-body').html(json.value);
  60. // Add onlick event to all template thumbs
  61. $('.templateselector a').on('click', function () {
  62. SetTemplate(selectedLabels, this.id);
  63. });
  64. } else
  65. toastr.error(json.value);
  66. })
  67. .catch(err => toastr.error(err.url, `${err.status} (${err.statusText})`))
  68. .finally(() => $('#spinnerloader').addClass('d-none'));
  69. });
  70. $('.templateselector').modal('show');
  71. }
  72. // MAIN CLASS
  73. export class ModalTemplateSelector {
  74. constructor(modalClass) {
  75. let self = this;
  76. this.abort = null;
  77. this.modalClass = modalClass;
  78. this.timer = null;
  79. $(modalClass).on('show.bs.modal', function () {
  80. });
  81. $(modalClass).on('hide.bs.modal', function () {
  82. self.abort();
  83. clearTimeout(self.timer);
  84. });
  85. }
  86. OpenESLDetails(labelID) {
  87. $(`${this.modalClass}`).modal('show');
  88. }
  89. }