1
0
mirror of https://github.com/webslides/WebSlides.git synced 2025-08-16 18:14:09 +02:00
This commit is contained in:
Luis
2017-04-18 20:12:13 +02:00
2 changed files with 52 additions and 45 deletions

View File

@@ -5,10 +5,17 @@ const CLASSES = {
CURRENT: 'current' CURRENT: 'current'
}; };
const Events = {
ENTER: 'dom:enter',
LEAVE: 'dom:leave',
ENABLE: 'slide:enable',
DISABLE: 'slide:disable'
};
/** /**
* Wrapper for the Slide section. * Wrapper for the Slide section.
*/ */
export default class Slide { class Slide {
/** /**
* Bootstraps the slide by saving some data, adding a class and hiding it. * Bootstraps the slide by saving some data, adding a class and hiding it.
* @param {Element} el Section element. * @param {Element} el Section element.
@@ -28,18 +35,6 @@ export default class Slide {
* @type {number} * @type {number}
*/ */
this.i = i; this.i = i;
/**
* Enable callbacks.
* @type {Array<Function>}
* @private
*/
this.onEnable_ = [];
/**
* Disable callbacks
* @type {Array<Function>}
* @private
*/
this.onDisable_ = [];
this.el.id = `section-${(i + 1)}`; this.el.id = `section-${(i + 1)}`;
this.el.classList.add(CLASSES.SLIDE); this.el.classList.add(CLASSES.SLIDE);
@@ -66,50 +61,55 @@ export default class Slide {
/** /**
* Moves the section to the bottom of the section's list. * Moves the section to the bottom of the section's list.
* @fires Slide#dom:leave
* @fires Slide#dom:enter
*/ */
moveAfterLast() { moveAfterLast() {
const last = this.parent.childNodes[this.parent.childElementCount - 1]; const last = this.parent.childNodes[this.parent.childElementCount - 1];
this.fire_(Events.LEAVE);
this.parent.insertBefore(this.el, last.nextSibling); this.parent.insertBefore(this.el, last.nextSibling);
this.fire_(Events.ENTER);
} }
/** /**
* Moves the section to the top of the section's list. * Moves the section to the top of the section's list.
* @fires Slide#dom:leave
* @fires Slide#dom:enter
*/ */
moveBeforeFirst() { moveBeforeFirst() {
const first = this.parent.childNodes[0]; const first = this.parent.childNodes[0];
this.fire_(Events.LEAVE);
this.parent.insertBefore(this.el, first); this.parent.insertBefore(this.el, first);
this.fire_(Events.ENTER);
} }
/** /**
* Adds a callback to the enable event. * Fires an enable event.
* @param {Function} cb Callback to add. * @fires Slide#slide:enable
*/
onEnable(cb) {
this.onEnable_.push(cb);
}
/**
* Adds a callback to the disable event.
* @param {Function} cb Callback to add.
*/
onDisable(cb) {
this.onDisable_.push(cb);
}
/**
* Runs every on enable callback.
*/ */
enable() { enable() {
this.onEnable_.forEach(f => f(this)); this.fire_(Events.ENABLE);
} }
/** /**
* Runs every on disable callback. * Fires a disable event.
* @fires Slide#slide:disable
*/ */
disable() { disable() {
this.onDisable_.forEach(f => f(this)); this.fire_(Events.DISABLE);
}
/**
* Fires an event passing the slide instance on the detail.
* @param {String} name Name of the event to fire.
* @private
*/
fire_(name) {
DOM.fireEvent(this.el, name, {
slide: this
});
} }
/** /**
@@ -146,3 +146,8 @@ export default class Slide {
return {section, i}; return {section, i};
} }
} }
export {
Slide as default,
Events
};

View File

@@ -1,5 +1,5 @@
import DOM from '../utils/dom'; import DOM from '../utils/dom';
import Slide from '../modules/slide'; import {default as Slide, Events as SlideEvents} from '../modules/slide';
/** /**
* Video plugin. Video plugin that allows to autoplay videos once the slide gets * Video plugin. Video plugin that allows to autoplay videos once the slide gets
@@ -21,33 +21,35 @@ export default class Video {
if (videos.length) { if (videos.length) {
videos.forEach(video => { videos.forEach(video => {
if (!video.hasAttribute('autoplay')) {
return;
}
video.removeAttribute('autoplay'); video.removeAttribute('autoplay');
const {i} = Slide.getSectionFromEl(video); const {i} = Slide.getSectionFromEl(video);
const slide = wsInstance.slides[i - 1]; const slide = wsInstance.slides[i - 1];
/**
* @type {HTMLMediaElement}
*/
slide.video = video; slide.video = video;
slide.onEnable(Video.onSectionEnabled);
slide.onDisable(Video.onSectionDisabled); slide.el.addEventListener(SlideEvents.ENABLE, Video.onSectionEnabled);
slide.el.addEventListener(SlideEvents.DISABLE, Video.onSectionDisabled);
}); });
} }
} }
/** /**
* On Section enable hook. Will play the video. * On Section enable hook. Will play the video.
* @param {Slide} slide * @param {CustomEvent} event
*/ */
static onSectionEnabled(slide) { static onSectionEnabled(event) {
slide.video.play(); event.detail.slide.video.play();
} }
/** /**
* On Section enable hook. Will pause the video. * On Section enable hook. Will pause the video.
* @param {Slide} slide * @param {CustomEvent} event
*/ */
static onSectionDisabled(slide) { static onSectionDisabled(event) {
slide.video.pause(); event.detail.slide.video.pause();
} }
} }