-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathcarousel.js
133 lines (120 loc) · 4.29 KB
/
carousel.js
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
/**
* Patterns carousel
*
* Copyright 2017 Syslab.com GmbH Alexander Pilz
*/
import "regenerator-runtime/runtime"; // needed for ``await`` support
import $ from "jquery";
import Base from "../../core/base";
import logging from "../../core/logging";
import Parser from "../../core/parser";
const log = logging.getLogger("pat.carousel");
export const parser = new Parser("carousel");
parser.addArgument("auto-play", false);
parser.addArgument("auto-play-speed", 1000);
parser.addArgument("speed", 500);
parser.addArgument("height", "fixed");
parser.addArgument("arrows", "show");
parser.addArgument("slides-to-show", 1);
parser.addArgument("slides-to-scroll", 1);
parser.addArgument("dots", "show");
parser.addArgument("append-dots", "");
parser.addArgument("infinite", false);
export default Base.extend({
name: "carousel",
trigger: ".pat-carousel",
async init() {
if (window.__patternslib_import_styles) {
import("slick-carousel/slick/slick.scss");
}
await import("slick-carousel");
const ImagesLoaded = (await import("imagesloaded")).default;
this.options = parser.parse(this.el, this.options);
this.settings = {
autoplay: this.options.auto.play,
autoplaySpeed: this.options.auto["play-speed"],
speed: this.options.speed,
adaptiveHeight: this.options.height === "adaptive",
arrows: this.options.arrows === "show",
slidesToShow: this.options.slides["to-show"],
slidesToScroll: this.options.slides["to-scroll"],
dots: this.options.dots === "show",
infinite: this.options.infinite,
};
if (this.options.appendDots) {
this.settings.appendDots = this.options.appendDots;
}
ImagesLoaded(this.el, () => this.setup());
},
setup() {
const $carousel = $(this.el).slick(this.settings);
let $panel_links = $();
$carousel
.children()
.each((index, obj) => {
if (!obj.id) {
return;
}
var $links = $("a[href=#" + obj.id + "]");
// TODO: fix this.
// eslint-disable-next-line no-undef
if (index === control.currentPage) {
$links.addClass("current");
} else {
$links.removeClass("current");
}
$links.on(
"click.pat-carousel",
null,
// TODO: fix this.
// eslint-disable-next-line no-undef
{ control: control, index: index },
this.onPanelLinkClick.bind(this)
);
$panel_links = $panel_links.add($links);
})
.end()
.on(
"slide_complete.pat-carousel",
null,
$panel_links,
this.onSlideComplete.bind(this)
);
},
_loadPanelImages(slider, page) {
let $img;
log.info("Loading lazy images on panel " + page);
slider.$items
.eq(page)
.find("img")
.addBack()
.filter("[data-src]")
.each((idx, img) => {
$img = $(img);
this.src = $img.attr("data-src");
$img.removeAttr("data-src");
});
},
onPanelLinkClick: function (event) {
event.data.control.gotoPage(event.data.index, false);
event.preventDefault();
},
onInitialized: function (event, slider) {
this._loadPanelImages(slider, slider.options.startPanel);
this._loadPanelImages(slider, slider.options.startPanel + 1);
this._loadPanelImages(slider, 0);
this._loadPanelImages(slider, slider.pages + 1);
},
onSlideInit: function (event, slider) {
this._loadPanelImages(slider, slider.targetPage);
},
onSlideComplete: function (event, slider) {
var $panel_links = event.data;
$panel_links.removeClass("current");
if (slider.$targetPage[0].id)
$panel_links
.filter("[href=#" + slider.$targetPage[0].id + "]")
.addClass("current");
this._loadPanelImages(slider, slider.targetPage + 1);
},
});