1/*! 2 * Flickity hash v1.0.3 3 * Enable hash navigation for Flickity 4 */ 5 6/*jshint browser: true, undef: true, unused: true, strict: true*/ 7 8( function( window, factory ) { 9 // universal module definition 10 /*jshint strict: false */ /*globals define, module, require */ 11 if ( typeof define == 'function' && define.amd ) { 12 // AMD 13 define( [ 14 'flickity/js/index', 15 ], factory ); 16 } else if ( typeof module == 'object' && module.exports ) { 17 // CommonJS 18 module.exports = factory( 19 require('flickity') 20 ); 21 } else { 22 // browser global 23 factory( 24 window.Flickity 25 ); 26 } 27 28}( window, function factory( Flickity ) { 29 30'use strict'; 31 32Flickity.createMethods.push('_createHash'); 33 34var proto = Flickity.prototype; 35 36proto._createHash = function() { 37 if ( !this.options.hash ) { 38 return; 39 } 40 this.connectedHashLinks = []; 41 // hash link listener 42 // use HTML5 history pushState to prevent page scroll jump 43 this.onHashLinkClick = function( event ) { 44 event.preventDefault(); 45 this.selectCell( event.currentTarget.hash ); 46 history.replaceState( null, '', event.currentTarget.hash ); 47 }.bind( this ); 48 49 // events 50 this.on( 'activate', this.activateHash ); 51 this.on( 'deactivate', this.deactivateHash ); 52};
53 54proto.activateHash = function() { 55 this.on( 'change', this.onChangeHash ); 56 57 // overwrite initialIndex 58 if ( this.options.initialIndex === undefined && location.hash ) { 59 var cell = this.queryCell( location.hash ); 60 if ( cell ) { 61 this.options.initialIndex = this.getCellSlideIndex( cell ); 62 } 63 } 64 65 this.connectHashLinks(); 66}; 67 68 69proto.deactivateHash = function() { 70 this.off( 'change', this.onChangeHash ); 71 this.disconnectHashLinks(); 72}; 73 74proto.onChangeHash = function() { 75 var id = this.selectedElement.id; 76 if ( id ) { 77 var url = '#' + id; 78 history.replaceState( null, '', url ); 79 } 80}; 81 82 83proto.connectHashLinks = function() { 84 var links = document.querySelectorAll('a'); 85 for ( var i=0; i < links.length; i++ ) { 86 this.connectHashLink( links[i] ); 87 } 88}; 89 90// used to test if link is on same page 91var proxyLink = document.createElement('a'); 92 93proto.connectHashLink = function( link ) { 94 if ( !link.hash ) { 95 return; 96 } 97 // check that link is for the same page 98 proxyLink.href = link.href; 99 if ( proxyLink.pathname != location.pathname ) { 100 return; 101 } 102 var cell = this.queryCell( link.hash ); 103 if ( !cell ) { 104 return; 105 } 106 link.addEventListener( 'click', this.onHashLinkClick ); 107 this.connectedHashLinks.push( link ); 108}; 109 110proto.disconnectHashLinks = function() { 111 this.connectedHashLinks.forEach( function( link ) { 112 link.removeEventListener( 'click', this.onHashLinkClick ); 113 }, this ); 114 this.connectedHashLinks = []; 115}; 116 117// ----- ----- // 118 119return Flickity; 120 121}));
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.