1
0
mirror of https://github.com/webslides/WebSlides.git synced 2025-08-19 11:21:20 +02:00

Starting to add comments and JSDoc

This commit is contained in:
Antonio Laguna
2017-01-27 15:28:40 +01:00
parent 01fa93d50e
commit d4e6662ab6
2 changed files with 65 additions and 2 deletions

View File

@@ -1,7 +1,32 @@
const HASH = '#slide';
const slideRegex = /#slide=(\d+)/;
/**
* Static class with methods to manipulate and extract infro from the hash of
* the URL.
*/
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() {
let results = document.location.hash.match(slideRegex);
let slide = 0;
@@ -19,7 +44,16 @@ export default class Hash {
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) {
history.pushState(null, `Slide ${number}`, `${HASH}=${number}`);
if (Hash.getSlideNumber() !== (number - 1)) {
history.pushState({
slideI: number - 1
}, `Slide ${number}`, `${HASH}=${number}`);
}
}
}

View File

@@ -1,5 +1,17 @@
/**
* Static class for DOM helper.
*/
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);
node.id = id;
@@ -10,18 +22,35 @@ export default class DOM {
return node;
}
/**
* Hides an element setting the display to none.
* @param {Element} el Element to be hidden.
*/
static hide(el) {
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) {
el.style.display = '';
}
/**
* Locks the scroll on the document by setting the HTML to have a hidden
* overflow.
*/
static lockScroll() {
document.documentElement.style.overflow = 'hidden';
}
/**
* Unlocks the scroll on the document by setting the HTML to have an auto
* overflow.
*/
static unlockScroll() {
document.documentElement.style.overflow = 'auto';
}