SHX-Theme/snippets/shx-3d-render-input.liquid

214 lines
6.7 KiB
Plaintext

{% assign content_type = content_type | default: 'body' %}
{% if content_type == 'body' %}
{% comment %}
<div id="shx-3d-render-input-container">
<style>.custom.form__label{margin-bottom: 0.6rem}.field.custom{margin-top:0}.custom .field__input{padding-top:0.8rem}</style>
<label class="form__label custom" for="your-label">Dein Text</label>
<div class="field custom">
<input class="field__input" form="{{ 'product-form-' | append: section.id }}" type="text" id="your-label" name="properties[Your label]">
</div>
</div>
{% endcomment %}
<shx-input-forms>
</shx-input-forms>
{% elsif content_type == 'init' %}
<style>
.shx-input-forms-container {
overflow: hidden;
}
.shx_ifc_renderButton {
margin-top: 1rem;
}
</style>
<script type="text/javascript">
const threeJSisLading = false;
const threeJSloaded = false;
let globalInputFormsObjValues = {
"test1": {
"text1": "test"
},
"test2": {
"text1": "test1",
"text2": "test2"
}
};
function SHX_IFC_genTextInput(inputID, labelText, inputName, inputVal) {
return `
<label class="form__label custom" for="${inputID}-label">${labelText}</label>
<div class="field custom">
<input class="field__input" form="{{ 'product-form-' | append: section.id }}" type="text" id="${inputID}" name="properties[${inputName}]" value="${inputVal}">
</div>
`;
}
function SHX_IFC_genRenderButton() {
return `
<button class="button shx_ifc_renderButton" onclick="document.querySelector('shx-input-forms').renderView()">Vorschau generieren</button>
`;
}
const defaultStyle = `<style>.custom.form__label{margin-bottom: 0.6rem}.field.custom{margin-top: 0.6rem}.custom .field__input{padding-top:0.8rem}</style>`;
let globalInputFormsObj = {
"test1": {
"renderHTML": () => {
let html = defaultStyle;
html += SHX_IFC_genTextInput('custom-text-1', 'Custom Text 1', 'Custom Text 1', globalInputFormsObjValues['test1']['text1']);
html += SHX_IFC_genRenderButton();
return html;
}
},"test2": {
"renderHTML": () => {
let html = defaultStyle;
html += SHX_IFC_genTextInput('custom-text-1', 'Custom Text 1', 'Custom Text 1', globalInputFormsObjValues['test2']['text1']);
html += SHX_IFC_genTextInput('custom-text-2', 'Custom Text 2', 'Custom Text 2', globalInputFormsObjValues['test2']['text2']);
html += SHX_IFC_genRenderButton();
return html;
}
}
};
// create new html tag called "shx-input-forms"
class InputFormsElement extends HTMLElement {
constructor() {
super();
// element functionality written in here
this.currentVariant = null;
this.container = null;
}
connectedCallback() {
this.innerHTML = '<div class="shx-input-forms-container"></div>';
this.container = this.querySelector('.shx-input-forms-container');
if(!threeJSisLading && !threeJSloaded) {
// load threejs
let script = document.createElement('script');
script.src = {{ 'three.module.min.js' | asset_url }};
script.id = 'threejs-script';
script.type = "module";
document.head.appendChild(script);
script.onload = () => {
threeJSloaded = true;
console.log('threejs loaded');
}
}
}
initCurrentVariant(id) {
this.currentVariant = id;
// edit inner html
this.reloadCurrentVariant(id, true);
}
disconnectedCallback() {
// Clean up the element when it is removed from the DOM.
}
renderView() {
// request data from server
fetch('https://render.shinnex.de/render/' + this.currentVariant, { method: 'POST', body: JSON.stringify(globalInputFormsObjValues[this.currentVariant]) })
.then((response) => {
// stl file
return response.blob();
}).then((blob) => {
// log size in kB
console.log(blob.size / 1024 + 'kB');
});
}
reloadCurrentVariant(id, openAnimation = false) {
this.currentVariant = id;
let container = this.container;
// edit inner html
container.innerHTML = "";
for(let key in globalInputFormsObj) {
if(key === this.currentVariant) {
container.innerHTML = globalInputFormsObj[key].renderHTML();
}
}
if(!container) {
console.error('container not found');
return;
}
console.log('reloadCurrentVariant', id, openAnimation);
if(openAnimation) {
// calculate of this height
let height = container.offsetHeight;
container.style.height = '0px';
function animate() {
anime({
targets: container,
height: height,
duration: 500,
easing: 'cubicBezier(0.590, 0.190, 0.050, 0.990)',
complete: function() {
container.style.height = 'auto';
container.style.overflow = 'visible';
}
});
}
//animejs fade in
//dom ready
if (document.getElementById('animejs-script').readyState === 'complete' || document.getElementById('animejs-script').readyState === 'loaded'){
animate();
} else {
document.getElementById('animejs-script').addEventListener('load', function() {
animate();
});
}
} else {
container.style.opacity = 0;
anime({
targets: container,
opacity: 1,
duration: 500,
easing: 'cubicBezier(0.590, 0.190, 0.050, 0.990)'
});
}
}
}
customElements.define('shx-input-forms', InputFormsElement);
</script>
{% endif %}