-
Notifications
You must be signed in to change notification settings - Fork 0
/
translate-xy.js
49 lines (46 loc) · 1.88 KB
/
translate-xy.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
if (!customElements.get("translate-xy")) {
customElements.define("translate-xy", class extends HTMLElement {
constructor() {
super();
this.onMutation = this.onMutation.bind(this);
}
connectedCallback() {
this.load();
this.observer = new MutationObserver(this.onMutation);
this.observer.observe(this, {
childList: true,
subtree: true
});
}
disconnectedCallback() {
this.observer.disconnect();
}
load() {
let self = this,
translatexy = JSON.parse(self.dataset.translatexy) || {},
translatexySort = Object.keys(translatexy).sort().reverse().reduce((r, k) => (r[k] = translatexy[k], r), {});
Object.entries(translatexySort).forEach(entry => {
var originalStr = entry[0],
translateStr = entry[1];
var regex = new RegExp(originalStr, 'g');
var elements = self.getElementsByTagName('*');
for (var i = 0; i < elements.length; i++) {
var element = elements[i];
for (var j = 0; j < element.childNodes.length; j++) {
var node = element.childNodes[j];
if (node.nodeType === 3) {
var text = node.nodeValue;
var replacedText = text.replace(regex,translateStr);
if (replacedText !== text) {
element.replaceChild(document.createTextNode(replacedText), node);
}
}
}
}
});
}
onMutation(mutations) {
this.load();
}
})
}