Skip to content

Commit

Permalink
Fixed a bug in the Tag filter when annotations had no tags.
Browse files Browse the repository at this point in the history
Moved filter callback into class method on the Tags object and added
unit tests
  • Loading branch information
aron committed Jul 4, 2011
1 parent 6341bdd commit cd3ea64
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
39 changes: 26 additions & 13 deletions src/plugin/tags.coffee
Expand Up @@ -5,7 +5,7 @@ class Annotator.Plugin.Tags extends Annotator.Plugin
# The field element added to the Annotator.Editor wrapped in jQuery. Cached to
# save having to recreate it everytime the editor is displayed.
field: null

# The input element added to the Annotator.Editor wrapped in jQuery. Cached to
# save having to recreate it everytime the editor is displayed.
input: null
Expand All @@ -30,20 +30,10 @@ class Annotator.Plugin.Tags extends Annotator.Plugin

# Add a filter to the Filter plugin if loaded.
if @annotator.plugins.Filter
@annotator.plugins.Filter.addFilter({
@annotator.plugins.Filter.addFilter
label: 'Tag'
property: 'tags'
isFiltered: (input, tags) ->
if input and tags?.length
matched = 0
keywords = input.split(/\s+/g)
for keyword in keywords
for tag in tags
if tag.indexOf(keyword) != -1
matched += 1

matched == keywords.length
})
isFiltered: Annotator.Plugin.Tags.filterCallback

@input = $(@field).find(':input')

Expand Down Expand Up @@ -138,3 +128,26 @@ class Annotator.Plugin.Tags extends Annotator.Plugin
)
else
field.remove()

# Checks an input string of keywords against an array of tags. If the keywords
# match _all_ tags the function returns true. This should be used as a callback
# in the Filter plugin.
#
# input - A String of keywords from a input field.
#
# Examples
#
# Tags.filterCallback('cat dog mouse', ['cat', 'dog', 'mouse']) //=> true
# Tags.filterCallback('cat dog', ['cat', 'dog', 'mouse']) //=> true
# Tags.filterCallback('cat dog', ['cat']) //=> false
#
# Returns true if the input keywords match all tags.
Annotator.Plugin.Tags.filterCallback = (input, tags = []) ->
matches = 0
keywords = []
if input
keywords = input.split(/\s+/g)
for keyword in keywords when tags.length
matches += 1 for tag in tags when tag.indexOf(keyword) != -1

matches == keywords.length
13 changes: 13 additions & 0 deletions test/spec/plugin/tags_spec.coffee
Expand Up @@ -74,3 +74,16 @@ describe 'Annotator.Plugin.Tags', ->

plugin.updateViewer(field, annotation)
expect($(field).parent().length).toEqual(0)


describe 'Annotator.Plugin.Tags.filterCallback', ->
filter = null
beforeEach -> filter = Annotator.Plugin.Tags.filterCallback

it 'should return true if all tags are matched by keywords', ->
expect(filter('cat dog mouse', ['cat', 'dog', 'mouse'])).toBe(true)
expect(filter('cat dog', ['cat', 'dog', 'mouse'])).toBe(true)

it 'should NOT return true if all tags are NOT matched by keywords', ->
expect(filter('cat dog', ['cat'])).toBe(false)
expect(filter('cat dog', [])).toBe(false)

0 comments on commit cd3ea64

Please sign in to comment.