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
    var Animations;
7

    
8
    UI.component('switcher', {
9

    
10
        defaults: {
11
            connect   : false,
12
            toggle    : '>*',
13
            active    : 0,
14
            animation : false,
15
            duration  : 200,
16
            swiping   : true
17
        },
18

    
19
        animating: false,
20

    
21
        boot: function() {
22

    
23
            // init code
24
            UI.ready(function(context) {
25

    
26
                UI.$('[data-uk-switcher]', context).each(function() {
27
                    var switcher = UI.$(this);
28

    
29
                    if (!switcher.data('switcher')) {
30
                        var obj = UI.switcher(switcher, UI.Utils.options(switcher.attr('data-uk-switcher')));
31
                    }
32
                });
33
            });
34
        },
35

    
36
        init: function() {
37

    
38
            var $this = this;
39

    
40
            this.on('click.uk.switcher', this.options.toggle, function(e) {
41
                e.preventDefault();
42
                $this.show(this);
43
            });
44

    
45
            if (!this.options.connect) {
46
                return;
47
            }
48

    
49
            this.connect = UI.$(this.options.connect);
50

    
51
            if (!this.connect.length) {
52
                return;
53
            }
54

    
55
            this.connect.on('click.uk.switcher', '[data-uk-switcher-item]', function(e) {
56

    
57
                e.preventDefault();
58

    
59
                var item = UI.$(this).attr('data-uk-switcher-item');
60

    
61
                if ($this.index == item) return;
62

    
63
                switch(item) {
64
                    case 'next':
65
                    case 'previous':
66
                        $this.show($this.index + (item=='next' ? 1:-1));
67
                        break;
68
                    default:
69
                        $this.show(parseInt(item, 10));
70
                }
71
            });
72

    
73
            if (this.options.swiping) {
74

    
75
                this.connect.on('swipeRight swipeLeft', function(e) {
76
                    e.preventDefault();
77
                    if (!window.getSelection().toString()) {
78
                        $this.show($this.index + (e.type == 'swipeLeft' ? 1 : -1));
79
                    }
80
                });
81
            }
82

    
83
            this.update();
84
        },
85

    
86
        update: function() {
87

    
88
            this.connect.children().removeClass('uk-active').attr('aria-hidden', 'true');
89

    
90
            var toggles = this.find(this.options.toggle),
91
                active  = toggles.filter('.uk-active');
92

    
93
            if (active.length) {
94
                this.show(active, false);
95
            } else {
96

    
97
                if (this.options.active===false) return;
98

    
99
                active = toggles.eq(this.options.active);
100
                this.show(active.length ? active : toggles.eq(0), false);
101
            }
102

    
103
            // Init ARIA for toggles
104
            toggles.not(active).attr('aria-expanded', 'false');
105
            active.attr('aria-expanded', 'true');
106
        },
107

    
108
        show: function(tab, animate) {
109

    
110
            if (this.animating) {
111
                return;
112
            }
113

    
114
            var toggles = this.find(this.options.toggle);
115

    
116
            if (isNaN(tab)) {
117
                tab = UI.$(tab);
118
            } else {
119
                tab = tab < 0 ? toggles.length-1 : tab;
120
                tab = toggles.eq(toggles[tab] ? tab : 0);
121
            }
122

    
123
            var $this     = this,
124
                active    = UI.$(tab),
125
                animation = Animations[this.options.animation] || function(current, next) {
126

    
127
                    if (!$this.options.animation) {
128
                        return Animations.none.apply($this);
129
                    }
130

    
131
                    var anim = $this.options.animation.split(',');
132

    
133
                    if (anim.length == 1) {
134
                        anim[1] = anim[0];
135
                    }
136

    
137
                    anim[0] = anim[0].trim();
138
                    anim[1] = anim[1].trim();
139

    
140
                    return coreAnimation.apply($this, [anim, current, next]);
141
                };
142

    
143
            if (animate===false || !UI.support.animation) {
144
                animation = Animations.none;
145
            }
146

    
147
            if (active.hasClass("uk-disabled")) return;
148

    
149
            // Update ARIA for Toggles
150
            toggles.attr('aria-expanded', 'false');
151
            active.attr('aria-expanded', 'true');
152

    
153
            toggles.filter(".uk-active").removeClass("uk-active");
154
            active.addClass("uk-active");
155

    
156
            if (this.options.connect && this.connect.length) {
157

    
158
                this.index = this.find(this.options.toggle).index(active);
159

    
160
                if (this.index == -1 ) {
161
                    this.index = 0;
162
                }
163

    
164
                this.connect.each(function() {
165

    
166
                    var container = UI.$(this),
167
                        children  = UI.$(container.children()),
168
                        current   = UI.$(children.filter('.uk-active')),
169
                        next      = UI.$(children.eq($this.index));
170

    
171
                        $this.animating = true;
172

    
173
                        animation.apply($this, [current, next]).then(function(){
174

    
175
                            current.removeClass("uk-active");
176
                            next.addClass("uk-active");
177

    
178
                            // Update ARIA for connect
179
                            current.attr('aria-hidden', 'true');
180
                            next.attr('aria-hidden', 'false');
181

    
182
                            UI.Utils.checkDisplay(next, true);
183

    
184
                            $this.animating = false;
185

    
186
                        });
187
                });
188
            }
189

    
190
            this.trigger("show.uk.switcher", [active]);
191
        }
192
    });
