From 4ce65d59ca91871cfd126497158200a818720bce Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 13 Apr 2024 13:30:08 +0200 Subject: Adding upstream version 0.13.1. Signed-off-by: Daniel Baumann --- asset/js/widget/SearchEditor.js | 79 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 asset/js/widget/SearchEditor.js (limited to 'asset/js/widget/SearchEditor.js') diff --git a/asset/js/widget/SearchEditor.js b/asset/js/widget/SearchEditor.js new file mode 100644 index 0000000..b8235f0 --- /dev/null +++ b/asset/js/widget/SearchEditor.js @@ -0,0 +1,79 @@ +define(["../notjQuery", "../vendor/Sortable"], function ($, Sortable) { + + "use strict"; + + class SearchEditor { + constructor(form) { + this.form = form; + } + + bind() { + $(this.form).on('end', this.onRuleDropped, this); + + this.form.querySelectorAll('ol').forEach(sortable => { + let options = { + scroll: true, + group: 'rules', + direction: 'vertical', + invertSwap: true, + handle: '.drag-initiator' + }; + + Sortable.create(sortable, options); + }); + + return this; + } + + refresh(form) { + if (form === this.form) { + // If the DOM node is still the same, nothing has changed + return; + } + + this.form = form; + this.bind(); + } + + destroy() { + this.form = null; + this.filterInput = null; + } + + onRuleDropped(event) { + if (event.to === event.from && event.newIndex === event.oldIndex) { + // The user dropped the rule at its previous position + return; + } + + let placement = 'before'; + let neighbour = event.to.querySelector(':scope > :nth-child(' + (event.newIndex + 2) + ')'); + if (! neighbour) { + // User dropped the rule at the end of a group + placement = 'after'; + neighbour = event.to.querySelector(':scope > :nth-child(' + event.newIndex + ')') + if (! neighbour) { + // User dropped the rule into an empty group + placement = 'to'; + neighbour = event.to.closest('[id]'); + } + } + + // It's a submit element, the very first one, otherwise Icinga Web 2 sends another "structural-change" + this.form.insertBefore( + $.render( + '' + ), + this.form.firstChild + ); + this.form.insertBefore( + $.render(''), + this.form.firstChild + ); + + $(this.form).trigger('submit'); + } + } + + return SearchEditor; +}); -- cgit v1.2.3