-
Notifications
You must be signed in to change notification settings - Fork 0
/
slideshow.js
277 lines (259 loc) · 8.99 KB
/
slideshow.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
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
const currentScript = document.currentScript;
const workerPath = currentScript.src.replace("slideshow.js", "parseAlbum.js");
function createSlideshow(el, options) {
var $slideshows = document.querySelectorAll(el), // a collection of all of the slideshow
$slideshow = {},
Slideshow = {
init: function (el, options) {
options = options || {}; // if options object not passed in, then set to empty object
options.auto = options.auto || false; // if options.auto object not passed in, then set to false
this.opts = {
selector:
typeof options.selector === "undefined"
? "figure"
: options.selector,
auto: typeof options.auto === "undefined" ? false : options.auto,
speed:
typeof options.auto.speed === "undefined"
? 1500
: options.auto.speed,
pauseOnHover:
typeof options.auto.pauseOnHover === "undefined"
? false
: options.auto.pauseOnHover,
fullScreen:
typeof options.fullScreen === "undefined"
? false
: options.fullScreen,
swipe: typeof options.swipe === "undefined" ? false : options.swipe,
};
this.counter = 0; // to keep track of current slide
this.el = el; // current slideshow container
this.$items = el.querySelectorAll(this.opts.selector); // a collection of all of the slides, caching for performance
this.numItems = this.$items.length; // total number of slides
this.$items[0].classList.add("bss-show"); // add show class to first figure
this.injectControls(el);
this.addEventListeners(el);
if (this.opts.auto) {
this.autoCycle(this.el, this.opts.speed, this.opts.pauseOnHover);
}
if (this.opts.fullScreen) {
this.addFullScreen(this.el);
}
if (this.opts.swipe) {
this.addSwipe(this.el);
}
},
showCurrent: function (i) {
// increment or decrement this.counter depending on whether i === 1 or i === -1
if (i > 0) {
this.counter =
this.counter + 1 === this.numItems ? 0 : this.counter + 1;
} else {
this.counter =
this.counter - 1 < 0 ? this.numItems - 1 : this.counter - 1;
}
const item = this.$items[this.counter];
const setActive = () => {
[].forEach.call(this.$items, function (el) {
el.classList.remove("bss-show");
});
item.classList.add("bss-show");
};
const img = item.querySelector("img");
// lazy load img
if (!img.src) {
img.addEventListener("load", () => {
setActive();
});
img.src = img.getAttribute("data-src");
img.removeAttribute("data-src");
} else {
setActive();
}
},
injectControls: function (el) {
// build and inject prev/next controls
// first create all the new elements
var spanPrev = document.createElement("span"),
spanNext = document.createElement("span"),
docFrag = document.createDocumentFragment();
// add classes
spanPrev.classList.add("bss-prev");
spanNext.classList.add("bss-next");
// add contents
spanPrev.innerHTML = "«";
spanNext.innerHTML = "»";
// append elements to fragment, then append fragment to DOM
docFrag.appendChild(spanPrev);
docFrag.appendChild(spanNext);
el.appendChild(docFrag);
},
addEventListeners: function (el) {
var that = this;
el.querySelector(".bss-next").addEventListener(
"click",
function () {
that.showCurrent(1); // increment & show
},
false
);
el.querySelector(".bss-prev").addEventListener(
"click",
function () {
that.showCurrent(-1); // decrement & show
},
false
);
el.onkeydown = function (e) {
e = e || window.event;
if (e.keyCode === 37) {
that.showCurrent(-1); // decrement & show
} else if (e.keyCode === 39) {
that.showCurrent(1); // increment & show
}
};
},
autoCycle: function (el, speed, pauseOnHover) {
var that = this,
interval = window.setInterval(function () {
that.showCurrent(1); // increment & show
}, speed);
if (pauseOnHover) {
el.addEventListener(
"mouseover",
function () {
clearInterval(interval);
interval = null;
},
false
);
el.addEventListener(
"mouseout",
function () {
if (!interval) {
interval = window.setInterval(function () {
that.showCurrent(1); // increment & show
}, speed);
}
},
false
);
} // end pauseonhover
},
addFullScreen: function (el) {
var that = this,
fsControl = document.createElement("span");
fsControl.classList.add("bss-fullscreen");
el.appendChild(fsControl);
el.querySelector(".bss-fullscreen").addEventListener(
"click",
function () {
that.toggleFullScreen(el);
},
false
);
},
addSwipe: function (el) {
var that = this,
ht = new Hammer(el);
ht.on("swiperight", function (e) {
that.showCurrent(-1); // decrement & show
});
ht.on("swipeleft", function (e) {
that.showCurrent(1); // increment & show
});
},
toggleFullScreen: function (el) {
// https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Using_full_screen_mode
if (
!document.fullscreenElement && // alternative standard method
!document.mozFullScreenElement &&
!document.webkitFullscreenElement &&
!document.msFullscreenElement
) {
// current working methods
if (document.documentElement.requestFullscreen) {
el.requestFullscreen();
} else if (document.documentElement.msRequestFullscreen) {
el.msRequestFullscreen();
} else if (document.documentElement.mozRequestFullScreen) {
el.mozRequestFullScreen();
} else if (document.documentElement.webkitRequestFullscreen) {
el.webkitRequestFullscreen(el.ALLOW_KEYBOARD_INPUT);
}
} else {
if (document.exitFullscreen) {
document.exitFullscreen();
} else if (document.msExitFullscreen) {
document.msExitFullscreen();
} else if (document.mozCancelFullScreen) {
document.mozCancelFullScreen();
} else if (document.webkitExitFullscreen) {
document.webkitExitFullscreen();
}
}
}, // end toggleFullScreen
}; // end Slideshow object .....
// make instances of Slideshow as needed
[].forEach.call($slideshows, function (el) {
$slideshow = Object.create(Slideshow);
$slideshow.init(el, options);
});
}
function getWorkerPath() {
const scripts = document.getElementsByTagName("script");
const paths = Array.from(scripts).map((s) => s.src);
const thisScript = paths.find((s) => s.match("slideshow"));
return thisScript?.replace("slideshow.js", "parseAlbum.js");
}
function showError(elem) {
elem.html("Er is een fout opgetreden bij het laden van de foto's");
}
function loadAlbum($, link, elemId) {
const elem = jQuery(`#${elemId}`);
try {
const width = Math.floor(Math.min(800, jQuery(elem).width()));
const height = Math.floor((width * 2) / 3);
elem.css({ maxWidth: width, height });
const albumWorker = new Worker(workerPath || getWorkerPath());
albumWorker.postMessage(link);
albumWorker.onmessage = (e) => {
const fotos = e.data;
if (fotos?.length) {
elem.html(""); // remove spinner
fotos.forEach(function (foto, index) {
const figure = $("<figure />");
const url = `${foto}=w${width}-h${height}`;
const attributes = index
? {
"data-src": url,
// loading: 'lazy'
}
: { src: url };
const img = $("<img />", attributes);
figure.append(img);
elem.append(figure);
});
elem.append($('<span class="bss-prev>«</span'));
elem.append($('<span class="bss-next>»</span'));
createSlideshow("#" + elemId, {
auto: {
speed: 2500,
pauseOnHover: true,
},
});
} else {
elem.html("Er zijn geen foto's gevonden");
}
albumWorker.terminate();
};
albumWorker.onerror = (e) => {
showError(elem);
albumWorker.terminate();
};
} catch (e) {
showError(elem);
console.log(e);
}
}