From 7f42d61161ae010e5a970934d9f827ddafa0d586 Mon Sep 17 00:00:00 2001 From: Thomas Lange Date: Sat, 31 Mar 2018 15:47:05 +0200 Subject: Initial commit --- rsrc/main.js | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 rsrc/main.js (limited to 'rsrc/main.js') 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 -- cgit v1.2.3