ZwiiCMS/core/core.js.php

554 lines
18 KiB
PHP
Raw Permalink Normal View History

2018-04-02 08:29:19 +02:00
/**
* This file is part of Zwii.
* For full copyright and license information, please see the LICENSE
* file that was distributed with this source code.
*
* @author Rémi Jean <remi.jean@outlook.com>
* @copyright Copyright (C) 2008-2018, Rémi Jean
2021-02-17 13:49:58 +01:00
* @author Frédéric Tempez <frederic.tempez@outlook.com>
2024-01-14 19:31:28 +01:00
* @copyright Copyright (C) 2018-2024, Frédéric Tempez
* @license CC Attribution-NonCommercial-NoDerivatives 4.0 International
* @link http://zwiicms.fr/
2018-04-02 08:29:19 +02:00
*/
2022-12-26 11:03:36 +01:00
var core = {};
/**
* Crée un message d'alerte
*/
2023-02-18 22:30:00 +01:00
core.alert = function (text) {
var lightbox = lity(function ($) {
2022-12-31 15:42:58 +01:00
return $("<div>")
.addClass("lightbox")
.append(
$("<span>").text(text),
$("<div>")
.addClass("lightboxButtons")
.append(
$("<a>")
.addClass("button")
.text("Ok")
2023-02-18 22:30:00 +01:00
.on("click", function () {
2022-12-31 15:42:58 +01:00
lightbox.close();
})
)
)
}(jQuery));
// Validation de la lightbox avec le bouton entrée
2023-02-18 22:30:00 +01:00
$(document).on("keyup", function (event) {
2022-12-31 15:42:58 +01:00
if (event.keyCode === 13) {
lightbox.close();
}
});
return false;
2022-12-26 11:03:36 +01:00
};
/**
* Génère des variations d'une couleur
*/
2023-02-18 22:30:00 +01:00
core.colorVariants = function (rgba) {
2022-12-31 15:42:58 +01:00
rgba = rgba.match(/\(+(.*)\)/);
rgba = rgba[1].split(", ");
return {
"normal": "rgba(" + rgba[0] + "," + rgba[1] + "," + rgba[2] + "," + rgba[3] + ")",
"darken": "rgba(" + Math.max(0, rgba[0] - 15) + "," + Math.max(0, rgba[1] - 15) + "," + Math.max(0, rgba[2] - 15) + "," + rgba[3] + ")",
"veryDarken": "rgba(" + Math.max(0, rgba[0] - 20) + "," + Math.max(0, rgba[1] - 20) + "," + Math.max(0, rgba[2] - 20) + "," + rgba[3] + ")",
"text": core.relativeLuminanceW3C(rgba) > .22 ? "#222" : "#DDD"
};
2022-12-26 11:03:36 +01:00
};
/**
* Crée un message de confirmation
*/
2023-02-18 22:30:00 +01:00
core.confirm = function (text, yesCallback, noCallback) {
var lightbox = lity(function ($) {
2022-12-31 15:42:58 +01:00
return $("<div>")
.addClass("lightbox")
.append(
$("<span>").text(text),
$("<div>")
.addClass("lightboxButtons")
.append(
$("<a>")
.addClass("button grey")
.text("<?php echo helper::translate('Non');?>")
2023-02-18 22:30:00 +01:00
.on("click", function () {
2022-12-31 15:42:58 +01:00
lightbox.options('button', true);
lightbox.close();
if (typeof noCallback !== "undefined") {
noCallback();
}
}),
$("<a>")
.addClass("button")
.text("<?php echo helper::translate('Oui');?>")
2023-02-18 22:30:00 +01:00
.on("click", function () {
2022-12-31 15:42:58 +01:00
lightbox.options('button', true);
lightbox.close();
if (typeof yesCallback !== "undefined") {
yesCallback();
}
})
)
)
}(jQuery));
// Callback lors d'un clic sur le fond et sur la croix de fermeture
lightbox.options('button', false);
2023-02-18 22:30:00 +01:00
$(document).on('lity:close', function (event, instance) {
2022-12-31 15:42:58 +01:00
if (
instance.options('button') === false &&
typeof noCallback !== "undefined"
) {
noCallback();
}
});
// Validation de la lightbox avec le bouton entrée
2023-02-18 22:30:00 +01:00
$(document).on("keyup", function (event) {
2022-12-31 15:42:58 +01:00
if (event.keyCode === 13) {
lightbox.close();
if (typeof yesCallback !== "undefined") {
yesCallback();
}
}
});
return false;
2022-12-26 11:03:36 +01:00
};
/**
* Scripts à exécuter en dernier
*/
2023-02-18 22:30:00 +01:00
core.end = function () {
2022-12-31 15:42:58 +01:00
/**
* Modifications non enregistrées du formulaire
*/
var formDOM = $("form");
// Ignore :
// - TinyMCE car il gère lui même le message
// - Les champs avec data-no-dirty
var inputsDOM = formDOM.find("input:not([data-no-dirty]), select:not([data-no-dirty]), textarea:not(.editorWysiwyg):not([data-no-dirty])");
var inputSerialize = inputsDOM.serialize();
2023-02-18 22:30:00 +01:00
$(window).on("beforeunload", function () {
2022-12-31 15:42:58 +01:00
if (inputsDOM.serialize() !== inputSerialize) {
message = "<?php echo helper::translate('Les modifications que vous avez apportées ne seront peut-être pas enregistrées.');?>";
return message;
}
});
2023-02-18 22:30:00 +01:00
formDOM.submit(function () {
2022-12-31 15:42:58 +01:00
$(window).off("beforeunload");
});
2022-12-26 11:03:36 +01:00
};
2023-02-18 22:30:00 +01:00
$(function () {
2022-12-31 15:42:58 +01:00
core.end();
2022-12-26 11:03:36 +01:00
});
/**
* Ajoute une notice
*/
2023-02-18 22:30:00 +01:00
core.noticeAdd = function (id, notice) {
2022-12-31 15:42:58 +01:00
$("#" + id + "Notice").text(notice).removeClass("displayNone");
$("#" + id).addClass("notice");
2022-12-26 11:03:36 +01:00
};
/**
* Supprime une notice
*/
2023-02-18 22:30:00 +01:00
core.noticeRemove = function (id) {
2022-12-31 15:42:58 +01:00
$("#" + id + "Notice").text("").addClass("displayNone");
$("#" + id).removeClass("notice");
2022-12-26 11:03:36 +01:00
};
/**
* Scripts à exécuter en premier
*/
2023-02-18 22:30:00 +01:00
core.start = function () {
2022-12-31 15:42:58 +01:00
/**
* Remonter en haut au clic sur le bouton
*/
var backToTopDOM = $("#backToTop");
2023-02-18 22:30:00 +01:00
backToTopDOM.on("click", function () {
$("body, html").animate({
scrollTop: 0
}, "400");
2022-12-31 15:42:58 +01:00
});
/**
* Affiche / Cache le bouton pour remonter en haut
*/
2023-02-18 22:30:00 +01:00
$(window).on("scroll", function () {
2022-12-31 15:42:58 +01:00
if ($(this).scrollTop() > 200) {
backToTopDOM.fadeIn();
} else {
backToTopDOM.fadeOut();
}
});
/**
* Cache les notifications
*/
var notificationTimer;
$("#notification")
2023-02-18 22:30:00 +01:00
.on("mouseenter", function () {
2022-12-31 15:42:58 +01:00
clearTimeout(notificationTimer);
$("#notificationProgress")
.stop()
.width("100%");
})
2023-02-18 22:30:00 +01:00
.on("mouseleave", function () {
2022-12-31 15:42:58 +01:00
// Disparition de la notification
2023-02-18 22:30:00 +01:00
notificationTimer = setTimeout(function () {
2022-12-31 15:42:58 +01:00
$("#notification").fadeOut();
}, 3000);
// Barre de progression
$("#notificationProgress").animate({
"width": "0%"
}, 3000, "linear");
})
.trigger("mouseleave");
2023-02-18 22:30:00 +01:00
$("#notificationClose").on("click", function () {
2022-12-31 15:42:58 +01:00
clearTimeout(notificationTimer);
$("#notification").fadeOut();
$("#notificationProgress").stop();
});
/**
* Traitement du formulaire cookies
*/
2023-02-18 22:30:00 +01:00
$("#cookieForm").submit(function (event) {
2022-12-31 15:42:58 +01:00
// Variables des cookies
var getUrl = window.location;
var domain = "domain=" + getUrl.hostname + ";";
2024-09-15 19:00:53 +02:00
var basePath = getUrl.pathname.substring(0, getUrl.pathname.lastIndexOf('/') + 1);
var path = "path=" + basePath + ";";
2022-12-31 15:42:58 +01:00
var e = new Date();
e.setFullYear(e.getFullYear() + 1);
2024-09-15 19:00:53 +02:00
var expires = "expires=" + e.toUTCString() + ";";
2022-12-31 15:42:58 +01:00
// Stocke le cookie d'acceptation
2024-09-15 19:00:53 +02:00
document.cookie = "ZWII_COOKIE_CONSENT=true; samesite=lax; " + domain + path + expires;
2022-12-31 15:42:58 +01:00
});
/**
* Fermeture de la popup des cookies
*/
2023-02-18 22:30:00 +01:00
$("#cookieConsent .cookieClose").on("click", function () {
2022-12-31 15:42:58 +01:00
$('#cookieConsent').addClass("displayNone");
});
/**
* Commande de gestion des cookies dans le footer
*/
2023-02-18 22:30:00 +01:00
$("#footerLinkCookie").on("click", function () {
2022-12-31 15:42:58 +01:00
$("#cookieConsent").removeClass("displayNone");
});
/**
* Affiche / Cache le menu en mode responsive
*/
var menuDOM = $("#menu");
2023-02-18 22:30:00 +01:00
$("#toggle").on("click", function () {
2022-12-31 15:42:58 +01:00
menuDOM.slideToggle();
});
2023-02-18 22:30:00 +01:00
$(window).on("resize", function () {
2022-12-31 15:42:58 +01:00
if ($(window).width() > 768) {
menuDOM.css("display", "");
}
});
2023-02-18 22:30:00 +01:00
2022-12-31 15:42:58 +01:00
/**
* Choix de page dans la barre de membre
*/
2023-02-18 22:30:00 +01:00
$("#barSelectPage").on("change", function () {
2022-12-31 15:42:58 +01:00
var pageUrl = $(this).val();
if (pageUrl) {
$(location).attr("href", pageUrl);
}
});
/**
* Champs d'upload de fichiers
*/
// Mise à jour de l'affichage des champs d'upload
2023-02-18 22:30:00 +01:00
$(".inputFileHidden").on("change", function () {
2022-12-31 15:42:58 +01:00
var inputFileHiddenDOM = $(this);
var fileName = inputFileHiddenDOM.val();
if (fileName === "") {
2023-01-02 18:03:48 +01:00
//fileName = "Sélectionner un fichier";
fileName = "<?php echo helper::translate('Sélectionner un fichier');?>";
2022-12-31 15:42:58 +01:00
$(inputFileHiddenDOM).addClass("disabled");
} else {
$(inputFileHiddenDOM).removeClass("disabled");
}
inputFileHiddenDOM.parent().find(".inputFileLabel").text(fileName);
}).trigger("change");
// Suppression du fichier contenu dans le champ
2023-02-18 22:30:00 +01:00
$(".inputFileDelete").on("click", function () {
2022-12-31 15:42:58 +01:00
$(this).parents(".inputWrapper").find(".inputFileHidden").val("").trigger("change");
});
// Suppression de la date contenu dans le champ
2023-02-18 22:30:00 +01:00
$(".inputDateDelete").on("click", function () {
2022-12-31 15:42:58 +01:00
$(this).parents(".inputWrapper").find(".datepicker").val("").trigger("change");
});
// Confirmation de mise à jour
2023-02-18 22:30:00 +01:00
$("#barUpdate").on("click", function () {
message = "<?php echo helper::translate('Mise à jour') . ' ?';?>";
2023-02-18 22:30:00 +01:00
return core.confirm(message, function () {
2022-12-31 15:42:58 +01:00
$(location).attr("href", $("#barUpdate").attr("href"));
});
});
// Confirmation de déconnexion
2023-02-18 22:30:00 +01:00
$("#barLogout").on("click", function () {
2023-12-19 17:30:28 +01:00
message = "<?php echo helper::translate('Se déconnecter') . ' ?';?>";
2023-02-18 22:30:00 +01:00
return core.confirm(message, function () {
2022-12-31 15:42:58 +01:00
$(location).attr("href", $("#barLogout").attr("href"));
});
});
/**
* Bloque la multi-soumission des boutons
*/
2023-02-18 22:30:00 +01:00
$("form").on("submit", function () {
2022-12-31 15:42:58 +01:00
$(this).find(".uniqueSubmission")
.addClass("disabled")
.prop("disabled", true)
.empty()
.append(
$("<span>").addClass("zwiico-spin animate-spin")
)
});
/**
* Check adresse email
*/
2023-02-18 22:30:00 +01:00
$("[type=email]").on("change", function () {
2022-12-31 15:42:58 +01:00
var _this = $(this);
var pattern = /^([a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+(\.[a-z\d!#$%&'*+\-\/=?^_`{|}~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]+)*|"((([ \t]*\r\n)?[ \t]+)?([\x01-\x08\x0b\x0c\x0e-\x1f\x7f\x21\x23-\x5b\x5d-\x7e\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|\\[\x01-\x09\x0b\x0c\x0d-\x7f\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))*(([ \t]*\r\n)?[ \t]+)?")@(([a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\d\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.)+([a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]|[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF][a-z\d\-._~\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]*[a-z\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])\.?$/i;
if (pattern.test(_this.val())) {
core.noticeRemove(_this.attr("id"));
} else {
message = "<?php echo helper::translate('Format incorrect');?>";
core.noticeAdd(_this.attr("id"), message);
}
});
/**
* Iframes et vidéos responsives
*/
var elementDOM = $("iframe, video, embed, object");
// Calcul du ratio et suppression de la hauteur / largeur des iframes
2023-02-18 22:30:00 +01:00
elementDOM.each(function () {
2022-12-31 15:42:58 +01:00
var _this = $(this);
_this
.data("ratio", _this.height() / _this.width())
.data("maxwidth", _this.width())
.removeAttr("width height");
});
// Prend la largeur du parent et détermine la hauteur à l'aide du ratio lors du resize de la fenêtre
2023-02-18 22:30:00 +01:00
$(window).on("resize", function () {
elementDOM.each(function () {
2022-12-31 15:42:58 +01:00
var _this = $(this);
var width = _this.parent().first().width();
2023-02-18 22:30:00 +01:00
if (width > _this.data("maxwidth")) {
width = _this.data("maxwidth");
}
2022-12-31 15:42:58 +01:00
_this
.width(width)
.height(width * _this.data("ratio"));
});
}).trigger("resize");
/*
* Header responsive
*/
2023-02-18 22:30:00 +01:00
$(window).on("resize", function () {
2022-12-31 15:42:58 +01:00
var responsive = "<?php echo $this->getdata(['theme','header','imageContainer']);?>";
if (responsive === "cover" || responsive === "contain") {
var widthpx = "<?php echo $this->getdata(['theme','site','width']);?>";
var width = widthpx.substr(0, widthpx.length - 2);
var heightpx = "<?php echo $this->getdata(['theme','header','height']);?>";
var height = heightpx.substr(0, heightpx.length - 2);
var ratio = width / height;
if (($(window).width() / ratio) <= height) {
$("header").height($(window).width() / ratio);
}
}
}).trigger("resize");
2022-12-26 11:03:36 +01:00
};
core.start();
/**
* Confirmation de suppression
2022-12-31 15:42:58 +01:00
*/
2023-02-18 22:30:00 +01:00
$("#pageDelete").on("click", function () {
2022-12-31 15:42:58 +01:00
var _this = $(this);
message = "<?php echo helper::translate('Confirmez-vous la suppression de cette page ?');?>";
2023-02-18 22:30:00 +01:00
return core.confirm(message, function () {
2022-12-31 15:42:58 +01:00
$(location).attr("href", _this.attr("href"));
});
2022-12-26 11:03:36 +01:00
});
/**
* Calcul de la luminance relative d'une couleur
*/
2023-02-18 22:30:00 +01:00
core.relativeLuminanceW3C = function (rgba) {
2022-12-31 15:42:58 +01:00
// Conversion en sRGB
var RsRGB = rgba[0] / 255;
var GsRGB = rgba[1] / 255;
var BsRGB = rgba[2] / 255;
// Ajout de la transparence
var RsRGBA = rgba[3] * RsRGB + (1 - rgba[3]);
var GsRGBA = rgba[3] * GsRGB + (1 - rgba[3]);
var BsRGBA = rgba[3] * BsRGB + (1 - rgba[3]);
// Calcul de la luminance
var R = (RsRGBA <= .03928) ? RsRGBA / 12.92 : Math.pow((RsRGBA + .055) / 1.055, 2.4);
var G = (GsRGBA <= .03928) ? GsRGBA / 12.92 : Math.pow((GsRGBA + .055) / 1.055, 2.4);
var B = (BsRGBA <= .03928) ? BsRGBA / 12.92 : Math.pow((BsRGBA + .055) / 1.055, 2.4);
return .2126 * R + .7152 * G + .0722 * B;
2022-12-26 11:03:36 +01:00
};
2022-12-31 15:42:58 +01:00
// Fonctions
function setCookie(name, value, days) {
var expires = "";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toUTCString();
}
document.cookie = name + "=" + (value || "") + expires + "; path=/; samesite=lax";
}
function getCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == ' ') c = c.substring(1, c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
}
return null;
}
// Define function to capitalize the first letter of a string
function capitalizeFirstLetter(string) {
return string.charAt(0).toUpperCase() + string.slice(1);
}
2023-02-18 22:30:00 +01:00
$(document).ready(function () {
2022-12-31 15:42:58 +01:00
/**
* Affiche le sous-menu quand il est sticky
*/
2023-02-18 22:30:00 +01:00
$("nav").mouseenter(function () {
$("#navfixedlogout .navSub").css({
'pointer-events': 'auto'
});
$("#navfixedconnected .navSub").css({
'pointer-events': 'auto'
});
2022-12-31 15:42:58 +01:00
});
2023-02-18 22:30:00 +01:00
$("nav").mouseleave(function () {
$("#navfixedlogout .navSub").css({
'pointer-events': 'none'
});
$("#navfixedconnected .navSub").css({
'pointer-events': 'none'
});
2022-12-31 15:42:58 +01:00
});
/**
* Chargement paresseux des images et des iframes
*/
2024-06-07 09:57:26 +02:00
$("img").attr("loading", "lazy");
2022-12-31 15:42:58 +01:00
/**
* Effet accordéon
*/
2023-02-18 22:30:00 +01:00
$('.accordion').each(function (e) {
2022-12-31 15:42:58 +01:00
// on stocke l'accordéon dans une variable locale
var accordion = $(this);
// on récupère la valeur data-speed si elle existe
var toggleSpeed = accordion.attr('data-speed') || 100;
// fonction pour afficher un élément
function open(item, speed) {
// on récupère tous les éléments, on enlève l'élément actif de ce résultat, et on les cache
accordion.find('.accordion-item').not(item).removeClass('active')
.find('.accordion-content').slideUp(speed);
// on affiche l'élément actif
item.addClass('active')
.find('.accordion-content').slideDown(speed);
}
function close(item, speed) {
accordion.find('.accordion-item').removeClass('active')
.find('.accordion-content').slideUp(speed);
}
// on initialise l'accordéon, sans animation
open(accordion.find('.active:first'), 0);
// au clic sur un titre...
2023-02-18 22:30:00 +01:00
accordion.on('click', '.accordion-title', function (ev) {
2022-12-31 15:42:58 +01:00
ev.preventDefault();
// Masquer l'élément déjà actif
if ($(this).closest('.accordion-item').hasClass('active')) {
close($(this).closest('.accordion-item'), toggleSpeed);
} else {
// ...on lance l'affichage de l'élément, avec animation
open($(this).closest('.accordion-item'), toggleSpeed);
}
});
});
/**
* Icône du Menu Burger
*/
2023-02-18 22:30:00 +01:00
$("#toggle").click(function () {
2022-12-31 15:42:58 +01:00
var changeIcon = $('#toggle').children("span");
if ($(changeIcon).hasClass('zwiico-menu')) {
$(changeIcon).removeClass('zwiico-menu').addClass('zwiico-cancel');
} else {
$(changeIcon).addClass('zwiico-menu');
};
});
/**
* Remove ID Facebook from URL
*/
2023-02-20 18:42:16 +01:00
if (/^\?fbclid=/.test(location.search)) {
2022-12-31 15:42:58 +01:00
location.replace(location.href.replace(/\?fbclid.+/, ""));
2023-02-20 18:42:16 +01:00
};
/**
* Sélection d'une langue du site
*/
$("select#barSelectLanguage").on("change", function () {
// La langue courante ne déclenche pas de chargement
var langSelected = $(this).val();
var langSelected = langSelected.split("/");
// Lit le cookie de langue
2024-05-06 16:18:35 +02:00
var langSession = "<?php echo isset($_SESSION['ZWII_SITE_CONTENT']) ? $_SESSION['ZWII_SITE_CONTENT'] : '';?>";
2023-02-20 18:42:16 +01:00
// Découpe l'URL pour exclure le changement de page avec le thème
var url = window.location;
var currentUrl = url.href.split("/");
// Change si différent, corrige le problème avec le thème et le rechargement de la langue.
if ((currentUrl !== "?theme" ||
currentUrl !== "theme") &&
2023-04-12 18:35:46 +02:00
langSelected[6] !== langSession
2023-02-20 18:42:16 +01:00
) {
//$(location).attr("href", langUrl);
var select = document.getElementById("barSelectLanguage");
var selectedOption = select.options[select.selectedIndex];
if (selectedOption.value !== "") {
window.location = selectedOption.value; }
}
});
2022-12-31 15:42:58 +01:00
});