vanillajs-deck/js/navigator.js

161 lines
4.4 KiB
JavaScript
Raw Normal View History

2019-11-24 00:02:31 +00:00
// @ts-check
2019-11-23 00:37:45 +00:00
import { loadSlides } from "./slideLoader.js"
2019-11-24 00:02:31 +00:00
import { Slide } from "./slide.js"
2019-11-23 00:37:45 +00:00
import { Router } from "./router.js"
2019-11-23 20:07:22 +00:00
import { Animator } from "./animator.js"
2019-11-22 19:28:19 +00:00
2019-11-24 00:02:31 +00:00
/**
* The main class that handles rendering the slide decks
* @property {Animator} _animator Animation helper
* @property {Router} _router Routing helper
* @property {string} _route The current route
* @property {CustomEvent} slidesChangedEvent Event fired when slide changes
*/
export class Navigator extends HTMLElement {
2019-11-23 00:37:45 +00:00
2019-11-24 00:02:31 +00:00
/**
* Create an instance of the custom navigator element
*/
2019-11-23 00:37:45 +00:00
constructor() {
super();
2019-11-23 20:07:22 +00:00
this._animator = new Animator();
2019-11-23 00:37:45 +00:00
this._router = new Router();
this._route = this._router.getRoute();
this.slidesChangedEvent = new CustomEvent("slideschanged", {
bubbles: true,
cancelable: false
});
this._router.eventSource.addEventListener("routechanged", () => {
if (this._route !== this._router.getRoute()) {
this._route = this._router.getRoute();
if (this._route) {
var slide = parseInt(this._route) - 1;
this.jumpTo(slide);
2019-11-24 00:02:31 +00:00
}
2019-11-23 00:37:45 +00:00
}
});
}
2019-11-24 00:02:31 +00:00
/**
* Get the list of observed attributes
* @returns {string[]}
*/
2019-11-23 00:37:45 +00:00
static get observedAttributes() {
return ["start"];
}
2019-11-24 00:02:31 +00:00
/**
* Called when an attribute changes
* @param {string} attrName
* @param {string} oldVal
* @param {string} newVal
*/
2019-11-23 00:37:45 +00:00
async attributeChangedCallback(attrName, oldVal, newVal) {
if (attrName === "start") {
if (oldVal !== newVal) {
this._slides = await loadSlides(newVal);
this._route = this._router.getRoute();
var slide = 0;
if (this._route) {
slide = parseInt(this._route) - 1;
}
this.jumpTo(slide);
2019-11-23 20:28:04 +00:00
this._title = document.querySelectorAll("title")[0];
2019-11-23 00:37:45 +00:00
}
}
2019-11-22 19:28:19 +00:00
}
2019-11-24 00:02:31 +00:00
/**
* Current slide index
* @returns {number}
*/
2019-11-22 19:28:19 +00:00
get currentIndex() {
return this._currentIndex;
}
2019-11-24 00:02:31 +00:00
/**
* Current slide
* @returns {Slide}
*/
2019-11-22 19:28:19 +00:00
get currentSlide() {
2019-11-23 00:37:45 +00:00
return this._slides ? this._slides[this._currentIndex] : null;
2019-11-22 19:28:19 +00:00
}
2019-11-24 00:02:31 +00:00
/**
* Total number of slides
* @returns {number}
*/
2019-11-22 19:28:19 +00:00
get totalSlides() {
2019-11-23 00:37:45 +00:00
return this._slides ? this._slides.length : 0;
2019-11-22 19:28:19 +00:00
}
2019-11-24 00:02:31 +00:00
/**
* True if a previous slide exists
* @returns {boolean}
*/
2019-11-22 19:28:19 +00:00
get hasPrevious() {
return this._currentIndex > 0;
}
2019-11-24 00:02:31 +00:00
/**
* True if a next slide exists
* @returns {boolean}
*/
2019-11-22 19:28:19 +00:00
get hasNext() {
return this._currentIndex < (this.totalSlides - 1);
}
2019-11-24 00:02:31 +00:00
/**
* Main slide navigation: jump to specific slide
* @param {number} slideIdx
*/
2019-11-22 19:28:19 +00:00
jumpTo(slideIdx) {
2019-11-23 20:07:22 +00:00
if (this._animator.transitioning) {
return;
}
2019-11-22 19:28:19 +00:00
if (slideIdx >= 0 && slideIdx < this.totalSlides) {
this._currentIndex = slideIdx;
2019-11-23 00:37:45 +00:00
this.innerHTML = '';
this.appendChild(this.currentSlide.html);
2019-11-24 00:02:31 +00:00
this._router.setRoute((slideIdx + 1).toString());
2019-11-23 00:37:45 +00:00
this._route = this._router.getRoute();
2019-11-24 00:02:31 +00:00
document.title = `${this.currentIndex + 1}/${this.totalSlides}: ${this.currentSlide.title}`;
2019-11-23 00:37:45 +00:00
this.dispatchEvent(this.slidesChangedEvent);
2019-11-23 20:07:22 +00:00
if (this._animator.animationReady) {
this._animator.endAnimation(this.querySelector("div"));
}
2019-11-22 19:28:19 +00:00
}
}
2019-11-24 00:02:31 +00:00
/**
* Advance to next slide, if it exists. Applies animation if transition is specified
*/
2019-11-22 19:28:19 +00:00
next() {
if (this.hasNext) {
2019-11-23 20:07:22 +00:00
if (this.currentSlide.transition !== null) {
this._animator.beginAnimation(
2019-11-24 00:02:31 +00:00
this.currentSlide.transition,
2019-11-23 20:07:22 +00:00
this.querySelector("div"),
2019-11-24 00:02:31 +00:00
() => this.jumpTo(this.currentIndex + 1));
2019-11-23 20:07:22 +00:00
}
else {
this.jumpTo(this.currentIndex + 1);
}
2019-11-22 19:28:19 +00:00
}
}
2019-11-24 00:02:31 +00:00
/**
* Move to previous slide, if it exists
*/
2019-11-22 19:28:19 +00:00
previous() {
if (this.hasPrevious) {
this.jumpTo(this.currentIndex - 1);
}
}
2019-11-23 00:37:45 +00:00
}
2019-11-24 00:02:31 +00:00
/**
* Register the custom slide-deck component
*/
2019-11-23 00:37:45 +00:00
export const registerDeck = () => customElements.define('slide-deck', Navigator);