ldc-legaulois/resources/js/front/functions.js
Jérémy Munsch 34da0f98a1 tons of fix
2021-05-05 15:13:33 +02:00

104 lines
3.8 KiB
JavaScript
Vendored

window.__Gaul = {
isDescendant: function (parent, child) {
var node = child.parentNode;
while (node != null) {
if (node == parent) {
return true;
}
node = node.parentNode;
}
return false;
},
debounce: (fn) => {
// This holds the requestAnimationFrame reference, so we can cancel it if we wish
let frame;
// The debounce function returns a new function that can receive a variable number of arguments
return (...params) => {
// If the frame variable has been defined, clear it now, and queue for next frame
if (frame)
cancelAnimationFrame(frame);
// Queue our function call for the next frame
frame = requestAnimationFrame(() => {
// Call our function and pass any params we received
fn(...params);
});
}
},
scrollToContent: (idContent, headerOffset) => {
let element = document.getElementById(idContent);
let elementPosition = element.getBoundingClientRect().top;
let offsetPosition = window.pageYOffset + elementPosition - headerOffset;
scrollTo({
top: offsetPosition,
behavior: "smooth"
});
},
handleMediaShares: () => {
let mediaShares = document.getElementsByClassName('mediaShare');
for (let mediaShare of mediaShares) {
mediaShare.addEventListener('click', (e) => {
e.preventDefault();
return window.open(
mediaShare.href,
"media-share-dialog",
"width=640,height=320"
);
});
}
}
}
document.addEventListener('scroll', __Gaul.debounce(() => {
document.documentElement.dataset.scroll = window.scrollY;
}), { passive: true });
window.__Gaul.ProgressBarInfos = class ProgressBarInfos {
constructor(bar, text, formInputs) {
this.bar = bar;
this.text = text;
this.nbFields = formInputs.length - 1;
this.fieldPercentage = 100 / this.nbFields;
this.fieldsOk = [];
this.percent = 0;
// Gestion des inputs qui ont déjà une valeur
for (let i = 0; i <= this.nbFields; i++) {
if (formInputs[i].value.length > 0) {
if (formInputs[i].name !== "_token") {
if (
formInputs[i].type === "checkbox" &&
formInputs[i].checked
) {
this.percent += this.fieldPercentage;
this.fieldsOk.push(formInputs[i].name);
} else if (formInputs[i].type !== "checkbox") {
this.percent += this.fieldPercentage;
this.fieldsOk.push(formInputs[i].name);
}
}
}
this.percent = this.percent > 100 ? 100 : this.percent;
}
// Mise à jour des valeurs
this.bar.style.width = Math.ceil(this.percent) + "%";
this.text.innerHTML = Math.ceil(this.percent) + "%";
}
changeValue(field, size) {
let contain = this.fieldsOk.includes(field);
if (contain && size === 0) {
this.fieldsOk.splice(this.fieldsOk.indexOf(field), 1);
this.percent -= this.fieldPercentage;
this.percent < 0 ? (this.percent = 0) : null;
} else if (!contain && size > 0) {
this.fieldsOk.push(field);
this.percent += this.fieldPercentage;
this.percent > 100 ? (this.percent = 100) : null;
}
this.bar.style.width = Math.ceil(this.percent) + "%";
this.text.innerHTML = parseInt(this.percent) + "%";
}
};