Skip to content

Commit b071444

Browse files
committed
Annotator release v1.2.9
1 parent f4d470a commit b071444

File tree

140 files changed

+24124
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

140 files changed

+24124
-0
lines changed

pkg/_annotator-full.js

Lines changed: 3228 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/_annotator-full.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/_annotator-full.min.js

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/_annotator-full.min.map

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/_annotator-full_mapsrc/src/annotator.coffee

Lines changed: 791 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,245 @@
1+
# Public: Delegator is the base class that all of Annotators objects inherit
2+
# from. It provides basic functionality such as instance options, event
3+
# delegation and pub/sub methods.
4+
class Delegator
5+
# Public: Events object. This contains a key/pair hash of events/methods that
6+
# should be bound. See Delegator#addEvents() for usage.
7+
events: {}
8+
9+
# Public: Options object. Extended on initialisation.
10+
options: {}
11+
12+
# A jQuery object wrapping the DOM Element provided on initialisation.
13+
element: null
14+
15+
# Public: Constructor function that sets up the instance. Binds the @events
16+
# hash and extends the @options object.
17+
#
18+
# element - The DOM element that this intance represents.
19+
# options - An Object literal of options.
20+
#
21+
# Examples
22+
#
23+
# element = document.getElementById('my-element')
24+
# instance = new Delegator(element, {
25+
# option: 'my-option'
26+
# })
27+
#
28+
# Returns a new instance of Delegator.
29+
constructor: (element, options) ->
30+
@options = $.extend(true, {}, @options, options)
31+
@element = $(element)
32+
33+
# Delegator creates closures for each event it binds. This is a private
34+
# registry of created closures, used to enable event unbinding.
35+
@_closures = {}
36+
37+
this.on = this.subscribe
38+
this.addEvents()
39+
40+
# Public: binds the function names in the @events Object to their events.
41+
#
42+
# The @events Object should be a set of key/value pairs where the key is the
43+
# event name with optional CSS selector. The value should be a String method
44+
# name on the current class.
45+
#
46+
# This is called by the default Delegator constructor and so shouldn't usually
47+
# need to be called by the user.
48+
#
49+
# Examples
50+
#
51+
# # This will bind the clickedElement() method to the click event on @element.
52+
# @options = {"click": "clickedElement"}
53+
#
54+
# # This will delegate the submitForm() method to the submit event on the
55+
# # form within the @element.
56+
# @options = {"form submit": "submitForm"}
57+
#
58+
# # This will bind the updateAnnotationStore() method to the custom
59+
# # annotation:save event. NOTE: Because this is a custom event the
60+
# # Delegator#subscribe() method will be used and updateAnnotationStore()
61+
# # will not recieve an event parameter like the previous two examples.
62+
# @options = {"annotation:save": "updateAnnotationStore"}
63+
#
64+
# Returns nothing.
65+
addEvents: ->
66+
for event in Delegator._parseEvents(@events)
67+
this._addEvent event.selector, event.event, event.functionName
68+
69+
# Public: unbinds functions previously bound to events by addEvents().
70+
#
71+
# The @events Object should be a set of key/value pairs where the key is the
72+
# event name with optional CSS selector. The value should be a String method
73+
# name on the current class.
74+
#
75+
# Returns nothing.
76+
removeEvents: ->
77+
for event in Delegator._parseEvents(@events)
78+
this._removeEvent event.selector, event.event, event.functionName
79+
80+
# Binds an event to a callback function represented by a String. A selector
81+
# can be provided in order to watch for events on a child element.
82+
#
83+
# The event can be any standard event supported by jQuery or a custom String.
84+
# If a custom string is used the callback function will not recieve an
85+
# event object as it's first parameter.
86+
#
87+
# selector - Selector String matching child elements. (default: '')
88+
# event - The event to listen for.
89+
# functionName - A String function name to bind to the event.
90+
#
91+
# Examples
92+
#
93+
# # Listens for all click events on instance.element.
94+
# instance._addEvent('', 'click', 'onClick')
95+
#
96+
# # Delegates the instance.onInputFocus() method to focus events on all
97+
# # form inputs within instance.element.
98+
# instance._addEvent('form :input', 'focus', 'onInputFocus')
99+
#
100+
# Returns itself.
101+
_addEvent: (selector, event, functionName) ->
102+
closure = => this[functionName].apply(this, arguments)
103+
104+
if selector == '' and Delegator._isCustomEvent(event)
105+
this.subscribe(event, closure)
106+
else
107+
@element.delegate(selector, event, closure)
108+
109+
@_closures["#{selector}/#{event}/#{functionName}"] = closure
110+
111+
this
112+
113+
# Unbinds a function previously bound to an event by the _addEvent method.
114+
#
115+
# Takes the same arguments as _addEvent(), and an event will only be
116+
# successfully unbound if the arguments to removeEvent() are exactly the same
117+
# as the original arguments to _addEvent(). This would usually be called by
118+
# _removeEvents().
119+
#
120+
# selector - Selector String matching child elements. (default: '')
121+
# event - The event to listen for.
122+
# functionName - A String function name to bind to the event.
123+
#
124+
# Returns itself.
125+
_removeEvent: (selector, event, functionName) ->
126+
closure = @_closures["#{selector}/#{event}/#{functionName}"]
127+
128+
if selector == '' and Delegator._isCustomEvent(event)
129+
this.unsubscribe(event, closure)
130+
else
131+
@element.undelegate(selector, event, closure)
132+
133+
delete @_closures["#{selector}/#{event}/#{functionName}"]
134+
135+
this
136+
137+
138+
# Public: Fires an event and calls all subscribed callbacks with any parameters
139+
# provided. This is essentially an alias of @element.triggerHandler() but
140+
# should be used to fire custom events.
141+
#
142+
# NOTE: Events fired using .publish() will not bubble up the DOM.
143+
#
144+
# event - A String event name.
145+
# params - An Array of parameters to provide to callbacks.
146+
#
147+
# Examples
148+
#
149+
# instance.subscribe('annotation:save', (msg) -> console.log(msg))
150+
# instance.publish('annotation:save', ['Hello World'])
151+
# # => Outputs "Hello World"
152+
#
153+
# Returns itself.
154+
publish: () ->
155+
@element.triggerHandler.apply @element, arguments
156+
this
157+
158+
# Public: Listens for custom event which when published will call the provided
159+
# callback. This is essentially a wrapper around @element.bind() but removes
160+
# the event parameter that jQuery event callbacks always recieve. These
161+
# parameters are unnessecary for custom events.
162+
#
163+
# event - A String event name.
164+
# callback - A callback function called when the event is published.
165+
#
166+
# Examples
167+
#
168+
# instance.subscribe('annotation:save', (msg) -> console.log(msg))
169+
# instance.publish('annotation:save', ['Hello World'])
170+
# # => Outputs "Hello World"
171+
#
172+
# Returns itself.
173+
subscribe: (event, callback) ->
174+
closure = -> callback.apply(this, [].slice.call(arguments, 1))
175+
176+
# Ensure both functions have the same unique id so that jQuery will accept
177+
# callback when unbinding closure.
178+
closure.guid = callback.guid = ($.guid += 1)
179+
180+
@element.bind event, closure
181+
this
182+
183+
# Public: Unsubscribes a callback from an event. The callback will no longer
184+
# be called when the event is published.
185+
#
186+
# event - A String event name.
187+
# callback - A callback function to be removed.
188+
#
189+
# Examples
190+
#
191+
# callback = (msg) -> console.log(msg)
192+
# instance.subscribe('annotation:save', callback)
193+
# instance.publish('annotation:save', ['Hello World'])
194+
# # => Outputs "Hello World"
195+
#
196+
# instance.unsubscribe('annotation:save', callback)
197+
# instance.publish('annotation:save', ['Hello Again'])
198+
# # => No output.
199+
#
200+
# Returns itself.
201+
unsubscribe: ->
202+
@element.unbind.apply @element, arguments
203+
this
204+
205+
206+
# Parse the @events object of a Delegator into an array of objects containing
207+
# string-valued "selector", "event", and "func" keys.
208+
Delegator._parseEvents = (eventsObj) ->
209+
events = []
210+
for sel, functionName of eventsObj
211+
[selector..., event] = sel.split ' '
212+
events.push({
213+
selector: selector.join(' '),
214+
event: event,
215+
functionName: functionName
216+
})
217+
return events
218+
219+
220+
# Native jQuery events that should recieve an event object. Plugins can
221+
# add their own methods to this if required.
222+
Delegator.natives = do ->
223+
specials = (key for own key, val of jQuery.event.special)
224+
"""
225+
blur focus focusin focusout load resize scroll unload click dblclick
226+
mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave
227+
change select submit keydown keypress keyup error
228+
""".split(/[^a-z]+/).concat(specials)
229+
230+
231+
# Checks to see if the provided event is a DOM event supported by jQuery or
232+
# a custom user event.
233+
#
234+
# event - String event name.
235+
#
236+
# Examples
237+
#
238+
# Delegator._isCustomEvent('click') # => false
239+
# Delegator._isCustomEvent('mousedown') # => false
240+
# Delegator._isCustomEvent('annotation:created') # => true
241+
#
242+
# Returns true if event is a custom user event.
243+
Delegator._isCustomEvent = (event) ->
244+
[event] = event.split('.')
245+
$.inArray(event, Delegator.natives) == -1
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Stub the console when not available so that everything still works.
2+
3+
functions = [
4+
"log", "debug", "info", "warn", "exception", "assert", "dir", "dirxml",
5+
"trace", "group", "groupEnd", "groupCollapsed", "time", "timeEnd", "profile",
6+
"profileEnd", "count", "clear", "table", "error", "notifyFirebug", "firebug",
7+
"userObjects"
8+
]
9+
10+
if console?
11+
# Opera's console doesn't have a group function as of 2010-07-01
12+
if not console.group?
13+
console.group = (name) -> console.log "GROUP: ", name
14+
15+
# Webkit's developer console has yet to implement groupCollapsed as of 2010-07-01
16+
if not console.groupCollapsed?
17+
console.groupCollapsed = console.group
18+
19+
# Stub out any remaining functions
20+
for fn in functions
21+
if not console[fn]?
22+
console[fn] = -> console.log _t("Not implemented:") + " console.#{name}"
23+
else
24+
this.console = {}
25+
26+
for fn in functions
27+
this.console[fn] = ->
28+
29+
this.console['error'] = (args...) ->
30+
alert("ERROR: #{args.join(', ')}")
31+
32+
this.console['warn'] = (args...) ->
33+
alert("WARNING: #{args.join(', ')}")

0 commit comments

Comments
 (0)