1
0
mirror of https://github.com/webslides/WebSlides.git synced 2025-09-01 01:01:47 +02:00

Adding a way to go to sections by id

#71
This commit is contained in:
Antonio Laguna
2018-01-01 16:32:35 +01:00
parent 5dd1b9c649
commit ea0f2cb833
6 changed files with 68 additions and 9 deletions

View File

@@ -37,7 +37,7 @@ class Slide {
*/
this.i = i;
this.el.id = `section-${(i + 1)}`;
this.el.id = this.el.id ? this.el.id : `section-${(i + 1)}`;
this.el.classList.add(CLASSES.SLIDE);
// Hide slides by default

View File

@@ -60,6 +60,9 @@ export default class Navigation {
this.el.appendChild(this.counter);
this.ws_.el.appendChild(this.el);
this.slides = Array.prototype.slice.call(
document.querySelectorAll('#webslides section')).map(s => s.id);
this.bindEvents_();
}
@@ -73,6 +76,29 @@ export default class Navigation {
this.next.addEventListener('click', this.onButtonClicked_.bind(this));
this.prev.addEventListener('click', this.onButtonClicked_.bind(this));
this.counter.addEventListener('click', this.onButtonClicked_.bind(this));
document.body.addEventListener('click', this.onBodyClicked_.bind(this));
}
/**
* Whenever the body is clicked, check if the element has [data-slide] attr
* and if so, navigate to it.
* @param {MouseEvent} event Click event
*/
onBodyClicked_(event) {
const matches = document.body.matches || document.body.msMatchesSelector;
let el;
if (matches.call(event.target, '[data-slide]')) {
el = event.target;
} else if (matches.call(event.target, '[data-slide] *')) {
el = event.target.querySelector('[data-slide]');
}
if (el) {
event.preventDefault();
const i = this.slides.indexOf(el.dataset.slide);
this.ws_.goToSlide(i);
}
}
/**