Skip to content

Commit

Permalink
Filter plugin no longer tries to navigate annotations if there are none
Browse files Browse the repository at this point in the history
  • Loading branch information
aron committed Jun 30, 2011
1 parent 33a595e commit 8bf784a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 26 deletions.
52 changes: 27 additions & 25 deletions src/plugin/filter.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -287,26 +287,39 @@ class Annotator.Plugin.Filter extends Annotator.Plugin
filter = $(event.target).parent().data('filter')
this.updateFilter filter if filter

# Locates the next highlighted element in @highlights from the current one
# or goes to the very first element.
# Locates the next/previous highlighted element in @highlights from the
# current one or goes to the very first/last element respectively.
#
# event - A click Event.
# previous - If true finds the previously highlighted element.
#
# Returns nothing
_onNextClick: (event) =>
# Returns itself.
_findNextHighlight: (previous) ->
return this unless @highlights.length

offset = if previous then 0 else -1
resetOffset = if previous then -1 else 0
operator = if previous then 'lt' else 'gt'

active = @highlights.not('.' + @classes.hl.hide)
current = active.filter('.' + @classes.hl.active)
unless current.length
current = active.eq(-1)
current = active.eq(offset) unless current.length

annotation = current.data('annotation')
annotation = current.data 'annotation'

index = active.index(current[0])
next = active.filter(':gt(' + index + ')').not(annotation.highlights).eq(0)
unless next.length
next = active.eq(0)
index = active.index current[0]
next = active.filter(":#{operator}(#{index})").not(annotation.highlights).eq(resetOffset)
next = active.eq(resetOffset) unless next.length

this._scrollToHighlight(next.data('annotation').highlights)
this._scrollToHighlight next.data('annotation').highlights

# Locates the next highlighted element in @highlights from the current one
# or goes to the very first element.
#
# event - A click Event.
#
# Returns nothing
_onNextClick: (event) =>
this._findNextHighlight()

# Locates the previous highlighted element in @highlights from the current one
# or goes to the very last element.
Expand All @@ -315,18 +328,7 @@ class Annotator.Plugin.Filter extends Annotator.Plugin
#
# Returns nothing
_onPreviousClick: (event) =>
active = @highlights.not('.' + @classes.hl.hide)
current = active.filter('.' + @classes.hl.active)
unless current.length
current = active.eq(0)
annotation = current.data('annotation')

index = active.index(current[0])
prev = active.filter(':lt(' + index + ')').not(annotation.highlights).eq(-1)
unless prev.length
prev = active.eq(-1)

this._scrollToHighlight(prev.data('annotation').highlights)
this._findNextHighlight true

# Scrolls to the highlight provided. An adds an active class to it.
#
Expand Down
12 changes: 11 additions & 1 deletion test/spec/plugin/filter_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ describe "Filter", ->
plugin._onNextClick()
expect(plugin._scrollToHighlight).toHaveBeenCalledWith([element3[0]])

it "should do nothing if there are no annotations", ->
plugin.highlights = $()
plugin._onNextClick()
expect(plugin._scrollToHighlight).not.toHaveBeenCalled()

describe "_onPreviousClick", ->
it "should advance to the previous element", ->
element3.addClass(plugin.classes.hl.active)
Expand All @@ -342,9 +347,14 @@ describe "Filter", ->
it "should only navigate through non hidden elements", ->
element3.addClass(plugin.classes.hl.active)
element2.addClass(plugin.classes.hl.hide)
plugin._onNextClick()
plugin._onPreviousClick()
expect(plugin._scrollToHighlight).toHaveBeenCalledWith([element1[0]])

it "should do nothing if there are no annotations", ->
plugin.highlights = $()
plugin._onPreviousClick()
expect(plugin._scrollToHighlight).not.toHaveBeenCalled()

describe "_scrollToHighlight", ->
mockjQuery = null

Expand Down

0 comments on commit 8bf784a

Please sign in to comment.