summaryrefslogtreecommitdiff
path: root/assets/js/src/Dropdown.js
blob: 20f5b56d86cdac0c7803d5f5c392fe5e907ba3b0 (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
Kanboard.Dropdown = function(app) {
    this.app = app;
};

// TODO: rewrite this code
Kanboard.Dropdown.prototype.listen = function() {
    var self = this;

    $(document).on('click', function() {
        self.close();
    });

    $(document).on('click', '.dropdown-menu', function(e) {
        e.preventDefault();
        e.stopImmediatePropagation();
        self.close();

        var submenu = $(this).next('ul');
        var offset = $(this).offset();

        // Clone the submenu outside of the column to avoid clipping issue with overflow
        $("body").append(jQuery("<div>", {"id": "dropdown"}));
        submenu.clone().appendTo("#dropdown");

        var clone = $("#dropdown ul");
        clone.addClass('dropdown-submenu-open');

        var submenuHeight = clone.outerHeight();
        var submenuWidth = clone.outerWidth();

        if (offset.top + submenuHeight - $(window).scrollTop() < $(window).height() || $(window).scrollTop() + offset.top < submenuHeight) {
            clone.css('top', offset.top + $(this).height());
        }
        else {
            clone.css('top', offset.top - submenuHeight - 5);
        }

        if (offset.left + submenuWidth > $(window).width()) {
            clone.css('left', offset.left - submenuWidth + $(this).outerWidth());
        }
        else {
            clone.css('left', offset.left);
        }

        if (document.getElementById('dropdown') !== null) {
            KB.trigger('dropdown.afterRender');
        }
    });

    $(document).on('click', '.dropdown-submenu-open li', function(e) {
    	
        if ($(e.target).is('li')) {
            KB.trigger('dropdown.clicked');

            var element = $(this).find('a:visible');

            if (element.length > 0) {
                element[0].click(); // Calling native click() not the jQuery one
            }
        }
    });
};

Kanboard.Dropdown.prototype.close = function() {
    if (document.getElementById('dropdown') !== null) {
        KB.trigger('dropdown.beforeDestroy');
    }

    $("#dropdown").remove();
};