/
jq-helpers.js
154 lines (134 loc) · 3.99 KB
/
jq-helpers.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
145
146
147
148
149
150
151
152
153
154
import serialize, { serializeJSON } from './serialize';
function $(selector) {
const nodes = typeof selector === 'string' ? Array.from((this && Array.isArray(this) ? this[0] : document).querySelectorAll(selector)) : [selector];
const _returnee = {
$nodes: nodes,
$: $.bind(nodes),
on: (event, selector, callback) => {
if (typeof callback === 'undefined') {
callback = selector;
selector = null;
}
if (selector) {
nodes.forEach(node => {
node.addEventListener(event, e => {
if (e.target.matches(selector)) {
callback.call(node, e)
}
})
})
} else {
nodes.forEach(node => node[`on${event}`] = callback.bind(node));
}
return _returnee;
},
addClass: className => {
nodes.forEach(node => node.classList.add(className));
return _returnee;
},
removeClass: className => {
nodes.forEach(node => node.classList.remove(className));
return _returnee;
},
attr: (attribute, value) => {
if (value === undefined && nodes.length > 1) {
throw new Error('Can\'t access value of several nodes\' attributes');
}
if (value === undefined) {
return nodes[0].getAttribute(attribute);
} else if (value !== null) {
nodes.forEach(node => node.setAttribute(attribute, value));
}
return _returnee;
},
removeAttr: attribute => {
nodes.forEach(node => node.removeAttribute(attribute));
return _returnee;
},
append: innerHTML => {
nodes.forEach(node => node.insertAdjacentHTML('beforeend', innerHTML));
return _returnee;
},
html: innerHTML => {
if (innerHTML === undefined) {
if (nodes.length > 1) {
throw new Error('Can\'t get several nodes innerHTML at once');
}
return nodes[0].innerHTML;
}
nodes.forEach(node => node.innerHTML = innerHTML);
return _returnee;
},
text: innerText => {
if (innerText === undefined) {
if (nodes.length > 1) {
throw new Error('Can\'t get several nodes innerText at once');
}
return nodes[0].innerText;
}
if (innerText !== null) {
nodes.forEach(node => node.innerText = innerText);
}
return _returnee;
},
val: value => {
if (value === undefined) {
if (nodes.length > 1) {
throw new Error('Can\'t get several nodes value at once');
}
return nodes[0].value;
}
nodes.forEach(node => node.value = value);
return _returnee;
},
submit: () => nodes.forEach(node => node.submit()),
serialize: (json = false) => {
if (nodes.length > 1) {
throw new Error('Can\'t serialize forms at once');
}
if (json) {
return serializeJSON(nodes[0]);
}
return serialize(nodes[0]);
},
length: nodes.length,
}
nodes.forEach((node, index) => _returnee[index] = node);
return _returnee;
}
$.scrollTo = function scrollTo(element, to, duration) {
if (duration <= 0) return;
var difference = to - element.scrollTop;
var perTick = difference / duration * 10;
setTimeout(function() {
element.scrollTop = element.scrollTop + perTick;
if (element.scrollTop === to) return;
scrollTo(element, to, duration - 10);
}, 10);
}
$.ajax = function ajax({
method,
url,
data,
options = {
contentType: "application/json;charset=UTF-8"
}
}) {
const xhr = new XMLHttpRequest();
xhr.open(method.toUpperCase(), url);
xhr.setRequestHeader("Content-Type", options.contentType);
xhr.send(data);
return new Promise((resolve, reject) => {
xhr.onreadystatechange = () => {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
resolve(JSON.parse(xhr.responseXML || xhr.responseText));
} else {
reject(xhr.statusText);
}
}
}
})
}
$.post = (url, data, options) => $.ajax({ method: 'post', url, data, options })
export default $;