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 @@
# attached iframes for display in the sidebar. This covers both tool and
# rendered state such as selected highlights.
createAnnotationUI = ->
value = (selection) ->
if Object.keys(selection).length then selection else null
{
TOOL_COMMENT: 'comment'
TOOL_HIGHLIGHT: 'highlight'
......@@ -10,8 +13,10 @@ createAnnotationUI = ->
visibleHighlights: false
# Contains a map of annotation id:true pairs.
focusedAnnotationMap: null
# Contains a map of annotation id:true pairs.
selectedAnnotationMap: null
###*
......@@ -24,7 +29,23 @@ createAnnotationUI = ->
focusAnnotations: (annotations) ->
selection = {}
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
......@@ -36,7 +57,7 @@ createAnnotationUI = ->
selectAnnotations: (annotations) ->
selection = {}
selection[id] = true for {id} in annotations
@selectedAnnotationMap = selection
@selectedAnnotationMap = value(selection)
###*
# @ngdoc method
......@@ -46,13 +67,13 @@ createAnnotationUI = ->
# selectedAnnotationMap if not present otherwise removes them.
###
xorSelectedAnnotations: (annotations) ->
selection = @selectedAnnotationMap or {}
selection = angular.extend({}, @selectedAnnotationMap)
for {id} in annotations
if selection[id]
delete selection[id]
else
selection[id] = true
@selectedAnnotationMap = selection
@selectedAnnotationMap = value(selection)
###*
# @ngdoc method
......@@ -61,8 +82,19 @@ createAnnotationUI = ->
# @description removes an annotation from the current selection.
###
removeSelectedAnnotation: (annotation) ->
return unless @selectedAnnotationMap
delete @selectedAnnotationMap[annotation.id]
selection = angular.extend({}, @selectedAnnotationMap)
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)
......@@ -22,6 +22,38 @@ describe 'AnnotationUI', ->
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()', ->
it 'adds the passed annotations to the selectedAnnotationMap', ->
annotationUI.selectAnnotations([{id: 1}, {id: 2}, {id: 3}])
......@@ -36,6 +68,16 @@ describe 'AnnotationUI', ->
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()', ->
it 'adds annotations missing from the selectedAnnotationMap', ->
annotationUI.selectedAnnotationMap = {1: true, 2: true}
......@@ -43,10 +85,21 @@ describe 'AnnotationUI', ->
assert.deepEqual(annotationUI.selectedAnnotationMap, {
1: true, 2: true, 3: true, 4: true
})
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}])
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', ->
it 'removes an annotation from the selectedAnnotationMap', ->
......@@ -55,3 +108,19 @@ describe 'AnnotationUI', ->
assert.deepEqual(annotationUI.selectedAnnotationMap, {
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