Project

General

Profile

1
import { Toggable } from '../mixin/index';
2
import { $, getIndex, isTouch, toJQuery, query } from '../util/index';
3

    
4
export default function (UIkit) {
5

    
6
    UIkit.component('switcher', {
7

    
8
        mixins: [Toggable],
9

    
10
        args: 'connect',
11

    
12
        props: {
13
            connect: String,
14
            toggle: String,
15
            active: Number,
16
            swiping: Boolean
17
        },
18

    
19
        defaults: {
20
            connect: false,
21
            toggle: ' > *',
22
            active: 0,
23
            swiping: true,
24
            cls: 'uk-active',
25
            clsContainer: 'uk-switcher',
26
            attrItem: 'uk-switcher-item',
27
            queued: true
28
        },
29

    
30
        connected() {
31
            this.$emitSync();
32
        },
33

    
34
        computed: {
35

    
36
            connects() {
37
                return query(this.connect, this.$el) || $(this.$el.next(`.${this.clsContainer}`));
38
            },
39

    
40
            toggles() {
41
                return $(this.toggle, this.$el);
42
            }
43

    
44
        },
45

    
46
        events: [
47

    
48
            {
49

    
50
                name: 'click',
51

    
52
                delegate() {
53
                    return `${this.toggle}:not(.uk-disabled)`;
54
                },
55

    
56
                handler(e) {
57
                    e.preventDefault();
58
                    this.show(e.currentTarget);
59
                }
60

    
61
            },
62

    
63
            {
64
                name: 'click',
65

    
66
                el() {
67
                    return this.connects;
68
                },
69

    
70
                delegate() {
71
                    return `[${this.attrItem}],[data-${this.attrItem}]`;
72
                },
73

    
74
                handler(e) {
75
                    e.preventDefault();
76
                    this.show($(e.currentTarget)[e.currentTarget.hasAttribute(this.attrItem) ? 'attr' : 'data'](this.attrItem));
77
                }
78
            },
79

    
80
            {
81
                name: 'swipeRight swipeLeft',
82

    
83
                filter() {
84
                    return this.swiping;
85
                },
86

    
87
                el() {
88
                    return this.connects;
89
                },
90

    
91
                delegate() {
92
                    return `[${this.attrItem}],[data-${this.attrItem}]`;
93
                },
94

    
95
                handler(e) {
96
                    if (!isTouch(e)) {
97
                        return;
98
                    }
99

    
100
                    e.preventDefault();
101
                    if (!window.getSelection().toString()) {
102
                        this.show(e.type === 'swipeLeft' ? 'next' : 'previous');
103
                    }
104
                }
105
            }
106

    
107
        ],
108

    
109
        update() {
110

    
111
            this.updateAria(this.connects.children());
112
            this.show(toJQuery(this.toggles.filter(`.${this.cls}:first`)) || toJQuery(this.toggles.eq(this.active)) || this.toggles.first());
113

    
114
        },
115

    
116
        methods: {
117

    
118
            show(item) {
119

    
120
                var length = this.toggles.length,
121
                    prev = this.connects.children(`.${this.cls}`).index(),
122
                    hasPrev = prev >= 0,
123
                    index = getIndex(item, this.toggles, prev),
124
                    dir = item === 'previous' ? -1 : 1,
125
                    toggle;
126

    
127
                for (var i = 0; i < length; i++, index = (index + dir + length) % length) {
128
                    if (!this.toggles.eq(index).is('.uk-disabled, [disabled]')) {
129
                        toggle = this.toggles.eq(index);
130
                        break;
131
                    }
132
                }
133

    
134
                if (!toggle || prev >= 0 && toggle.hasClass(this.cls) || prev === index) {
135
                    return;
136
                }
137

    
138
                this.toggles.removeClass(this.cls).attr('aria-expanded', false);
139
                toggle.addClass(this.cls).attr('aria-expanded', true);
140

    
141
                if (!hasPrev) {
142
                    this.toggleNow(this.connects.children(`:nth-child(${index + 1})`));
143
                } else {
144
                    this.toggleElement(this.connects.children(`:nth-child(${prev + 1}),:nth-child(${index + 1})`));
145
                }
146
            }
147

    
148
        }
149

    
150
    });
151

    
152
}
(26-26/28)