-
Notifications
You must be signed in to change notification settings - Fork 1
/
slide-builder.js
85 lines (62 loc) · 2.01 KB
/
slide-builder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
;(function(global, Reveal){
// pass shown/hidden events down to the fragments and slides
var shown = new CustomEvent("shown", {}),
hidden = new CustomEvent("hidden", {}),
currentSlide;
Reveal.addEventListener( 'fragmentshown', function( event ) {
event.fragment.dispatchEvent(shown);
})
Reveal.addEventListener( 'fragmenthidden', function( event ) {
event.fragment.dispatchEvent(hidden);
});
Reveal.addEventListener( 'slidechanged', function( event ) {
if(event.previousSlide) event.previousSlide.dispatchEvent(hidden);
if(event.currentSlide) event.currentSlide.dispatchEvent(shown);
currentSlide = event.currentSlide;
});
Reveal.addEventListener( 'ready', function( event ) {
currentSlide = event.currentSlide;
});
function SlideBuilder(element){
this.slide_el = element;
// if this is loaded dynamically, reveal might have loaded
// check to see if we're displayed
if(element === currentSlide) {
setTimeout(() =>{
element.dispatchEvent(shown)
}, 10)
}
}
// forward event listeners to slide element
SlideBuilder.prototype.addEventListener = function( type, listener, useCapture ) {
if( 'addEventListener' in window ) {
this.slide_el.addEventListener( type, listener, useCapture );
}
return this;
}
SlideBuilder.prototype.addFragment = function(text){
var el = document.createElement('div');
el.className = 'fragment';
el.innerText = text || '';
this.slide_el.appendChild(el);
return el;
}
// shorthand
SlideBuilder.prototype.fragments = function(callbacks){
var self = this;
callbacks.forEach(function(fn){
self.addFragment()
.addEventListener('shown', fn);
})
return this
}
SlideBuilder.prototype.shown = function(fn) {
this.addEventListener('shown', fn)
return this
}
SlideBuilder.prototype.hidden = function(fn) {
this.addEventListener('hidden', fn)
return this
}
global.SlideBuilder = SlideBuilder;
})(this, Reveal);