Commit cc80fbb7 authored by Aron Carroll's avatar Aron Carroll

Ensure a digest is called after UI events

As this channel is outside of the angular framework we need to let it
know that it's internal state may have changed and to re-run it's
watchers.
parent 5cafdeaa
......@@ -12,7 +12,7 @@ class AnnotationUISync
# interface and updates the applications internal state. It also ensures
# that the messages are broadcast out to other frames.
###
constructor: ($window, bridge, annotationSync, annotationUI) ->
constructor: ($rootScope, $window, bridge, annotationSync, annotationUI) ->
# Retrieves annotations from the annotationSync cache.
getAnnotationsByTags = (tags) ->
tags.map(annotationSync.getAnnotationForTag, annotationSync)
......@@ -27,6 +27,7 @@ class AnnotationUISync
hide = notifyHost.bind(null, method: 'hideFrame')
show = notifyHost.bind(null, method: 'showFrame')
channelListeners =
back: hide
open: show
......@@ -48,8 +49,16 @@ class AnnotationUISync
annotationUI.visibleHighlights = Boolean(state)
bridge.notify(method: 'setVisibleHighlights', params: state)
# Because the channel events are all outside of the angular framework we
# need to inform Angular that it needs to re-check it's state and re-draw
# any UI that may have been affected by the handlers.
ensureDigest = (fn) ->
->
fn.apply(this, arguments)
$rootScope.$digest()
for own channel, listener of channelListeners
bridge.on(channel, listener)
bridge.on(channel, ensureDigest(listener))
onConnect = (channel, source) ->
# Allow the host to define its own state
......
......@@ -47,7 +47,7 @@ class CrossFrameService
new AnnotationSync(bridge, options)
createAnnotationUISync = (bridge, annotationSync) ->
new AnnotationUISync($window, bridge, annotationSync, annotationUI)
new AnnotationUISync($rootScope, $window, bridge, annotationSync, annotationUI)
addProvider = (channel) =>
provider = {channel: channel, entities: []}
......
......@@ -322,7 +322,8 @@ class Annotator.Guest extends Annotator
# toggle: should this toggle membership in an existing selection?
selectAnnotations: (annotations, toggle) =>
if toggle
# Tell sidebar to add these annotations to the sidebar
# Tell sidebar to add these annotations to the sidebar if not already
# selected, otherwise remove them.
this.toggleAnnotationSelection annotations
else
# Tell sidebar to show the viewer for these annotations
......
......@@ -3,6 +3,7 @@ sinon.assert.expose(assert, prefix: '')
describe 'AnnotationUISync', ->
sandbox = sinon.sandbox.create()
$digest = null
uiSync = null
publish = null
fakeBridge = null
......@@ -13,7 +14,8 @@ describe 'AnnotationUISync', ->
PARENT_WINDOW = 'PARENT_WINDOW'
beforeEach module('h')
beforeEach inject (AnnotationUISync) ->
beforeEach inject (AnnotationUISync, $rootScope) ->
$digest = sandbox.stub($rootScope, '$digest')
listeners = {}
publish = ({method, params}) -> listeners[method]('ctx', params)
......@@ -38,7 +40,7 @@ describe 'AnnotationUISync', ->
createAnnotationUISync = ->
new AnnotationUISync(
fakeWindow, fakeBridge, fakeAnnotationSync, fakeAnnotationUI)
$rootScope, fakeWindow, fakeBridge, fakeAnnotationSync, fakeAnnotationUI)
afterEach: -> sandbox.restore()
......@@ -76,6 +78,11 @@ describe 'AnnotationUISync', ->
assert.notCalled(fakeBridge.links[1].channel.notify)
assert.notCalled(fakeBridge.links[2].channel.notify)
it 'triggers a digest', ->
createAnnotationUISync()
publish({method: 'back'})
assert.called($digest)
describe 'on "open" event', ->
it 'sends the "showFrame" message to the host only', ->
createAnnotationUISync()
......@@ -84,6 +91,11 @@ describe 'AnnotationUISync', ->
assert.notCalled(fakeBridge.links[1].channel.notify)
assert.notCalled(fakeBridge.links[2].channel.notify)
it 'triggers a digest', ->
createAnnotationUISync()
publish({method: 'open'})
assert.called($digest)
describe 'on "showEditor" event', ->
it 'sends the "showFrame" message to the host only', ->
createAnnotationUISync()
......@@ -92,6 +104,11 @@ describe 'AnnotationUISync', ->
assert.notCalled(fakeBridge.links[1].channel.notify)
assert.notCalled(fakeBridge.links[2].channel.notify)
it 'triggers a digest', ->
createAnnotationUISync()
publish({method: 'showEditor'})
assert.called($digest)
describe 'on "showAnnotations" event', ->
it 'sends the "showFrame" message to the host only', ->
createAnnotationUISync()
......@@ -114,6 +131,14 @@ describe 'AnnotationUISync', ->
{id: 1}, {id: 2}, {id: 3}
])
it 'triggers a digest', ->
createAnnotationUISync()
publish({
method: 'showAnnotations',
params: ['tag1', 'tag2', 'tag3']
})
assert.called($digest)
describe 'on "focusAnnotations" event', ->
it 'updates the annotationUI to show the provided annotations', ->
createAnnotationUISync()
......@@ -126,6 +151,14 @@ describe 'AnnotationUISync', ->
{id: 1}, {id: 2}, {id: 3}
])
it 'triggers a digest', ->
createAnnotationUISync()
publish({
method: 'focusAnnotations',
params: ['tag1', 'tag2', 'tag3']
})
assert.called($digest)
describe 'on "toggleAnnotationSelection" event', ->
it 'updates the annotationUI to show the provided annotations', ->
createAnnotationUISync()
......@@ -138,6 +171,14 @@ describe 'AnnotationUISync', ->
{id: 1}, {id: 2}, {id: 3}
])
it 'triggers a digest', ->
createAnnotationUISync()
publish({
method: 'toggleAnnotationSelection',
params: ['tag1', 'tag2', 'tag3']
})
assert.called($digest)
describe 'on "setTool" event', ->
it 'updates the annotationUI with the new tool', ->
createAnnotationUISync()
......@@ -158,6 +199,14 @@ describe 'AnnotationUISync', ->
params: 'highlighter'
})
it 'triggers a digest of the application state', ->
createAnnotationUISync()
publish({
method: 'setTool',
params: 'highlighter'
})
assert.called($digest)
describe 'on "setVisibleHighlights" event', ->
it 'updates the annotationUI with the new value', ->
createAnnotationUISync()
......@@ -177,3 +226,11 @@ describe 'AnnotationUISync', ->
method: 'setVisibleHighlights'
params: true
})
it 'triggers a digest of the application state', ->
createAnnotationUISync()
publish({
method: 'setVisibleHighlights',
params: true
})
assert.called($digest)
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