import {AjaxReq} from "./OMGUtils"; // Private Methods /** * Change display of ESL labels * * @param arrLabels * @param idTemplate */ function SetTemplate(arrLabels, idTemplate) { AjaxReq(`${BASEURL}/admin/config`, { method: 'POST', body: JSON.stringify({ "method": "SetDisplay", "labels": arrLabels, "id": idTemplate }) }).fetchPromise() .then(json => { $('.templateselector').modal('hide'); toastr.success(json.err); }) .catch(err => toastr.error(err.url, `${err.status} (${err.statusText})`)); } /** * Load html for template selector * * @param bool clearCache true clear cache */ function TemplateSelector(clearCache) { let AbortLoadTemplate; $('.templateselector').on('hide.bs.modal', function () { $('#spinnerloader').addClass('d-none'); AbortLoadTemplate(); $('.templateselector .modal-body').html(''); }); $('.templateselector').on('show.bs.modal', function (e) { let selectedRows = $('#datatable').DataTable().rows({selected: true}).data().toArray(); let selectedLabels = selectedRows.map(v => v[COL_ID_IDX]); let resolutions = selectedRows.map(v => v[COL_RESOLUTION_IDX]); if (selectedRows.length == 0) { toastr.warning('Nessuna label selezionata'); e.preventDefault(); e.stopImmediatePropagation(); return true; } $('#spinnerloader').removeClass('d-none'); let req = AjaxReq(`${BASEURL}/admin/config`, { method: 'POST', body: JSON.stringify({ "method": 'TemplateSelector', "clearCache": clearCache, "resolution": resolutions }) }); AbortLoadTemplate = req.abort; req.fetchPromise() .then(json => { if (json.err == 0) { $('.templateselector .modal-body').html(json.value); // Add onlick event to all template thumbs $('.templateselector a').on('click', function () { SetTemplate(selectedLabels, this.id); }); } else toastr.error(json.value); }) .catch(err => toastr.error(err.url, `${err.status} (${err.statusText})`)) .finally(() => $('#spinnerloader').addClass('d-none')); }); $('.templateselector').modal('show'); } // MAIN CLASS export class ModalTemplateSelector { constructor(modalClass) { let self = this; this.abort = null; this.modalClass = modalClass; this.timer = null; $(modalClass).on('show.bs.modal', function () { }); $(modalClass).on('hide.bs.modal', function () { self.abort(); clearTimeout(self.timer); }); } OpenESLDetails(labelID) { $(`${this.modalClass}`).modal('show'); } }