-
-
Save jamiefolsom/9f20a9884068189926fa to your computer and use it in GitHub Desktop.
Add ids to annotations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Public: Initialises an annotation either from an object representation or | |
# an annotation created with Annotator#createAnnotation(). It finds the | |
# selected range and higlights the selection in the DOM. | |
# | |
# annotation - An annotation Object to initialise. | |
# fireEvents - Will fire the 'annotationCreated' event if true. | |
# | |
# Examples | |
# | |
# # Create a brand new annotation from the currently selected text. | |
# annotation = annotator.createAnnotation() | |
# annotation = annotator.setupAnnotation(annotation) | |
# # 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, fireEvents=true) -> | |
root = @wrapper[0] | |
annotation.ranges or= @selectedRanges | |
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.highlights = [] | |
for normed in normedRanges | |
annotation.quote.push $.trim(normed.text()) | |
annotation.ranges.push normed.serialize(@wrapper[0], '.annotator-hl') | |
# per: http://lists.okfn.org/pipermail/annotator-dev/2011-September/000099.html | |
hlElems = this.highlightRange(normed) | |
if annotation.id? | |
$(hlElems).attr('id', annotation.id) | |
$.merge annotation.highlights, hlElems | |
# Join all the quotes into one string. | |
annotation.quote = annotation.quote.join(' / ') | |
# Save the annotation data on each highlighter element. | |
$(annotation.highlights).data('annotation', annotation) | |
# Fire annotationCreated events so that plugins can react to them. | |
if fireEvents | |
this.publish('annotationCreated', [annotation]) | |
annotation |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment