-
Notifications
You must be signed in to change notification settings - Fork 0
/
rye.dom.js
144 lines (126 loc) · 3.86 KB
/
rye.dom.js
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
(function(name, deps, definition){
if (typeof module !== 'undefined') module.exports = definition(require)
else if (typeof define === 'function') define(name, deps, definition)
else window[name] = definition(function(name){ return window[name] })
})('rye/dom', [], function(require){
var _slice = Array.prototype.slice
function getValue(element) {
if (element.multiple) {
return new Rye(element).find('option').filter(function(option) {
return option.selected && !option.disabled
}).pluck('value')
}
return element.value
}
function getAttribute(element, name) {
if (name === 'value' && element.nodeName == 'INPUT') {
return getValue(element)
}
return element.getAttribute(name)
}
function append (element, html) {
if (typeof html === 'string') {
element.insertAdjacentHTML('beforeend', html)
} else {
element.appendChild(html)
}
}
function prepend (element, html) {
var first
if (typeof html === 'string') {
element.insertAdjacentHTML('afterbegin', html)
} else if (first = element.childNodes[0]){
element.insertBefore(html, first)
} else {
element.appendChild(html)
}
}
function after (element, html) {
var next = element
if (typeof html === 'string') {
element.insertAdjacentHTML('afterend', html)
return
}
do { next = next.nextSibling } while (next.nodeType !== 1)
if (next) {
element.parentNode.insertBefore(html, next)
} else {
element.parentNode.appendChild(html)
}
}
function before (element, html) {
if (typeof html === 'string') {
element.insertAdjacentHTML('beforebegin', html)
} else {
element.parentNode.insertBefore(html, element)
}
}
function text (element, text) {
if (text == null) {
return element.textContent
}
element.textContent = text
}
function html (element, html) {
if (html == null) {
return element.innerHTML
}
element.innerHTML = html
}
function empty (element) {
while (element.firstChild) {
elem.removeChild(elem.firstChild)
}
}
function clone (deep) {
return element.cloneNode(deep === false ? false : true)
}
function remove (element) {
var parent
if (parent = element.parentNode) {
parent.removeChild(element)
}
}
function val (element) {
if (value == null) {
return getValue(element)
}
return element.value = value
}
function attr (element, name, value) {
if (typeof name === 'object') {
var keys = Object.keys(name)
while (key = keys.pop()) element.setAttribute(key, name[key])
}
if (typeof value !== 'undefined') {
element.setAttribute(name, value)
}
return getAttribute(element, name)
}
function prop (element, name, value) {
if (typeof name === 'object') {
var keys = Object.keys(name)
while (key = keys.pop()) element[key] = name[key]
}
if (typeof value !== 'undefined') {
element[name] = value
}
return element[name]
}
return {
getValue : getValue
, getAttribute : getAttribute
, append : append
, prepend : prepend
, after : after
, before : before
, text : text
, html : html
, empty : empty
, clone : clone
, remove : remove
, val : val
, attr : attr
, prop : prop
}
})