2020-03-16 14:18:47 +00:00
|
|
|
import { extend, queryAll } from '../utils/util.js'
|
2020-03-09 17:51:07 +00:00
|
|
|
|
|
|
|
/**
|
2020-03-09 19:57:12 +00:00
|
|
|
* Handles sorting and navigation of slide fragments.
|
|
|
|
* Fragments are elements within a slide that are
|
|
|
|
* revealed/animated incrementally.
|
2020-03-09 17:51:07 +00:00
|
|
|
*/
|
|
|
|
export default class Fragments {
|
|
|
|
|
|
|
|
constructor( Reveal ) {
|
|
|
|
|
|
|
|
this.Reveal = Reveal;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-16 10:45:47 +00:00
|
|
|
/**
|
2020-03-16 12:53:23 +00:00
|
|
|
* Called when the reveal.js config is updated.
|
2020-03-16 10:45:47 +00:00
|
|
|
*/
|
|
|
|
configure( config, oldConfig ) {
|
|
|
|
|
|
|
|
if( config.fragments === false ) {
|
|
|
|
this.disable();
|
|
|
|
}
|
|
|
|
else if( oldConfig.fragments === false ) {
|
|
|
|
this.enable();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-09 17:51:07 +00:00
|
|
|
/**
|
2020-03-12 18:11:19 +00:00
|
|
|
* If fragments are disabled in the deck, they should all be
|
|
|
|
* visible rather than stepped through.
|
2020-03-09 17:51:07 +00:00
|
|
|
*/
|
2020-03-12 18:11:19 +00:00
|
|
|
disable() {
|
2020-03-09 17:51:07 +00:00
|
|
|
|
2020-03-16 14:18:47 +00:00
|
|
|
queryAll( this.Reveal.getSlidesElement(), '.fragment' ).forEach( element => {
|
2020-03-09 17:51:07 +00:00
|
|
|
element.classList.add( 'visible' );
|
|
|
|
element.classList.remove( 'current-fragment' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-12 18:11:19 +00:00
|
|
|
/**
|
|
|
|
* Reverse of #disable(). Only called if fragments have
|
|
|
|
* previously been disabled.
|
|
|
|
*/
|
|
|
|
enable() {
|
|
|
|
|
2020-03-16 14:18:47 +00:00
|
|
|
queryAll( this.Reveal.getSlidesElement(), '.fragment' ).forEach( element => {
|
2020-03-12 18:11:19 +00:00
|
|
|
element.classList.remove( 'visible' );
|
|
|
|
element.classList.remove( 'current-fragment' );
|
|
|
|
} );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-09 17:51:07 +00:00
|
|
|
/**
|
|
|
|
* Returns an object describing the available fragment
|
|
|
|
* directions.
|
|
|
|
*
|
|
|
|
* @return {{prev: boolean, next: boolean}}
|
|
|
|
*/
|
|
|
|
availableRoutes() {
|
|
|
|
|
|
|
|
let currentSlide = this.Reveal.getCurrentSlide();
|
|
|
|
if( currentSlide && this.Reveal.getConfig().fragments ) {
|
2020-03-19 14:18:14 +00:00
|
|
|
let fragments = currentSlide.querySelectorAll( '.fragment:not(.disabled)' );
|
|
|
|
let hiddenFragments = currentSlide.querySelectorAll( '.fragment:not(.disabled):not(.visible)' );
|
2020-03-09 17:51:07 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
prev: fragments.length - hiddenFragments.length > 0,
|
|
|
|
next: !!hiddenFragments.length
|
|
|
|
};
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return { prev: false, next: false };
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a sorted fragments list, ordered by an increasing
|
|
|
|
* "data-fragment-index" attribute.
|
|
|
|
*
|
|
|
|
* Fragments will be revealed in the order that they are returned by
|
|
|
|
* this function, so you can use the index attributes to control the
|
|
|
|
* order of fragment appearance.
|
|
|
|
*
|
|
|
|
* To maintain a sensible default fragment order, fragments are presumed
|
|
|
|
* to be passed in document order. This function adds a "fragment-index"
|
|
|
|
* attribute to each node if such an attribute is not already present,
|
|
|
|
* and sets that attribute to an integer value which is the position of
|
|
|
|
* the fragment within the fragments list.
|
|
|
|
*
|
|
|
|
* @param {object[]|*} fragments
|
|
|
|
* @param {boolean} grouped If true the returned array will contain
|
|
|
|
* nested arrays for all fragments with the same index
|
|
|
|
* @return {object[]} sorted Sorted array of fragments
|
|
|
|
*/
|
|
|
|
sort( fragments, grouped = false ) {
|
|
|
|
|
2020-03-16 14:18:47 +00:00
|
|
|
fragments = Array.from( fragments );
|
2020-03-09 17:51:07 +00:00
|
|
|
|
|
|
|
let ordered = [],
|
|
|
|
unordered = [],
|
|
|
|
sorted = [];
|
|
|
|
|
|
|
|
// Group ordered and unordered elements
|
|
|
|
fragments.forEach( fragment => {
|
|
|
|
if( fragment.hasAttribute( 'data-fragment-index' ) ) {
|
|
|
|
let index = parseInt( fragment.getAttribute( 'data-fragment-index' ), 10 );
|
|
|
|
|
|
|
|
if( !ordered[index] ) {
|
|
|
|
ordered[index] = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
ordered[index].push( fragment );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
unordered.push( [ fragment ] );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Append fragments without explicit indices in their
|
|
|
|
// DOM order
|
|
|
|
ordered = ordered.concat( unordered );
|
|
|
|
|
|
|
|
// Manually count the index up per group to ensure there
|
|
|
|
// are no gaps
|
|
|
|
let index = 0;
|
|
|
|
|
|
|
|
// Push all fragments in their sorted order to an array,
|
|
|
|
// this flattens the groups
|
|
|
|
ordered.forEach( group => {
|
|
|
|
group.forEach( fragment => {
|
|
|
|
sorted.push( fragment );
|
|
|
|
fragment.setAttribute( 'data-fragment-index', index );
|
|
|
|
} );
|
|
|
|
|
|
|
|
index ++;
|
|
|
|
} );
|
|
|
|
|
|
|
|
return grouped === true ? ordered : sorted;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sorts and formats all of fragments in the
|
|
|
|
* presentation.
|
|
|
|
*/
|
|
|
|
sortAll() {
|
|
|
|
|
|
|
|
this.Reveal.getHorizontalSlides().forEach( horizontalSlide => {
|
|
|
|
|
2020-03-16 14:18:47 +00:00
|
|
|
let verticalSlides = queryAll( horizontalSlide, 'section' );
|
2020-03-09 17:51:07 +00:00
|
|
|
verticalSlides.forEach( ( verticalSlide, y ) => {
|
|
|
|
|
|
|
|
this.sort( verticalSlide.querySelectorAll( '.fragment' ) );
|
|
|
|
|
|
|
|
}, this );
|
|
|
|
|
|
|
|
if( verticalSlides.length === 0 ) this.sort( horizontalSlide.querySelectorAll( '.fragment' ) );
|
|
|
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Refreshes the fragments on the current slide so that they
|
|
|
|
* have the appropriate classes (.visible + .current-fragment).
|
|
|
|
*
|
|
|
|
* @param {number} [index] The index of the current fragment
|
|
|
|
* @param {array} [fragments] Array containing all fragments
|
|
|
|
* in the current slide
|
|
|
|
*
|
|
|
|
* @return {{shown: array, hidden: array}}
|
|
|
|
*/
|
|
|
|
update( index, fragments ) {
|
|
|
|
|
|
|
|
let changedFragments = {
|
|
|
|
shown: [],
|
|
|
|
hidden: []
|
|
|
|
};
|
|
|
|
|
|
|
|
let currentSlide = this.Reveal.getCurrentSlide();
|
|
|
|
if( currentSlide && this.Reveal.getConfig().fragments ) {
|
|
|
|
|
|
|
|
fragments = fragments || this.sort( currentSlide.querySelectorAll( '.fragment' ) );
|
|
|
|
|
|
|
|
if( fragments.length ) {
|
|
|
|
|
|
|
|
let maxIndex = 0;
|
|
|
|
|
|
|
|
if( typeof index !== 'number' ) {
|
|
|
|
let currentFragment = this.sort( currentSlide.querySelectorAll( '.fragment.visible' ) ).pop();
|
|
|
|
if( currentFragment ) {
|
|
|
|
index = parseInt( currentFragment.getAttribute( 'data-fragment-index' ) || 0, 10 );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-16 14:18:47 +00:00
|
|
|
Array.from( fragments ).forEach( ( el, i ) => {
|
2020-03-09 17:51:07 +00:00
|
|
|
|
|
|
|
if( el.hasAttribute( 'data-fragment-index' ) ) {
|
|
|
|
i = parseInt( el.getAttribute( 'data-fragment-index' ), 10 );
|
|
|
|
}
|
|
|
|
|
|
|
|
maxIndex = Math.max( maxIndex, i );
|
|
|
|
|
|
|
|
// Visible fragments
|
|
|
|
if( i <= index ) {
|
2020-03-12 16:08:20 +00:00
|
|
|
let wasVisible = el.classList.contains( 'visible' )
|
2020-03-09 17:51:07 +00:00
|
|
|
el.classList.add( 'visible' );
|
|
|
|
el.classList.remove( 'current-fragment' );
|
|
|
|
|
|
|
|
if( i === index ) {
|
2020-03-13 07:58:59 +00:00
|
|
|
// Announce the fragments one by one to the Screen Reader
|
|
|
|
this.Reveal.announceStatus( this.Reveal.getStatusText( el ) );
|
|
|
|
|
2020-03-09 17:51:07 +00:00
|
|
|
el.classList.add( 'current-fragment' );
|
|
|
|
this.Reveal.slideContent.startEmbeddedContent( el );
|
|
|
|
}
|
2020-03-12 16:08:20 +00:00
|
|
|
|
|
|
|
if( !wasVisible ) {
|
|
|
|
changedFragments.shown.push( el )
|
|
|
|
this.Reveal.dispatchEvent({
|
|
|
|
target: el,
|
|
|
|
type: 'visible',
|
|
|
|
bubbles: false
|
|
|
|
});
|
|
|
|
}
|
2020-03-09 17:51:07 +00:00
|
|
|
}
|
|
|
|
// Hidden fragments
|
|
|
|
else {
|
2020-03-12 16:08:20 +00:00
|
|
|
let wasVisible = el.classList.contains( 'visible' )
|
2020-03-09 17:51:07 +00:00
|
|
|
el.classList.remove( 'visible' );
|
|
|
|
el.classList.remove( 'current-fragment' );
|
2020-03-12 16:08:20 +00:00
|
|
|
|
|
|
|
if( wasVisible ) {
|
|
|
|
changedFragments.hidden.push( el );
|
|
|
|
this.Reveal.dispatchEvent({
|
|
|
|
target: el,
|
|
|
|
type: 'hidden',
|
|
|
|
bubbles: false
|
|
|
|
});
|
|
|
|
}
|
2020-03-09 17:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
// Write the current fragment index to the slide <section>.
|
|
|
|
// This can be used by end users to apply styles based on
|
|
|
|
// the current fragment index.
|
|
|
|
index = typeof index === 'number' ? index : -1;
|
|
|
|
index = Math.max( Math.min( index, maxIndex ), -1 );
|
|
|
|
currentSlide.setAttribute( 'data-fragment', index );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return changedFragments;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-14 08:25:48 +00:00
|
|
|
/**
|
|
|
|
* Formats the fragments on the given slide so that they have
|
|
|
|
* valid indices. Call this if fragments are changed in the DOM
|
|
|
|
* after reveal.js has already initialized.
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} slide
|
|
|
|
* @return {Array} a list of the HTML fragments that were synced
|
|
|
|
*/
|
|
|
|
sync( slide = this.Reveal.getCurrentSlide() ) {
|
|
|
|
|
|
|
|
return this.sort( slide.querySelectorAll( '.fragment' ) );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-03-09 17:51:07 +00:00
|
|
|
/**
|
|
|
|
* Navigate to the specified slide fragment.
|
|
|
|
*
|
|
|
|
* @param {?number} index The index of the fragment that
|
|
|
|
* should be shown, -1 means all are invisible
|
|
|
|
* @param {number} offset Integer offset to apply to the
|
|
|
|
* fragment index
|
|
|
|
*
|
|
|
|
* @return {boolean} true if a change was made in any
|
|
|
|
* fragments visibility as part of this call
|
|
|
|
*/
|
|
|
|
goto( index, offset = 0 ) {
|
|
|
|
|
|
|
|
let currentSlide = this.Reveal.getCurrentSlide();
|
|
|
|
if( currentSlide && this.Reveal.getConfig().fragments ) {
|
|
|
|
|
2020-03-19 14:18:14 +00:00
|
|
|
let fragments = this.sort( currentSlide.querySelectorAll( '.fragment:not(.disabled)' ) );
|
2020-03-09 17:51:07 +00:00
|
|
|
if( fragments.length ) {
|
|
|
|
|
|
|
|
// If no index is specified, find the current
|
|
|
|
if( typeof index !== 'number' ) {
|
2020-03-19 14:18:14 +00:00
|
|
|
let lastVisibleFragment = this.sort( currentSlide.querySelectorAll( '.fragment:not(.disabled).visible' ) ).pop();
|
2020-03-09 17:51:07 +00:00
|
|
|
|
|
|
|
if( lastVisibleFragment ) {
|
|
|
|
index = parseInt( lastVisibleFragment.getAttribute( 'data-fragment-index' ) || 0, 10 );
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
index = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the offset if there is one
|
|
|
|
index += offset;
|
|
|
|
|
|
|
|
let changedFragments = this.update( index, fragments );
|
|
|
|
|
|
|
|
if( changedFragments.hidden.length ) {
|
2020-03-12 16:08:20 +00:00
|
|
|
this.Reveal.dispatchEvent({
|
|
|
|
type: 'fragmenthidden',
|
|
|
|
data: {
|
|
|
|
fragment: changedFragments.hidden[0],
|
|
|
|
fragments: changedFragments.hidden
|
|
|
|
}
|
|
|
|
});
|
2020-03-09 17:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( changedFragments.shown.length ) {
|
2020-03-12 16:08:20 +00:00
|
|
|
this.Reveal.dispatchEvent({
|
|
|
|
type: 'fragmentshown',
|
|
|
|
data: {
|
|
|
|
fragment: changedFragments.shown[0],
|
|
|
|
fragments: changedFragments.shown
|
|
|
|
}
|
|
|
|
});
|
2020-03-09 17:51:07 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 12:53:23 +00:00
|
|
|
this.Reveal.controls.update();
|
2020-03-16 13:30:36 +00:00
|
|
|
this.Reveal.progress.update();
|
2020-03-09 17:51:07 +00:00
|
|
|
|
|
|
|
if( this.Reveal.getConfig().fragmentInURL ) {
|
2020-03-10 20:30:39 +00:00
|
|
|
this.Reveal.location.writeURL();
|
2020-03-09 17:51:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return !!( changedFragments.shown.length || changedFragments.hidden.length );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate to the next slide fragment.
|
|
|
|
*
|
|
|
|
* @return {boolean} true if there was a next fragment,
|
|
|
|
* false otherwise
|
|
|
|
*/
|
|
|
|
next() {
|
|
|
|
|
|
|
|
return this.goto( null, 1 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate to the previous slide fragment.
|
|
|
|
*
|
|
|
|
* @return {boolean} true if there was a previous fragment,
|
|
|
|
* false otherwise
|
|
|
|
*/
|
|
|
|
prev() {
|
|
|
|
|
|
|
|
return this.goto( null, -1 );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|