1 | 'use strict';
|
2 |
|
3 | import $ from 'jquery';
|
4 | import { Keyboard } from './foundation.util.keyboard';
|
5 | import { Motion } from './foundation.util.motion';
|
6 | import { Timer } from './foundation.util.timer';
|
7 | import { onImagesLoaded } from './foundation.util.imageLoader';
|
8 | import { GetYoDigits } from './foundation.util.core';
|
9 | import { Plugin } from './foundation.plugin';
|
10 | import { Touch } from './foundation.util.touch'
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 | class Orbit extends Plugin {
|
24 | |
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 | _setup(element, options){
|
32 | this.$element = element;
|
33 | this.options = $.extend({}, Orbit.defaults, this.$element.data(), options);
|
34 | this.className = 'Orbit';
|
35 |
|
36 | Touch.init($);
|
37 |
|
38 | this._init();
|
39 |
|
40 | Keyboard.register('Orbit', {
|
41 | 'ltr': {
|
42 | 'ARROW_RIGHT': 'next',
|
43 | 'ARROW_LEFT': 'previous'
|
44 | },
|
45 | 'rtl': {
|
46 | 'ARROW_LEFT': 'next',
|
47 | 'ARROW_RIGHT': 'previous'
|
48 | }
|
49 | });
|
50 | }
|
51 |
|
52 | |
53 |
|
54 |
|
55 |
|
56 |
|
57 | _init() {
|
58 |
|
59 | this._reset();
|
60 |
|
61 | this.$wrapper = this.$element.find(`.${this.options.containerClass}`);
|
62 | this.$slides = this.$element.find(`.${this.options.slideClass}`);
|
63 |
|
64 | var $images = this.$element.find('img'),
|
65 | initActive = this.$slides.filter('.is-active'),
|
66 | id = this.$element[0].id || GetYoDigits(6, 'orbit');
|
67 |
|
68 | this.$element.attr({
|
69 | 'data-resize': id,
|
70 | 'id': id
|
71 | });
|
72 |
|
73 | if (!initActive.length) {
|
74 | this.$slides.eq(0).addClass('is-active');
|
75 | }
|
76 |
|
77 | if (!this.options.useMUI) {
|
78 | this.$slides.addClass('no-motionui');
|
79 | }
|
80 |
|
81 | if ($images.length) {
|
82 | onImagesLoaded($images, this._prepareForOrbit.bind(this));
|
83 | } else {
|
84 | this._prepareForOrbit();
|
85 | }
|
86 |
|
87 | if (this.options.bullets) {
|
88 | this._loadBullets();
|
89 | }
|
90 |
|
91 | this._events();
|
92 |
|
93 | if (this.options.autoPlay && this.$slides.length > 1) {
|
94 | this.geoSync();
|
95 | }
|
96 |
|
97 | if (this.options.accessible) {
|
98 | this.$wrapper.attr('tabindex', 0);
|
99 | }
|
100 | }
|
101 |
|
102 | |
103 |
|
104 |
|
105 |
|
106 |
|
107 | _loadBullets() {
|
108 | this.$bullets = this.$element.find(`.${this.options.boxOfBullets}`).find('button');
|
109 | }
|
110 |
|
111 | |
112 |
|
113 |
|
114 |
|
115 | geoSync() {
|
116 | var _this = this;
|
117 | this.timer = new Timer(
|
118 | this.$element,
|
119 | {
|
120 | duration: this.options.timerDelay,
|
121 | infinite: false
|
122 | },
|
123 | function() {
|
124 | _this.changeSlide(true);
|
125 | });
|
126 | this.timer.start();
|
127 | }
|
128 |
|
129 | |
130 |
|
131 |
|
132 |
|
133 |
|
134 | _prepareForOrbit() {
|
135 | var _this = this;
|
136 | this._setWrapperHeight();
|
137 | }
|
138 |
|
139 | |
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | _setWrapperHeight(cb) {
|
146 | var max = 0, temp, counter = 0, _this = this;
|
147 |
|
148 | this.$slides.each(function() {
|
149 | temp = this.getBoundingClientRect().height;
|
150 | $(this).attr('data-slide', counter);
|
151 |
|
152 | if (_this.$slides.filter('.is-active')[0] !== _this.$slides.eq(counter)[0]) {
|
153 | $(this).css({'position': 'relative', 'display': 'none'});
|
154 | }
|
155 | max = temp > max ? temp : max;
|
156 | counter++;
|
157 | });
|
158 |
|
159 | if (counter === this.$slides.length) {
|
160 | this.$wrapper.css({'height': max});
|
161 | if(cb) {cb(max);}
|
162 | }
|
163 | }
|
164 |
|
165 | |
166 |
|
167 |
|
168 |
|
169 |
|
170 | _setSlideHeight(height) {
|
171 | this.$slides.each(function() {
|
172 | $(this).css('max-height', height);
|
173 | });
|
174 | }
|
175 |
|
176 | |
177 |
|
178 |
|
179 |
|
180 |
|
181 | _events() {
|
182 | var _this = this;
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | this.$element.off('.resizeme.zf.trigger').on({
|
190 | 'resizeme.zf.trigger': this._prepareForOrbit.bind(this)
|
191 | })
|
192 | if (this.$slides.length > 1) {
|
193 |
|
194 | if (this.options.swipe) {
|
195 | this.$slides.off('swipeleft.zf.orbit swiperight.zf.orbit')
|
196 | .on('swipeleft.zf.orbit', function(e){
|
197 | e.preventDefault();
|
198 | _this.changeSlide(true);
|
199 | }).on('swiperight.zf.orbit', function(e){
|
200 | e.preventDefault();
|
201 | _this.changeSlide(false);
|
202 | });
|
203 | }
|
204 |
|
205 |
|
206 | if (this.options.autoPlay) {
|
207 | this.$slides.on('click.zf.orbit', function() {
|
208 | _this.$element.data('clickedOn', _this.$element.data('clickedOn') ? false : true);
|
209 | _this.timer[_this.$element.data('clickedOn') ? 'pause' : 'start']();
|
210 | });
|
211 |
|
212 | if (this.options.pauseOnHover) {
|
213 | this.$element.on('mouseenter.zf.orbit', function() {
|
214 | _this.timer.pause();
|
215 | }).on('mouseleave.zf.orbit', function() {
|
216 | if (!_this.$element.data('clickedOn')) {
|
217 | _this.timer.start();
|
218 | }
|
219 | });
|
220 | }
|
221 | }
|
222 |
|
223 | if (this.options.navButtons) {
|
224 | var $controls = this.$element.find(`.${this.options.nextClass}, .${this.options.prevClass}`);
|
225 | $controls.attr('tabindex', 0)
|
226 |
|
227 | .on('click.zf.orbit touchend.zf.orbit', function(e){
|
228 | e.preventDefault();
|
229 | _this.changeSlide($(this).hasClass(_this.options.nextClass));
|
230 | });
|
231 | }
|
232 |
|
233 | if (this.options.bullets) {
|
234 | this.$bullets.on('click.zf.orbit touchend.zf.orbit', function() {
|
235 | if (/is-active/g.test(this.className)) { return false; }
|
236 | var idx = $(this).data('slide'),
|
237 | ltr = idx > _this.$slides.filter('.is-active').data('slide'),
|
238 | $slide = _this.$slides.eq(idx);
|
239 |
|
240 | _this.changeSlide(ltr, $slide, idx);
|
241 | });
|
242 | }
|
243 |
|
244 | if (this.options.accessible) {
|
245 | this.$wrapper.add(this.$bullets).on('keydown.zf.orbit', function(e) {
|
246 |
|
247 | Keyboard.handleKey(e, 'Orbit', {
|
248 | next: function() {
|
249 | _this.changeSlide(true);
|
250 | },
|
251 | previous: function() {
|
252 | _this.changeSlide(false);
|
253 | },
|
254 | handled: function() {
|
255 | if ($(e.target).is(_this.$bullets)) {
|
256 | _this.$bullets.filter('.is-active').focus();
|
257 | }
|
258 | }
|
259 | });
|
260 | });
|
261 | }
|
262 | }
|
263 | }
|
264 |
|
265 | |
266 |
|
267 |
|
268 | _reset() {
|
269 |
|
270 | if (typeof this.$slides == 'undefined') {
|
271 | return;
|
272 | }
|
273 |
|
274 | if (this.$slides.length > 1) {
|
275 |
|
276 | this.$element.off('.zf.orbit').find('*').off('.zf.orbit')
|
277 |
|
278 |
|
279 | if (this.options.autoPlay) {
|
280 | this.timer.restart();
|
281 | }
|
282 |
|
283 |
|
284 | this.$slides.each(function(el) {
|
285 | $(el).removeClass('is-active is-active is-in')
|
286 | .removeAttr('aria-live')
|
287 | .hide();
|
288 | });
|
289 |
|
290 |
|
291 | this.$slides.first().addClass('is-active').show();
|
292 |
|
293 |
|
294 | this.$element.trigger('slidechange.zf.orbit', [this.$slides.first()]);
|
295 |
|
296 |
|
297 | if (this.options.bullets) {
|
298 | this._updateBullets(0);
|
299 | }
|
300 | }
|
301 | }
|
302 |
|
303 | |
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 | changeSlide(isLTR, chosenSlide, idx) {
|
312 | if (!this.$slides) {return; }
|
313 | var $curSlide = this.$slides.filter('.is-active').eq(0);
|
314 |
|
315 | if (/mui/g.test($curSlide[0].className)) { return false; }
|
316 |
|
317 | var $firstSlide = this.$slides.first(),
|
318 | $lastSlide = this.$slides.last(),
|
319 | dirIn = isLTR ? 'Right' : 'Left',
|
320 | dirOut = isLTR ? 'Left' : 'Right',
|
321 | _this = this,
|
322 | $newSlide;
|
323 |
|
324 | if (!chosenSlide) {
|
325 | $newSlide = isLTR ?
|
326 | (this.options.infiniteWrap ? $curSlide.next(`.${this.options.slideClass}`).length ? $curSlide.next(`.${this.options.slideClass}`) : $firstSlide : $curSlide.next(`.${this.options.slideClass}`))
|
327 | :
|
328 | (this.options.infiniteWrap ? $curSlide.prev(`.${this.options.slideClass}`).length ? $curSlide.prev(`.${this.options.slideClass}`) : $lastSlide : $curSlide.prev(`.${this.options.slideClass}`));
|
329 | } else {
|
330 | $newSlide = chosenSlide;
|
331 | }
|
332 |
|
333 | if ($newSlide.length) {
|
334 | |
335 |
|
336 |
|
337 |
|
338 | this.$element.trigger('beforeslidechange.zf.orbit', [$curSlide, $newSlide]);
|
339 |
|
340 | if (this.options.bullets) {
|
341 | idx = idx || this.$slides.index($newSlide);
|
342 | this._updateBullets(idx);
|
343 | }
|
344 |
|
345 | if (this.options.useMUI && !this.$element.is(':hidden')) {
|
346 | Motion.animateIn(
|
347 | $newSlide.addClass('is-active').css({'position': 'absolute', 'top': 0}),
|
348 | this.options[`animInFrom${dirIn}`],
|
349 | function(){
|
350 | $newSlide.css({'position': 'relative', 'display': 'block'})
|
351 | .attr('aria-live', 'polite');
|
352 | });
|
353 |
|
354 | Motion.animateOut(
|
355 | $curSlide.removeClass('is-active'),
|
356 | this.options[`animOutTo${dirOut}`],
|
357 | function(){
|
358 | $curSlide.removeAttr('aria-live');
|
359 | if(_this.options.autoPlay && !_this.timer.isPaused){
|
360 | _this.timer.restart();
|
361 | }
|
362 |
|
363 | });
|
364 | } else {
|
365 | $curSlide.removeClass('is-active is-in').removeAttr('aria-live').hide();
|
366 | $newSlide.addClass('is-active is-in').attr('aria-live', 'polite').show();
|
367 | if (this.options.autoPlay && !this.timer.isPaused) {
|
368 | this.timer.restart();
|
369 | }
|
370 | }
|
371 | |
372 |
|
373 |
|
374 |
|
375 | this.$element.trigger('slidechange.zf.orbit', [$newSlide]);
|
376 | }
|
377 | }
|
378 |
|
379 | |
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 | _updateBullets(idx) {
|
386 | var $oldBullet = this.$element.find(`.${this.options.boxOfBullets}`)
|
387 | .find('.is-active').removeClass('is-active').blur(),
|
388 | span = $oldBullet.find('span:last').detach(),
|
389 | $newBullet = this.$bullets.eq(idx).addClass('is-active').append(span);
|
390 | }
|
391 |
|
392 | |
393 |
|
394 |
|
395 |
|
396 | _destroy() {
|
397 | this.$element.off('.zf.orbit').find('*').off('.zf.orbit').end().hide();
|
398 | }
|
399 | }
|
400 |
|
401 | Orbit.defaults = {
|
402 | |
403 |
|
404 |
|
405 |
|
406 |
|
407 |
|
408 | bullets: true,
|
409 | |
410 |
|
411 |
|
412 |
|
413 |
|
414 |
|
415 | navButtons: true,
|
416 | |
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 | animInFromRight: 'slide-in-right',
|
423 | |
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 | animOutToRight: 'slide-out-right',
|
430 | |
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 | animInFromLeft: 'slide-in-left',
|
438 | |
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 | animOutToLeft: 'slide-out-left',
|
445 | |
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 | autoPlay: true,
|
452 | |
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 | timerDelay: 5000,
|
459 | |
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 | infiniteWrap: true,
|
466 | |
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 | swipe: true,
|
473 | |
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 | pauseOnHover: true,
|
480 | |
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 | accessible: true,
|
487 | |
488 |
|
489 |
|
490 |
|
491 |
|
492 |
|
493 | containerClass: 'orbit-container',
|
494 | |
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 | slideClass: 'orbit-slide',
|
501 | |
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 | boxOfBullets: 'orbit-bullets',
|
508 | |
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 | nextClass: 'orbit-next',
|
515 | |
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|
521 | prevClass: 'orbit-previous',
|
522 | |
523 |
|
524 |
|
525 |
|
526 |
|
527 |
|
528 | useMUI: true
|
529 | };
|
530 |
|
531 | export {Orbit};
|