Commit 431e2caa authored by Aron Carroll's avatar Aron Carroll

Do not namespace the arguments to Bridge

There is no need for the called to know what objects are being
instantiated inside the Bridge plugin.
parent abfc4fdc
$ = Annotator.$
extract = extract = (obj, keys...) ->
ret = {}
ret[key] = obj[key] for key in keys when obj.hasOwnProperty(key)
ret
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, extract(options, 'server'))
bridge = new window.CrossFrameBridge(extract(options, 'scope'))
syncOpts = extract(options, 'on', 'emit', 'formatter', 'parser')
annotationSync = new window.AnnotationSync(syncOpts, @bridge)
this.pluginInit = ->
onDiscoveryCallback = (source, origin, token) ->
......@@ -13,7 +20,8 @@ class Annotator.Plugin.Bridge extends Annotator.Plugin
discovery.startDiscovery(onDiscoveryCallback)
this.destroy = ->
Annotator.Plugin::destroy.apply(this, arguments) # super doesnt work here :(
# super doesnt work here :(
Annotator.Plugin::destroy.apply(this, arguments)
discovery.stopDiscovery()
this.sync = (annotations, cb) ->
......
......@@ -52,37 +52,34 @@ class Annotator.Guest extends Annotator
delete @options.app
bridgePluginOptions =
discoveryOptions: {}
bridgeOptions:
scope: 'annotator:bridge'
annotationSyncOptions:
on: (event, handler) =>
this.subscribe(event, handler)
emit: (event, args...) =>
switch event
# AnnotationSync tries to emit some events without taking actions.
# We catch them and perform the right action (which will then emit
# the event for real)
when 'annotationDeleted'
this.deleteAnnotation(args...)
when 'loadAnnotations'
this.loadAnnotations(args...)
# Other events can simply be emitted.
else
this.publish(event, args)
formatter: (annotation) =>
formatted = {}
formatted.uri = @getHref()
for k, v of annotation when k isnt 'anchors'
formatted[k] = v
# Work around issue in jschannel where a repeated object is considered
# recursive, even if it is not its own ancestor.
if formatted.document?.title
formatted.document.title = formatted.document.title.slice()
formatted
this.addPlugin('Bridge', bridgePluginOptions)
bridgeOptions =
scope: 'annotator:bridge'
on: (event, handler) =>
this.subscribe(event, handler)
emit: (event, args...) =>
switch event
# AnnotationSync tries to emit some events without taking actions.
# We catch them and perform the right action (which will then emit
# the event for real)
when 'annotationDeleted'
this.deleteAnnotation(args...)
when 'loadAnnotations'
this.loadAnnotations(args...)
# Other events can simply be emitted.
else
this.publish(event, args)
formatter: (annotation) =>
formatted = {}
formatted.uri = @getHref()
for k, v of annotation when k isnt 'anchors'
formatted[k] = v
# Work around issue in jschannel where a repeated object is considered
# recursive, even if it is not its own ancestor.
if formatted.document?.title
formatted.document.title = formatted.document.title.slice()
formatted
this.addPlugin('Bridge', bridgeOptions)
@bridge = this._connectAnnotationUISync(this.plugins.Bridge)
# Load plugins
......
......@@ -9,11 +9,10 @@ describe 'Annotator.Plugin.Bridge', ->
createBridge = (options) ->
defaults =
annotationSyncOptions:
on: sandbox.stub()
emit: sandbox.stub()
on: sandbox.stub()
emit: sandbox.stub()
element = document.createElement('div')
return new Annotator.Plugin.Bridge(element, $.extend(true, {}, defaults, options))
return new Annotator.Plugin.Bridge(element, $.extend({}, defaults, options))
beforeEach ->
fakeCFDiscovery =
......@@ -46,7 +45,7 @@ describe 'Annotator.Plugin.Bridge', ->
assert.calledWith(CrossFrameDiscovery, window)
it 'passes the options along to the bridge', ->
createBridge(discoveryOptions: {server: true})
createBridge(server: true)
assert.called(CrossFrameDiscovery)
assert.calledWith(CrossFrameDiscovery, window, server: true)
......@@ -56,7 +55,7 @@ describe 'Annotator.Plugin.Bridge', ->
assert.calledWith(CrossFrameDiscovery)
it 'passes the options along to the bridge', ->
createBridge(bridgeOptions: {scope: 'myscope'})
createBridge(scope: 'myscope')
assert.called(CrossFrameBridge)
assert.calledWith(CrossFrameBridge, scope: 'myscope')
......@@ -66,7 +65,7 @@ describe 'Annotator.Plugin.Bridge', ->
it 'passes along options to AnnotationSync', ->
formatter = (x) -> x
createBridge(annotationSyncOptions: {formatter: formatter})
createBridge(formatter: formatter)
assert.called(AnnotationSync)
assert.calledWith(AnnotationSync, {
on: sinon.match.func
......
......@@ -23,18 +23,18 @@ describe 'Annotator.Guest', ->
it 'sets the scope for the cross frame bridge', ->
guest = createGuest()
options = Annotator.Plugin.Bridge.lastCall.args[1]
assert.equal(options.bridgeOptions.scope, 'annotator:bridge')
assert.equal(options.scope, 'annotator:bridge')
it 'provides an event bus for the annotation sync module', ->
guest = createGuest()
options = Annotator.Plugin.Bridge.lastCall.args[1]
assert.isFunction(options.annotationSyncOptions.on)
assert.isFunction(options.annotationSyncOptions.emit)
assert.isFunction(options.on)
assert.isFunction(options.emit)
it 'provides a formatter for the annotation sync module', ->
guest = createGuest()
options = Annotator.Plugin.Bridge.lastCall.args[1]
assert.isFunction(options.annotationSyncOptions.formatter)
assert.isFunction(options.formatter)
it 'publishes the "panelReady" event when a connection is established', ->
handler = sandbox.stub()
......@@ -49,7 +49,7 @@ describe 'Annotator.Guest', ->
beforeEach ->
guest = createGuest()
options = Annotator.Plugin.Bridge.lastCall.args[1].annotationSyncOptions
options = Annotator.Plugin.Bridge.lastCall.args[1]
it 'proxies the event into the annotator event system', ->
fooHandler = sandbox.stub()
......@@ -70,7 +70,7 @@ describe 'Annotator.Guest', ->
beforeEach ->
guest = createGuest()
options = Annotator.Plugin.Bridge.lastCall.args[1].annotationSyncOptions
options = Annotator.Plugin.Bridge.lastCall.args[1]
it 'calls deleteAnnotation when an annotationDeleted event is recieved', ->
ann = {id: 1, $$tag: 'tag1'}
......@@ -123,7 +123,7 @@ describe 'Annotator.Guest', ->
beforeEach ->
guest = createGuest()
guest.plugins.Document = {uri: -> 'http://example.com'}
options = Annotator.Plugin.Bridge.lastCall.args[1].annotationSyncOptions
options = Annotator.Plugin.Bridge.lastCall.args[1]
it 'applies a "uri" property to the formatted object', ->
ann = {$$tag: 'tag1'}
......
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