-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
293 lines (254 loc) · 6.93 KB
/
index.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
'use strict';
var domify = require('domify'),
_each = require('lodash/each'),
_includes = require('lodash/includes');
/**
* Get url without _how_ to access it, creating a uri.
*
* Removes port and protocol.
*
* @returns {string}
*/
function uri() {
var location = document.location;
return location.hostname + location.pathname;
}
/**
* get page uri
* note: page uri should be in the data-uri attribute of the <html> element
* @returns {string}
*/
function pageUri() {
return document.firstElementChild.getAttribute('data-uri');
}
/**
* This function can be minimized smaller than document.querySelector
* @param {Element} [el]
* @param {string} selector
* @returns {Element}
* @example find('ul') //finds globally
* @example find(el, '.list') //finds within
*/
function find(el, selector) {
if (!selector) {
selector = el;
el = document;
}
return el.querySelector(selector);
}
/**
* This function can be minimized smaller than document.querySelector
* @param {Element} [el]
* @param {string} selector
* @returns {NodeList}
* @example findAll('ul') //finds globally
* @example findAll(el, '.list') //finds within
*/
function findAll(el, selector) {
if (!selector) {
selector = el;
el = document;
}
return el.querySelectorAll(selector);
}
/**
* NOTE: nodeType of 1 means Element
* @param {Element} parent
* @returns {Element} cursor
*/
function getFirstChildElement(parent) {
var cursor = parent.firstChild;
while (cursor && cursor.nodeType !== 1) {
cursor = cursor.nextSibling;
}
return cursor;
}
/**
* Returns true if the element would be selected by the specified selector.
* Essentially a polyfill, but necessary for `closest`.
* @param {Node} node preferably an Element for better performance, but it will accept any Node.
* @param {string} selector
* @returns {boolean}
*/
function matches(node, selector) {
var parent, matches, i;
if (node.matches) {
return node.matches(selector);
} else {
parent = node.parentElement;
matches = parent ? parent.querySelectorAll(selector) : [];
i = 0;
while (matches[i] && matches[i] !== node) {
i++;
}
return !!matches[i];
}
}
/**
* get closest element that matches selector starting with the element itself and traversing up through parents.
* @param {Element} node
* @param {string} parentSelector
* @return {Element|null}
*/
function closest(node, parentSelector) {
var cursor = node;
if (!parentSelector || typeof parentSelector !== 'string') {
throw new Error('Please specify a selector to match against!');
}
while (cursor && !matches(cursor, parentSelector)) {
cursor = cursor.parentNode;
}
if (!cursor) {
return null;
} else {
return cursor;
}
}
function prependChild(parent, child) {
if (parent.firstChild) {
parent.insertBefore(child, parent.firstChild);
} else {
parent.appendChild(child);
}
}
function insertBefore(node, newNode) {
if (node.parentNode) {
node.parentNode.insertBefore(newNode, node);
}
}
function insertAfter(node, newNode) {
if (node.parentNode) {
node.parentNode.insertBefore(newNode, node.nextSibling);
}
}
/**
* Fast way to clear all children
* @see http://jsperf.com/innerhtml-vs-removechild/294
* @param {Element} el
*/
function clearChildren(el) {
while (el.firstChild) {
el.removeChild(el.firstChild);
}
}
/**
* Remove a single element from its parent
* @param {Element} el
*/
function removeElement(el) {
el.parentNode.removeChild(el);
}
function preventDefault(e) {
e.preventDefault ? e.preventDefault() : e.returnValue = false; // eslint-disable-line
}
function replaceElement(el, replacementEl) {
var parent = el.parentNode;
if (parent) {
parent.replaceChild(replacementEl, el);
}
}
/**
* wrap elements in another element
* @param {NodeList|Element} els
* @param {string} wrapper
* @returns {Element} wrapperEl
*/
function wrapElements(els, wrapper) {
var wrapperEl = document.createElement(wrapper);
// make sure elements are in an array
if (els instanceof HTMLElement) {
els = [els];
} else {
els = Array.prototype.slice.call(els);
}
_each(els, function (el) {
// put it into the wrapper, remove it from its parent
el.parentNode.removeChild(el);
wrapperEl.appendChild(el);
});
// return the wrapped elements
return wrapperEl;
}
/**
* unwrap elements from another element
* @param {Element} parent
* @param {Element} wrapper
*/
function unwrapElements(parent, wrapper) {
var el = wrapper.childNodes[0];
// ok, so this looks weird, right?
// turns out, appending nodes to another node will remove them
// from the live NodeList, so we can keep iterating over the
// first item in that list and grab all of them. Nice!
while (el) {
parent.appendChild(el);
el = wrapper.childNodes[0];
}
parent.removeChild(wrapper);
}
/**
* Create a remove node handler that runs fn and removes the observer.
* @param {Element} el
* @param {Function} fn
* @returns {Function}
*/
function createRemoveNodeHandler(el, fn) {
return function (mutations, observer) {
mutations.forEach(function (mutation) {
if (_includes(mutation.removedNodes, el)) {
fn();
observer.disconnect();
}
});
};
}
/**
* Run a function when an element is removed from the DOM.
* Note: Observer is removed after the function is run once.
* @param {Element} el Element to observe.
* @param {Function} fn Function to execute when element is removed.
*/
function onRemove(el, fn) {
var observer = new MutationObserver(this.createRemoveNodeHandler(el, fn));
observer.observe(el.parentNode, {childList: true});
}
/**
* Get the position of a DOM element
* @param {Element} el
* @return {object}
*/
function getPos(el) {
var rect = el.getBoundingClientRect(),
scrollY = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
return {
top: rect.top + scrollY,
bottom: rect.top + rect.height + scrollY,
height: rect.height
};
}
// creating elements
module.exports.create = domify;
// getting uri stuff
module.exports.uri = uri;
module.exports.pageUri = pageUri;
// finding elements (these minify nicely)
module.exports.find = find;
module.exports.findAll = findAll;
module.exports.matches = matches;
module.exports.closest = closest;
module.exports.getFirstChildElement = getFirstChildElement;
module.exports.getPos = getPos;
// manipulating elements
module.exports.prependChild = prependChild;
module.exports.insertBefore = insertBefore;
module.exports.insertAfter = insertAfter;
module.exports.replaceElement = replaceElement;
module.exports.removeElement = removeElement;
module.exports.clearChildren = clearChildren;
// wrapping and unwrapping elements
module.exports.wrapElements = wrapElements;
module.exports.unwrapElements = unwrapElements;
// event handling
module.exports.preventDefault = preventDefault;
module.exports.createRemoveNodeHandler = createRemoveNodeHandler;
module.exports.onRemove = onRemove;