Commit 0574eb49 by Arthur Barrett

initialize annotator on any element by note:init event

parent 71209989
class @StudentNotes class StudentNotes
targets: [] _debug: true
storeConfig: targets: [] # elements with annotator() instances
prefix: ''
annotationData:
uri: ''
constructor: () -> constructor: ($, el) ->
@storeConfig = console.log 'student notes init', arguments, this if @_debug
prefix: @getPrefix()
annotationData:
uri: @getURIPath()
$(document).ready(@init) if $(el).data('notes-ready') isnt 'yes'
$(el).delegate '*', 'notes:init': @onInitNotes
init: ($) => $(el).data('notes-ready', 'yes')
if not StudentNotes.ready
$(document).delegate('*', {
'notes:init': @onInitNotes,
'notes:load': @onLoadNotes
})
StudentNotes.ready = true
onInitNotes: (event, annotationData=null) => onInitNotes: (event, annotationData=null) =>
found = (target for target in @targets when target is event.target) event.stopPropagation()
storeConfig = $.extend {}, @storeConfig found = @targets.some (target) -> target is event.target
$.extend(storeConfig.annotationData, annotationData) if annotationData
if found.length is 0 if found
annotator = $(event.target).data('annotator')
store = annotator.plugins['Store']
store.options.annotationData = annotationData if annotationData
store.loadAnnotations()
else
$(event.target).annotator() $(event.target).annotator()
.annotator('addPlugin', 'Tags') .annotator('addPlugin', 'Tags')
.annotator('addPlugin', 'Store', storeConfig) .annotator('addPlugin', 'Store', @getStoreConfig(annotationData))
@targets.push(event.target) @targets.push(event.target)
onLoadNotes: (event) => getStoreConfig: (annotationData) ->
if event.target in @targets storeConfig =
$(event.target).annotator().annotator('loadAnnotations') prefix: @getPrefix()
annotationData:
uri: @getURIPath() # defaults to current URI path
$.extend storeConfig.annotationData, annotationData if annotationData
storeConfig
getPrefix: () -> getPrefix: () ->
re = /^(\/courses\/[^/]+\/[^/]+\/[^/]+)/ re = /^(\/courses\/[^/]+\/[^/]+\/[^/]+)/
...@@ -47,3 +43,5 @@ class @StudentNotes ...@@ -47,3 +43,5 @@ class @StudentNotes
getURIPath: () -> getURIPath: () ->
window.location.href.toString().split(window.location.host)[1] window.location.href.toString().split(window.location.host)[1]
$(document).ready ($) -> new StudentNotes($, this)
\ No newline at end of file
...@@ -32,9 +32,9 @@ ...@@ -32,9 +32,9 @@
} }
var onComplete = function(url) { var onComplete = function(url) {
return function() { return function() {
annotationData = { 'uri': url } var annotationData = { 'uri': url }
$(this).trigger('notes:init', [annotationData]); $('#viewerContainer').trigger('notes:init', [annotationData]);
} }
}; };
......
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