ModalTemplateSelector.js 2.2 KB

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