custom.js 5.0 KB

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