summaryrefslogtreecommitdiff
path: root/assets/js/src/Dropdown.js
blob: 20fca8a7fc94aa0f766177c8666798907ab5d8c4 (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
function Dropdown() {
}

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 submenuHeight = 240;
        var offset = $(this).offset();
        var height = $(this).height();

        // 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.css('left', offset.left);

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

        clone.addClass('dropdown-submenu-open');
    });

    $(document).on('click', '.dropdown-submenu-open li', function(e) {
        if ($(e.target).is('li')) {
            $(this).find('a:visible')[0].click(); // Calling native click() not the jQuery one
        }
    });
};

Dropdown.prototype.close = function() {
    $("#dropdown").remove();
};