summaryrefslogtreecommitdiffstats
path: root/asset/js/widget/SearchBar.js
blob: 276de1713cba4249ffcaee5f3f33d935805bb8ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
define(["../notjQuery"], function ($) {

    "use strict";

    class SearchBar {
        constructor(form) {
            this.form = form;
            this.filterInput = null;
        }

        bind() {
            $(this.form.parentNode).on('click', '[data-search-editor-url]', this.onOpenerClick, this);

            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;
        }

        setFilterInput(filterInput) {
            this.filterInput = filterInput;

            return this;
        }

        onOpenerClick(event) {
            let opener = event.currentTarget;
            let editorUrl = opener.dataset.searchEditorUrl;
            let filterQueryString = this.filterInput.getQueryString();
            let layout = document.getElementById('layout');

            editorUrl += (editorUrl.indexOf('?') > -1 ? '&' : '?') + filterQueryString;

            // Disable pointer events to block further function calls
            opener.style.pointerEvents = 'none';

            let observer = new MutationObserver((mutations) => {
                for (let mutation of mutations) {
                    if (mutation.type === 'childList') {
                        mutation.removedNodes.forEach((node) => {
                            // Remove the pointerEvent none style to make the button clickable again
                            // after the modal has been removed
                            if (node.id === 'modal') {
                                opener.style.pointerEvents = '';
                                observer.disconnect();
                            }
                        });
                    }
                }
            });

            observer.observe(layout, {childList: true});

            // The search editor should open in a modal. We simulate a click on an anchor
            // appropriately prepared so that Icinga Web 2 will handle it natively.
            let a = document.createElement('a');
            a.classList.add('modal-opener');
            a.href = editorUrl;
            a.dataset.noIcingaAjax = '';
            a.dataset.icingaModal = '';

            opener.parentNode.insertBefore(a, opener.nextSibling);
            a.click();
            a.remove();
        }
    }

    return SearchBar;
});