Commit 630d4807 authored by Aron Carroll's avatar Aron Carroll

Remove public properties from Guest plugin

This prevents other objects reaching in and calling methods further
down the chain. If a method needs to be called then it should be
proxied by the Guest object.
parent abf19b65
......@@ -3,19 +3,27 @@ $ = Annotator.$
class Annotator.Plugin.Bridge extends Annotator.Plugin
constructor: (elem, options) ->
super
@discovery = new window.CrossFrameDiscovery(window, options.discoveryOptions)
@bridge = new window.CrossFrameBridge(options.bridgeOptions)
@annotationSync = new window.AnnotationSync(options.annotationSyncOptions, @bridge)
discovery = new window.CrossFrameDiscovery(window, options.discoveryOptions)
bridge = new window.CrossFrameBridge(options.bridgeOptions)
annotationSync = new window.AnnotationSync(options.annotationSyncOptions, @bridge)
pluginInit: ->
onDiscoveryCallback = (source, origin, token) =>
@bridge.createChannel(source, origin, token)
@discovery.startDiscovery(onDiscoveryCallback)
this.pluginInit = ->
onDiscoveryCallback = (source, origin, token) ->
bridge.createChannel(source, origin, token)
discovery.startDiscovery(onDiscoveryCallback)
destroy: ->
super
@discovery.stopDiscovery()
return
this.destroy = ->
Annotator.Plugin::destroy.apply(this, arguments) # super doesnt work here :(
discovery.stopDiscovery()
this.sync = (annotations, cb) ->
annotationSync.sync(annotations, cb)
this.on = (event, fn) ->
bridge.on(event, fn)
this.notify = (message) ->
bridge.notify(message)
sync: (annotations, cb) ->
@annotationSync.sync(annotations, cb)
this.onConnect = (fn) ->
bridge.onConnect(fn)
......@@ -82,8 +82,8 @@ class Annotator.Guest extends Annotator
formatted.document.title = formatted.document.title.slice()
formatted
this.addPlugin 'Bridge', bridgePluginOptions
@panel = this._connectAnnotationUISync(this.plugins.Bridge.bridge)
this.addPlugin('Bridge', bridgePluginOptions)
@panel = this._connectAnnotationUISync(this.plugins.Bridge)
# Load plugins
for own name, opts of @options
......@@ -182,7 +182,6 @@ class Annotator.Guest extends Annotator
uri: @getHref()
metadata: @getMetadata()
.catch (e) ->
setTimeout -> throw e
trans.delayReturn(true)
bridge.on 'setTool', (ctx, name) =>
......
......@@ -22,6 +22,9 @@ describe 'Annotator.Plugin.Bridge', ->
fakeCFBridge =
createChannel: sandbox.stub()
onConnect: sandbox.stub()
notify: sandbox.stub()
on: sandbox.stub()
fakeAnnotationSync =
sync: sandbox.stub()
......@@ -95,3 +98,22 @@ describe 'Annotator.Plugin.Bridge', ->
bridge = createBridge()
bridge.sync()
assert.called(fakeAnnotationSync.sync)
describe '.on', ->
it 'proxies the call to the bridge', ->
bridge = createBridge()
bridge.on('event', 'arg')
assert.calledWith(fakeCFBridge.on, 'event', 'arg')
describe '.notify', ->
it 'proxies the call to the bridge', ->
bridge = createBridge()
bridge.notify(method: 'method')
assert.calledWith(fakeCFBridge.notify, method: 'method')
describe '.onConnect', ->
it 'proxies the call to the bridge', ->
bridge = createBridge()
fn = ->
bridge.onConnect(fn)
assert.calledWith(fakeCFBridge.onConnect, fn)
......@@ -3,7 +3,7 @@ sinon.assert.expose(assert, prefix: '')
describe 'Annotator.Guest', ->
sandbox = sinon.sandbox.create()
fakeCFBridge = null
fakeBridge = null
createGuest = (options) ->
element = document.createElement('div')
return new Annotator.Guest(element, options || {})
......@@ -13,12 +13,11 @@ describe 'Annotator.Guest', ->
afterEach -> sandbox.restore()
beforeEach ->
fakeCFBridge =
fakeBridge =
onConnect: sandbox.stub()
on: sandbox.stub()
sandbox.stub(Annotator.Plugin, 'Bridge').returns
bridge: fakeCFBridge
sandbox.stub(Annotator.Plugin, 'Bridge').returns(fakeBridge)
describe 'setting up the bridge', ->
it 'sets the scope for the cross frame bridge', ->
......@@ -41,7 +40,7 @@ describe 'Annotator.Guest', ->
handler = sandbox.stub()
guest = createGuest()
guest.subscribe('panelReady', handler)
fakeCFBridge.onConnect.yield()
fakeBridge.onConnect.yield()
assert.called(handler)
describe 'the event bus .on method', ->
......@@ -155,7 +154,7 @@ describe 'Annotator.Guest', ->
describe 'annotation UI events', ->
emitGuestEvent = (event, args...) ->
fn(args...) for [evt, fn] in fakeCFBridge.on.args when event == evt
fn(args...) for [evt, fn] in fakeBridge.on.args when event == evt
describe 'on "onEditorHide" event', ->
it 'hides the editor', ->
......
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