summaryrefslogtreecommitdiffstats
path: root/public/js/module.js
blob: 067842149b5cd99a77f21ea851a19470b1d36e2d (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*! Icinga Web 2 | (c) 2016 Icinga Development Team | GPLv2+ */

;(function(Icinga) {

    var EventDB = function(module) {
        this.module = module;
        this.initialize();
    };

    EventDB.prototype = {
        initialize: function() {
            this.module.on('rendered', this.enableCopyable);
            this.module.on('submit', 'form.severity-filter-form', this.severitySubmit);

            var addCSSRule = function(sheet, selector, rules, index) {
                if('insertRule' in sheet) {
                    sheet.insertRule(selector + '{' + rules + '}', index);
                } else if('addRule' in sheet) {
                    sheet.addRule(selector, rules, index);
                } else {
                    this.module.icinga.logger.debug('Can\'t insert CSS rule');
                }
            };

            var sheet = (function() {
                var style = document.createElement('style');
                // WebKit hack
                style.appendChild(document.createTextNode(''));
                document.head.appendChild(style);
                return style.sheet;
            })();

            addCSSRule(
                sheet,
                '#layout.twocols.wide-layout #col1.module-eventdb, #layout.twocols.wide-layout #col1.module-eventdb ~ #col2',
                'width: 50%',
                0
            );
        },
        enableCopyable: function() {
            var e = this;
            $('.copyable').each(function() {
                var $button = $('<a>')
                    .attr('href', '#')
                    .addClass('action-link icon icon-globe copyable-button')
                    .text('Copy text');

                $button.on('click', function() {
                    var $el = $(this).parent().siblings('.copyable');
                    if ($el) {
                        e.selectText($el[0]);
                        document.execCommand('copy');
                        setTimeout(function () {
                            e.clearSelection()
                        }, 500);
                        if (icinga) {
                            icinga.loader.createNotice('info', 'Text copied to clipboard')
                        }
                    }
                });

                var $div = $('<div>').addClass('copyable-actions').append($button);

                $(this).before($div);
            });
        },
        selectText: function (text) {
            var doc = document, range, selection;
            if (doc.body.createTextRange) {
                range = document.body.createTextRange();
                range.moveToElementText(text);
                range.select();
            } else if (window.getSelection) {
                selection = window.getSelection();
                range = document.createRange();
                range.selectNodeContents(text);
                selection.removeAllRanges();
                selection.addRange(range);
            }
        },
        clearSelection: function() {
            if (document.selection) {
                document.selection.empty();
            } else if (window.getSelection) {
                window.getSelection().removeAllRanges();
            }
        },
        severitySubmit: function(ev) {
            $(ev.currentTarget)
                .find('input[type=submit]')
                .prop('disabled', true)
                .addClass('disabled');
        }
    };

    Icinga.availableModules.eventdb = EventDB;
}(Icinga));