summaryrefslogtreecommitdiffstats
path: root/public/js/icinga/behavior/filtereditor.js
blob: ffcad01106e9e198094b1faeb944db361f667d7c (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
/* Icinga Web 2 | (c) 2018 Icinga Development Team | GPLv2+ */

/**
 * Icinga.Behavior.FilterEditor
 *
 * Initially expanded, but collapsable subtrees
 */
(function(Icinga, $) {

    'use strict';

    var containerId = /^col(\d+)$/;
    var filterEditors = {};

    function FilterEditor(icinga) {
        Icinga.EventListener.call(this, icinga);

        this.on('beforerender', '#main > .container', this.beforeRender, this);
        this.on('rendered', '#main > .container', this.onRendered, this);
    }

    FilterEditor.prototype = new Icinga.EventListener();

    FilterEditor.prototype.beforeRender = function(event) {
        if (event.currentTarget !== event.target) {
            // Nested containers are ignored
            return;
        }

        var $container = $(event.target);
        var match = containerId.exec($container.attr('id'));

        if (match !== null) {
            var id = match[1];
            var subTrees = {};
            filterEditors[id] = subTrees;

            $container.find('.tree .handle').each(function () {
                var $li = $(this).closest('li');

                subTrees[$li.find('select').first().attr('name')] = $li.hasClass('collapsed');
            });
        }
    };

    FilterEditor.prototype.onRendered = function(event) {
        if (event.currentTarget !== event.target) {
            // Nested containers are ignored
            return;
        }

        var $container = $(event.target);
        var match = containerId.exec($container.attr('id'));

        if (match !== null) {
            var id = match[1];

            if (typeof filterEditors[id] !== "undefined") {
                var subTrees = filterEditors[id];
                delete filterEditors[id];

                $container.find('.tree .handle').each(function () {
                    var $li = $(this).closest('li');
                    var name = $li.find('select').first().attr('name');
                    if (typeof subTrees[name] !== "undefined" && subTrees[name] !== $li.hasClass('collapsed')) {
                        $li.toggleClass('collapsed');
                    }
                });
            }
        }
    };

    Icinga.Behaviors = Icinga.Behaviors || {};

    Icinga.Behaviors.FilterEditor = FilterEditor;

}) (Icinga, jQuery);