Skip to content
This repository has been archived by the owner on Jul 28, 2018. It is now read-only.

Commit

Permalink
Make Turbolinks.visit() with :change scroll to the #hash by default
Browse files Browse the repository at this point in the history
Fixes #589.
  • Loading branch information
Thibaut committed Oct 4, 2015
1 parent 8446ca1 commit 01ec408
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/assets/javascripts/turbolinks.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ fetch = (url, options = {}) ->
options.showProgressBar = false
options.scroll = false
else
options.scroll ?= false if options.change
options.scroll ?= (url.hash || false) if options.change

fetchReplacement url, options

Expand Down Expand Up @@ -264,6 +264,8 @@ updateScrollPosition = (position) ->
if Array.isArray(position)
window.scrollTo position[0], position[1]
else if position isnt false
if typeof position is 'string' and (element = document.querySelector(position))

This comment has been minimized.

Copy link
@tortuetorche

tortuetorche Oct 5, 2015

Only elements with IDs are supported here. But browser will natively also scroll to matching <a name="..."> elements.
It will be good to support those as well, isn't it ?

See: defunkt/jquery-pjax@fab2641
And the PJAX code we can borrowed.

This comment has been minimized.

Copy link
@Thibaut

Thibaut Oct 7, 2015

Author Collaborator

@tortuetorche Thanks. I'll make the change soon.

element.scrollIntoView()
if document.location.hash
document.location.href = document.location.href
rememberCurrentUrlAndState()
Expand Down
6 changes: 6 additions & 0 deletions test/javascript/turbolinks_visit_test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,12 @@ suite 'Turbolinks.visit()', ->
@Turbolinks.enableTransitionCache()
@Turbolinks.visit('iframe2.html')

test "scroll to #anchor with :change", (done) ->
@document.addEventListener 'page:partial-load', =>
assert.closeTo @window.pageYOffset, @$('#change').offsetTop, 100
done()
@Turbolinks.visit('iframe2.html#change', change: ['change'])

test "doesn't scroll to top with :change", (done) ->
@window.scrollTo(42, 42)
@document.addEventListener 'page:partial-load', =>
Expand Down

0 comments on commit 01ec408

Please sign in to comment.