123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- 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 );
- }
-
- getIndicesFromHash( hash=window.location.hash ) {
-
- let name = hash.replace( /^#\/?/, '' );
- let bits = name.split( '/' );
-
-
- 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 ) { }
- if( element ) {
- return { ...this.Reveal.getIndices( element ), f };
- }
- }
- else {
- const config = this.Reveal.getConfig();
- 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;
- }
- }
- return { h, v, f };
- }
-
- return null
- }
-
- readURL() {
- const currentIndices = this.Reveal.getIndices();
- const newIndices = this.getIndicesFromHash();
- if( newIndices ) {
- if( ( newIndices.h !== currentIndices.h || newIndices.v !== currentIndices.v || newIndices.f !== undefined ) ) {
- this.Reveal.slide( newIndices.h, newIndices.v, newIndices.f );
- }
- }
-
-
- else {
- this.Reveal.slide( currentIndices.h || 0, currentIndices.v || 0 );
- }
- }
-
- 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 ) {
- let hash = this.getHash();
-
-
- if( config.history ) {
- window.location.hash = hash;
- }
-
-
- else if( config.hash ) {
-
- if( hash === '/' ) {
- window.history.replaceState( null, null, window.location.pathname + window.location.search );
- }
- else {
- window.history.replaceState( null, null, '#' + hash );
- }
- }
-
-
-
-
-
-
-
-
-
-
- }
- }
-
- 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();
- }
- }
|