/home/bdqbpbxa/demo-subdomains/paytx.goodface.com.ua/js/custom-solutions.js
// Prevent default function
function preventDefault(e) {
e.preventDefault();
}
// Scroll lock
function lockScroll() {
const html = document.documentElement;
const body = document.body;
const cookies = document.querySelector('.cookies')
const scrollTop = window.scrollY;
html.classList.add('-scroll-lock');
body.classList.add('-scroll-lock');
cookies.classList.add('-scroll-lock');
document.body.scrollTo(0, scrollTop);
html.setAttribute('data-scroll', scrollTop);
$('.--modal-scrollable-element').on('touchmove pointermove', preventDefault);
}
function unlockScroll() {
const html = document.documentElement;
const body = document.body;
const cookies = document.querySelector('.cookies')
const scrollPositionBeforeLock = html.getAttribute('data-scroll');
html.classList.remove('-scroll-lock');
body.classList.remove('-scroll-lock');
cookies.classList.remove('-scroll-lock');
window.scrollTo(0, scrollPositionBeforeLock);
document.body.scrollTo(0, 0);
$('.--modal-scrollable-element').off('touchmove pointermove', preventDefault);
}
// Check device type
const isApple = navigator.userAgent.toLowerCase().indexOf('mac') !== -1;
const isAndroid = navigator.userAgent.toLowerCase().indexOf('android') !== -1;
if (isApple) {
document.body.classList.add('-apple');
}
if (isAndroid) {
document.body.classList.add('-android');
}
// Check device size
window.isPc = window.innerWidth > 1024;
window.isTablet = window.innerWidth > 759 && window.innerWidth <= 1024;
window.isMobile = window.innerWidth < 760;
function checkDeviceSize() {
window.isPc = window.innerWidth > 1024;
window.isTablet = window.innerWidth > 759 && window.innerWidth <= 1024;
window.isMobile = window.innerWidth < 760;
}
window.addEventListener('DOMContentLoaded', checkDeviceSize);
window.addEventListener('resize', checkDeviceSize);
// Set CSS variable with window.innerHeight
function setCssWindowInnerHeight() {
document.documentElement.style.setProperty(
"--window-inner-height",
`${window.innerHeight}px`
);
}
window.addEventListener("DOMContentLoaded", setCssWindowInnerHeight);
window.addEventListener("resize", setCssWindowInnerHeight);
// Set CSS variable with scrollbar width
function setScrollbarWidthInCSS() {
$('body').append(`
<div id="scrollbar-width-test" style="position: absolute;top: -999px;left: -999px;width: 50px;height: 50px;overflow: scroll;">
<div style="height: 100px;"></div>
</div>
`);
const scrollbarWidthTestEl = $('#scrollbar-width-test')[0];
const scrollbarWidth = scrollbarWidthTestEl.offsetWidth - scrollbarWidthTestEl.clientWidth;
document.documentElement.style.setProperty(
"--scrollbar-width",
`${scrollbarWidth}px`
);
window.scrollbarWidth = scrollbarWidth;
scrollbarWidthTestEl.remove();
}
window.addEventListener("DOMContentLoaded", setScrollbarWidthInCSS);
window.addEventListener("resize", setScrollbarWidthInCSS);
// Accordions
$(document).on('click', '.--accordion__open', function () {
const accordion = $(this).closest('.--accordion');
const isAccordionLock = accordion.find('[data-accordion-in-process]').length || accordion.parent().closest('[data-accordion-in-process]').length;
if (isAccordionLock) return;
accordion.attr('data-accordion-in-process', true);
const isOpen = accordion.hasClass('-open');
if (isOpen) {
closeCustomAccordion(accordion);
} else {
openCustomAccordion(accordion);
}
});
function openCustomAccordion(accordion) {
const container = accordion.find('.--accordion__content-container').eq(0);
const contentHeight = accordion.find('.--accordion__content').eq(0).outerHeight();
accordion.addClass('-open -active');
container.css('height', `${contentHeight}px`);
closeSameAccordionGroup(accordion);
}
function closeCustomAccordion(accordion) {
const container = accordion.find('.--accordion__content-container').eq(0);
const containerCurrentHeight = container.outerHeight();
container.css({
height: `${containerCurrentHeight}px`,
transition: 'unset'
});
container.outerHeight(); // Lifehack
container.css({
height: '0px',
transition: ''
});
accordion.removeClass('-open -active');
closeAccordionChildren(accordion);
}
function closeAccordionChildren(accordion) {
if (accordion.is('[data-close-children]')) {
accordion.find('.--accordion').each(function () {
closeCustomAccordion($(this));
});
}
}
function closeSameAccordionGroup(accordion) {
const groupName = accordion.attr('data-accordion-group');
if (groupName) {
$(`.--accordion[data-accordion-group="${groupName}"]`).each(function () {
if (accordion[0] !== this) {
closeCustomAccordion($(this));
}
});
}
}
fullTransitionendCallback('.--accordion__content-container', function (e) {
const accordion = $(e.target).closest('.--accordion');
const isOpen = accordion.hasClass('-open');
accordion.removeAttr('data-accordion-in-process');
if (isOpen) {
$(e.target).css('height', 'auto');
}
}, 'height');
// Selector full transitionend callback
function fullTransitionendCallback(element, callback, customProperty = false) {
if ($(element).length === 0) return;
const transitionProperties = $(element).css('transition-property').split(',').map(property => {
return property.trim();
});
const transitionDurations = $(element).css('transition-duration').split(',').map(duration => {
return parseFloat(duration);
});
let longestProperty = false;
if (transitionProperties.length > 1 && customProperty === false) {
longestProperty = transitionProperties[transitionDurations.indexOf(Math.max(...transitionDurations))];
}
$(element).on('transitionstart', function () {
$(this).removeAttr('data-transitionend-triggered');
});
$(element).on('transitionend', function (e) {
const isTriggered = $(this).is('[data-transitionend-triggered]');
if (isTriggered) return;
const isCustomProperty = customProperty && e.originalEvent.propertyName === customProperty;
const isSingleCallback = customProperty === false && longestProperty === false && typeof callback === "function";
const isLongestPropertyCallback = longestProperty && e.originalEvent.propertyName === longestProperty && typeof callback === "function";
if (isCustomProperty || isSingleCallback || isLongestPropertyCallback) {
$(this).attr('data-transitionend-triggered', true);
callback(e);
}
});
}
// Dropdowns functionality
$(document).on('click', '.--dropdown__value', function () {
$(this).closest('.--dropdown').toggleClass('-active');
});
$(document).on('click', '.--dropdown__list-item', function () {
const dropdown = $(this).closest('.--dropdown');
const valueEl = dropdown.find('.--dropdown__value-text');
const valueContainer = dropdown.find('.--dropdown__value');
const text = $(this).text().trim();
const dataValue = $(this).data('value');
const value = dataValue ? dataValue : text;
if (valueEl.length) {
valueEl.html(value);
} else {
valueContainer.html(value);
}
dropdown.find('input').attr('value', value);
dropdown.find('.--dropdown__list-item').removeClass('-active');
dropdown.addClass('-selected').removeClass('-active');
$(this).addClass('-active');
});
$(document).on('click', '.--dropdown__clear', function () {
const dropdown = $(this).closest('.--dropdown');
const placeholder = dropdown.data('placeholder');
const valueEl = dropdown.find('.--dropdown__value-text');
const valueContainer = dropdown.find('.--dropdown__value');
if (valueEl.length) {
valueEl.html(placeholder ? placeholder : '');
} else {
valueContainer.html(placeholder ? placeholder : '');
}
dropdown.find('input').removeAttr('value');
dropdown.find('.--dropdown__list-item').removeClass('-active');
dropdown.removeClass('-selected -active');
});
customClickOutsideCallback('.--dropdown.-active', function (e, selector) {
$(selector).removeClass('-active');
});
// Click outside callback
function customClickOutsideCallback(selector, callback) {
$(document).on("mouseup", function (e) {
const isSelector = $(e.target).is(selector);
const hasParent = $(e.target).closest(selector).length;
if (!isSelector && !hasParent && typeof callback === "function") {
callback(e, selector);
}
});
}
// Checkboxes
$(document).on('click', '.--checkbox', function () {
const input = $(this).find('input[type="checkbox"]');
const checked = input.is(':checked');
$(this).toggleClass('-checked');
input[0].checked = !checked;
});
function customCheckboxReady() {
$('.--checkbox').each(function () {
const checked = $(this).find('input[type="checkbox"]').is(':checked');
const hasCheckedClass = $(this).hasClass('-checked');
if (checked && !hasCheckedClass) {
$(this).addClass('-checked');
} else if (!checked && hasCheckedClass) {
$(this).removeClass('-checked');
}
});
}
$(window).on('load', customCheckboxReady);
// Form validation
$(document).on('submit', '.form form', function (e) {
e.preventDefault();
// formValidation(e.target);
// Version with callback
const isFormValid = formValidation(e.target);
if (isFormValid) {
$(e.target).closest('.form-wrapper').addClass('-valid')
updateFormHeight($(e.target));
}
});
function formValidation(form) {
$(form)
.find('[data-important]')
.removeClass('-valid -invalid')
.each(function () {
const input = $(this).find('input, textarea');
const validationType = $(this).attr('data-important');
if (validationType === 'email') emailValidator(input);
if (validationType === 'count') countValidator(input);
if (validationType === 'length') lengthValidator(input);
if (validationType === 'checked') isCheckedValidator(input);
if (validationType === 'not-empty') notEmptyValidator(input);
if (validationType === 'inputmask') inputmaskValidator(input);
});
return $(form).find('[data-important].-invalid').length === 0;
}
$(document).on('focus', '.form [data-important] input, .form [data-important] textarea', function () {
$(this).closest('[data-important]').removeClass('-valid -invalid');
});
// Form validation - helper
function setInputValidation(input, status) {
if (status) {
input.closest('[data-important]').addClass('-valid');
} else {
input.closest('[data-important]').addClass('-invalid');
}
}
// Form validation - validators
function notEmptyValidator(input) {
setInputValidation(input, input.val().trim().length > 0);
}
function inputmaskValidator(input) {
setInputValidation(input, input.inputmask('isComplete'));
}
function isCheckedValidator(input) {
setInputValidation(input, input.is(':checked'));
}
function emailValidator(input) {
const value = input.val().trim();
const reg = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{1,}))$/;
setInputValidation(input, reg.test(String(value).toLowerCase()));
}
function countValidator(input) {
let isValid = true;
const value = input.val().trim();
const min = input.data('min');
const max = input.data('max');
if (value.length === 0) isValid = false;
if (min && value < min) isValid = false;
if (max && value > max) isValid = false;
setInputValidation(input, isValid);
}
function lengthValidator(input) {
const value = input.val().trim();
const minLength = input.closest('[data-important]').attr('data-min-length');
const maxLength = input.closest('[data-important]').attr('data-max-length');
let isValid = false;
if (minLength && maxLength) {
isValid = value.length >= minLength && value.length <= maxLength;
}
if (minLength && !maxLength) {
isValid = value.length >= minLength;
}
if (!minLength && maxLength) {
isValid = value.length <= maxLength;
}
setInputValidation(input, isValid);
}
function updateFormHeight(el) {
if (isPc) return;
let container = el.closest('.form-wrapper');
let thanksWindow = container.find('.thanks-window');
container.css('height', container.innerHeight());
container.outerWidth();
container.css('height', thanksWindow.innerHeight());
}
// Show file name
$(document).on("change", 'input[type="file"]', function () {
let container = $(this).closest(".input.-file");
let file = $(this).val();
if (file) {
let fileName = file.split("/").pop().split("\\").pop();
container.find(".file-name .text").text(fileName);
container.addClass("-file-uploaded");
} else {
container.removeClass("-file-uploaded");
setTimeout(function () {
container.find('.file-name .text').text('');
}, 400);
}
});
$(document).on('click', '.file-name', function (e) {
$(this).closest(".input").removeClass('-invalid, -valid');
let container = $(this).closest(".input.-file");
container.find('input').val('').change();
})
// $(window).on('load', function() {
// let pageName = $('html').find('.breadcrumbs p').text();
// if (!pageName) {
// pageName = 'Homepage';
// }
// if ($('#page-name').length) {
// $('#page-name').val(pageName).change();
// }
// })
// Lazyload - params
// if you set DEFAULT_LAZYLOAD_OFFSET to 0 or false
// then all elements on the page will be loaded the first time the scroll event fires
const DEFAULT_LAZYLOAD_OFFSET = 700;
// Lazyload - loaders
function imgLazyload(customSelector, customLazyloadOffset) {
const isSelectorValid = typeof customSelector === 'string';
const selector = isSelectorValid ? `${customSelector} img.-lazyload` : 'img.-lazyload';
const notSelector = isSelectorValid ? '.-loaded' : '.-loaded, [data-custom-lazyload-trigger] .-lazyload';
$(selector).not(notSelector).each(function () {
if (!customSelector && !isElementAllowToLoad(this, customLazyloadOffset)) return;
$(this).on('load', lazyloadFullLoadedCallback);
$(this).removeAttr('srcset').addClass('-loaded');
});
}
function bgLazyload(customSelector, customLazyloadOffset) {
const isSelectorValid = typeof customSelector === 'string';
const selector = isSelectorValid ? `${customSelector} .-bg-lazyload` : '.-bg-lazyload';
const notSelector = isSelectorValid ? '.-loaded' : '.-loaded, [data-custom-lazyload-trigger] .-bg-lazyload';
$(selector).not(notSelector).each(function () {
if (!customSelector && !isElementAllowToLoad(this, customLazyloadOffset)) return;
const src = $(this).attr('data-src');
$(this).css('background-image', `url(${src})`).removeAttr('data-src').addClass('-loaded');
});
}
function videoLazyload(customSelector, customLazyloadOffset) {
const isSelectorValid = typeof customSelector === 'string';
const selector = isSelectorValid ? `${customSelector} video.-lazyload` : 'video.-lazyload';
const notSelector = isSelectorValid ? '.-loaded' : '.-loaded, [data-custom-lazyload-trigger] .-lazyload';
$(selector).not(notSelector).each(function () {
if (!customSelector && !isElementAllowToLoad(this, customLazyloadOffset)) return;
$(this).on('loadeddata', lazyloadFullLoadedCallback);
const src = $(this).attr('data-src');
const dataStart = $(this).attr('data-start-position');
const t = dataStart ? `#t=${dataStart}` : '';
const source = `<source type="video/mp4" src="${src}${t}" />`;
$(this).html(source).removeAttr('data-src').addClass('-loaded');
});
}
function iframeLazyload(customSelector, customLazyloadOffset) {
const isSelectorValid = typeof customSelector === 'string';
const selector = isSelectorValid ? `${customSelector} iframe.-lazyload` : 'iframe.-lazyload';
const notSelector = isSelectorValid ? '.-loaded' : '.-loaded, [data-custom-lazyload-trigger] iframe.-lazyload';
$(selector).not(notSelector).each(function () {
if (!customSelector && !isElementAllowToLoad(this, customLazyloadOffset)) return;
const src = $(this).attr('data-src');
$(this).on('load', lazyloadFullLoadedCallback);
$(this).attr('src', src).removeAttr('data-src').addClass('-loaded');
});
}
// Lazyload - helpers
function isElementAllowToLoad(el, customLazyloadOffset) {
const lazyloadOffset = customLazyloadOffset ? customLazyloadOffset : DEFAULT_LAZYLOAD_OFFSET;
if (!lazyloadOffset) return true;
const offsetTop = $(el).offset().top;
const offsetToLoad = window.scrollY + window.innerHeight + lazyloadOffset;
return offsetTop < offsetToLoad;
}
function lazyloadFullLoadedCallback(e) {
$(e.target).addClass('-full-loaded');
$(e.target).off('load', lazyloadFullLoadedCallback);
}
function lazyload(customSelector, customLazyloadOffset) {
imgLazyload(customSelector, customLazyloadOffset);
bgLazyload(customSelector, customLazyloadOffset);
videoLazyload(customSelector, customLazyloadOffset);
iframeLazyload(customSelector, customLazyloadOffset);
const notLoadedElementsCount = $('.-lazyload, .-bg-lazyload').not('.-loaded').length;
if (notLoadedElementsCount === 0) {
$(window).off('scroll', lazyloadOnScroll);
window.dispatchEvent(new Event('lazyload-final'));
}
}
function viewportLazyload() {
lazyload(false, 1);
}
function lazyloadOnScroll() {
if (window.scrollY > 0) {
lazyload();
}
}
$(window).on('load', viewportLazyload);
$(window).on('load scroll', lazyloadOnScroll);
// Check in viewport
function checkElementsInViewport(entries) {
entries.forEach(entry => {
const element = entry.target;
if (entry.isIntersecting) {
element.classList.add('-in-viewport');
element.classList.remove('-not-in-viewport');
} else {
element.classList.remove('-in-viewport');
element.classList.add('-not-in-viewport');
}
});
}
const observer = new IntersectionObserver(checkElementsInViewport, {
root: null,
rootMargin: '0px',
threshold: 0
});
document.querySelectorAll('[data-check-in-viewport]').forEach(element => {
observer.observe(element);
});
//Cookies Assept
document.addEventListener("DOMContentLoaded", function () {
const acceptButton = document.querySelector(".accept-cookies");
const cookiesAccepted = localStorage.getItem("cookiesAccepted");
const cookiesSection = document.querySelector(".cookies");
acceptButton.addEventListener("click", function () {
localStorage.setItem("cookiesAccepted", "true");
cookiesSection.classList.add("-accepted");
cookiesSection.classList.remove("-open");
});
if (cookiesAccepted === "true") {
cookiesSection.classList.add("-accepted");
} else {
setTimeout(function () {
cookiesSection.classList.add("-open");
}, 2000);
}
});