-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
37 lines (33 loc) · 1.01 KB
/
util.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
(function(global){
"use strict";
global.elem = function elem(tag, attrs, children){
attrs = attrs || {};
children = children || [];
let e = document.createElement(tag);
Object.keys(attrs).forEach(
key => e.setAttribute(key, attrs[key])
);
children.forEach(function(child){
if (typeof child == 'string'){
child = document.createTextNode(child);
}
e.appendChild(child);
});
return e;
};
global.matches = function matches(elem, selector){
return elem.matches(selector);
};
global.trigger = function trigger(name, target){
target.dispatchEvent( new CustomEvent(name, {bubbles: true, cancelable: false}));
};
global.closest = function closest(elem, selector){
while (elem){
if (matches(elem, selector)){
return elem;
}
elem = elem.parentElement;
}
return null;
};
})(window);