forked from enyo/opentip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter-prototype.coffee
161 lines (120 loc) · 4.77 KB
/
adapter-prototype.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
# Prototype Opentip Adapter
# ======================
#
# Uses the prototype framework
do ->
Element.addMethods
addTip: (element, content, title, options) ->
new Opentip element, content, title, options
# Needs this function because of IE8
isArrayOrNodeList = (element) ->
if (element instanceof Array) or (element? and typeof element.length == 'number' and typeof element.item == 'function' and typeof element.nextNode == 'function' and typeof element.reset == 'function')
return yes
return no
# And now the class
class Adapter
name: "prototype"
domReady: (callback) ->
if document.loaded
callback()
else
$(document).observe "dom:loaded", callback
# DOM
# ===
# Using bonzo to create html
create: (html) -> new Element('div').update(html).childElements()
# Element handling
# ----------------
# Wraps the element
wrap: (element) ->
if isArrayOrNodeList element
throw new Error "Multiple elements provided." if element.length > 1
element = @unwrap element
else if typeof element == "string"
element = $$(element)[0]
$ element
# Returns the unwrapped element
unwrap: (element) ->
if isArrayOrNodeList element
element[0]
else
element
# Returns the tag name of the element
tagName: (element) -> @unwrap(element).tagName
# Returns or sets the given attribute of element
#
# It's important not to simply forward name and value because the value
# is set whether or not the value argument is present
attr: (element, args...) ->
if args.length == 1
@wrap(element).readAttribute args[0]
else
@wrap(element).writeAttribute args...
# Returns or sets the given data of element
# It's important not to simply forward name and value because the value
# is set whether or not the value argument is present
data: (element, name, value) ->
@wrap(element)
if arguments.length > 2
element.store name, value
else
arg = element.readAttribute "data-#{name.underscore().dasherize()}"
return arg if arg?
element.retrieve name
# Finds elements by selector
find: (element, selector) -> @wrap(element).select(selector)[0]
# Finds all elements by selector
findAll: (element, selector) -> @wrap(element).select selector
# Updates the content of the element
update: (element, content, escape) ->
@wrap(element).update if escape then content.escapeHTML() else content
# Appends given child to element
append: (element, child) -> @wrap(element).insert @wrap child
# Removes element
remove: (element) -> @wrap(element).remove()
# Add a class
addClass: (element, className) -> @wrap(element).addClassName className
# Remove a class
removeClass: (element, className) -> @wrap(element).removeClassName className
# Set given css properties
css: (element, properties) -> @wrap(element).setStyle properties
# Returns an object with given dimensions
dimensions: (element) -> @wrap(element).getDimensions()
# Returns the scroll offsets of current document
scrollOffset: ->
offsets = document.viewport.getScrollOffsets()
[ offsets.left, offsets.top ]
# Returns the dimensions of the viewport (currently visible browser area)
viewportDimensions: -> document.viewport.getDimensions()
# Returns an object with x and y
mousePosition: (e) ->
return null unless e?
x: Event.pointerX(e), y: Event.pointerY(e)
# Returns the offset of the element
offset: (element) ->
offset = @wrap(element).cumulativeOffset()
left: offset.left, top: offset.top
# Observe given eventName
observe: (element, eventName, observer) -> Event.observe @wrap(element), eventName, observer
# Stop observing event
stopObserving: (element, eventName, observer) -> Event.stopObserving @wrap(element), eventName, observer
# Perform an AJAX request and call the appropriate callbacks.
ajax: (options) ->
throw new Error "No url provided" unless options.url?
new Ajax.Request options.url, {
method: options.method?.toUpperCase() ? "GET"
onSuccess: (response) -> options.onSuccess? response.responseText
onFailure: (response) -> options.onError? "Server responded with status #{response.status}"
onComplete: -> options.onComplete?()
}
# Utility functions
# =================
# Creates a shallow copy of the object
clone: (object) -> Object.clone(object)
# Copies all properties from sources to target
extend: (target, sources...) ->
for source in sources
Object.extend target, source
target
# Add the adapter to the list
Opentip.addAdapter new Adapter