Skip to content

Commit 13624cc

Browse files
committed
[Update] Added RenderStatick and script cleanup
Added the RenderStatick script removed unnecessary comments from formatNumbers removed unnecessary space from formcheck.
1 parent 3565a4f commit 13624cc

3 files changed

Lines changed: 81 additions & 12 deletions

File tree

Dist/Functional/FormCheck.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ class FormCheck {
101101
}
102102

103103
this.form.reset();
104-
105104
this.submitButton.textContent = `${this.defaultSubmitText}`;
106105
}
107106
}

Dist/Functional/FormatNumbers.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
/*
2-
* Number formating using Intl.NumberFormat.
3-
* Requirements:
4-
* Element with custom attribute fn-formatnumber="true"
5-
* A Style in the same element to format choosing between "Currency", "decimal", "percent", "unit"
6-
* In case of the element being "Currency" or "Unit" the sent "fn-formatnumber-currency" or "fn-formatnumber-unit" option
7-
* Optional: set locales for the formatting
8-
* list of units can be found here for now: https://tc39.es/proposal-unified-intl-numberformat/section6/locales-currencies-tz_proposed_out.html#sec-issanctionedsimpleunitidentifier
9-
* currency uses regular notation for them.
10-
*/
11-
121
'use strict'
132

143
let FormatNo = (element, locales, number, options) => {

Dist/WebflowOnly/RenderStatic.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
'use strict'
2+
3+
class RenderStatic {
4+
constructor() {
5+
this.cloneables = [];
6+
this.container = null;
7+
this.gap = 0;
8+
this.observer = null;
9+
this.init();
10+
}
11+
12+
init() {
13+
document.addEventListener('DOMContentLoaded', () => {
14+
setTimeout(() => {
15+
this.initializeRenderStatic();
16+
this.observeContainer();
17+
}, 250);
18+
});
19+
}
20+
21+
insertChildAtIndex(parent, child, index = 0) {
22+
if (!child) return;
23+
let childClone = child.cloneNode(true);
24+
if (parent) {
25+
parent.insertBefore(childClone, parent.children[index]);
26+
}
27+
}
28+
29+
initializeRenderStatic() {
30+
this.container = this.container || document.querySelector("[wt-renderstatic-element='container']");
31+
this.cloneables = this.cloneables.length === 0 ? document.querySelectorAll("[wt-renderstatic-element='cloneable']") : this.cloneables;
32+
33+
if (!this.container || this.cloneables.length === 0) return;
34+
35+
this.gap = this.gap !== 0 ? this.gap : +this.container.getAttribute("wt-renderstatic-gap") || 0;
36+
37+
// Stop observing to prevent the observer from reacting to clone removals
38+
if (this.observer) this.observer.disconnect();
39+
40+
let childrenArray = Array.from(this.container.children);
41+
42+
childrenArray.forEach(child => {
43+
if (child.hasAttribute("wt-renderstatic-element")) child.remove();
44+
});
45+
46+
let insertIndex = this.gap;
47+
const totalChildren = childrenArray.length;
48+
const totalInsertions = Math.round(totalChildren / this.gap);
49+
let cloneIndex = 0;
50+
51+
for (let i = 0; i <= totalChildren + totalInsertions; i++) {
52+
if (i === insertIndex) {
53+
this.insertChildAtIndex(this.container, this.cloneables[cloneIndex], insertIndex);
54+
cloneIndex = (cloneIndex < this.cloneables.length - 1) ? cloneIndex + 1 : 0;
55+
insertIndex = i + this.gap + 1;
56+
}
57+
}
58+
59+
// Re-observe the container
60+
this.observeContainer();
61+
}
62+
63+
observeContainer() {
64+
if (!this.container) return;
65+
66+
this.observer = new MutationObserver((mutations) => {
67+
let hasNonCloneChanges = mutations.some(mutation => {
68+
return Array.from(mutation.addedNodes).some(node => !node.hasAttribute("wt-renderstatic-element")) ||
69+
Array.from(mutation.removedNodes).some(node => !node.hasAttribute("wt-renderstatic-element"));
70+
});
71+
72+
if (hasNonCloneChanges) {
73+
this.initializeRenderStatic();
74+
}
75+
});
76+
77+
this.observer.observe(this.container, { childList: true });
78+
}
79+
}
80+
81+
new RenderStatic();

0 commit comments

Comments
 (0)