mirror of
https://github.com/webslides/WebSlides.git
synced 2025-08-20 03:41:38 +02:00
Starting to add comments and JSDoc
This commit is contained in:
@@ -1,7 +1,32 @@
|
|||||||
const HASH = '#slide';
|
const HASH = '#slide';
|
||||||
const slideRegex = /#slide=(\d+)/;
|
const slideRegex = /#slide=(\d+)/;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Static class with methods to manipulate and extract infro from the hash of
|
||||||
|
* the URL.
|
||||||
|
*/
|
||||||
export default class Hash {
|
export default class Hash {
|
||||||
|
/**
|
||||||
|
* Listens to the hashchange event and reacts to it by making the
|
||||||
|
* WebSlide instance navigate to the needed slide.
|
||||||
|
* @param {WebSlides} wsInstance
|
||||||
|
*/
|
||||||
|
static init(wsInstance) {
|
||||||
|
window.addEventListener('hashchange', () => {
|
||||||
|
const newSlideIndex = Hash.getSlideNumber();
|
||||||
|
|
||||||
|
if (newSlideIndex !== null) {
|
||||||
|
wsInstance.goToSlide(newSlideIndex);
|
||||||
|
}
|
||||||
|
}, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the slide number from the hash by a regex matching `#slide=` and gets
|
||||||
|
* the number after it. If the number is invalid or less than 0, it will
|
||||||
|
* return null as an invalid value.
|
||||||
|
* @return {?number}
|
||||||
|
*/
|
||||||
static getSlideNumber() {
|
static getSlideNumber() {
|
||||||
let results = document.location.hash.match(slideRegex);
|
let results = document.location.hash.match(slideRegex);
|
||||||
let slide = 0;
|
let slide = 0;
|
||||||
@@ -19,7 +44,16 @@ export default class Hash {
|
|||||||
return slide;
|
return slide;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* It will update the hash (if it's different) so it reflects the slide
|
||||||
|
* number being visible.
|
||||||
|
* @param {number} number The number of the slide we're transitioning to.
|
||||||
|
*/
|
||||||
static setSlideNumber(number) {
|
static setSlideNumber(number) {
|
||||||
history.pushState(null, `Slide ${number}`, `${HASH}=${number}`);
|
if (Hash.getSlideNumber() !== (number - 1)) {
|
||||||
|
history.pushState({
|
||||||
|
slideI: number - 1
|
||||||
|
}, `Slide ${number}`, `${HASH}=${number}`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -1,5 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* Static class for DOM helper.
|
||||||
|
*/
|
||||||
export default class DOM {
|
export default class DOM {
|
||||||
static createNode(tag, id = '', text = null) {
|
/**
|
||||||
|
* Creates a node with optional parameters.
|
||||||
|
* @param {string} tag The name of the tag of the needed element.
|
||||||
|
* @param {string} id The desired id for the element. It defaults to an
|
||||||
|
* empty string.
|
||||||
|
* @param {string} text The desired text to go inside of the element. It defaults
|
||||||
|
* to an empty string.
|
||||||
|
* @return {Element}
|
||||||
|
*/
|
||||||
|
static createNode(tag, id = '', text = '') {
|
||||||
const node = document.createElement(tag);
|
const node = document.createElement(tag);
|
||||||
node.id = id;
|
node.id = id;
|
||||||
|
|
||||||
@@ -10,18 +22,35 @@ export default class DOM {
|
|||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Hides an element setting the display to none.
|
||||||
|
* @param {Element} el Element to be hidden.
|
||||||
|
*/
|
||||||
static hide(el) {
|
static hide(el) {
|
||||||
el.style.display = 'none';
|
el.style.display = 'none';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Shows an element by removing the display property. This is only intended
|
||||||
|
* to be used in conjunction with DOM.hide.
|
||||||
|
* @param {Element} el Element to be shown.
|
||||||
|
*/
|
||||||
static show(el) {
|
static show(el) {
|
||||||
el.style.display = '';
|
el.style.display = '';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Locks the scroll on the document by setting the HTML to have a hidden
|
||||||
|
* overflow.
|
||||||
|
*/
|
||||||
static lockScroll() {
|
static lockScroll() {
|
||||||
document.documentElement.style.overflow = 'hidden';
|
document.documentElement.style.overflow = 'hidden';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Unlocks the scroll on the document by setting the HTML to have an auto
|
||||||
|
* overflow.
|
||||||
|
*/
|
||||||
static unlockScroll() {
|
static unlockScroll() {
|
||||||
document.documentElement.style.overflow = 'auto';
|
document.documentElement.style.overflow = 'auto';
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user