193

    
194
    Animations = {
195

    
196
        'none': function() {
197
            var d = UI.$.Deferred();
198
            d.resolve();
199
            return d.promise();
200
        },
201

    
202
        'fade': function(current, next) {
203
            return coreAnimation.apply(this, ['uk-animation-fade', current, next]);
204
        },
205

    
206
        'slide-bottom': function(current, next) {
207
            return coreAnimation.apply(this, ['uk-animation-slide-bottom', current, next]);
208
        },
209

    
210
        'slide-top': function(current, next) {
211
            return coreAnimation.apply(this, ['uk-animation-slide-top', current, next]);
212
        },
213

    
214
        'slide-vertical': function(current, next, dir) {
215

    
216
            var anim = ['uk-animation-slide-top', 'uk-animation-slide-bottom'];
217

    
218
            if (current && current.index() > next.index()) {
219
                anim.reverse();
220
            }
221

    
222
            return coreAnimation.apply(this, [anim, current, next]);
223
        },
224

    
225
        'slide-left': function(current, next) {
226
            return coreAnimation.apply(this, ['uk-animation-slide-left', current, next]);
227
        },
228

    
229
        'slide-right': function(current, next) {
230
            return coreAnimation.apply(this, ['uk-animation-slide-right', current, next]);
231
        },
232

    
233
        'slide-horizontal': function(current, next, dir) {
234

    
235
            var anim = ['uk-animation-slide-right', 'uk-animation-slide-left'];
236

    
237
            if (current && current.index() > next.index()) {
238
                anim.reverse();
239
            }
240

    
241
            return coreAnimation.apply(this, [anim, current, next]);
242
        },
243

    
244
        'scale': function(current, next) {
245
            return coreAnimation.apply(this, ['uk-animation-scale-up', current, next]);
246
        }
247
    };
248

    
249
    UI.switcher.animations = Animations;
250

    
251

    
252
    // helpers
253

    
254
    function coreAnimation(cls, current, next) {
255

    
256
        var d = UI.$.Deferred(), clsIn = cls, clsOut = cls, release;
257

    
258
        if (next[0]===current[0]) {
259
            d.resolve();
260
            return d.promise();
261
        }
262

    
263
        if (typeof(cls) == 'object') {
264
            clsIn  = cls[0];
265
            clsOut = cls[1] || cls[0];
266
        }
267

    
268
        UI.$body.css('overflow-x', 'hidden'); // fix scroll jumping in iOS
269

    
270
        release = function() {
271

    
272
            if (current) current.hide().removeClass('uk-active '+clsOut+' uk-animation-reverse');
273

    
274
            next.addClass(clsIn).one(UI.support.animation.end, function() {
275

    
276
                setTimeout(function () {
277
                    next.removeClass(''+clsIn+'').css({opacity:'', display:''});
278
                }, 0);
279

    
280
                d.resolve();
281

    
282
                UI.$body.css('overflow-x', '');
283

    
284
                if (current) current.css({opacity:'', display:''});
285

    
286
            }.bind(this)).show();
287
        };
288

    
289
        next.css('animation-duration', this.options.duration+'ms');
290

    
291
        if (current && current.length) {
292

    
293
            current.css('animation-duration', this.options.duration+'ms');
294

    
295
            current.css('display', 'none').addClass(clsOut+' uk-animation-reverse').one(UI.support.animation.end, function() {
296
                release();
297
            }.bind(this)).css('display', '');
298

    
299
        } else {
300
            next.addClass('uk-active');
301
            release();
302
        }
303

    
304
        return d.promise();
305
    }
306

    
307
})(UIkit2);
(23-23/32)