summaryrefslogtreecommitdiffstats
path: root/rsrc/main.js
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2018-03-31 15:47:05 +0200
committerThomas Lange <code@nerdmind.de>2018-03-31 15:47:05 +0200
commit7f42d61161ae010e5a970934d9f827ddafa0d586 (patch)
tree38345fdbceb931f1aba90128ab60b7cf4f56f3b2 /rsrc/main.js
downloadslideshow-7f42d61161ae010e5a970934d9f827ddafa0d586.tar.gz
slideshow-7f42d61161ae010e5a970934d9f827ddafa0d586.tar.xz
slideshow-7f42d61161ae010e5a970934d9f827ddafa0d586.zip
Initial commit
Diffstat (limited to 'rsrc/main.js')
-rw-r--r--rsrc/main.js61
1 files changed, 61 insertions, 0 deletions
diff --git a/rsrc/main.js b/rsrc/main.js
new file mode 100644
index 0000000..c35f983
--- /dev/null
+++ b/rsrc/main.js
@@ -0,0 +1,61 @@
+class Slideshow {
+ constructor(node) {
+ let prev, next, time;
+
+ this.autostop = false;
+ this.timeout = false;
+ this.current = 0;
+ this.list = node.getElementsByTagName("ul")[0].getElementsByTagName("li");
+
+ if(prev = node.getElementsByClassName("prev")[0]) {
+ prev.onclick = () => {
+ this.initializeTimeout();
+ this.changeDirection("prev")
+ };
+ }
+
+ if(next = node.getElementsByClassName("next")[0]) {
+ next.onclick = () => {
+ this.initializeTimeout();
+ this.changeDirection("next");
+ };
+ }
+
+ if(time = node.getAttribute("data-time")) {
+ setInterval(() => {
+ !this.autostop && this.changeDirection("next");
+ }, time);
+ }
+ }
+
+ initializeTimeout() {
+ if(this.timeout) {
+ clearTimeout(this.timeout);
+ }
+
+ this.autostop = true;
+ this.timeout = setTimeout(() => {
+ this.autostop = false;
+ }, 15000);
+ }
+
+ changeDirection(direction) {
+ (direction === "prev" && --this.current);
+ (direction === "next" && ++this.current);
+
+ if(this.list[this.current] === undefined) {
+ switch(direction) {
+ case "prev": this.current = this.list.length - 1; break;
+ case "next": this.current = 0;
+ }
+ }
+
+ for(let i = 0; i < this.list.length; ++i) {
+ if(this.current === i) {
+ this.list[i].getAttribute("class") !== "show" && this.list[i].setAttribute("class", "show " + direction);
+ } else {
+ this.list[i].getAttribute("class") !== "hide" && this.list[i].setAttribute("class", "hide");
+ }
+ }
+ }
+} \ No newline at end of file