Commit e693f4ba authored by Randall Leeds's avatar Randall Leeds

Consistency fixes

- anchor / describe both take options
- TextQuoteAnchor takes position as an option but it's not stored
- helper closures sprinkled throughout have fewer _ prefixes
parent 76f42211
......@@ -72,11 +72,7 @@ exports.anchor = (selectors, options = {}) ->
return promise
exports.describe = (range) ->
options =
root: document.body
ignoreSelector: '[class^="annotator-"]'
exports.describe = (range, options = {}) ->
maybeDescribeWith = (type) ->
return Promise.resolve(type)
.then((t) -> t.fromRange(range, options))
......
......@@ -160,7 +160,7 @@ exports.anchor = (selectors, options = {}) ->
return promise
exports.describe = (range) ->
exports.describe = (range, options = {}) ->
range = new xpathRange.BrowserRange(range).normalize()
startTextLayer = getNodeTextLayer(range.start)
......@@ -192,8 +192,8 @@ exports.describe = (range) ->
r.setStartBefore(startRange.start)
r.setEndAfter(endRange.end)
options = {root: startTextLayer}
quote = Promise.resolve(TextQuoteAnchor.fromRange(r, options))
pageOptions = {root: startTextLayer}
quote = Promise.resolve(TextQuoteAnchor.fromRange(r, pageOptions))
.then((a) -> a.toSelector())
return Promise.all([position, quote])
......@@ -178,11 +178,9 @@ class TextPositionAnchor extends Anchor
# :param String quote: The anchor text to match.
# :param String prefix: A prefix that preceeds the anchor text.
# :param String suffix: A suffix that follows the anchor text.
# :param Number start: The start offset for the anchor text.
# :param Number end: The end offset for the anchor text.
###
class TextQuoteAnchor extends Anchor
constructor: (@quote, @prefix='', @suffix='', @start, @end) ->
constructor: (@quote, @prefix='', @suffix='') ->
unless @quote? then missingParameter('quote')
@fromRange: (range, options = {}) ->
......@@ -203,12 +201,11 @@ class TextQuoteAnchor extends Anchor
prefix = corpus.substr(prefixStart, start - prefixStart)
suffix = corpus.substr(end, 32)
return new TextQuoteAnchor(exact, prefix, suffix, start, end)
return new TextQuoteAnchor(exact, prefix, suffix)
@fromSelector: (selector, options = {}) ->
{start, end} = options.position ? {}
@fromSelector: (selector) ->
{exact, prefix, suffix} = selector
return new TextQuoteAnchor(exact, prefix, suffix, start, end)
return new TextQuoteAnchor(exact, prefix, suffix)
toRange: (options = {}) ->
return this.toPositionAnchor(options).toRange()
......@@ -236,7 +233,7 @@ class TextQuoteAnchor extends Anchor
return acc
slices = @quote.match(/(.|[\r\n]){1,32}/g)
loc = @start ? root.textContent.length / 2
loc = options.position?.start ? root.textContent.length / 2
# TODO: use the suffix
dmp.Match_Distance = root.textContent.length * 2
......
......@@ -149,27 +149,27 @@ module.exports = class Guest extends Annotator
setupAnnotation: (annotation) ->
self = this
locate = (target) ->
options = {ignoreSelector: '[class^="annotator-"]'}
return new Promise(raf)
.then(-> anchoring.anchor(target.selector, options))
.then((range) -> {annotation, target, range})
.catch(-> {annotation, target})
highlight = (anchor) ->
if anchor.range?
return new Promise(raf).then ->
range = Annotator.Range.sniff(anchor.range)
normedRange = range.normalize(self.element[0])
anchor.highlights = highlighter.highlightRange(normedRange)
return anchor
return anchor
setup = ->
anchors = []
anchoredTargets = []
deadHighlights = []
_anchor = (target) ->
options = {ignoreSelector: '[class^="annotator-"]'}
return new Promise(raf)
.then(-> anchoring.anchor(target.selector, options))
.then((range) -> {annotation, target, range})
.catch(-> {annotation, target})
_highlight = (anchor) ->
if anchor.range?
return new Promise(raf).then ->
range = Annotator.Range.sniff(anchor.range)
normedRange = range.normalize(self.element[0])
anchor.highlights = highlighter.highlightRange(normedRange)
return anchor
return anchor
for anchor in self.anchors.splice(0, self.anchors.length)
if anchor.annotation is annotation
if anchor.range? and anchor.target in annotation.target
......@@ -186,7 +186,7 @@ module.exports = class Guest extends Annotator
new Promise(raf).then(-> highlighter.removeHighlights(deadHighlights))
for target in annotation.target when target not in anchoredTargets
anchor = _anchor(target).then(_highlight)
anchor = locate(target).then(highlight)
anchors.push(anchor)
return Promise.all(anchors)
......@@ -201,11 +201,14 @@ module.exports = class Guest extends Annotator
self.plugins.BucketBar.update()
self.plugins.CrossFrame.sync([annotation])
cleanup = ->
delete annotation.anchors
annotation.target ?= []
annotation.anchors ?= Promise.resolve(annotation.anchors)
.then(setup)
.then(sync)
.then(-> delete annotation.anchors)
.then(cleanup, cleanup)
return annotation
......@@ -213,6 +216,10 @@ module.exports = class Guest extends Annotator
ranges = @selectedRanges
@selectedRanges = null
getSelectors = (range) ->
options = {ignoreSelector: '[class^="annotator-"]'}
return anchoring.describe(range, options)
setDocumentInfo = ({metadata, uri}) ->
annotation.uri = uri
if metadata?
......@@ -223,7 +230,7 @@ module.exports = class Guest extends Annotator
annotation.target = ({source, selector} for selector in selectors)
info = this.getDocumentInfo().then(setDocumentInfo)
selectors = Promise.all(ranges.map(anchoring.describe))
selectors = Promise.all(ranges.map(getSelectors))
targets = Promise.all([info, selectors]).then(setTargets)
targets.then(=> this.setupAnnotation(annotation))
......
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