|
- import { supportsHistoryAPI } from '../utils/device.js'
-
-
- export default class Location {
-
- constructor( Reveal ) {
-
- this.Reveal = Reveal;
-
-
- this.writeURLTimeout = 0;
-
- this.onWindowHashChange = this.onWindowHashChange.bind( this );
-
- }
-
- bind() {
-
- window.addEventListener( 'hashchange', this.onWindowHashChange, false );
-
- }
-
- unbind() {
-
- window.removeEventListener( 'hashchange', this.onWindowHashChange, false );
-
- }
-
-
-
- readURL() {
-
- let config = this.Reveal.getConfig();
- let indices = this.Reveal.getIndices();
- let currentSlide = this.Reveal.getCurrentSlide();
-
- let hash = window.location.hash;
-
-
- let bits = hash.slice( 2 ).split( '/' ),
- name = hash.replace( /#\/?/gi, '' );
-
-
-
- if( !/^[0-9]*$/.test( bits[0] ) && name.length ) {
- let element;
-
- let f;
-
-
- if( /\/[-\d]+$/g.test( name ) ) {
- f = parseInt( name.split( '/' ).pop(), 10 );
- f = isNaN(f) ? undefined : f;
- name = name.split( '/' ).shift();
- }
-
-
- try {
- element = document.getElementById( decodeURIComponent( name ) );
- }
- catch ( error ) { }
-
-
- let isSameNameAsCurrentSlide = currentSlide ? currentSlide.getAttribute( 'id' ) === name : false;
-
- if( element ) {
-
- if ( !isSameNameAsCurrentSlide || typeof f !== 'undefined' ) {
-
- let slideIndices = this.Reveal.getIndices( element );
- this.Reveal.slide( slideIndices.h, slideIndices.v, f );
- }
- }
-
- else {
- this.Reveal.slide( indices.h || 0, indices.v || 0 );
- }
- }
- else {
- let hashIndexBase = config.hashOneBasedIndex ? 1 : 0;
-
-
- let h = ( parseInt( bits[0], 10 ) - hashIndexBase ) || 0,
- v = ( parseInt( bits[1], 10 ) - hashIndexBase ) || 0,
- f;
-
- if( config.fragmentInURL ) {
- f = parseInt( bits[2], 10 );
- if( isNaN( f ) ) {
- f = undefined;
- }
- }
-
- if( h !== indices.h || v !== indices.v || f !== undefined ) {
- this.Reveal.slide( h, v, f );
- }
- }
-
- }
-
-
-
- writeURL( delay ) {
-
- let config = this.Reveal.getConfig();
- let currentSlide = this.Reveal.getCurrentSlide();
-
-
- clearTimeout( this.writeURLTimeout );
-
-
- if( typeof delay === 'number' ) {
- this.writeURLTimeout = setTimeout( this.writeURL, delay );
- }
- else if( currentSlide ) {
-
-
- if( config.history || supportsHistoryAPI === false ) {
- window.location.hash = this.getHash();
- }
-
-
- else if( config.hash ) {
- window.history.replaceState( null, null, '#' + this.getHash() );
- }
-
-
-
-
-
-
-
-
-
-
- }
-
- }
-
-
-
- getHash( slide ) {
-
- let url = '/';
-
-
- let s = slide || this.Reveal.getCurrentSlide();
- let id = s ? s.getAttribute( 'id' ) : null;
- if( id ) {
- id = encodeURIComponent( id );
- }
-
- let index = this.Reveal.getIndices( slide );
- if( !this.Reveal.getConfig().fragmentInURL ) {
- index.f = undefined;
- }
-
-
-
- if( typeof id === 'string' && id.length ) {
- url = '/' + id;
-
-
-
- if( index.f >= 0 ) url += '/' + index.f;
- }
-
- else {
- let hashIndexBase = this.Reveal.getConfig().hashOneBasedIndex ? 1 : 0;
- if( index.h > 0 || index.v > 0 || index.f >= 0 ) url += index.h + hashIndexBase;
- if( index.v > 0 || index.f >= 0 ) url += '/' + (index.v + hashIndexBase );
- if( index.f >= 0 ) url += '/' + index.f;
- }
-
- return url;
-
- }
-
-
-
- onWindowHashChange( event ) {
-
- this.readURL();
-
- }
-
- }
|