forked from openannotation/annotator
/
annotator.coffee
830 lines (698 loc) · 26 KB
/
annotator.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
$ = require 'jquery'
extend = require 'backbone-extend-standalone'
Delegator = require './class'
Range = require './range'
Util = require './util'
Widget = require './widget'
Viewer = require './viewer'
Editor = require './editor'
Notification = require './notification'
Registry = require './registry'
AnnotationProvider = require './annotations'
_t = Util.TranslationString
# Selection and range creation reference for the following code:
# http://www.quirksmode.org/dom/range_intro.html
#
# I've removed any support for IE TextRange (see commit d7085bf2 for code)
# for the moment, having no means of testing it.
# Store a reference to the current Annotator object.
_Annotator = this.Annotator
handleError = ->
console.error.apply(console, arguments)
class Annotator extends Delegator
# Events to be bound on Annotator#element.
events:
".annotator-adder button click": "onAdderClick"
".annotator-adder button mousedown": "onAdderMousedown"
".annotator-hl mouseover": "onHighlightMouseover"
".annotator-hl mouseout": "startViewerHideTimer"
html:
adder: '<div class="annotator-adder"><button type="button">' + _t('Annotate') + '</button></div>'
wrapper: '<div class="annotator-wrapper"></div>'
options: # Configuration options
store: null # Store plugin to use. If null, Annotator will use a default store.
readOnly: false # Start Annotator in read-only mode. No controls will be shown.
loadQuery: {} # Initial query to load Annotations
plugins: {}
editor: null
viewer: null
selectedRanges: null
mouseIsDown: false
ignoreMouseup: false
viewerHideTimer: null
# Public: Creates an instance of the Annotator. Requires a DOM Element in
# which to watch for annotations as well as any options.
#
# NOTE: If the Annotator is not supported by the current browser it will not
# perform any setup and simply return a basic object. This allows plugins
# to still be loaded but will not function as expected. It is reccomended
# to call Annotator.supported() before creating the instance or using the
# Unsupported plugin which will notify users that the Annotator will not work.
#
# element - A DOM Element in which to annotate.
# options - An options Object. NOTE: There are currently no user options.
#
# Examples
#
# annotator = new Annotator(document.body)
#
# # Example of checking for support.
# if Annotator.supported()
# annotator = new Annotator(document.body)
# else
# # Fallback for unsupported browsers.
#
# Returns a new instance of the Annotator.
constructor: (element, options) ->
super
@plugins = {}
Annotator._instances.push(this)
# Return early if the annotator is not supported.
return this unless Annotator.supported()
# Create the registry and start the application
Registry.createApp(this, options)
# Public: Creates a subclass of Annotator.
#
# See the documentation from Backbone: http://backbonejs.org/#Model-extend
#
# Examples
#
# var ExtendedAnnotator = Annotator.extend({
# setupAnnotation: function (annotation) {
# // Invoke the built-in implementation
# try {
# Annotator.prototype.setupAnnotation.call(this, annotation);
# } catch (e) {
# if (e instanceof Annotator.Range.RangeError) {
# // Try to locate the Annotation using the quote
# } else {
# throw e;
# }
# }
#
# return annotation;
# });
#
# var annotator = new ExtendedAnnotator(document.body, /* {options} */);
@extend: extend
# Wraps the children of @element in a @wrapper div. NOTE: This method will also
# remove any script elements inside @element to prevent them re-executing.
#
# Returns itself to allow chaining.
_setupWrapper: ->
@wrapper = $(@html.wrapper)
# We need to remove all scripts within the element before wrapping the
# contents within a div. Otherwise when scripts are reappended to the DOM
# they will re-execute. This is an issue for scripts that call
# document.write() - such as ads - as they will clear the page.
@element.find('script').remove()
@element.wrapInner(@wrapper)
@wrapper = @element.find('.annotator-wrapper')
this
# Creates an instance of Annotator.Viewer and assigns it to the @viewer
# property, appends it to the @wrapper and sets up event listeners.
#
# Returns itself to allow chaining.
_setupViewer: ->
@viewer = new Annotator.Viewer(readOnly: @options.readOnly)
@viewer.hide()
.on("edit", this.onEditAnnotation)
.on("delete", (annotation) =>
@viewer.hide()
this.publish('beforeAnnotationDeleted', [annotation])
# Delete highlight elements.
this.cleanupAnnotation(annotation)
# Delete annotation
this.annotations.delete(annotation)
.done => this.publish('annotationDeleted', [annotation])
)
.addField({
load: (field, annotation) =>
if annotation.text
$(field).html(Util.escape(annotation.text))
else
$(field).html("<i>#{_t 'No Comment'}</i>")
this.publish('annotationViewerTextField', [field, annotation])
})
.element.appendTo(@wrapper).bind({
"mouseover": this.clearViewerHideTimer
"mouseout": this.startViewerHideTimer
})
this
# Creates an instance of the Annotator.Editor and assigns it to @editor.
# Appends this to the @wrapper and sets up event listeners.
#
# Returns itself for chaining.
_setupEditor: ->
@editor = new Annotator.Editor()
@editor.hide()
.on('hide', this.onEditorHide)
.on('save', this.onEditorSubmit)
.addField({
type: 'textarea',
label: _t('Comments') + '\u2026'
load: (field, annotation) ->
$(field).find('textarea').val(annotation.text || '')
submit: (field, annotation) ->
annotation.text = $(field).find('textarea').val()
})
@editor.element.appendTo(@wrapper)
this
# Sets up the selection event listeners to watch mouse actions on the document.
#
# Returns itself for chaining.
_setupDocumentEvents: ->
$(document).bind({
"mouseup": this.checkForEndSelection
"mousedown": this.checkForStartSelection
})
this
# Sets up any dynamically calculated CSS for the Annotator.
#
# Returns itself for chaining.
_setupDynamicStyle: ->
style = $('#annotator-dynamic-style')
if (!style.length)
style = $('<style id="annotator-dynamic-style"></style>').appendTo(document.head)
sel = '*' + (":not(.annotator-#{x})" for x in ['adder', 'outer', 'notice', 'filter']).join('')
# use the maximum z-index in the page
max = Util.maxZIndex($(document.body).find(sel))
# but don't go smaller than 1010, because this isn't bulletproof --
# dynamic elements in the page (notifications, dialogs, etc.) may well
# have high z-indices that we can't catch using the above method.
max = Math.max(max, 1000)
style.text [
".annotator-adder, .annotator-outer, .annotator-notice {"
" z-index: #{max + 20};"
"}"
".annotator-filter {"
" z-index: #{max + 10};"
"}"
].join("\n")
this
# Public: Load and draw annotations from a given query.
#
# query - the query to pass to the backend
#
# Returns a Promise that resolves when loading is complete.
load: (query) ->
@annotations.load(query)
.then (annotations, meta) =>
this.loadAnnotations(annotations)
# Public: Destroy the current Annotator instance, unbinding all events and
# disposing of all relevant elements.
#
# Returns nothing.
destroy: ->
$(document).unbind({
"mouseup": this.checkForEndSelection
"mousedown": this.checkForStartSelection
})
$('#annotator-dynamic-style').remove()
@adder.remove()
@viewer.destroy()
@editor.destroy()
@wrapper.find('.annotator-hl').each ->
$(this).contents().insertBefore(this)
$(this).remove()
@wrapper.contents().insertBefore(@wrapper)
@wrapper.remove()
@element.data('annotator', null)
for name, plugin of @plugins
@plugins[name].destroy()
this.removeEvents()
idx = Annotator._instances.indexOf(this)
if idx != -1
Annotator._instances.splice(idx, 1)
# Public: Gets the current selection excluding any nodes that fall outside of
# the @wrapper. Then returns and Array of NormalizedRange instances.
#
# Examples
#
# # A selection inside @wrapper
# annotation.getSelectedRanges()
# # => Returns [NormalizedRange]
#
# # A selection outside of @wrapper
# annotation.getSelectedRanges()
# # => Returns []
#
# Returns Array of NormalizedRange instances.
getSelectedRanges: ->
selection = Util.getGlobal().getSelection()
ranges = []
rangesToIgnore = []
unless selection.isCollapsed
ranges = for i in [0...selection.rangeCount]
r = selection.getRangeAt(i)
browserRange = new Range.BrowserRange(r)
normedRange = browserRange.normalize().limit(@wrapper[0])
# If the new range falls fully outside the wrapper, we
# should add it back to the document but not return it from
# this method
rangesToIgnore.push(r) if normedRange is null
normedRange
# BrowserRange#normalize() modifies the DOM structure and deselects the
# underlying text as a result. So here we remove the selected ranges and
# reapply the new ones.
selection.removeAllRanges()
for r in rangesToIgnore
selection.addRange(r)
# Remove any ranges that fell outside of @wrapper.
$.grep ranges, (range) ->
# Add the normed range back to the selection if it exists.
selection.addRange(range.toRange()) if range
range
# Public: Initialises an annotation from an object representation. It finds
# the selected range and higlights the selection in the DOM.
#
# annotation - An annotation Object to initialise.
#
# Examples
#
# # Create a brand new annotation from the currently selected text.
# annotation = annotator.setupAnnotation({ranges: annotator.selectedRanges})
# # annotation has now been assigned the currently selected range
# # and a highlight appended to the DOM.
#
# # Add an existing annotation that has been stored elsewere to the DOM.
# annotation = getStoredAnnotationWithSerializedRanges()
# annotation = annotator.setupAnnotation(annotation)
#
# Returns the initialised annotation.
setupAnnotation: (annotation) ->
root = @wrapper[0]
normedRanges = []
for r in annotation.ranges
try
normedRanges.push(Range.sniff(r).normalize(root))
catch e
if e instanceof Range.RangeError
this.publish('rangeNormalizeFail', [annotation, r, e])
else
# Oh Javascript, why you so crap? This will lose the traceback.
throw e
annotation.quote = []
annotation.ranges = []
annotation._local = {}
annotation._local.highlights = []
for normed in normedRanges
annotation.quote.push $.trim(normed.text())
annotation.ranges.push normed.serialize(@wrapper[0], '.annotator-hl')
$.merge annotation._local.highlights, this.highlightRange(normed)
# Join all the quotes into one string.
annotation.quote = annotation.quote.join(' / ')
# Save the annotation data on each highlighter element.
$(annotation._local.highlights).data('annotation', annotation)
annotation
# Public: Deletes the annotation by removing the highlight from the DOM.
#
# annotation - An annotation Object to delete.
#
# Returns deleted annotation.
cleanupAnnotation: (annotation) ->
if annotation._local?.highlights?
for h in annotation._local.highlights when h.parentNode?
$(h).replaceWith(h.childNodes)
delete annotation._local.highlights
annotation
# Public: Loads an Array of annotations into the @element. Breaks the task
# into chunks of 10 annotations.
#
# annotations - An Array of annotation Objects.
#
# Examples
#
# loadAnnotationsFromStore (annotations) ->
# annotator.loadAnnotations(annotations)
#
# Returns itself for chaining.
loadAnnotations: (annotations=[]) ->
loader = (annList=[]) =>
now = annList.splice(0,10)
for n in now
this.setupAnnotation(n)
# If there are more to do, do them after a 10ms break (for browser
# responsiveness).
if annList.length > 0
setTimeout((-> loader(annList)), 10)
else
this.publish 'annotationsLoaded', [clone]
clone = annotations.slice()
loader annotations
this
# Public: Calls the Store#dumpAnnotations() method.
#
# Returns dumped annotations Array or false if Store is not loaded.
dumpAnnotations: () ->
if @plugins['Store']
@plugins['Store'].dumpAnnotations()
else
console.warn(_t("Can't dump annotations without Store plugin."))
return false
# Public: Wraps the DOM Nodes within the provided range with a highlight
# element of the specified class and returns the highlight Elements.
#
# normedRange - A NormalizedRange to be highlighted.
# cssClass - A CSS class to use for the highlight (default: 'annotator-hl')
#
# Returns an array of highlight Elements.
highlightRange: (normedRange, cssClass='annotator-hl') ->
white = /^\s*$/
hl = $("<span class='#{cssClass}'></span>")
# Ignore text nodes that contain only whitespace characters. This prevents
# spans being injected between elements that can only contain a restricted
# subset of nodes such as table rows and lists. This does mean that there
# may be the odd abandoned whitespace node in a paragraph that is skipped
# but better than breaking table layouts.
for node in normedRange.textNodes() when not white.test(node.nodeValue)
$(node).wrapAll(hl).parent().show()[0]
# Public: highlight a list of ranges
#
# normedRanges - An array of NormalizedRanges to be highlighted.
# cssClass - A CSS class to use for the highlight (default: 'annotator-hl')
#
# Returns an array of highlight Elements.
highlightRanges: (normedRanges, cssClass='annotator-hl') ->
highlights = []
for r in normedRanges
$.merge highlights, this.highlightRange(r, cssClass)
highlights
# Public: Registers a plugin with the Annotator. A plugin can only be
# registered once. The plugin will be instantiated in the following order.
#
# 1. A new instance of the plugin will be created (providing the @element and
# options as params) then assigned to the @plugins registry.
# 2. The current Annotator instance will be attached to the plugin.
# 3. The Plugin#pluginInit() method will be called if it exists.
#
# name - Plugin to instantiate. Must be in the Annotator.Plugins namespace.
# options - Any options to be provided to the plugin constructor.
#
# Examples
#
# annotator
# .addPlugin('Tags')
# .addPlugin('Store', {
# prefix: '/store'
# })
# .addPlugin('Permissions', {
# user: 'Bill'
# })
#
# Returns itself to allow chaining.
addPlugin: (name, options) ->
if @plugins[name]
console.error _t("You cannot have more than one instance of any plugin.")
else
klass = Annotator.Plugin[name]
if typeof klass is 'function'
@plugins[name] = new klass(@element[0], options)
@plugins[name].annotator = this
@plugins[name].pluginInit?()
else
console.error _t("Could not load ") + name + _t(" plugin. Have you included the appropriate <script> tag?")
this # allow chaining
# Public: Waits for the @editor to submit or hide, returning a promise that
# is resolved or rejected depending on whether the annotation was saved or
# cancelled.
editAnnotation: (annotation, position) ->
dfd = $.Deferred()
resolve = dfd.resolve.bind(dfd, annotation)
reject = dfd.reject.bind(dfd, annotation)
this.showEditor(annotation, position)
this.subscribe('annotationEditorSubmit', resolve)
this.once 'annotationEditorHidden', =>
this.unsubscribe('annotationEditorSubmit', resolve)
reject() if dfd.state() is 'pending'
dfd.promise()
# Public: Loads the @editor with the provided annotation and updates its
# position in the window.
#
# annotation - An annotation to load into the editor.
# location - Position to set the Editor in the form {top: y, left: x}
#
# Examples
#
# annotator.showEditor({text: "my comment"}, {top: 34, left: 234})
#
# Returns itself to allow chaining.
showEditor: (annotation, location) =>
@editor.element.css(location)
@editor.load(annotation)
this.publish('annotationEditorShown', [@editor, annotation])
this
# Callback method called when the @editor fires the "hide" event. Itself
# publishes the 'annotationEditorHidden' event and resets the @ignoreMouseup
# property to allow listening to mouse events.
#
# Returns nothing.
onEditorHide: =>
this.publish('annotationEditorHidden', [@editor])
@ignoreMouseup = false
# Callback method called when the @editor fires the "save" event. Itself
# publishes the 'annotationEditorSubmit' event and creates/updates the
# edited annotation.
#
# Returns nothing.
onEditorSubmit: (annotation) =>
this.publish('annotationEditorSubmit', [@editor, annotation])
# Public: Loads the @viewer with an Array of annotations and positions it
# at the location provided. Calls the 'annotationViewerShown' event.
#
# annotation - An Array of annotations to load into the viewer.
# location - Position to set the Viewer in the form {top: y, left: x}
#
# Examples
#
# annotator.showViewer(
# [{text: "my comment"}, {text: "my other comment"}],
# {top: 34, left: 234})
# )
#
# Returns itself to allow chaining.
showViewer: (annotations, location) =>
@viewer.element.css(location)
@viewer.load(annotations)
this.publish('annotationViewerShown', [@viewer, annotations])
# Annotator#element event callback. Allows 250ms for mouse pointer to get from
# annotation highlight to @viewer to manipulate annotations. If timer expires
# the @viewer is hidden.
#
# Returns nothing.
startViewerHideTimer: =>
# Don't do this if timer has already been set by another annotation.
if not @viewerHideTimer
@viewerHideTimer = setTimeout @viewer.hide, 250
# Viewer#element event callback. Clears the timer set by
# Annotator#startViewerHideTimer() when the @viewer is moused over.
#
# Returns nothing.
clearViewerHideTimer: () =>
clearTimeout(@viewerHideTimer)
@viewerHideTimer = false
# Annotator#element callback. Sets the @mouseIsDown property used to
# determine if a selection may have started to true. Also calls
# Annotator#startViewerHideTimer() to hide the Annotator#viewer.
#
# event - A mousedown Event object.
#
# Returns nothing.
checkForStartSelection: (event) =>
unless event and this.isAnnotator(event.target)
this.startViewerHideTimer()
@mouseIsDown = true
# Annotator#element callback. Checks to see if a selection has been made
# on mouseup and if so displays the Annotator#adder. If @ignoreMouseup is
# set will do nothing. Also resets the @mouseIsDown property.
#
# event - A mouseup Event object.
#
# Returns nothing.
checkForEndSelection: (event) =>
@mouseIsDown = false
# This prevents the note image from jumping away on the mouseup
# of a click on icon.
if @ignoreMouseup
return
# Get the currently selected ranges.
@selectedRanges = this.getSelectedRanges()
for range in @selectedRanges
container = range.commonAncestor
if $(container).hasClass('annotator-hl')
container = $(container).parents('[class!=annotator-hl]')[0]
return if this.isAnnotator(container)
if event and @selectedRanges.length
@adder
.css(Util.mousePosition(event, @wrapper[0]))
.show()
else
@adder.hide()
# Public: Determines if the provided element is part of the annotator plugin.
# Useful for ignoring mouse actions on the annotator elements.
# NOTE: The @wrapper is not included in this check.
#
# element - An Element or TextNode to check.
#
# Examples
#
# span = document.createElement('span')
# annotator.isAnnotator(span) # => Returns false
#
# annotator.isAnnotator(annotator.viewer.element) # => Returns true
#
# Returns true if the element is a child of an annotator element.
isAnnotator: (element) ->
!!$(element).parents().addBack().filter('[class^=annotator-]').not(@wrapper).length
configure: (@registry) ->
registry.include(AnnotationProvider)
run: (@registry) ->
# Set up the core interface components
this._setupDocumentEvents() unless @options.readOnly
this._setupWrapper()._setupViewer()._setupEditor()
this._setupDynamicStyle()
# Create adder
this.adder = $(this.html.adder).appendTo(@wrapper).hide()
# Do initial load
if @options.loadQuery then this.load(@options.loadQuery)
# Annotator#element callback. Displays viewer with all annotations
# associated with highlight Elements under the cursor.
#
# event - A mouseover Event object.
#
# Returns nothing.
onHighlightMouseover: (event) =>
# Cancel any pending hiding of the viewer.
this.clearViewerHideTimer()
# Don't do anything if we're making a selection
return false if @mouseIsDown
# If the viewer is already shown, hide it first
@viewer.hide() if @viewer.isShown()
annotations = $(event.target)
.parents('.annotator-hl')
.addBack()
.map( -> return $(this).data("annotation"))
.toArray()
# Now show the viewer with the wanted annotations
this.showViewer(annotations, Util.mousePosition(event, @wrapper[0]))
# Annotator#element callback. Sets @ignoreMouseup to true to prevent
# the annotation selection events firing when the adder is clicked.
#
# event - A mousedown Event object
#
# Returns nothing.
onAdderMousedown: (event) =>
event?.preventDefault()
@ignoreMouseup = true
# Annotator#element callback. Displays the @editor in place of the @adder and
# loads in a newly created annotation Object. The click event is used as well
# as the mousedown so that we get the :active state on the @adder when clicked
#
# event - A mousedown Event object
#
# Returns nothing.
onAdderClick: (event) =>
event?.preventDefault()
# Hide the adder
position = @adder.position()
@adder.hide()
annotation = {ranges: @selectedRanges}
$.when(annotation)
.done (annotation) =>
this.publish('beforeAnnotationCreated', [annotation])
# Set up the annotation
.then (annotation) =>
this.setupAnnotation(annotation)
# Show a temporary highlight so the user can see what they selected
.done (annotation) =>
$(annotation._local.highlights).addClass('annotator-hl-temporary')
# Edit the annotation
.then (annotation) =>
this.editAnnotation(annotation, position)
.then (annotation) =>
this.annotations.create(annotation)
# Handle storage errors
.fail(handleError)
# Clean up the highlights
.done (annotation) =>
$(annotation._local.highlights).removeClass('annotator-hl-temporary')
.done (annotation) =>
this.publish('annotationCreated', [annotation])
# Clean up (if, for example, editing was cancelled, or storage failed)
.fail(this.cleanupAnnotation)
# Annotator#viewer callback function. Displays the Annotator#editor in the
# positions of the Annotator#viewer and loads the passed annotation for
# editing.
#
# annotation - An annotation Object for editing.
#
# Returns nothing.
onEditAnnotation: (annotation) =>
position = @viewer.element.position()
@viewer.hide()
$.when(annotation)
.done (annotation) =>
this.publish('beforeAnnotationUpdated', [annotation])
.then (annotation) =>
this.editAnnotation(annotation, position)
.then (annotation) =>
this.annotations.update(annotation)
# Handle storage errors
.fail(handleError)
.done (annotation) =>
this.publish('annotationUpdated', [annotation])
# Create namespace for Annotator plugins
class Annotator.Plugin extends Delegator
constructor: (element, options) ->
super
pluginInit: ->
destroy: ->
this.removeEvents()
# Sniff the browser environment and attempt to add missing functionality.
g = Util.getGlobal()
if not g.document?.evaluate?
$.getScript('http://assets.annotateit.org/vendor/xpath.min.js')
if not g.getSelection?
$.getScript('http://assets.annotateit.org/vendor/ierange.min.js')
if not g.JSON?
$.getScript('http://assets.annotateit.org/vendor/json2.min.js')
# Ensure the Node constants are defined
if not g.Node?
g.Node =
ELEMENT_NODE : 1
ATTRIBUTE_NODE : 2
TEXT_NODE : 3
CDATA_SECTION_NODE : 4
ENTITY_REFERENCE_NODE : 5
ENTITY_NODE : 6
PROCESSING_INSTRUCTION_NODE : 7
COMMENT_NODE : 8
DOCUMENT_NODE : 9
DOCUMENT_TYPE_NODE : 10
DOCUMENT_FRAGMENT_NODE : 11
NOTATION_NODE : 12
# Export other modules for use in plugins.
Annotator.Delegator = Delegator
Annotator.Range = Range
Annotator.Util = Util
Annotator.Widget = Widget
Annotator.Viewer = Viewer
Annotator.Editor = Editor
Annotator.Notification = Notification
# Attach notification methods to the Annotation object
notification = new Notification
Annotator.showNotification = notification.show
Annotator.hideNotification = notification.hide
# Expose a global instance registry
Annotator._instances = []
# Bind gettext helper so plugins can use localisation.
Annotator._t = _t
# Returns true if the Annotator can be used in the current browser.
Annotator.supported = -> (-> !!this.getSelection)()
# Restores the Annotator property on the global object to it's
# previous value and returns the Annotator.
Annotator.noConflict = ->
Util.getGlobal().Annotator = _Annotator
this
# Export Annotator object.
module.exports = Annotator