-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
37 lines (32 loc) · 818 Bytes
/
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
import Observer, {shallowListener} from 'destam/Observer.js';
import {isInstance, noop} from 'destam/util.js';
export { mount, getFirst } from './dom.js';
export const createElement = (elem, ns) => {
if (ns) {
return document.createElementNS(ns, elem);
} else {
return document.createElement(elem);
}
};
export const createTextNode = text => document.createTextNode(text);
const update = (cb, obs) => {
cb(obs.get());
};
export const watch = (obs, cb) => {
if (isInstance(obs, Observer)) {
const l = shallowListener(obs, update.bind(null, cb, obs));
update(cb, obs);
return l;
} else {
cb(obs);
return noop;
}
};
export const setAttribute = (e, name, val) => {
val = val ?? false;
if (typeof val === 'boolean') {
e.toggleAttribute(name, val);
} else {
e.setAttribute(name, val);
}
};