diff options
author | Robert Speicher <rspeicher@gmail.com> | 2015-06-19 02:01:53 -0400 |
---|---|---|
committer | Robert Speicher <rspeicher@gmail.com> | 2015-06-19 04:31:24 -0400 |
commit | e59aad6e83cbdafcaf100bb86f6fb925f2fb779e (patch) | |
tree | af61d3de4ff6cdab28505ef8ff7d8a630164ae6a /app/assets | |
parent | 32366d18118281b32b5e770824d637a01d15093b (diff) | |
download | gitlab-ce-e59aad6e83cbdafcaf100bb86f6fb925f2fb779e.tar.gz |
Refactor LineHighlighterrs-blobview-js-refactor
Diffstat (limited to 'app/assets')
-rw-r--r-- | app/assets/javascripts/line_highlighter.js.coffee | 70 |
1 files changed, 29 insertions, 41 deletions
diff --git a/app/assets/javascripts/line_highlighter.js.coffee b/app/assets/javascripts/line_highlighter.js.coffee index a60a04783ac..d7de5516421 100644 --- a/app/assets/javascripts/line_highlighter.js.coffee +++ b/app/assets/javascripts/line_highlighter.js.coffee @@ -26,9 +26,13 @@ # </pre> # </div> # </div> +# class @LineHighlighter + # CSS class applied to highlighted lines + highlightClass: 'hll' + # Internal copy of location.hash so we're not dependent on `location` in tests - @_hash = '' + _hash: '' # Initialize a LineHighlighter object # @@ -41,7 +45,7 @@ class @LineHighlighter unless hash == '' range = @hashToRange(hash) - unless isNaN(range[0]) + if range[0] @highlightRange(range) # Scroll to the first highlighted line on initial load @@ -69,7 +73,7 @@ class @LineHighlighter lineNumber = $(event.target).data('line-number') current = @hashToRange(@_hash) - if isNaN(current[0]) or !event.shiftKey + unless current[0] and event.shiftKey # If there's no current selection, or there is but Shift wasn't held, # treat this like a single-line selection. @setHash(lineNumber) @@ -85,7 +89,7 @@ class @LineHighlighter # Unhighlight previously highlighted lines clearHighlight: -> - $('.hll').removeClass('hll') + $(".#{@highlightClass}").removeClass(@highlightClass) # Convert a URL hash String into line numbers # @@ -93,60 +97,44 @@ class @LineHighlighter # # Examples: # - # hashToRange('#L5') # => [5, NaN] + # hashToRange('#L5') # => [5, null] # hashToRange('#L5-15') # => [5, 15] - # hashToRange('#foo') # => [NaN, NaN] + # hashToRange('#foo') # => [null, null] # # Returns an Array hashToRange: (hash) -> - first = parseInt(hash.replace(/^#L(\d+)/, '$1')) - last = parseInt(hash.replace(/^#L\d+-(\d+)/, '$1')) + matches = hash.match(/^#?L(\d+)(?:-(\d+))?$/) + + if matches and matches.length + first = parseInt(matches[1]) + last = matches[2] and parseInt(matches[2]) or null - [first, last] + [first, last] + else + [null, null] # Highlight a single line # - # lineNumber - Number to highlight. Must be parsable as an Integer. - # - # Returns undefined if lineNumber is not parsable as an Integer. - highlightLine: (lineNumber) -> - return if isNaN(parseInt(lineNumber)) - - $("#LC#{lineNumber}").addClass('hll') + # lineNumber - Line number to highlight + highlightLine: (lineNumber) => + $("#LC#{lineNumber}").addClass(@highlightClass) # Highlight all lines within a range # - # range - An Array of starting and ending line numbers. - # - # Examples: - # - # # Highlight lines 5 through 15 - # highlightRange([5, 15]) - # - # # The first value is required, and must be a number - # highlightRange(['foo', 15]) # Invalid, returns undefined - # highlightRange([NaN, NaN]) # Invalid, returns undefined - # - # # The second value is optional; if omitted, only highlights the first line - # highlightRange([5, NaN]) # Valid - # - # Returns undefined if the first line is NaN. + # range - Array containing the starting and ending line numbers highlightRange: (range) -> - return if isNaN(range[0]) - - if isNaN(range[1]) - @highlightLine(range[0]) - else + if range[1] for lineNumber in [range[0]..range[1]] @highlightLine(lineNumber) + else + @highlightLine(range[0]) + # Set the URL hash string setHash: (firstLineNumber, lastLineNumber) => - return if isNaN(parseInt(firstLineNumber)) - - if isNaN(parseInt(lastLineNumber)) - hash = "#L#{firstLineNumber}" - else + if lastLineNumber hash = "#L#{firstLineNumber}-#{lastLineNumber}" + else + hash = "#L#{firstLineNumber}" @_hash = hash @__setLocationHash__(hash) |