script45.js 4.8 KB
let scripts = function () {

    $('.js-toggle').on('click', function () {
        $(this).toggleClass('active');
    });
    $('.js-parent-toggle').on('click', function () {
        $(this).parent().toggleClass('active');
    });
    $('.js-parent-remove').on('click', function () {
        $(this).parent().remove();
    });
    $('.js-menu-toggle').on('click', function () {
        window.scrollTo(0, 0);
        $('#body').toggleClass('menu-is-actived');
    });
    $('.js-cookies-close').on('click', function () {
        $('#body').removeClass('cookies-is-actived');
    });
    $('.js-works-edit').on('click', function () {
        $(this).parent().parent().parent().addClass('active');
    });
    $('.js-works-remove').on('click', function () {
        $(this).parent().parent().parent().parent().remove();
    });

    $('[data-tab]').on('click', function () {
        $('[data-tab]').removeClass('active');
        $('[data-body]').removeClass('showed');
        $(this).addClass('active');
        var id = $(this).data('tab');
        $('[data-body=' + id + ']').addClass('showed');
    });

    $('.js-password-show').on('click', function () {
        $(this).parent().addClass('active');
        $(this).parent().parent().find('input').attr('type', 'text');
    });

    $('.js-password-hide').on('click', function () {
        $(this).parent().removeClass('active');
        $(this).parent().parent().find('input').attr('type', 'password');
    });

    let checkScrollTop = function () {
        if ($(document).scrollTop() == 0) {
            $('#body').removeClass('begin');
        } else {
            $('#body').addClass('begin');
        }
    }
    checkScrollTop();
    $(document).on('scroll', function () {
        checkScrollTop();
    });

    let closeAll = function () {
        $('.js-toggle').removeClass('active');
        $('.js-parent-toggle').parent().removeClass('active');
        $('#body').removeClass('menu-is-actived');
        $('#body').removeClass('cookies-is-actived');
    }

    $(document).keyup(function (e) {
        if (e.key === "Escape") {
            closeAll();
        }
    });

    $('.js-scroll-to').bind('click', function (e) {
        let anchor = $(this);
        $('html,body').stop().animate({
            scrollTop: $(anchor.attr('href')).offset().top
        }, 300);
        e.preventDefault();
    });

    if ($('[type=tel]').is('[type=tel]')) {
        $('[type=tel]').mask('+7 (999) 999-99-99');
    }

    if ($('.js-select2').is('.js-select2')) {
        $('.js-select2').select2();
    }

    const starRating = document.querySelectorAll(".js-stars");
    if (starRating.length) {
        starRating.forEach(item => {
            new StarRating(item);
        });
    }
};

document.addEventListener("DOMContentLoaded", () => {
    scripts();
    swipers();
});

$(window).resize(function () {
    swipers();
});

let swipers = function () {

    if ($('.js-employer-swiper').is('.js-employer-swiper')) {
        let slider = new Swiper('.js-employer-swiper', {
            autoplay: {
                delay: 5000,
            },
            pagination: {
                el: '.swiper-pagination',
                clickable: true
            },
            breakpoints: {
                768: {
                    slidesPerView: 2,
                },
                992: {
                    slidesPerView: 3,
                },
                1280: {
                    slidesPerView: 4,
                },
            }
        });
    }

    if ($('.js-news-swiper').is('.js-news-swiper')) {
        let slider = new Swiper('.js-news-swiper', {
            spaceBetween: 20,
            pagination: {
                el: '.swiper-pagination',
                clickable: true
            },
            navigation: {
                prevEl: '.js-news-swiper-button-prev',
                nextEl: '.js-news-swiper-button-next',
            },
            breakpoints: {
                768: {
                    slidesPerView: 2,
                },
                992: {
                    slidesPerView: 3,
                },
            }
        });
    }

    // cookies
    const cookieItems = document.querySelectorAll(".js-ck");
    if (cookieItems.length) {
        cookieItems.forEach(item => {
            const id = item.dataset.id;
            const like = item.querySelector(".like");
            const checkLike = function () {
                if (like.classList.contains("active")) {
                    Cookies.set('favor_vacan-'+id, id);
                } else {
                    Cookies.remove(id);
                }
            }

            like.addEventListener("click", () => {
                checkLike();
            });

            if (Cookies.get('favor_vacan-'+id) == id) {
                like.classList.add("active");
            }
        });
    }
    console.log(Cookies.get());

};