This repository has been archived by the owner on Nov 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathutils.js
67 lines (59 loc) · 1.72 KB
/
utils.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
'use strict';
/**
* @param {string} selector One or more CSS selectors separated by commas
* @param {Element} [parent] The element to look inside of
* @return {?Element} The element found, if any
*/
function select(selector, parent) {
return (parent || document).querySelector(selector);
}
/**
* @param {string} selector One or more CSS selectors separated by commas
* @param {Element} [parent] The element to look inside of
* @return {boolean} Whether it's been found
*/
select.exists = function (selector, parent) {
return Boolean(select(selector, parent));
};
/**
* @param {string} selector One or more CSS selectors separated by commas
* @param {Element|Element[]} [parent] The element or list of elements to look inside of
* @return {Element[]} An array of elements found
*/
select.all = function (selector, parent) {
// Can be: select.all('selector') or select.all('selector', singleElementOrDocument)
if (!parent || typeof parent.querySelectorAll === 'function') {
return Array.apply(null, (parent || document).querySelectorAll(selector));
}
var current;
var i;
var ii;
var all;
for (i = 0; i < parent.length; i++) {
current = parent[i].querySelectorAll(selector);
if (!all) {
all = Array.apply(null, current);
continue;
}
for (ii = 0; ii < current.length; ii++) {
if (all.indexOf(current[ii]) < 0) {
all.push(current[ii]);
}
}
}
return all;
};
function observeEl(el, listener, options = {childList: true}) {
if (typeof el === 'string') {
el = select(el);
}
if (!el) {
return;
}
// Run first
listener([]);
// Run on updates
const observer = new MutationObserver(listener);
observer.observe(el, options);
return observer;
};