-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtlg-count.js
84 lines (72 loc) · 3.65 KB
/
tlg-count.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
document.addEventListener("DOMContentLoaded", function () {
const root = document.documentElement;
const elementsData = []; // Array to store elements and their attributes
const parseValue = (value) => {
return parseFloat(value.replace(',', '.').replace(/[^\d.-]/g, ''));
};
const addThousandSeparator = (value, separator, decimalSeparator) => {
let [integerPart, decimalPart] = value.replace(decimalSeparator, '.').split('.');
integerPart = integerPart.replace(/\B(?=(\d{3})+(?!\d))/g, separator);
return decimalPart ? integerPart + decimalSeparator + decimalPart : integerPart;
};
const updateElementText = (elementData, numericValue) => {
let formattedValue = numericValue.toFixed(elementData.decimals);
if (!isNaN(elementData.countDigits)) {
// Apply thousand separator before padding with zeros
formattedValue = addThousandSeparator(formattedValue, elementData.thousandSeparator, elementData.decimalSeparator);
const parts = formattedValue.split(elementData.decimalSeparator);
let integerPartWithPadding = parts[0].replace(/,/g, ''); // Remove existing separators for accurate padding
integerPartWithPadding = integerPartWithPadding.padStart(elementData.countDigits, '0');
// Reapply thousand separator after padding
parts[0] = addThousandSeparator(integerPartWithPadding, elementData.thousandSeparator, '.');
formattedValue = parts.join(elementData.decimalSeparator);
} else {
// If countDigits is not specified, just apply thousand separator
formattedValue = addThousandSeparator(formattedValue, elementData.thousandSeparator, elementData.decimalSeparator);
}
if (elementData.element.innerText !== formattedValue) {
elementData.element.innerText = formattedValue;
}
};
const initAndUpdateElements = () => {
const elements = document.querySelectorAll('[tlg-count^="number-"]');
elements.forEach((element, index) => {
const decimalAttribute = element.getAttribute('tlg-count-decimals');
const elementData = {
element: element,
index: element.getAttribute('tlg-count').split('-')[1],
decimals: isNaN(parseInt(decimalAttribute)) ? 0 : parseInt(decimalAttribute),
thousandSeparator: element.getAttribute('tlg-count-thousand-separator') || '',
decimalSeparator: element.getAttribute('tlg-count-decimal-separator') || '.',
countDigits: parseInt(element.getAttribute('tlg-count-digits')),
};
elementsData.push(elementData); // Store element and attributes for later use
// Update numbers initially
elementsData.forEach(elementData => {
let rawValue = root.style.getPropertyValue(`--tlg--count-${elementData.index}`).trim() || getComputedStyle(root).getPropertyValue(`--tlg--count-${elementData.index}`).trim();
if (rawValue) {
const numericValue = parseValue(rawValue);
updateElementText(elementData, numericValue);
}
});
});
};
const observer = new MutationObserver((mutations) => {
mutations.forEach(mutation => {
if (mutation.type === 'attributes' && mutation.attributeName === 'style') {
elementsData.forEach(elementData => {
let rawValue = root.style.getPropertyValue(`--tlg--count-${elementData.index}`).trim() || getComputedStyle(root).getPropertyValue(`--tlg--count-${elementData.index}`).trim();
if (rawValue) {
const numericValue = parseValue(rawValue);
updateElementText(elementData, numericValue);
}
});
}
});
});
observer.observe(root, {
attributes: true,
attributeFilter: ['style']
});
initAndUpdateElements(); // Initial setup
});