Commit 3ec977f3 authored by Nick Stenning's avatar Nick Stenning

Use util.coffee from upstream

parent ce3f0905
......@@ -4,40 +4,6 @@
# I've removed any support for IE TextRange (see commit d7085bf2 for code)
# for the moment, having no means of testing it.
util =
uuid: (-> counter = 0; -> counter++)()
getGlobal: -> (-> this)()
# Return the maximum z-index of any element in $elements (a jQuery collection).
maxZIndex: ($elements) ->
all = for el in $elements
if $(el).css('position') == 'static'
-1
else
# Use parseFloat since we may get scientific notation for large
# values.
parseFloat($(el).css('z-index')) or -1
Math.max.apply(Math, all)
mousePosition: (e, offsetEl) ->
# If the offset element is not a positioning root use its offset parent
unless $(offsetEl).css('position') in ['absolute', 'fixed', 'relative']
offsetEl = $(offsetEl).offsetParent()[0]
offset = $(offsetEl).offset()
{
top: e.pageY - offset.top,
left: e.pageX - offset.left
}
# Checks to see if an event parameter is provided and contains the prevent
# default method. If it does it calls it.
#
# This is useful for methods that can be optionally used as callbacks
# where the existance of the parameter must be checked before calling.
preventEventDefault: (event) ->
event?.preventDefault?()
# Store a reference to the current Annotator object.
_Annotator = this.Annotator
......@@ -190,7 +156,7 @@ class Annotator extends Delegator
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))
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
......@@ -543,7 +509,7 @@ class Annotator extends Delegator
else
# Show the adder button
@adder
.css(util.mousePosition(event, @wrapper[0]))
.css(Util.mousePosition(event, @wrapper[0]))
.show()
true
......@@ -686,7 +652,7 @@ class Annotator extends Delegator
return false if @mouseIsDown or @viewer.isShown()
this.showViewer event.data.getAnnotations(event),
util.mousePosition(event, @wrapper[0])
Util.mousePosition(event, @wrapper[0])
onAnchorMouseout: (event) ->
this.startViewerHideTimer()
......@@ -706,7 +672,7 @@ class Annotator.Plugin extends Delegator
this.removeEvents()
# Sniff the browser environment and attempt to add missing functionality.
g = util.getGlobal()
g = Util.getGlobal()
# Checks for the presence of wicked-good-xpath
# It is always safe to install it, it'll not overwrite existing functions
......@@ -740,7 +706,6 @@ Annotator.$ = $
# Export other modules for use in plugins.
Annotator.Delegator = Delegator
Annotator.Range = Range
Annotator.util = util
Annotator.Util = Util
# Expose a global instance registry
......@@ -757,7 +722,7 @@ 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
Util.getGlobal().Annotator = _Annotator
this
# Create global access for Annotator
......
......@@ -72,7 +72,7 @@ class Annotator.Editor extends Annotator.Widget
#
# Returns itself.
show: (event) =>
util.preventEventDefault event
Annotator.Util.preventEventDefault event
@element.removeClass(@classes.hide)
@element.find('.annotator-save').addClass(@classes.focus)
......@@ -104,7 +104,7 @@ class Annotator.Editor extends Annotator.Widget
#
# Returns itself.
hide: (event) =>
util.preventEventDefault event
Annotator.Util.preventEventDefault event
@element.addClass(@classes.hide)
this.publish('hide')
......@@ -161,7 +161,7 @@ class Annotator.Editor extends Annotator.Widget
#
# Returns itself.
submit: (event) =>
util.preventEventDefault event
Annotator.Util.preventEventDefault event
for field in @fields
field.submit(field.element, @annotation)
......@@ -227,7 +227,7 @@ class Annotator.Editor extends Annotator.Widget
# Returns the created <li> Element.
addField: (options) ->
field = $.extend({
id: 'annotator-field-' + util.uuid()
id: 'annotator-field-' + Annotator.Util.uuid()
type: 'input'
label: ''
load: ->
......
......@@ -34,7 +34,7 @@ class Annotator.Plugin.TextSelection extends Annotator.Plugin
#
# Returns Array of NormalizedRange instances.
_getSelectedRanges: ->
selection = @Annotator.util.getGlobal().getSelection()
selection = @Annotator.Util.getGlobal().getSelection()
ranges = []
rangesToIgnore = []
......
......@@ -33,6 +33,19 @@ Util.flatten = (array) ->
flatten(array)
# Public: decides whether node A is an ancestor of node B.
#
# This function purposefully ignores the native browser function for this,
# because it acts weird in PhantomJS.
# Issue: https://github.com/ariya/phantomjs/issues/11479
Util.contains = (parent, child) ->
node = child
while node?
if node is parent then return true
node = node.parentNode
return false
# Public: Finds all text nodes within the elements in the current collection.
#
# Returns a new jQuery collection of text nodes.
......@@ -97,6 +110,17 @@ Util.getFirstTextNodeNotBefore = (n) ->
else
null
# Public: read out the text value of a range using the selection API
#
# This method selects the specified range, and asks for the string
# value of the selection. What this returns is very close to what the user
# actually sees.
Util.readRangeViaSelection = (range) ->
sel = Util.getGlobal().getSelection() # Get the browser selection object
sel.removeAllRanges() # clear the selection
sel.addRange range.toRange() # Select the range
sel.toString() # Read out the selection
Util.xpathFromNode = (el, relativeRoot) ->
try
result = simpleXPathJQuery.call el, relativeRoot
......@@ -121,3 +145,36 @@ Util.escape = (html) ->
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/"/g, '&quot;')
Util.uuid = (-> counter = 0; -> counter++)()
Util.getGlobal = -> (-> this)()
# Return the maximum z-index of any element in $elements (a jQuery collection).
Util.maxZIndex = ($elements) ->
all = for el in $elements
if $(el).css('position') == 'static'
-1
else
# Use parseFloat since we may get scientific notation for large
# values.
parseFloat($(el).css('z-index')) or -1
Math.max.apply(Math, all)
Util.mousePosition = (e, offsetEl) ->
# If the offset element is not a positioning root use its offset parent
unless $(offsetEl).css('position') in ['absolute', 'fixed', 'relative']
offsetEl = $(offsetEl).offsetParent()[0]
offset = $(offsetEl).offset()
{
top: e.pageY - offset.top,
left: e.pageX - offset.left
}
# Checks to see if an event parameter is provided and contains the prevent
# default method. If it does it calls it.
#
# This is useful for methods that can be optionally used as callbacks
# where the existance of the parameter must be checked before calling.
Util.preventEventDefault = (event) ->
event?.preventDefault?()
......@@ -70,7 +70,7 @@ class Annotator.Viewer extends Annotator.Widget
#
# Returns itself.
show: (event) =>
util.preventEventDefault event
Annotator.Util.preventEventDefault event
controls = @element
.find('.annotator-controls')
......@@ -110,7 +110,7 @@ class Annotator.Viewer extends Annotator.Widget
#
# Returns itself.
hide: (event) =>
util.preventEventDefault event
Annotator.Util.preventEventDefault event
@element.addClass(@classes.hide)
this.publish('hide')
......
......@@ -33,7 +33,7 @@ class Annotator.Widget extends Delegator
checkOrientation: ->
this.resetOrientation()
window = $(util.getGlobal())
window = $(Annotator.Util.getGlobal())
widget = @element.children(":first")
offset = widget.offset()
viewport = {
......
......@@ -353,7 +353,7 @@ class Annotator.Guest extends Annotator
event.stopPropagation()
@adder.hide()
annotation = this.setupAnnotation(this.createAnnotation())
Annotator.util.getGlobal().getSelection().removeAllRanges()
Annotator.Util.getGlobal().getSelection().removeAllRanges()
this.showEditor(annotation)
onSetTool: (name) ->
......
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