window.widget_tophosts_form = new class { init({templateid}) { this._form = document.getElementById('widget-dialogue-form'); this._templateid = templateid; this._list_columns = document.getElementById('list_columns'); this.initSortable(this._list_columns); this._list_columns.addEventListener('click', (e) => this.processColumnsAction(e)); } initSortable(element) { const is_disabled = element.querySelectorAll('tr.sortable').length < 2; $(element).sortable({ disabled: is_disabled, items: 'tbody tr.sortable', axis: 'y', containment: 'parent', cursor: 'grabbing', handle: 'div.', tolerance: 'pointer', opacity: 0.6, helper: function(e, ui) { for (let td of ui.find('>td')) { let $td = $(td); $td.attr('width', $td.width()) } return ui; }, stop: function(e, ui) { ui.item.find('>td').removeAttr('width'); ui.item.removeAttr('style'); }, start: function(e, ui) { $(ui.placeholder).height($(ui.helper).height()); } }); for (const drag_icon of element.querySelectorAll('div.')) { drag_icon.classList.toggle('', is_disabled); } } processColumnsAction(e) { const target = e.target; let column_popup; switch (target.getAttribute('name')) { case 'add': this._column_index = this._list_columns.querySelectorAll('tr').length; column_popup = PopUp('widget.tophosts.column.edit', {templateid: this._templateid}).$dialogue[0]; column_popup.addEventListener('dialogue.submit', (e) => this.updateColumns(e)); column_popup.addEventListener('dialogue.close', this.removeColorpicker); break; case 'edit': const form_fields = getFormFields(this._form); this._column_index = target.closest('tr').querySelector('[name="sortorder[columns][]"]').value; column_popup = PopUp('widget.tophosts.column.edit', {...form_fields.columns[this._column_index], edit: 1, templateid: this._templateid}).$dialogue[0]; column_popup.addEventListener('dialogue.submit', (e) => this.updateColumns(e)); column_popup.addEventListener('dialogue.close', this.removeColorpicker); break; case 'remove': target.closest('tr').remove(); ZABBIX.Dashboard.reloadWidgetProperties(); break; } } updateColumns(e) { const data = e.detail; const input = document.createElement('input'); input.setAttribute('type', 'hidden'); if (data.edit) { this._list_columns.querySelectorAll(`[name^="columns[${this._column_index}][`) .forEach((node) => node.remove()); delete data.edit; } else { input.setAttribute('name', `sortorder[columns][]`); input.setAttribute('value', this._column_index); this._form.appendChild(input.cloneNode()); } if (data.thresholds) { for (const [key, value] of Object.entries(data.thresholds)) { input.setAttribute('name', `columns[${this._column_index}][thresholds][${key}][color]`); input.setAttribute('value', value.color); this._form.appendChild(input.cloneNode()); input.setAttribute('name', `columns[${this._column_index}][thresholds][${key}][threshold]`); input.setAttribute('value', value.threshold); this._form.appendChild(input.cloneNode()); } delete data.thresholds; } for (const [key, value] of Object.entries(data)) { input.setAttribute('name', `columns[${this._column_index}][${key}]`); input.setAttribute('value', value); this._form.appendChild(input.cloneNode()); } ZABBIX.Dashboard.reloadWidgetProperties(); } // Need to remove function after sub-popups auto close. removeColorpicker() { $('#color_picker').hide(); } };