var indice_graph = 0; $(document).ready(function() { /* ========================================================================== Run functions ========================================================================== */ var baserootsite = $("body").data("baserootsite"); var baseroot = $("body").data("baseroot"); var domain = $("body").data("domain"); /* ========================================================================== Mobile navigation ========================================================================== */ // mobile nav $('.navbar_burger').click(function(e) { e.preventDefault(); $body=$('body'); if ($body.hasClass('nav_is_open')) { $body.addClass('nav_is_close').removeClass('nav_is_open'); } else { $body.addClass('nav_is_open').removeClass('nav_is_close'); } }); // burger icon $('.burger_icon').click(function(e) { e.preventDefault(); if($(this).hasClass('is-opened')) { $(this).addClass('is-closed').removeClass('is-opened'); } else { $(this).removeClass('is-closed').addClass('is-opened'); } }); /* ========================================================================== Optin ========================================================================== */ $(".optin").click(function(){ $(this).toggleClass("active"); var value; if($(this).hasClass("active")){ value = 1; } else { value = 0; } $(this).parent().find("input").val(value); }); /* ========================================================================== resize navbar on scroll ========================================================================== */ $(window).scroll(function(){ if($(this).scrollTop() > 66){ $("body").addClass("navbar_scroll"); } else{ $("body").removeClass("navbar_scroll"); } }); /* ========================================================================== Switch lang hover ========================================================================== */ $('.subnav_item_03').mouseenter(function(){ $(this).parent().css("margin-right", "0"); }); $('.subnav_item_03').mouseleave(function(){ $(this).parent().css("margin-right", "-90px"); }); /* ========================================================================== Scroll to ========================================================================== */ // Select all links with hashes // Remove links that don't actually link to anything $('a[href*="#"]').not('[href="#"]').not('[href="#0"]').click(function(event) { // On-page links if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) { // Figure out element to scroll to var target = $(this.hash); target = target.length ? target : $('[name=' + this.hash.slice(1) + ']'); // Does a scroll target exist? if (target.length) { // Only prevent default if animation is actually gonna happen event.preventDefault(); $('html, body').animate({ scrollTop: target.offset().top }, 450, function() { // Callback after animation // Must change focus! var $target = $(target); $target.focus(); if ($target.is(":focus")) { // Checking if the target was focused return false; } else { $target.attr('tabindex','-1'); // Adding tabindex for elements not focusable $target.focus(); // Set focus again }; }); } } }); $('.clase_error').on('click', function(e){ e.preventDefault(); $(".form_error").hide(); }); /* ========================================================================== Check form | Main ========================================================================== */ /* Check if email */ var verifmail2 = /^(([^<>()[\]\\.,;:\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]{2,}))$/; /* Force numbers */ $(".num").on("keyup keypress keydown focusout blur", function () { if (this.value != this.value.replace(/[^0-9\.]/g, '')) { this.value = this.value.replace(/[^0-9\.]/g, ''); } }); /* ========================================================================== Check form | Login ========================================================================== */ var clic_login = false; var my = $('.login_form_submit').attr('href'); function checkLogin() { var erreur = 0; $('.login_form .input_require').each(function() { if($(this).val()=='') { $(this).addClass('red'); erreur++; }else { $(this).removeClass('red'); } $('.login_form input[type=email]').each(function() { if(verifmail2.exec($(this).val()) == null) { $(this).addClass('red'); erreur++; }else { $(this).removeClass('red'); } }); }); $('.login_form .input_require').focusout(function(){ if($(this).val()==''){ $(this).addClass('red'); erreur++; }else { $(this).removeClass('red'); } }); if(erreur == 0 && !clic_login) { clic_login = true; ajaxLogin(); } } function ajaxLogin() { var fields = $('.login_form').serialize(); $.ajax({ type :"POST", data :fields, url :baseroot + "/data/login.php", success :function(retour){ if(retour == "ok"){ window.location = ""+domain+"/"+my+""; } else if(retour == "error") { clic_login = false; $(".form_ko_login").show(); }else { clic_login = false; $(".form_ko2").show(); } } }); } $('.login_form_submit').click(function(e) { e.preventDefault(); checkLogin(); }); $('.login_form input').keypress(function(e){ if (e.which == 13) { $(".login_form_submit").trigger("click"); return false; } }); /* ========================================================================== Logout ========================================================================== */ $('.btn_popup_logout').on('click', function(){ $.ajax({ type :"POST", url :baseroot + "/data/logout.php", success :function(retour){ window.location = ""+domain+""; } }); }); /* ========================================================================== Popup ========================================================================== */ $('.btn_popup_contact').on('click', function(e){ e.preventDefault(); $('body').addClass('popup_contact_visible'); $('body').addClass('no_scroll'); }); $('.popup_close').on('click', function(e){ e.preventDefault(); $('body').removeClass('popup_contact_visible'); $('body').removeClass('no_scroll'); }); /* ========================================================================== Check form | Contact ========================================================================== */ var clic_contact = false; function checkContact() { var erreur = 0; $('.contact_form .input_require').each(function() { if($(this).val()=='') { $(this).addClass('red'); erreur++; }else { $(this).removeClass('red'); } $('.contact_form input[type=email]').each(function() { if(verifmail2.exec($(this).val()) == null) { $(this).addClass('red'); erreur++; }else { $(this).removeClass('red'); } }); }); $('.contact_form .input_require').focusout(function(){ if($(this).val()==''){ $(this).addClass('red'); erreur++; }else { $(this).removeClass('red'); } }); if(erreur == 0 && !clic_contact) { clic_contact = true; ajaxContact(); } } function ajaxContact() { var fields = $('.contact_form').serialize(); $.ajax({ type :"POST", data :fields, url :baseroot + "/data/contact.php", success :function(retour){ if(retour == "ok"){ $(".form_ok").show(); } else if(retour == "ko") { $(".form_ko").show(); } else if(retour == "error") { $(".form_error").show(); } else { clic_form = false; var erreurs = retour.split("|"); for (i = 0; i < erreurs.length; i++) { $(".contact_form input[name="+erreurs[i]+"]").addClass("red"); } } } }); } $('.contact_form_submit').click(function(e) { e.preventDefault(); checkContact(); }); /* ========================================================================== Mot de passe oubliƩ ========================================================================== */ $('.mdp_open').on('click', function(e){ e.preventDefault(); $('.mdp').show(); }); $('.mdp_close').on('click', function(e){ e.preventDefault(); $('.mdp').hide(); }); /* ========================================================================== Graph images ========================================================================== */ $('.my_graphs_btn_day a, .my_graphs_btn_week a, .my_graphs_btn_month a, .my_graphs_btn_year a').on('click', function(e){ e.preventDefault(); var date = $(this).attr('data-date'); var id = $(this).attr('data-id'); $('.my_graphs_img[data-id="'+id+'"]').hide(); $('.my_graphs_img[data-date="'+date+'"]').show(); $('.my_graphs_btn_refresh a[data-id="'+id+'"]').attr('data-date' , date); // load graph date var elem_src = $(this).parents(".elem_iframe").find(".my_graphs_img[data-date="+date+"] iframe").attr("data-url"); var graph_size = $(this).parents(".elem_iframe").find(".my_graphs_img[data-date="+date+"] iframe").attr("data-graphsize"); $(this).parents(".elem_iframe").find(".my_graphs_img[data-date="+date+"] iframe").attr("src",elem_src + graph_size); }); $('.my_graphs_btn_refresh a').on('click', function(e){ e.preventDefault(); var date = $(this).attr('data-date'); $('.my_graphs_img[data-date="'+date+'"] .iframe_graph').attr('src', $('.my_graphs_img[data-date="'+date+'"] .iframe_graph').attr('src')); }); /* ========================================================================== Graph images / Size ========================================================================== */ var graphSize = 3; if ($(window).width() < 800) { graphSize = 1; } else if ($(window).width() < 1100) { graphSize = 2; } $(".my_graphs_img iframe").attr("data-graphsize", graphSize); $(".elem_iframe").addClass("graph_size_" + graphSize); /* ========================================================================== My Belcenter navigation ========================================================================== */ var entite = '0'; var produit = ''; $('.my_1_item a').on('click', function(e){ resetIndiceGraph(); e.preventDefault(); entite = $(this).data('entite'); $(".my_1").hide(); $(".my_2[data-entite="+entite+"]").show(); $('html, body').stop().animate({scrollTop : $("body").offset().top},800); }); $('.my_produits_item a').on('click', function(e){ resetIndiceGraph(); e.preventDefault(); $('html, body').stop().animate({scrollTop : $(".my_produits:visible").find('.scrollto_produit').offset().top - 100},800); produit = $(this).data('produit-item'); entiteParent = $(this).data('entite'); $(".my_produits_detail").hide(); $(".my_produits_detail[data-produit-item="+produit+"]").each(function(){ if ($(this).attr('data-entite') == entiteParent) { $(this).show(); } else { $(this).hide(); } }); // display first iframe $(".my_2[data-entite="+entiteParent+"]").find(".my_produits_detail[data-produit-item="+produit+"] .elem_iframe").each(function(){ if(!$(this).find(".my_graphs_img").first().find(".iframe_graph").hasClass('created')){ var elem_src = $(this).find(".my_graphs_img").first().find(".iframe_graph").attr("data-url"); var elem_src_base = $(this).find(".my_graphs_img").first().find(".iframe_graph").attr("src"); var graph_size = $(this).find(".my_graphs_img").first().find(".iframe_graph").attr("data-graphsize"); $graphAll = $(this).find(".my_graphs_img").first().find(".iframe_graph").attr("data-graphsize"); $(this).find(".my_graphs_img").first().find(".iframe_graph").attr("src",elem_src + graph_size).addClass('created'); }; /*var elem_src = $(this).find(".my_graphs_img").first().find(".iframe_graph").attr("data-url"); var graph_size = $(this).find(".my_graphs_img").first().find(".iframe_graph").attr("data-graphsize"); $graphAll = $(this).find(".my_graphs_img").first().find(".iframe_graph").attr("data-graphsize"); $(this).find(".my_graphs_img").first().find(".iframe_graph").attr("src",elem_src + graph_size);*/ }); $('.my_produits_item').removeClass('actif'); $(this).parent().addClass('actif'); }); $('.my_nav_item_produits a').on('click', function(e){ resetIndiceGraph(); e.preventDefault(); $(".my_infos").hide(); $(".my_factures").hide(); $(".my_produits[data-entite="+entite+"]").show(); $(".my_nav_item a").removeClass("actif"); $(this).addClass("actif"); $('html, body').stop().animate({scrollTop : $(".my_2:visible").offset().top - 0},400); }); $('.my_nav_item_infos a').on('click', function(e){ resetIndiceGraph(); e.preventDefault(); $(".my_produits").hide(); $(".my_factures").hide(); $(".my_infos[data-entite="+entite+"]").show(); $(".my_nav_item a").removeClass("actif"); $(this).addClass("actif"); $('html, body').stop().animate({scrollTop : $(".my_2:visible").offset().top - 0},400); }); $('.my_nav_item_factures a').on('click', function(e){ resetIndiceGraph(); e.preventDefault(); $(".my_produits").hide(); $(".my_infos").hide(); $(".my_factures[data-entite="+entite+"]").show(); $(".my_nav_item a").removeClass("actif"); $(this).addClass("actif"); $('html, body').stop().animate({scrollTop : $(".my_2:visible").offset().top - 0},400); }); $('.my_produit_scrolltop').on('click', function(){ $('html, body').stop().animate({scrollTop : $(".my_2:visible").offset().top - 0},400); }); /* ========================================================================== Pagination ========================================================================== */ $('.pagination_item').on('click', function(){ var pagination_item_id = $(this).data('id'); $('.pagination_item').removeClass('active'); $(this).addClass('active'); $('.my_factures_list').removeClass('show'); $('.my_factures_list[data-id='+pagination_item_id+']').addClass('show'); $('html, body').stop().animate({scrollTop : $(".my_2:visible").offset().top - 0},800); }); }); /* ========================================================================== Graph ========================================================================== */ function updateGraph(conso,index,chart,parent,direction){ // get direction if(direction=="prev"){ indice_graph++; } else { indice_graph--; } // show / hide prev arrow if(indice_graph!=0){ $(".my_produit_graph_next").show(); } else { $(".my_produit_graph_next").hide(); } // global vars chart_graph = chart; conso_graph = conso; parent_graph = parent; // show / hide prev arrow var length_graph = conso_graph.length; if(indice_graph==length_graph-1){ $(".my_produit_graph_previous").hide(); } else { $(".my_produit_graph_previous").show(); } updateGraphVars(); } function updateGraphVars(){ // update text data $(parent_graph).find(".data_date").text(conso_graph[indice_graph]['date']); $(parent_graph).find(".data_limit").text(conso_graph[indice_graph]['limit']); $(parent_graph).find(".data_conso").text(conso_graph[indice_graph]['conso']); $(parent_graph).find(".data_exceed").text(conso_graph[indice_graph]['exceed']); // update graph chart_graph.data.datasets[0].data[0] = conso_graph[indice_graph]['limit']; chart_graph.data.datasets[0].data[1] = conso_graph[indice_graph]['conso']; chart_graph.data.datasets[0].data[2] = conso_graph[indice_graph]['exceed']; chart_graph.update(); } function resetIndiceGraph(){ if(typeof chart_graph != 'undefined'){ // reset indice_graph = 0; $(".my_produit_graph_next").hide(); $(".my_produit_graph_previous").show(); updateGraphVars(); } }