From 492802920eacf3e7da18232f97fbfb5f3daa80a0 Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Mon, 20 Jan 2014 14:14:11 +0100 Subject: [PATCH] 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 --- assets/scripts/app/views/log.coffee | 2 +- assets/scripts/lib/travis/lines_selector.coffee | 17 +++++++++++++---- .../scripts/spec/unit/line_selector_spec.coffee | 8 +++++++- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/assets/scripts/app/views/log.coffee b/assets/scripts/app/views/log.coffee index 631765b3..87bf913b 100644 --- a/assets/scripts/app/views/log.coffee +++ b/assets/scripts/app/views/log.coffee @@ -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: -> diff --git a/assets/scripts/lib/travis/lines_selector.coffee b/assets/scripts/lib/travis/lines_selector.coffee index e49a456e..ba4242cb 100644 --- a/assets/scripts/lib/travis/lines_selector.coffee +++ b/assets/scripts/lib/travis/lines_selector.coffee @@ -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} diff --git a/assets/scripts/spec/unit/line_selector_spec.coffee b/assets/scripts/spec/unit/line_selector_spec.coffee index cbd4f12b..04e882cf 100644 --- a/assets/scripts/spec/unit/line_selector_spec.coffee +++ b/assets/scripts/spec/unit/line_selector_spec.coffee @@ -1,4 +1,10 @@ -fakeLocation = {} +fakeLocation = { + getHash: -> + @hash || '' + setHash: (hash) -> + @hash = hash +} + fakeScroll = tryScroll: sinon.spy()