store the last selected line and use it as the other end for multiple selection

This commit is contained in:
Damien Mathieu 2014-01-09 17:31:47 +01:00
parent ff1aad3f03
commit 228afe9a63
2 changed files with 32 additions and 8 deletions

View File

@ -2,9 +2,11 @@ class Travis.LinesSelector
element: null
scroll: null
location: null
last_selected_line: null
constructor: (@element, @scroll, @location) ->
Ember.run.scheduleOnce 'afterRender', this, ->
@last_selected_line = @getSelectedLines()?.first
@highlightLines()
@element.on 'click', 'a', (event) =>
@ -25,19 +27,19 @@ class Travis.LinesSelector
@removeAllHighlights()
if lines = @getSelectedLines()
@element.find('p:visible').slice(lines.first, lines.last).addClass('highlight')
@element.find('p:visible').slice(lines.first - 1, lines.last).addClass('highlight')
@scroll.tryScroll()
setHashValueWithLine: (line, multiple) ->
line_number = @getLineNumberFromElement(line)
if !multiple
hash = "#L#{line_number}"
else
selected_line = @element.find('p:visible.highlight:first')[0]
selected_number = @getLineNumberFromElement(selected_line)
lines = [line_number, selected_number].sort (a,b) -> a - b
if multiple && @last_selected_line?
lines = [line_number, @last_selected_line].sort (a,b) -> a - b
hash = "#L#{lines[0]}-L#{lines[1]}"
else
hash = "#L#{line_number}"
@last_selected_line = line_number
@location.hash = hash
getLineNumberFromElement: (element) ->
@ -48,6 +50,6 @@ class Travis.LinesSelector
getSelectedLines: ->
if match = @location.hash.match(/#L(\d+)(-L(\d+))?$/)
first = match[1] - 1
first = match[1]
last = match[3] || match[1]
{first: first, last: last}

View File

@ -57,6 +57,7 @@ test "selects multiple lines", ->
fakeLocation.hash = '#L2'
Ember.run ->
new Travis.LinesSelector(element, fakeScroll, fakeLocation)
wait().then ->
equal($('#fakeLog p.highlight').length, 1)
@ -68,3 +69,24 @@ test "selects multiple lines", ->
equal($('#fakeLog p:nth-child(1)').hasClass('highlight'), true)
equal($('#fakeLog p:nth-child(2)').hasClass('highlight'), true)
equal('#L1-L2', fakeLocation.hash)
test "uses the last selected line as second selection line", ->
selector = null
Ember.run ->
selector = new Travis.LinesSelector(element, fakeScroll, fakeLocation)
wait().then ->
$('#fakeLog p:last a').click()
equal($('#fakeLog p.highlight').length, 1)
equal(3, selector.last_selected_line)
event = jQuery.Event('click')
event.shiftKey = true
$('#fakeLog p:first a').trigger(event)
equal($('#fakeLog p.highlight').length, 3)
equal($('#fakeLog p:nth-child(1)').hasClass('highlight'), true)
equal($('#fakeLog p:nth-child(2)').hasClass('highlight'), true)
equal($('#fakeLog p:nth-child(3)').hasClass('highlight'), true)
equal('#L1-L3', fakeLocation.hash)
equal(1, selector.last_selected_line)