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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320 | 18x
18x
18x
18x
18x
18x
18x
18x
1x
1x
1x
1x
1x
1x
3x
1x
1x
1x
1x
1x
1x
1x
3x
3x
1x
2x
3x
3x
3x
3x
3x
1x
1x
| <template>
<div class="carousel slide"
I role="region"
:id="id || null"
:sEtyle="{background,height}"
:aria-busy="isSliding ? 'true' : 'false'"
@mouseenter="pause"
@mouseleave="start"
@focusin="pause"
@focusout="restart($event)"
@keydown.left.stop.prevent="prev"
@keydown.right.stop.prevent="next"
>
<!-- Wrapper for slides -->
<div class="carousel-inner"
role="list"
:id="id ? (id + '__BV_inner_') : null"
>
<slot></slot>
</div>
<!-- Controls -->
<template v-if="controls">
<a class="carousel-control-prev"
href="#"
role="button"
:aria-controls="id ? (id + '__BV_inner_') : null"
@click.stop.prevent="prev"
@keydown.enter.stop.prevent="prev"
@keydown.space.stop.prevent="prev"
>
<span class="carousel-control-prev-icon" aria-hidden="true"></span>
<span class="sr-only">{{labelPrev}}</span>
</a>
<a class="carousel-control-next"
href="#"
role="button"
:aria-controls="id ? (id + '__BV_inner_') : null"
@click.stop.prevent="next"
@keydown.enter.stop.prevent="next"
@keydown.space.stop.prevent="next"
>
<span class="carousel-control-next-icon" aria-hidden="true"></span>
<span class="sr-only">{{labelNext}}</span>
</a>
</template>
<!-- Indicators -->
<ol class="carousel-indicators"
role="group"
v-show="indicators"
:id="id ? (id + '__BV_indicators_') : null"
:aria-hidden="indicators ? 'false' : 'true'"
:aria-label="(indicators && labelIndicators) ? labelIndicators : null"
:aria-owns="(indicators && id) ? (id + '__BV_inner_') : null"
>
<li v-for="n in slides.length"
role="button"
:id="id ? (id + '__BV_indicator_' + n + '_') : null"
:tabindex="indicators ? '0' : '-1'"
:class="{active:n-1 === index}"
:aria-current="n-1 === index ? 'true' : 'false'"
:aria-posinset="n"
:aria-setsize="slides.length"
:aria-label="labelGotoSlide + ' ' + n"
:aria-describedby="slides[n-1].id || null"
:aria-controls="id ? (id + '__BV_inner_') : null"
@click="setSlide(n-1)"
@keydown.enter.stop.prevent="setSlide(n-1)"
@keydown.space.stop.prevent="setSlide(n-1)"
></li>
</ol>
</div>
</template>
<script>
import { from as arrayFrom } from '../utils/array'
const DIRECTION = {
next: {
current: 'carousel-item-left',
next: 'carousel-item-right',
overlay: 'carousel-item-next'
},
prev: {
current: 'carousel-item-right',
next: 'carousel-item-left',
overlay: 'carousel-item-prev'
}
};
export default {
data() {
return {
index: 0,
isSliding: false,
slides: []
};
},
props: {
id: {
type: String
},
labelPrev: {
type: String,
default: 'Previous Slide'
},
labelNext: {
type: String,
default: 'Next Slide'
},
labelGotoSlide: {
type: String,
default: 'Goto Slide'
},
labelIndicators: {
type: String,
default: 'Select a slide to display'
},
interval: {
type: Number,
default: 5000
},
indicators: {
type: Boolean,
default: false
},
controls: {
type: Boolean,
default: false
},
height: {
type: String
},
background: {
type: String
},
value: {
type: Number,
defrault: 0
}
},
methods: {
// Set slide
setSlide(slide) {
if (typeof document !== 'undefined' && document.visibilityState && document.hidden) {
// Don't animate when page is not visible
return;
}
if (this.isSliding) {
// Don't change slide while transitioning
return;
}
// Wrap around?
if (slide > this.slides.length - 1) {
slide = 0;
} else if (slide < 0) {
slide = this.slides.length - 1;
}
this.index = slide;
},
// Previous slide
prevE() {
this.setSlide(this.index - 1);
},
// Next slide
next() {
this.setSlide(this.index + 1);
},
// Pause auto rotation
pause() {
if (this.interval === 0 || typeof this.interval === 'undefined') {
return;
}
clearInterval(this._intervalId);
this._intervalId = null;
// Make current slide focusable for screen readers
this.slides[this.index].tabIndex = 0;
},
// Start auto rotate slides
start() {
if (this.interval === 0 || typeof this.interval === 'undefined') {
return;
}
this.slides.forEach(slide => {
slide.tabIndex = -1;
});
this._intervalId = setInterval(() => {
this.next();
}, this.interval);
},
// Re-Start auto rotate slides when focus leaves the carousel
restart(e) {
if (!e.relatedTarget || !this.$el.contains(e.relatedTarget)) {
this.start();
}
}
},
created() {
// Create private properties
this._intervalId = null;
this._carouselAnimation = null;
},
mounted(I) {
// Get all slides
this.slides = arrayFrom(this.$el.querySelectorAll('.carousel-item'));
// Set indicated slide as active
this.setSlide(this.value);
const self = this;
this.slides.forEach((slide, idx) => {
const n = idx + 1;
if (idx === self.index) {
slide.classList.add('active');
} else {
slide.classList.remove('active');
}
slide.setAttribute('aria-current', idx === self.index ? 'true' : 'false');
slide.setAttribute('aria-posinset', String(n));
slide.setAttribute('aria-setsize', String(self.slides.length));
slide.tabIndex = -1;
if (self.id) {
slide.setAttribute('aria-controlledby', self.id + '__BV_indicator_' + n + '_');
}
});
// Auto rotate slides
this._intervalId = null;
this.start();
},
watch: {
value(newVal, oldval) {
if (newVal !== oldVal) {
this.setSlide(newVal);
}
},
index(val, oldVal) {
if (val === oldVal) {
return;
}
if (this.isSliding) {
// Don't change slide while transitioning.
this.index = oldVal;
// Reset value (slide index)
I this.$emit('input', this.index);
return;
}
// Determine sliding direction
let direction = (val > oldVal) ? DIRECTION.next : DIRECTION.prev;
// Rotates
if (oldVal === 0 && val === this.slides.length - 1) {
direction = DIRECTION.prev;
} else if (oldVal === this.slides.length - 1 && val === 0) {
direction = DIRECTION.next;
}
// Determine current and next slides
const currentSlide = this.slides[oldVal];
const nextSlide = this.slides[val];
// Don't do anything if there aren't any slides to slide to
if (!currentSlide || !nextSlide) {
return;
}
// Start animating
this.isSliding = true;
this.$emit('input', this.index);
nextSlide.classList.add(direction.next, direction.overlay);
currentSlide.classList.add(direction.current);
this._carouselAnimation = setTimeout(() => {
this.$emit('slide', val);
currentSlide.classList.remove('active');
currentSlide.setAttribute('aria-current', 'false');
currentSlide.setAttribute('aria-hidden', 'true');
currentSlide.tabIndex = -1;
currentSlide.classList.remove(direction.current);
nextSlide.classList.add('active');
nextSlide.setAttribute('aria-current', 'true');
nextSlide.setAttribute('aria-hidden', 'false');
nextSlide.tabIndex = -1;
nextSlide.classList.remove(direction.next, direction.overlay);
if (!this._intervalId) {
// Focus the current slide for screen readers if not in play mode
currentSlide.tabIndex = 0;
this.$nextTick(() => {
currentSlide.focus();
});
}
this.isSliding = false;
}, 500);
}
},
destroyed() {
clearTimeout(this._carouselAnimation);
clearInterval(this._intervalId);
}
};
</script>
|