Add getLineNumbersForClass()

Also clean up and use klass
This commit is contained in:
Ben Ogle 2013-10-07 13:57:00 -07:00
parent 964e88f131
commit 1ff97fc21a
2 changed files with 41 additions and 22 deletions

View File

@ -1900,7 +1900,7 @@ describe "Editor", ->
expect($(element)).toHaveClass('line-number-3') expect($(element)).toHaveClass('line-number-3')
it "returns falsy when there is no line element", -> it "returns falsy when there is no line element", ->
expect(editor.gutter.getLineNumberElement(42)).toBeFalsy() expect(editor.gutter.getLineNumberElement(42).length).toBeFalsy()
it "can add and remove classes to all the line numbers", -> it "can add and remove classes to all the line numbers", ->
elements = editor.gutter.addClassToAllLines('heyok') elements = editor.gutter.addClassToAllLines('heyok')
@ -1916,6 +1916,19 @@ describe "Editor", ->
element = editor.gutter.getLineNumberElement(2) element = editor.gutter.getLineNumberElement(2)
expect($(element)).not.toHaveClass('heyok') expect($(element)).not.toHaveClass('heyok')
it "can fetch line numbers by their class", ->
editor.gutter.addClassToLine(1, 'heyok')
editor.gutter.addClassToLine(3, 'heyok')
elements = editor.gutter.getLineNumberElementsForClass('heyok')
expect(elements.length).toBe 2
expect($(elements[0])).toHaveClass 'line-number-1'
expect($(elements[0])).toHaveClass 'heyok'
expect($(elements[1])).toHaveClass 'line-number-3'
expect($(elements[1])).toHaveClass 'heyok'
describe "gutter line highlighting", -> describe "gutter line highlighting", ->
beforeEach -> beforeEach ->
editor.attachToDom(heightInLines: 5.5) editor.attachToDom(heightInLines: 5.5)

View File

@ -66,51 +66,57 @@ class Gutter extends View
# #
# Returns a list of {HTMLElement}s. # Returns a list of {HTMLElement}s.
getLineNumberElements: -> getLineNumberElements: ->
@lineNumbers[0].getElementsByClassName('line-number') @lineNumbers[0].childNodes
# Get all the line-number divs.
#
# Returns a list of {HTMLElement}s.
getLineNumberElementsForClass: (klass) ->
@lineNumbers[0].getElementsByClassName(klass)
# Get a single line-number div. # Get a single line-number div.
# #
# * lineNumber: 0 based line number # * bufferRow: 0 based line number
# #
# Returns a {HTMLElement} # Returns a list of {HTMLElement}s that correspond to the bufferRow
getLineNumberElement: (lineNumber) -> getLineNumberElement: (bufferRow) ->
@lineNumbers[0].getElementsByClassName("line-number-#{lineNumber}")[0] @getLineNumberElementsForClass("line-number-#{bufferRow}")
# Add a class to all line-number divs. # Add a class to all line-number divs.
# #
# * clas: string class name # * klass: string class name
# #
# Returns a list of {HTMLElement}s. # Returns a list of {HTMLElement}s.
addClassToAllLines: (clas)-> addClassToAllLines: (klass)->
$.fn.addClass.call(@getLineNumberElements(), clas) $.fn.addClass.call(@getLineNumberElements(), klass)
# Remove a class from all line-number divs. # Remove a class from all line-number divs.
# #
# * clas: string class name # * klass: string class name
# #
# Returns a list of {HTMLElement}s. # Returns a list of {HTMLElement}s.
removeClassFromAllLines: (clas)-> removeClassFromAllLines: (klass)->
$.fn.removeClass.call(@getLineNumberElements(), clas) $.fn.removeClass.call(@getLineNumberElements(), klass)
# Add a class to a single line-number div # Add a class to a single line-number div
# #
# * lineNumber: 0 based line number # * bufferRow: 0 based line number
# * clas: string class name # * klass: string class name
# #
# Returns the {HTMLElement} on which the class was set. undefined if the line was not found # Returns the {HTMLElement} on which the class was set. undefined if the line was not found
addClassToLine: (lineNumber, clas)-> addClassToLine: (bufferRow, klass)->
line = @lineNumbers[0].getElementsByClassName("line-number-#{lineNumber}") line = @getLineNumberElement(bufferRow)
$.fn.addClass.call(line, clas) if line $.fn.addClass.call(line, klass) if line and line.length
# Remove a class from a single line-number div # Remove a class from a single line-number div
# #
# * lineNumber: 0 based line number # * bufferRow: 0 based line number
# * clas: string class name # * klass: string class name
# #
# Returns the {HTMLElement} on which the class was set. undefined if the line was not found # Returns the {HTMLElement} on which the class was set. undefined if the line was not found
removeClassFromLine: (lineNumber, clas)-> removeClassFromLine: (bufferRow, klass)->
line = @lineNumbers[0].getElementsByClassName("line-number-#{lineNumber}") line = @getLineNumberElement(bufferRow)
$.fn.removeClass.call(line, clas) if line $.fn.removeClass.call(line, klass) if line and line.length
### Internal ### ### Internal ###