$(document).ready( function () { $('[data-url]').on('click', function(e) { window.location.href = $(this).data('url'); }); $('select[multiple] > option').mousedown(function(e) { if(e.which==1) { e.preventDefault(); $(this).prop('selected', !$(this).prop('selected')); return false; } }); var loc_uri = URI.parse(window.location.href); var loc_qry = URI.parseQuery(loc_uri.query) $('.filters-bar').children('select[data-filter]').each(function () { var filter_name = $(this).data('filter'); if(filter_name in loc_qry) { $(this).val(loc_qry[filter_name]); } }); $('#stories').on('change', 'select[data-filter]', function() { var filters = []; var filtersbar = $('#stories > .filters-bar'); filtersbar.children('select').each(function () { if (this.value.length > 0) { filters.push($(this).data('filter') + "=" + $(this).val()); } }); var url = document.URL; var target = url.split('?')[0]; if (filters.length > 0) { target = target + '?' + filters.join('&'); } window.location.href = target; }); $('#stories').on('click', 'a[data-page]', function() { var filters = []; var filtersbar = $('#stories > .filters-bar'); filtersbar.children('select').each(function () { if (this.value.length > 0) { filters.push($(this).data('filter') + "=" + $(this).val()); } }); var url = document.URL; var target = url.split('?')[0]; if (filters.length > 0) { target = target + '?' + filters.join('&') + '&page=' + $(this).data('page'); } else { target = target + '?page=' + $(this).data('page'); } window.location.href = target; }); $(document).on('click', function(event) { if(!$(event.target).closest('#user-panel').length) { if($('#user-dropdown').is(":visible")) { $('#user-dropdown').hide(); } } if(!$(event.target).closest('#notif-panel').length) { if($('#notif-dropdown').is(":visible")) { $('#notif-dropdown').hide(); } } }); $(document).on('click', '#user-show-btn', function(event) { event.preventDefault(); if($('#user-dropdown').is(":visible")) { $('#user-dropdown').hide(); } else { $('#user-dropdown').show(); } }); $(document).on('click', '#notif-show-btn', function(event) { event.preventDefault(); if($('#notif-dropdown').is(":visible")) { $('#notif-dropdown').hide(); } else { $('#notif-dropdown').show(); } }); function csrfSafeMethod(method) { // these HTTP methods do not require CSRF protection return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method)); } function getCookie(name) { var cookieValue = null; if (document.cookie && document.cookie !== '') { var cookies = document.cookie.split(';'); for (var i = 0; i < cookies.length; i++) { var cookie = jQuery.trim(cookies[i]); // Does this cookie string begin with the name we want? if (cookie.substring(0, name.length + 1) === (name + '=')) { cookieValue = decodeURIComponent(cookie.substring(name.length + 1)); break; } } } return cookieValue; } var csrftoken = getCookie('csrftoken'); $(".notif-seen").click(function(event) { event.preventDefault(); var notif = $(this).closest('.notif'); var notif_id = notif.data('id'); $.ajax({ type: "POST", url: "/notif/seen/" + notif_id + "/", data: '{notif_id:' + notif_id + '}', contentType: "application/json; charset=utf-8", dataType: "json", beforeSend: function(xhr, settings) { if (!csrfSafeMethod(settings.type) && !this.crossDomain) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } }, success: function (response) { notif.remove(); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }); }); $(".notif-all-seen").click(function(event) { event.preventDefault(); var notif_list = $('#notif-dropdown').find('.notif-list'); if (confirm('Êtes vous sûr de vouloir faire disparaitre toutes les notifications ?')) { $.ajax({ type: "POST", url: "/notif/allseen/", contentType: "application/json; charset=utf-8", dataType: "json", beforeSend: function(xhr, settings) { if (!csrfSafeMethod(settings.type) && !this.crossDomain) { xhr.setRequestHeader("X-CSRFToken", csrftoken); } }, success: function (response) { notif_list.html(''); }, failure: function (response) { alert(response.responseText); }, error: function (response) { alert(response.responseText); } }); } }); });