Project

General

Profile

1
/*! UIkit 2.27.5 | http://www.getuikit.com | (c) 2014 YOOtheme | MIT License */
2
(function(UI) {
3

    
4
    "use strict";
5

    
6
    UI.component('nav', {
7

    
8
        defaults: {
9
            toggle: '>li.uk-parent > a[href="#"]',
10
            lists: '>li.uk-parent > ul',
11
            multiple: false
12
        },
13

    
14
        boot: function() {
15

    
16
            // init code
17
            UI.ready(function(context) {
18

    
19
                UI.$('[data-uk-nav]', context).each(function() {
20
                    var nav = UI.$(this);
21

    
22
                    if (!nav.data('nav')) {
23
                        var obj = UI.nav(nav, UI.Utils.options(nav.attr('data-uk-nav')));
24
                    }
25
                });
26
            });
27
        },
28

    
29
        init: function() {
30

    
31
            var $this = this;
32

    
33
            this.on('click.uk.nav', this.options.toggle, function(e) {
34
                e.preventDefault();
35
                var ele = UI.$(this);
36
                $this.open(ele.parent()[0] == $this.element[0] ? ele : ele.parent("li"));
37
            });
38

    
39
            this.update();
40

    
41
            UI.domObserve(this.element, function(e) {
42
                if ($this.element.find($this.options.lists).not('[role]').length) {
43
                    $this.update();
44
                }
45
            });
46
        },
47

    
48
        update: function() {
49

    
50
            var $this = this;
51

    
52
            this.find(this.options.lists).each(function() {
53

    
54
                var $ele   = UI.$(this).attr('role', 'menu'),
55
                    parent = $ele.closest('li'),
56
                    active = parent.hasClass("uk-active");
57

    
58
                if (!parent.data('list-container')) {
59
                    $ele.wrap('<div style="overflow:hidden;height:0;position:relative;"></div>');
60
                    parent.data('list-container', $ele.parent()[active ? 'removeClass':'addClass']('uk-hidden'));
61
                }
62

    
63
                // Init ARIA
64
                parent.attr('aria-expanded', parent.hasClass("uk-open"));
65

    
66
                if (active) $this.open(parent, true);
67
            });
68
        },
69

    
70
        open: function(li, noanimation) {
71

    
72
            var $this = this, element = this.element, $li = UI.$(li), $container = $li.data('list-container');
73

    
74
            if (!this.options.multiple) {
75

    
76
                element.children('.uk-open').not(li).each(function() {
77

    
78
                    var ele = UI.$(this);
79

    
80
                    if (ele.data('list-container')) {
81
                        ele.data('list-container').stop().animate({height: 0}, function() {
82
                            UI.$(this).parent().removeClass('uk-open').end().addClass('uk-hidden');
83
                        });
84
                    }
85
                });
86
            }
87

    
88
            $li.toggleClass('uk-open');
89

    
90
            // Update ARIA
91
            $li.attr('aria-expanded', $li.hasClass('uk-open'));
92

    
93
            if ($container) {
94

    
95
                if ($li.hasClass('uk-open')) {
96
                    $container.removeClass('uk-hidden');
97
                }
98

    
99
                if (noanimation) {
100

    
101
                    $container.stop().height($li.hasClass('uk-open') ? 'auto' : 0);
102

    
103
                    if (!$li.hasClass('uk-open')) {
104
                        $container.addClass('uk-hidden');
105
                    }
106

    
107
                    this.trigger('display.uk.check');
108

    
109
                } else {
110

    
111
                    $container.stop().animate({
112
                        height: ($li.hasClass('uk-open') ? getHeight($container.find('ul:first')) : 0)
113
                    }, function() {
114

    
115
                        if (!$li.hasClass('uk-open')) {
116
                            $container.addClass('uk-hidden');
117
                        } else {
118
                            $container.css('height', '');
119
                        }
120

    
121
                        $this.trigger('display.uk.check');
122
                    });
123
                }
124
            }
125
        }
126
    });
127

    
128

    
129
    // helper
130

    
131
    function getHeight(ele) {
132

    
133
        var $ele = UI.$(ele), height = 'auto';
134

    
135
        if ($ele.is(':visible')) {
136
            height = $ele.outerHeight();
137
        } else {
138

    
139
            var tmp = {
140
                position: $ele.css('position'),
141
                visibility: $ele.css('visibility'),
142
                display: $ele.css('display')
143
            };
144

    
145
            height = $ele.css({position: 'absolute', visibility: 'hidden', display: 'block'}).outerHeight();
146

    
147
            $ele.css(tmp); // reset element
148
        }
149

    
150
        return height;
151
    }
152

    
153
})(UIkit2);
(15-15/32)