script.js 6.11 KB
$(
    function() {
        phoneMask('input[name="Feedback[phone]"]');


        /**
         * Modal form submit code
         */
        $(document)
            .on(
                'beforeSubmit', '#calculate-form, #feedback-form', function(e) {
                    var f = this;
                    var form = $(this);
                    var formData = form.serialize();
                    $.ajax(
                        {
                            url: form.attr("action"),
                            type: form.attr("method"),
                            data: formData,
                            success: function(data) {
                                f.reset();
                                $('#feedback-modal,#calculate-modal')
                                    .modal('hide');
                                form.data('yiiActiveForm').validated = false;
                                $('#success-modal')
                                    .modal('show');
                            },
                            error: function() {
                                $('#feedback-modal')
                                    .modal('hide');
                            }
                        }
                    );
                }
            )
            .on(
                'submit', '#calculate-form, #feedback-form', function(e) {
                    e.preventDefault();
                }
            );

        /**
         * Contact form submitting
         */
        $(document)
            .on(
                'beforeSubmit', '#contact-form', function(e) {
                    var f = this;
                    var form = $(this);
                    var formData = form.serialize();
                    $.ajax(
                        {
                            url: form.attr("action"),
                            type: form.attr("method"),
                            data: formData,
                            success: function(data) {
                                f.reset();
                                form.replaceWith(data.alert)
                            },
                            error: function() {

                            }
                        }
                    );
                }
            )
            .on(
                'submit', '#contact-form', function(e) {
                    e.preventDefault();
                }
            );

        /**
         * Button UP code
         */
        if ($('#back-to-top').length) {
            var scrollTrigger = 100, // px
                backToTop = function() {
                    var scrollTop = $(window)
                        .scrollTop();
                    if (scrollTop > scrollTrigger) {
                        $('#back-to-top')
                            .addClass('show');
                    } else {
                        $('#back-to-top')
                            .removeClass('show');
                    }
                };
            backToTop();
            $(window)
                .on(
                    'scroll', function() {
                        backToTop();
                    }
                );
            $('#back-to-top')
                .on(
                    'click', function(e) {
                        e.preventDefault();
                        $('html,body')
                            .animate(
                                {
                                    scrollTop: 0
                                }, 700
                            );
                    }
                );
        }
        $(".active a")
            .on(
                'click', function(){
                    $("#back-to-top").click();
                    return false;
                }
            );

        /* нужно для гугл аналитики */

       $(".press-consultation").on('click',function(){
           gtagEventSender.setState('consultation');
       });
       $(".press-specialist").on('click',function(){
           gtagEventSender.setState('specialist');
       });
       $(".press-calculate").on('click',function(){
           gtagEventSender.setState('calculate');
       });
       $(".press-order").on('click',function(){
           gtagEventSender.setState('order');
       });
       $(".press-call").on('click',function(){
           gtagEventSender.setState('call');
       });
       $(".press-callback").on('click',function(){
           gtagEventSender.setState('callback');
       });

       $(document).on('click', '.send-form', function() {
           gtagEventSender.pushEvent();
       });

        function phoneMask(maskCssSelector) {
            var phoneInput = maskCssSelector;

            if($('body').find(phoneInput).length>0){
                $(phoneInput).mask('+38(000)000-00-00',{placeholder:'+38(000)000-00-00'});
                $(phoneInput).focus(function () {
                    if(($(this).val())== '') {$(this).val('+38(0')}
                });
                $(phoneInput).focusout(function () {
                    var phoneVal = $(this).val()
                    //if(phoneVal == '+38(0' || phoneVal == '+38(' || phoneVal == '+38' || phoneVal == '+3' || phoneVal == '+') {$(this).val('')}
                    if(phoneVal.length <17) {$(this).val('')}
                });
            }
        }

        $('body').on('click','.cokeis-close',function () {
            $('.cookie-modal').addClass('hide-bl');
            var date = new Date(new Date().getTime() + 48 * 60 * 60 * 1000);
            document.cookie = "sowCookieModal=1; path=/; expires=" + date.toUTCString();
        });

        if($('.home-text-wr').length) {
            $('.home-text-wr').mCustomScrollbar(
                {
                    // autoHideScrollbar: false,
                    theme: 'minimal'
                }
            );
        }

        var gtagEventSender = {
          state : ''
        };

        gtagEventSender.setState = function(newState) {
            this.state = newState;
        };

        gtagEventSender.pushEvent = function() {
            gtag('event', 'send', {
                'event_category': this.state
            });
            console.log(this.state);
        }
    });