-
-
Notifications
You must be signed in to change notification settings - Fork 446
Expand file tree
/
Copy pathdom.js
More file actions
127 lines (95 loc) · 2.74 KB
/
dom.js
File metadata and controls
127 lines (95 loc) · 2.74 KB
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
import {
OFF,
} from './domClasses';
import {
change,
dppxchange,
} from './strings';
export const domEnv = typeof window != 'undefined';
export const doc = domEnv ? document : null;
export const win = domEnv ? window : null;
export let pxRatio;
//export const canHover = domEnv && !win.matchMedia('(hover: none)').matches;
let query;
function setPxRatio() {
let _pxRatio = devicePixelRatio;
// during print preview, Chrome fires off these dppx queries even without changes
if (pxRatio != _pxRatio) {
pxRatio = _pxRatio;
query && off(change, query, setPxRatio);
query = matchMedia(`(min-resolution: ${pxRatio - 0.001}dppx) and (max-resolution: ${pxRatio + 0.001}dppx)`);
on(change, query, setPxRatio);
win.dispatchEvent(new CustomEvent(dppxchange));
}
}
export function addClass(el, c) {
if (c != null) {
let cl = el.classList;
!cl.contains(c) && cl.add(c);
}
}
export function remClass(el, c) {
let cl = el.classList;
cl.contains(c) && cl.remove(c);
}
export function setStylePx(el, name, value) {
el.style[name] = value + "px";
}
export function placeTag(tag, cls, targ, refEl, frag) {
let el = doc.createElement(tag);
if (cls != null)
addClass(el, cls);
if (frag != null) {
frag.appendChild(el);
} else if (targ != null) {
targ.insertBefore(el, refEl);
}
return el;
}
export function placeDiv(cls, targ) {
return placeTag("div", cls, targ);
}
const xformCache = new WeakMap();
export function elTrans(el, xPos, yPos, xMax, yMax) {
let xform = "translate(" + xPos + "px," + yPos + "px)";
let xformOld = xformCache.get(el);
if (xform != xformOld) {
el.style.transform = xform;
xformCache.set(el, xform);
if (xPos < 0 || yPos < 0 || xPos > xMax || yPos > yMax)
addClass(el, OFF);
else
remClass(el, OFF);
}
}
const colorCache = new WeakMap();
export function elColor(el, background, borderColor) {
let newColor = background + borderColor;
let oldColor = colorCache.get(el);
if (newColor != oldColor) {
colorCache.set(el, newColor);
el.style.background = background;
el.style.borderColor = borderColor;
}
}
const sizeCache = new WeakMap();
export function elSize(el, newWid, newHgt, centered) {
let newSize = newWid + "" + newHgt;
let oldSize = sizeCache.get(el);
if (newSize != oldSize) {
sizeCache.set(el, newSize);
el.style.height = newHgt + "px";
el.style.width = newWid + "px";
el.style.marginLeft = centered ? -newWid/2 + "px" : 0;
el.style.marginTop = centered ? -newHgt/2 + "px" : 0;
}
}
const evOpts = {passive: true};
const evOpts2 = {...evOpts, capture: true};
export function on(ev, el, cb, capt) {
el.addEventListener(ev, cb, capt ? evOpts2 : evOpts);
}
export function off(ev, el, cb, capt) {
el.removeEventListener(ev, cb, capt ? evOpts2 : evOpts);
}
domEnv && setPxRatio();