@@ -27,6 +27,7 @@ export default class extends Component {
2727 this . handleQueryIndexChange = this . handleQueryIndexChange . bind ( this ) ;
2828 this . isElementInViewPort = this . isElementInViewPort . bind ( this ) ;
2929 this . setVisibleQueryIndex = this . setVisibleQueryIndex . bind ( this ) ;
30+ this . animateScroll = this . animateScroll . bind ( this ) ;
3031 this . debounceScrolling = this . debounceScrolling . bind ( this ) ;
3132 this . scrollListener = this . scrollListener . bind ( this ) ;
3233 this . copyURL = this . copyURL . bind ( this ) ;
@@ -56,7 +57,7 @@ export default class extends Component {
5657 this . setState ( { queryIndex : index + 1 } ) ;
5758 }
5859 window . addEventListener ( 'scroll' , this . scrollListener ) ;
59- $ ( 'a[href^="#Query_"]' ) . on ( 'click' , this . animateAnchorElements ) ;
60+ $ ( 'a[href^="#Query_"]' ) . on ( 'click' , this . animateScroll ) ;
6061 }
6162 componentWillUnmount ( ) {
6263 window . removeEventListener ( 'scroll' , this . scrollListener ) ;
@@ -99,25 +100,34 @@ export default class extends Component {
99100 let hash = `#Query_${ queryIndex } ` ;
100101 // if we can guarantee that the browser can handle change in url hash without the page jumping,
101102 // then we update the url hash after scroll. else, hash is only updated on click of next or prev button
102- if ( window . history . pushState ) {
103- window . history . pushState ( null , null , hash ) ;
104- }
103+ this . replaceUrlHash ( hash ) ;
105104 this . setState ( { queryIndex } ) ;
106105 }
107106 }
108- animateAnchorElements ( e ) {
109- // allow normal behavior in test mode to prevent warnings or errors from jquery
107+
108+ replaceUrlHash ( hash ) {
109+ if ( window . history . replaceState ) {
110+ window . history . replaceState ( null , null , hash ) ;
111+ } else {
112+ window . location . hash = hash ;
113+ }
114+ }
115+
116+ /**
117+ * this method is called on the element to be brought to the top of the page and animates the scroll process
118+ * @param {* } e target element
119+ */
120+ animateScroll ( e ) {
121+ // allow normal scroll behavior in test mode to prevent warnings or errors from jquery
110122 if ( isTestMode ( ) ) return ;
111123 e . preventDefault ( ) ;
124+ const hash = e . target . hash ;
112125 $ ( 'html, body' ) . animate ( {
113- scrollTop : $ ( this . hash ) . offset ( ) . top
126+ scrollTop : $ ( hash ) . offset ( ) . top
114127 } , 300 ) ;
115- if ( window . history . pushState ) {
116- window . history . pushState ( null , null , this . hash ) ;
117- } else {
118- window . location . hash = this . hash ;
119- }
128+ this . replaceUrlHash ( hash ) ;
120129 }
130+
121131 isElementInViewPort ( elem ) {
122132 const { top, left, right, bottom } = elem . getBoundingClientRect ( ) ;
123133 return (
@@ -146,8 +156,7 @@ export default class extends Component {
146156 anchorEl . setAttribute ( 'href' , '#Query_' + this . props . data . queries [ nextQuery - 1 ] . number ) ;
147157 anchorEl . setAttribute ( 'hidden' , true ) ;
148158 document . body . appendChild ( anchorEl ) ;
149- // add smooth scrolling animation with jquery
150- $ ( anchorEl ) . on ( 'click' , this . animateAnchorElements ) ;
159+ $ ( anchorEl ) . on ( 'click' , this . animateScroll ) ;
151160 anchorEl . click ( ) ;
152161 document . body . removeChild ( anchorEl ) ;
153162 this . setState ( { queryIndex : nextQuery } ) ;
0 commit comments