templates/Nordinfo/Main/parts/cookieManager.js.html.twig line 1

Open in your IDE?
  1. <script type="text/javascript">
  2.   $(function () {
  3.     
  4.     let selectorArea = '#areaSelectorContainer';
  5.     let selectorAreaLoader = '#areaLoader';
  6.     let selectorTopicsList = '#topicsList';
  7.     let selectorTopicsListLoader = '#topicsListLoader';
  8.     
  9.     $('[data-remove-item]').on('click', function (e) {
  10.       e.preventDefault();
  11.       e.stopPropagation();
  12.       let post_id = $(this).attr('data-remove-item');
  13.       {% if app.user is not null %}
  14.         removeFavori(post_id);
  15.         $(this).closest('.colArticleItem').remove();
  16.       {% else %}
  17.         let myFavorite = getCookie('favorite');
  18.         if (myFavorite == null || myFavorite === '') {
  19.           myFavorite = [];
  20.         } else {
  21.           myFavorite = myFavorite.split(',');
  22.         }
  23.         if (jQuery.inArray(post_id, myFavorite) >= -1) {
  24.           myFavorite.splice($.inArray(post_id, myFavorite), 1);
  25.         }
  26.         setCookie('favorite', myFavorite, 360)
  27.         $(this).closest('.colArticleItem').remove();
  28.       {% endif %}
  29.     });
  30.     
  31.     $('.fav').on('click', function (event) {
  32.       event.preventDefault();
  33.       let post_id = $(this).attr('data-id');
  34.       {% if app.user is not null %}
  35.         if ($(this).hasClass('_favorite')) {
  36.           $(this).removeClass('_favorite');
  37.           $(this).attr('aria-pressed', 'false');
  38.           removeFavori(post_id);
  39.         } else {
  40.           $(this).addClass('_favorite');
  41.           $(this).attr('aria-pressed', 'true');
  42.           addFavori(post_id);
  43.         }
  44.       {% else %}
  45.         let myFavorite = getCookie('favorite');
  46.         if (myFavorite == null || myFavorite === '') {
  47.           myFavorite = [];
  48.         } else {
  49.           myFavorite = myFavorite.split(',');
  50.         }
  51.         if ($(this).hasClass('_favorite')) {
  52.           $(this).removeClass('_favorite');
  53.           $(this).attr('aria-pressed', 'false');
  54.           if (jQuery.inArray(post_id, myFavorite) >= -1) {
  55.             myFavorite.splice($.inArray(post_id, myFavorite), 1);
  56.           }
  57.         } else {
  58.           $(this).addClass('_favorite');
  59.           $(this).attr('aria-pressed', 'true');
  60.           if (jQuery.inArray(post_id, myFavorite) === -1) {
  61.             myFavorite.push(post_id);
  62.           }
  63.         }
  64.         setCookie('favorite', myFavorite, 10);
  65.       {% endif %}
  66.     });
  67.     $('#search_territory').on('submit', function (event) {
  68.       event.preventDefault();
  69.       $('#containerResultTerritory').fadeIn();
  70.       $('#loadingSearchTerritory').fadeIn();
  71.       $('#myTerritory').fadeOut();
  72.       $('#loadingSearchTerritory')[0].setAttribute('aria-hidden', 'true');
  73.       $('#myTerritory')[0].setAttribute('aria-hidden', 'true');
  74.       $.ajax({
  75.         url: "{{ path('ajax_territory') }}",
  76.         data: {
  77.           'search': $('#commune').val()
  78.         },
  79.         type: "POST",
  80.         success: function (response) {
  81.           $('#loadingSearchTerritory').fadeOut();
  82.           $('#loadingSearchTerritory')[0].setAttribute('aria-hidden', 'true');
  83.           if (response.error === false) {
  84.             $('#myTerritory').empty().append("Mon territoire : " + response.t.name);
  85.             setTerritories();
  86.           } else {
  87.             $('#myTerritory').empty().append(response.error);
  88.           }
  89.           $('#myTerritory').fadeIn();
  90.           $('#myTerritory')[0].setAttribute('aria-hidden', 'false');
  91.         }
  92.       });
  93.       return false;
  94.     });
  95.     $('.select_territory').on('click', function () {
  96.       setTerritories();
  97.     });
  98.     $('.select_topic').on('click', function () {
  99.       setTopics();
  100.     });
  101.     
  102.     function setTerritories() {
  103.       let myTerritories = [];
  104.       let allTerritories = [];
  105.       $(".button_select.active").each(function () {
  106.         if (this.id !== 0 && this.id !== '0') {
  107.           myTerritories.push(this.id);
  108.         }
  109.       });
  110.       $(".button_select").each(function () {
  111.         if (this.id !== 0 && this.id !== '0') {
  112.           allTerritories.push(this.id);
  113.         }
  114.       });
  115.       {% if app.user is not null %}
  116.         $(selectorArea).addClass('loading');
  117.         $(selectorAreaLoader).fadeIn();
  118.         updateTerritories(allTerritories, myTerritories);
  119.       {% else %}
  120.         eraseCookie('territories');
  121.         if (myTerritories.length !== 0) {
  122.           setCookie('territories', myTerritories, 360);
  123.         }
  124.       {% endif %}
  125.     }
  126.     
  127.     function setTopics() {
  128.       let myTopics = [];
  129.       let allTopics = [];
  130.       $(".button_select.active").each(function () {
  131.         if (this.id !== 0 && this.id !== '0') {
  132.           myTopics.push(this.id);
  133.         }
  134.       });
  135.       $(".button_select").each(function () {
  136.         if (this.id !== 0 && this.id !== '0') {
  137.           allTopics.push(this.id);
  138.         }
  139.       });
  140.       {% if app.user is not null %}
  141.         $(selectorTopicsList).addClass('loading');
  142.         $(selectorTopicsListLoader).fadeIn();
  143.         updateTopics(allTopics, myTopics);
  144.       {% else %}
  145.         eraseCookie('topics');
  146.         myTopics = [];
  147.         $(".button_select.active").each(function () {
  148.           if (this.id !== 0) {
  149.             myTopics.push(this.id);
  150.           }
  151.         });
  152.         if (myTopics.length !== 0) {
  153.           setCookie('topics', myTopics, 360);
  154.         }
  155.       {% endif %}
  156.     }
  157.     function getFavoris() {
  158.       $.ajax({
  159.         url: "{{ path('ajax') }}",
  160.         data: {
  161.           'function': 'getFavoris'
  162.         },
  163.         type: "POST",
  164.         success: function (response) {}
  165.       });
  166.     }
  167.     function addFavori(id) {
  168.       $.ajax({
  169.         url: "{{ path('ajax') }}",
  170.         data: {
  171.           'function': 'addFavori',
  172.           'id': id
  173.         },
  174.         type: "POST",
  175.         success: function (response) {}
  176.       });
  177.     }
  178.     function removeFavori(id) {
  179.       $.ajax({
  180.         url: "{{ path('ajax') }}",
  181.         data: {
  182.           'function': 'removeFavori',
  183.           'id': id
  184.         },
  185.         type: "POST",
  186.         success: function (response) {}
  187.       });
  188.     }
  189.     
  190.     function getTopics() {
  191.       $.ajax({
  192.         url: "{{ path('ajax') }}",
  193.         data: {
  194.           'function': 'getTopics'
  195.         },
  196.         type: "POST",
  197.         success: function (response) {
  198.         }
  199.       });
  200.     }
  201.     
  202.     function updateTopics(allTopics, myTopics) {
  203.       $.ajax({
  204.         url: "{{ path('ajax') }}",
  205.         data: {
  206.           'function': 'removeTopics',
  207.           'ids': allTopics
  208.         },
  209.         type: "POST",
  210.         success: function () {
  211.           if (myTopics.length > 0) {
  212.             addTopics(myTopics);
  213.           } else {
  214.             $(selectorTopicsList).removeClass('loading');
  215.             $(selectorTopicsListLoader).fadeOut();
  216.           }
  217.         }
  218.       });
  219.     }
  220.     function addTopics(ids) {
  221.       $.ajax({
  222.         url: "{{ path('ajax') }}",
  223.         data: {
  224.           'function': 'addTopics',
  225.           'ids': ids
  226.         },
  227.         type: "POST",
  228.         success: function () {
  229.           $(selectorTopicsList).removeClass('loading');
  230.           $(selectorTopicsListLoader).fadeOut();
  231.         }
  232.       });
  233.     }
  234.     function getTerritories() {
  235.       $.ajax({
  236.         url: "{{ path('ajax') }}",
  237.         data: {
  238.           'function': 'getTerritories'
  239.         },
  240.         type: "POST",
  241.         success: function (response) {
  242.         }
  243.       });
  244.     }
  245.     
  246.     function updateTerritories(allTerritories, myTerritories) {
  247.       $.ajax({
  248.         url: "{{ path('ajax') }}",
  249.         data: {
  250.           'function': 'removeTerritories',
  251.           'ids': allTerritories
  252.         },
  253.         type: "POST",
  254.         success: function () {
  255.           if (myTerritories.length > 0) {
  256.             addTerritories(myTerritories);
  257.           } else {
  258.             $(selectorArea).removeClass('loading');
  259.             $(selectorAreaLoader).fadeOut();
  260.           }
  261.         }
  262.       });
  263.     }
  264.     function addTerritories(ids) {
  265.       $.ajax({
  266.         url: "{{ path('ajax') }}",
  267.         data: {
  268.           'function': 'addTerritories',
  269.           'ids': ids
  270.         },
  271.         type: "POST",
  272.         success: function () {
  273.           $(selectorArea).removeClass('loading');
  274.           $(selectorAreaLoader).fadeOut();
  275.         }
  276.       });
  277.     }
  278.     
  279.     function setCookie(key, value, expiry) {
  280.       let expires = new Date();
  281.       expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000));
  282.       document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
  283.     }
  284.     function getCookie(key) {
  285.       let keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
  286.       return keyValue ? keyValue[2] : null;
  287.     }
  288.     function eraseCookie(key) {
  289.       let keyValue = getCookie(key);
  290.       setCookie(key, keyValue, '-1');
  291.     }
  292.   });
  293. </script>