Use pushState to set urls

It seems that directly setting location.hash directly doesn't play nice
with Ember.js URL handling - using it to handle line numbers results in
weird bugs (URL stops being updated after setting hash manually).

This commit gets back to using window.history.pushState() which was
changed to direct hash manipulation in ff1aad3
This commit is contained in:
Piotr Sarnacki 2014-01-20 14:14:11 +01:00
parent 84037fb3c3
commit 492802920e
3 changed files with 21 additions and 6 deletions

View File

@ -47,7 +47,7 @@ Travis.reopen
console.log 'log view: create engine' if Log.DEBUG
@scroll = new Log.Scroll
@engine = Log.create(limit: Log.LIMIT, listeners: [@scroll])
@lineSelector = new Travis.LinesSelector(@$().find('#log'), @scroll, window.location)
@lineSelector = new Travis.LinesSelector(@$().find('#log'), @scroll)
@observeParts()
observeParts: ->

View File

@ -1,10 +1,19 @@
class Travis.LinesSelector
Location:
getHash: ->
window.location.hash
setHash: (hash) ->
path = "#{window.location.pathname}#{hash}"
window.history.pushState({ path: path }, null, path);
element: null
scroll: null
location: null
last_selected_line: null
constructor: (@element, @scroll, @location) ->
constructor: (@element, @scroll, location) ->
@location = location || @Location
Ember.run.scheduleOnce 'afterRender', this, ->
@last_selected_line = @getSelectedLines()?.first
@highlightLines()
@ -17,7 +26,7 @@ class Travis.LinesSelector
false
willDestroy: ->
@location.hash = ''
@location.setHash('')
loadLineNumbers: (element, multiple) ->
@setHashValueWithLine(element, multiple)
@ -40,7 +49,7 @@ class Travis.LinesSelector
hash = "#L#{line_number}"
@last_selected_line = line_number
@location.hash = hash
@location.setHash(hash)
getLineNumberFromElement: (element) ->
@element.find('p:visible').index(element) + 1
@ -49,7 +58,7 @@ class Travis.LinesSelector
@element.find('p.highlight').removeClass('highlight')
getSelectedLines: ->
if match = @location.hash.match(/#L(\d+)(-L(\d+))?$/)
if match = @location.getHash().match(/#L(\d+)(-L(\d+))?$/)
first = match[1]
last = match[3] || match[1]
{first: first, last: last}

View File

@ -1,4 +1,10 @@
fakeLocation = {}
fakeLocation = {
getHash: ->
@hash || ''
setHash: (hash) ->
@hash = hash
}
fakeScroll =
tryScroll: sinon.spy()