forked from msurguy/opentip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.component.coffee
138 lines (102 loc) · 3.58 KB
/
adapter.component.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
# Component Opentip Adapter
# ======================
#
# Uses github.com/component components
$ = require "jquery"
# The adapter class
module.exports = class Adapter
name: "component"
# Simply using $.domReady
domReady: (callback) -> $ callback
# DOM
# ===
# Using bonzo to create html
create: (html) -> $ html
# Element handling
# ----------------
# Wraps the element in ender
wrap: (element) ->
element = $ element
throw new Error "Multiple elements provided." if element.length > 1
element
# Returns the unwrapped element
unwrap: (element) -> $(element)[0]
# 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...) -> $(element).attr 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, args...) -> $(element).data args...
# Finds elements by selector
find: (element, selector) -> $(element).find selector
# Finds all elements by selector
findAll: -> @find.apply @, arguments
# Updates the content of the element
update: (element, content, escape) ->
element = $ element
if escape
element.text content
else
element.html content
# Appends given child to element
append: (element, child) -> $(element).append child
# Add a class
addClass: (element, className) -> $(element).addClass className
# Remove a class
removeClass: (element, className) -> $(element).removeClass className
# Set given css properties
css: (element, properties) -> $(element).css properties
# Returns an object with given dimensions
dimensions: (element) ->
{
width: $(element).outerWidth()
height: $(element).outerHeight()
}
# Returns the scroll offsets of current document
scrollOffset: ->
[
window.pageXOffset or document.documentElement.scrollLeft or document.body.scrollLeft
window.pageYOffset or document.documentElement.scrollTop or document.body.scrollTop
]
# Returns the dimensions of the viewport (currently visible browser area)
viewportDimensions: ->
{
width: document.documentElement.clientWidth
height: document.documentElement.clientHeight
}
# Returns an object with x and y
mousePosition: (e) ->
return null unless e?
x: e.pageX, y: e.pageY
# Returns the offset of the element
offset: (element) ->
offset = $(element).offset()
{
left: offset.left
top: offset.top
}
# Observe given eventName
observe: (element, eventName, observer) -> $(element).bind eventName, observer
# Stop observing event
stopObserving: (element, eventName, observer) -> $(element).unbind eventName, observer
# Perform an AJAX request and call the appropriate callbacks.
ajax: (options) ->
throw new Error "No url provided" unless options.url?
$.ajax(
url: options.url
type: options.method?.toUpperCase() ? "GET"
)
.done((content) -> options.onSuccess? content)
.fail((request) -> options.onError? "Server responded with status #{request.status}")
.always(-> options.onComplete?())
# Utility functions
# =================
# Creates a shallow copy of the object
clone: (object) -> $.extend { }, object
# Copies all properties from sources to target
extend: (target, sources...) -> $.extend target, sources...