From 228afe9a636083a642fdac7171dd40b3b517acfe Mon Sep 17 00:00:00 2001 From: Damien Mathieu <42@dmathieu.com> Date: Thu, 9 Jan 2014 17:31:47 +0100 Subject: [PATCH] store the last selected line and use it as the other end for multiple selection --- .../scripts/lib/travis/lines_selector.coffee | 18 ++++++++------- .../spec/unit/line_selector_spec.coffee | 22 +++++++++++++++++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/assets/scripts/lib/travis/lines_selector.coffee b/assets/scripts/lib/travis/lines_selector.coffee index 1e3cfdc5..e49a456e 100644 --- a/assets/scripts/lib/travis/lines_selector.coffee +++ b/assets/scripts/lib/travis/lines_selector.coffee @@ -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} diff --git a/assets/scripts/spec/unit/line_selector_spec.coffee b/assets/scripts/spec/unit/line_selector_spec.coffee index 6b2c177a..cbd4f12b 100644 --- a/assets/scripts/spec/unit/line_selector_spec.coffee +++ b/assets/scripts/spec/unit/line_selector_spec.coffee @@ -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)