Commit 1cb6e98d authored by Aron Carroll's avatar Aron Carroll

Add additional helper methods to annotatorUI service

i
These also now always set new map instances so that the angular
watcher picks up changes without having to do a deep comparison.
parent 6edf26fb
...@@ -2,6 +2,9 @@ ...@@ -2,6 +2,9 @@
# attached iframes for display in the sidebar. This covers both tool and # attached iframes for display in the sidebar. This covers both tool and
# rendered state such as selected highlights. # rendered state such as selected highlights.
createAnnotationUI = -> createAnnotationUI = ->
value = (selection) ->
if Object.keys(selection).length then selection else null
{ {
TOOL_COMMENT: 'comment' TOOL_COMMENT: 'comment'
TOOL_HIGHLIGHT: 'highlight' TOOL_HIGHLIGHT: 'highlight'
...@@ -10,8 +13,10 @@ createAnnotationUI = -> ...@@ -10,8 +13,10 @@ createAnnotationUI = ->
visibleHighlights: false visibleHighlights: false
# Contains a map of annotation id:true pairs.
focusedAnnotationMap: null focusedAnnotationMap: null
# Contains a map of annotation id:true pairs.
selectedAnnotationMap: null selectedAnnotationMap: null
###* ###*
...@@ -24,7 +29,23 @@ createAnnotationUI = -> ...@@ -24,7 +29,23 @@ createAnnotationUI = ->
focusAnnotations: (annotations) -> focusAnnotations: (annotations) ->
selection = {} selection = {}
selection[id] = true for {id} in annotations selection[id] = true for {id} in annotations
@focusedAnnotationMap = selection @focusedAnnotationMap = value(selection)
###*
# @ngdoc method
# @name annotationUI.hasSelectedAnnotations
# @returns true if there are any selected annotations.
###
hasSelectedAnnotations: ->
!!@selectedAnnotationMap
###*
# @ngdoc method
# @name annotationUI.isAnnotationSelected
# @returns true if the provided annotation is selected.
###
isAnnotationSelected: (id) ->
!!@selectedAnnotationMap?[id]
###* ###*
# @ngdoc method # @ngdoc method
...@@ -36,7 +57,7 @@ createAnnotationUI = -> ...@@ -36,7 +57,7 @@ createAnnotationUI = ->
selectAnnotations: (annotations) -> selectAnnotations: (annotations) ->
selection = {} selection = {}
selection[id] = true for {id} in annotations selection[id] = true for {id} in annotations
@selectedAnnotationMap = selection @selectedAnnotationMap = value(selection)
###* ###*
# @ngdoc method # @ngdoc method
...@@ -46,13 +67,13 @@ createAnnotationUI = -> ...@@ -46,13 +67,13 @@ createAnnotationUI = ->
# selectedAnnotationMap if not present otherwise removes them. # selectedAnnotationMap if not present otherwise removes them.
### ###
xorSelectedAnnotations: (annotations) -> xorSelectedAnnotations: (annotations) ->
selection = @selectedAnnotationMap or {} selection = angular.extend({}, @selectedAnnotationMap)
for {id} in annotations for {id} in annotations
if selection[id] if selection[id]
delete selection[id] delete selection[id]
else else
selection[id] = true selection[id] = true
@selectedAnnotationMap = selection @selectedAnnotationMap = value(selection)
###* ###*
# @ngdoc method # @ngdoc method
...@@ -61,8 +82,19 @@ createAnnotationUI = -> ...@@ -61,8 +82,19 @@ createAnnotationUI = ->
# @description removes an annotation from the current selection. # @description removes an annotation from the current selection.
### ###
removeSelectedAnnotation: (annotation) -> removeSelectedAnnotation: (annotation) ->
return unless @selectedAnnotationMap selection = angular.extend({}, @selectedAnnotationMap)
delete @selectedAnnotationMap[annotation.id] if selection
delete selection[annotation.id]
@selectedAnnotationMap = value(selection)
###*
# @ngdoc method
# @name annotationUI.clearSelectedAnnotations()
# @returns nothing
# @description removes all annotations from the current selection.
###
clearSelectedAnnotations: ->
@selectedAnnotationMap = null
} }
angular.module('h').factory('annotationUI', createAnnotationUI) angular.module('h').factory('annotationUI', createAnnotationUI)
...@@ -22,6 +22,38 @@ describe 'AnnotationUI', -> ...@@ -22,6 +22,38 @@ describe 'AnnotationUI', ->
2: true, 3: true 2: true, 3: true
}) })
it 'does not modify the original map object', ->
orig = annotationUI.focusedAnnotationMap = {1: true}
annotationUI.focusAnnotations([{id: 2}, {id: 3}])
assert.notEqual(annotationUI.focusedAnnotationMap, orig)
it 'nulls the map if no annotations are focused', ->
orig = annotationUI.focusedAnnotationMap = {1: true}
annotationUI.focusAnnotations([])
assert.isNull(annotationUI.focusedAnnotationMap)
describe '.hasSelectedAnnotations', ->
it 'returns true if there are any selected annotations', ->
annotationUI.selectedAnnotationMap = {1: true}
assert.isTrue(annotationUI.hasSelectedAnnotations())
it 'returns false if there are no selected annotations', ->
annotationUI.selectedAnnotationMap = null
assert.isFalse(annotationUI.hasSelectedAnnotations())
describe '.isAnnotationSelected', ->
it 'returns true if the id provided is selected', ->
annotationUI.selectedAnnotationMap = {1: true}
assert.isTrue(annotationUI.isAnnotationSelected(1))
it 'returns false if the id provided is not selected', ->
annotationUI.selectedAnnotationMap = {1: true}
assert.isFalse(annotationUI.isAnnotationSelected(2))
it 'returns false if there are no selected annotations', ->
annotationUI.selectedAnnotationMap = null
assert.isFalse(annotationUI.isAnnotationSelected(1))
describe '.selectAnnotations()', -> describe '.selectAnnotations()', ->
it 'adds the passed annotations to the selectedAnnotationMap', -> it 'adds the passed annotations to the selectedAnnotationMap', ->
annotationUI.selectAnnotations([{id: 1}, {id: 2}, {id: 3}]) annotationUI.selectAnnotations([{id: 1}, {id: 2}, {id: 3}])
...@@ -36,6 +68,16 @@ describe 'AnnotationUI', -> ...@@ -36,6 +68,16 @@ describe 'AnnotationUI', ->
2: true, 3: true 2: true, 3: true
}) })
it 'does not modify the original map object', ->
orig = annotationUI.selectedAnnotationMap = {1: true}
annotationUI.selectAnnotations([{id: 2}, {id: 3}])
assert.notEqual(annotationUI.selectedAnnotationMap, orig)
it 'nulls the map if no annotations are selected', ->
orig = annotationUI.selectedAnnotationMap = {1: true}
annotationUI.selectAnnotations([])
assert.isNull(annotationUI.selectedAnnotationMap)
describe '.xorSelectedAnnotations()', -> describe '.xorSelectedAnnotations()', ->
it 'adds annotations missing from the selectedAnnotationMap', -> it 'adds annotations missing from the selectedAnnotationMap', ->
annotationUI.selectedAnnotationMap = {1: true, 2: true} annotationUI.selectedAnnotationMap = {1: true, 2: true}
...@@ -43,10 +85,21 @@ describe 'AnnotationUI', -> ...@@ -43,10 +85,21 @@ describe 'AnnotationUI', ->
assert.deepEqual(annotationUI.selectedAnnotationMap, { assert.deepEqual(annotationUI.selectedAnnotationMap, {
1: true, 2: true, 3: true, 4: true 1: true, 2: true, 3: true, 4: true
}) })
it 'removes annotations already in the selectedAnnotationMap', -> it 'removes annotations already in the selectedAnnotationMap', ->
annotationUI.selectedAnnotationMap = {1: true, 2: true} annotationUI.selectedAnnotationMap = {1: true, 3: true}
annotationUI.xorSelectedAnnotations([{id: 1}, {id: 2}]) annotationUI.xorSelectedAnnotations([{id: 1}, {id: 2}])
assert.deepEqual(annotationUI.selectedAnnotationMap, {}) assert.deepEqual(annotationUI.selectedAnnotationMap, 2:true, 3: true)
it 'does not modify the original map object', ->
orig = annotationUI.selectedAnnotationMap = {1: true}
annotationUI.xorSelectedAnnotations([{id: 2}, {id: 3}])
assert.notEqual(annotationUI.selectedAnnotationMap, orig)
it 'nulls the map if no annotations are selected', ->
orig = annotationUI.selectedAnnotationMap = {1: true}
annotationUI.xorSelectedAnnotations([id: 1])
assert.isNull(annotationUI.selectedAnnotationMap)
describe '.removeSelectedAnnotation', -> describe '.removeSelectedAnnotation', ->
it 'removes an annotation from the selectedAnnotationMap', -> it 'removes an annotation from the selectedAnnotationMap', ->
...@@ -55,3 +108,19 @@ describe 'AnnotationUI', -> ...@@ -55,3 +108,19 @@ describe 'AnnotationUI', ->
assert.deepEqual(annotationUI.selectedAnnotationMap, { assert.deepEqual(annotationUI.selectedAnnotationMap, {
1: true, 3: true 1: true, 3: true
}) })
it 'does not modify the original map object', ->
orig = annotationUI.selectedAnnotationMap = {1: true}
annotationUI.removeSelectedAnnotation(id: 1)
assert.notEqual(annotationUI.selectedAnnotationMap, orig)
it 'nulls the map if no annotations are selected', ->
orig = annotationUI.selectedAnnotationMap = {1: true}
annotationUI.removeSelectedAnnotation(id: 1)
assert.isNull(annotationUI.selectedAnnotationMap)
describe '.clearSelectedAnnotations', ->
it 'removes all annotations from the selection', ->
annotationUI.selectedAnnotationMap = {1: true, 2: true, 3: true}
annotationUI.clearSelectedAnnotations()
assert.isNull(annotationUI.selectedAnnotationMap)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment