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