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

/**
 * Icinga.Behavior.Detach
 *
 * Detaches DOM elements before an auto-refresh and attaches them back afterwards
 */
(function(Icinga, $) {

    'use strict';

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

    Detach.prototype = new Icinga.EventListener();

    /**
     * Mutates the HTML before it is placed in the DOM after a reload
     *
     * @param   content     {string}    The content to be rendered
     * @param   $container  {jQuery}    The target container
     * @param   action      {string}    The URL that caused the reload
     * @param   autorefresh {bool}      Whether the rendering is due to an auto-refresh
     *
     * @return  {string|null}           The content to be rendered or null, when nothing should be changed
     */
    Detach.prototype.renderHook = function(content, $container, action, autorefresh) {
        // Exit early
        if (! autorefresh) {
            return content;
        } else {
            var containerId = $container.attr('id');

            if (containerId === 'menu' || containerId === 'application-state') {
                return content;
            }
        }

        if (! $container.find('.detach:first').length) {
            return content;
        }

        var $content = $('<div></div>').append(content);
        var icinga = this.icinga;

        $content.find('.detach').each(function() {
            // Selector only works w/ IDs because it was initially built to work w/ absolute paths only
            var $detachTarget = $(this);
            var detachTargetId = $detachTarget.attr('id');
            if (detachTargetId === undefined) {
                return;
            }

            var selector = '#' + detachTargetId + ':first';
            var $detachSource = $container.find(selector);

            if ($detachSource.length) {
                icinga.logger.debug('Detaching ' + selector);
                $detachSource.detach();
                $detachTarget.replaceWith($detachSource);
                $detachTarget.remove();
            }
        });

        return $content.html();
    };

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

    Icinga.Behaviors.Detach = Detach;

}) (Icinga, jQuery);