custom.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. $(document).ready( function () {
  2. $('[data-url]').on('click', function(e) {
  3. window.location.href = $(this).data('url');
  4. });
  5. $('option').mousedown(function(e) {
  6. e.preventDefault();
  7. $(this).prop('selected', !$(this).prop('selected'));
  8. return false;
  9. });
  10. var loc_uri = URI.parse(window.location.href);
  11. var loc_qry = URI.parseQuery(loc_uri.query)
  12. $('.filters-bar').children('select[data-filter]').each(function () {
  13. var filter_name = $(this).data('filter');
  14. if(filter_name in loc_qry) {
  15. $(this).val(loc_qry[filter_name]);
  16. }
  17. });
  18. $('#stories').on('change', 'select[data-filter]', function() {
  19. var filters = [];
  20. var filtersbar = $('#stories > .filters-bar');
  21. filtersbar.children('select').each(function () {
  22. if (this.value.length > 0) {
  23. filters.push($(this).data('filter') + "=" + $(this).val());
  24. }
  25. });
  26. var url = document.URL;
  27. var target = url.split('?')[0];
  28. if (filters.length > 0) {
  29. target = target + '?' + filters.join('&');
  30. }
  31. window.location.href = target;
  32. });
  33. $('#stories').on('click', 'a[data-page]', function() {
  34. var filters = [];
  35. var filtersbar = $('#stories > .filters-bar');
  36. filtersbar.children('select').each(function () {
  37. if (this.value.length > 0) {
  38. filters.push($(this).data('filter') + "=" + $(this).val());
  39. }
  40. });
  41. var url = document.URL;
  42. var target = url.split('?')[0];
  43. if (filters.length > 0) {
  44. target = target + '?' + filters.join('&') + '&page=' + $(this).data('page');
  45. } else {
  46. target = target + '?page=' + $(this).data('page');
  47. }
  48. window.location.href = target;
  49. });
  50. $(document).on('click', function(event) {
  51. if(!$(event.target).closest('#user-panel').length) {
  52. if($('#user-dropdown').is(":visible")) {
  53. $('#user-dropdown').hide();
  54. }
  55. }
  56. if(!$(event.target).closest('#notif-panel').length) {
  57. if($('#notif-dropdown').is(":visible")) {
  58. $('#notif-dropdown').hide();
  59. }
  60. }
  61. });
  62. $(document).on('click', '#user-show-btn', function(event) {
  63. event.preventDefault();
  64. if($('#user-dropdown').is(":visible")) {
  65. $('#user-dropdown').hide();
  66. }
  67. else {
  68. $('#user-dropdown').show();
  69. }
  70. });
  71. $(document).on('click', '#notif-show-btn', function(event) {
  72. event.preventDefault();
  73. if($('#notif-dropdown').is(":visible")) {
  74. $('#notif-dropdown').hide();
  75. }
  76. else {
  77. $('#notif-dropdown').show();
  78. }
  79. });
  80. function csrfSafeMethod(method) {
  81. // these HTTP methods do not require CSRF protection
  82. return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
  83. }
  84. function getCookie(name) {
  85. var cookieValue = null;
  86. if (document.cookie && document.cookie !== '') {
  87. var cookies = document.cookie.split(';');
  88. for (var i = 0; i < cookies.length; i++) {
  89. var cookie = jQuery.trim(cookies[i]);
  90. // Does this cookie string begin with the name we want?
  91. if (cookie.substring(0, name.length + 1) === (name + '=')) {
  92. cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
  93. break;
  94. }
  95. }
  96. }
  97. return cookieValue;
  98. }
  99. var csrftoken = getCookie('csrftoken');
  100. $(".notif-seen").click(function(event) {
  101. event.preventDefault();
  102. var notif = $(this).closest('.notif');
  103. var notif_id = notif.data('id');
  104. $.ajax({
  105. type: "POST",
  106. url: "/notif/seen/" + notif_id + "/",
  107. data: '{notif_id:' + notif_id + '}',
  108. contentType: "application/json; charset=utf-8",
  109. dataType: "json",
  110. beforeSend: function(xhr, settings) {
  111. if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
  112. xhr.setRequestHeader("X-CSRFToken", csrftoken);
  113. }
  114. },
  115. success: function (response) {
  116. notif.remove();
  117. },
  118. failure: function (response) {
  119. alert(response.responseText);
  120. },
  121. error: function (response) {
  122. alert(response.responseText);
  123. }
  124. });
  125. });
  126. $(".notif-all-seen").click(function(event) {
  127. event.preventDefault();
  128. var notif_list = $('#notif-dropdown').find('.notif-list');
  129. if (confirm('Êtes vous sûr de vouloir faire disparaitre toutes les notifications ?')) {
  130. $.ajax({
  131. type: "POST",
  132. url: "/notif/allseen/",
  133. contentType: "application/json; charset=utf-8",
  134. dataType: "json",
  135. beforeSend: function(xhr, settings) {
  136. if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
  137. xhr.setRequestHeader("X-CSRFToken", csrftoken);
  138. }
  139. },
  140. success: function (response) {
  141. notif_list.html('');
  142. },
  143. failure: function (response) {
  144. alert(response.responseText);
  145. },
  146. error: function (response) {
  147. alert(response.responseText);
  148. }
  149. });
  150. }
  151. });
  152. });