Project

General

Profile

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

    
5
    if (window.UIkit2) {
6
        component = addon(UIkit2);
7
    }
8

    
9
    if (typeof define == 'function' && define.amd) {
10
        define('uikit-accordion', ['uikit'], function(){
11
            return component || addon(UIkit2);
12
        });
13
    }
14
})(function(UI){
15

    
16
    "use strict";
17

    
18
    UI.component('accordion', {
19

    
20
        defaults: {
21
            showfirst  : true,
22
            collapse   : true,
23
            animate    : true,
24
            easing     : 'swing',
25
            duration   : 300,
26
            toggle     : '.uk-accordion-title',
27
            containers : '.uk-accordion-content',
28
            clsactive  : 'uk-active'
29
        },
30

    
31
        boot:  function() {
32

    
33
            // init code
34
            UI.ready(function(context) {
35

    
36
                setTimeout(function(){
37

    
38
                    UI.$('[data-uk-accordion]', context).each(function(){
39

    
40
                        var ele = UI.$(this);
41

    
42
                        if (!ele.data('accordion')) {
43
                            UI.accordion(ele, UI.Utils.options(ele.attr('data-uk-accordion')));
44
                        }
45
                    });
46

    
47
                }, 0);
48
            });
49
        },
50

    
51
        init: function() {
52

    
53
            var $this = this;
54

    
55
            this.element.on('click.uk.accordion', this.options.toggle, function(e) {
56

    
57
                e.preventDefault();
58

    
59
                $this.toggleItem(UI.$(this).data('wrapper'), $this.options.animate, $this.options.collapse);
60
            });
61

    
62
            this.update(true);
63

    
64
            UI.domObserve(this.element, function(e) {
65
                if ($this.element.children($this.options.containers).length) {
66
                    $this.update();
67
                }
68
            });
69
        },
70

    
71
        toggleItem: function(wrapper, animated, collapse) {
72

    
73
            var $this = this;
74

    
75
            wrapper.data('toggle').toggleClass(this.options.clsactive);
76
            wrapper.data('content').toggleClass(this.options.clsactive);
77

    
78
            var active = wrapper.data('toggle').hasClass(this.options.clsactive);
79

    
80
            if (collapse) {
81
                this.toggle.not(wrapper.data('toggle')).removeClass(this.options.clsactive);
82
                this.content.not(wrapper.data('content')).removeClass(this.options.clsactive)
83
                    .parent().stop().css('overflow', 'hidden').animate({ height: 0 }, {easing: this.options.easing, duration: animated ? this.options.duration : 0}).attr('aria-expanded', 'false');
84
            }
85

    
86
            wrapper.stop().css('overflow', 'hidden');
87

    
88
            if (animated) {
89

    
90
                wrapper.animate({ height: active ? getHeight(wrapper.data('content')) : 0 }, {easing: this.options.easing, duration: this.options.duration, complete: function() {
91

    
92
                    if (active) {
93
                        wrapper.css({'overflow': '', 'height': 'auto'});
94
                        UI.Utils.checkDisplay(wrapper.data('content'));
95
                    }
96

    
97
                    $this.trigger('display.uk.check');
98
                }});
99

    
100
            } else {
101

    
102
                wrapper.height(active ? 'auto' : 0);
103

    
104
                if (active) {
105
                    wrapper.css({'overflow': ''});
106
                    UI.Utils.checkDisplay(wrapper.data('content'));
107
                }
108

    
109
                this.trigger('display.uk.check');
110
            }
111

    
112
            // Update ARIA
113
            wrapper.attr('aria-expanded', active);
114

    
115
            this.element.trigger('toggle.uk.accordion', [active, wrapper.data('toggle'), wrapper.data('content')]);
116
        },
117

    
118
        update: function(init) {
119

    
120
            var $this = this, $content, $wrapper, $toggle;
121

    
122
            this.toggle = this.find(this.options.toggle);
123
            this.content = this.find(this.options.containers);
124

    
125
            this.content.each(function(index) {
126

    
127
                $content = UI.$(this);
128

    
129
                if ($content.parent().data('wrapper')) {
130
                    $wrapper = $content.parent();
131
                } else {
132
                    $wrapper = UI.$(this).wrap('<div data-wrapper="true" style="overflow:hidden;height:0;position:relative;"></div>').parent();
133

    
134
                    // Init ARIA
135
                    $wrapper.attr('aria-expanded', 'false');
136
                }
137

    
138
                $toggle = $this.toggle.eq(index);
139

    
140
                $wrapper.data('toggle', $toggle);
141
                $wrapper.data('content', $content);
142
                $toggle.data('wrapper', $wrapper);
143
                $content.data('wrapper', $wrapper);
144
            });
145

    
146
            this.element.trigger('update.uk.accordion', [this]);
147

    
148
            if (init && this.options.showfirst) {
149
                this.toggleItem(this.toggle.eq(0).data('wrapper'), false, false);
150
            }
151
        }
152

    
153
    });
154

    
155
    // helper
156

    
157
    function getHeight(ele) {
158

    
159
        var $ele = UI.$(ele), height = "auto";
160

    
161
        if ($ele.is(":visible")) {
162
            height = $ele.outerHeight();
163
        } else {
164

    
165
            var tmp = {
166
                position   : $ele.css('position'),
167
                visibility : $ele.css('visibility'),
168
                display    : $ele.css('display')
169
            };
170

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

    
173
            $ele.css(tmp); // reset element
174
        }
175

    
176
        return height;
177
    }
178

    
179
    return UI.accordion;
180
});
(1-1/46)