aboutsummaryrefslogtreecommitdiff
path: root/resources/assets/javascripts/lib/navigation_shrinker.js
blob: dde86186d3dadaf91c1d04b038769eb1941e32fb (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
import Cookie from './cookie.js';

// Enable shrinking of navigation
var shrinker = function() {
    var main = $('#barTopMenu'),
        sink = $('li.overflow', main),
        x = 0,
        index = false,
        total = 0;
    if (main.length === 0 || sink.length === 0) {
        return;
    }

    // Reset sink (hide and lose all content)
    main.removeClass('overflown');
    $('> label > a', sink).removeAttr('data-badge');
    $('li', sink)
        .remove()
        .insertBefore(sink);

    if ($('html').is('.responsive-display')) {
        return;
    }

    $('li:not(.overflow)', main).each(function(idx) {
        var this_x = $(this).position().left;
        if (this_x > x) {
            x = this_x;
        } else {
            index = idx;
            return false;
        }
    });

    if (index !== false) {
        $('li:not(.overflow)', main)
            .slice(index - 2)
            .detach()
            .prependTo($('ul', sink))
            .each(function() {
                total += parseInt($('a', this).data().badge, 10) || 0;
            });

        main.addClass('overflown');
        $('> label > a', sink).attr('data-badge', total);
    }

    Cookie.set('navigation-length', main.children(':not(.overflow)').length, 30);
};

export default shrinker;