main.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. $(function(){
  2. notification_user_notification_search();
  3. });
  4. //CHARGEMENT DE LA PAGE
  5. function init_plugin_notification(){
  6. switch($.urlParam('page')){
  7. default:
  8. $('#notifications').fill({
  9. action : 'notification_user_notification_search'
  10. }, function(r){
  11. if(!r.rows)
  12. $('#notifications').append('<div class="text-center no-notification">Aucune notification</div>');
  13. });
  14. break;
  15. }
  16. }
  17. $(document).ready(function(e){
  18. //Positionnement icône des notifications
  19. if($(document).width() <= 767){
  20. var notifIcon = $('#loginHeader > .notification_menu').detach();
  21. $('#mainMenu > button.navbar-toggler').before(notifIcon);
  22. }
  23. //Meilleure ergonomie pour choisir les catégories de notification
  24. $('#notification_categories').on('click', ' .list-group-item.list-group-item-action', function(e){
  25. var checkbox = $(this).find('label.check-component>input[type="checkbox"]');
  26. checkbox.prop('checked', !checkbox.prop("checked"));
  27. checkbox.attr('checked', !checkbox.prop("checked"));
  28. });
  29. });
  30. //Changement positionnement icône des
  31. //notifs au redimensionnement de la fenêtre
  32. $(window).resize(function(event) {
  33. if($(document).width() > 767){
  34. var notifIcon = $('#mainMenu > .notification_menu').detach();
  35. $('#loginHeader').prepend(notifIcon);
  36. }
  37. if($(document).width() <= 767){
  38. var notifIcon = $('#loginHeader > .notification_menu').detach();
  39. $('#mainMenu > button.navbar-toggler').before(notifIcon);
  40. }
  41. });
  42. //Marquer comme lu/non lu une notification
  43. function notification_user_notification_toggle_read(element,event,container){
  44. if(event) event.stopPropagation();
  45. if(!container) container = 'li';
  46. var line = $(element).closest(container);
  47. var number = $('.notification_menu .notification-number');
  48. var notifId = line.attr('data-id');
  49. $.action({
  50. action : 'notification_usernotification_toggle_read',
  51. id : notifId
  52. },function(r){
  53. var oldNumber = parseInt(number.text());
  54. if(!$.isNumeric(oldNumber)) oldNumber = 0;
  55. if(r.read){
  56. $('.notification-item[data-id='+notifId+']').removeClass('unread').removeClass('notification-unread');
  57. $(element).attr('title', 'Marquer comme non lu');
  58. if(oldNumber==1) number.removeAttr('style');
  59. number.text(oldNumber-1)
  60. } else {
  61. $('.notification-item[data-id='+notifId+']').addClass('unread').addClass('notification-unread');
  62. $(element).attr('title', 'Marquer comme lu');
  63. if(oldNumber==0) number.show();
  64. number.text(oldNumber+1);
  65. }
  66. });
  67. }
  68. function notification_user_notification_all_read(element, event){
  69. event.preventDefault();
  70. event.stopPropagation();
  71. var notifDropdown = $(element).closest('.dropdown-menu');
  72. var notifs = $('.dropdown-item.notification-unread', notifDropdown);
  73. if(!notifs.length) return;
  74. //Choix entre uniquement les notifs non lues affichées
  75. $.each(notifs, function(index, value){
  76. notification_user_notification_toggle_read(value, event, '.dropdown-item');
  77. });
  78. //Ou prendre toutes les notifs non lues
  79. // $.action({
  80. // action: 'notification_usernotification_all_read'
  81. // }, function(r){
  82. // });
  83. }
  84. //Récupération des notifications
  85. function notification_user_notification_search(){
  86. var menu = $('.notification_menu .dropdown-menu');
  87. menu.html('Chargement...');
  88. $.action({
  89. action : 'notification_user_notification_search',
  90. synthesis : true,
  91. },function(r){
  92. if(r.unread!='0') $('.notification-number').show().html(r.unread);
  93. menu.html('<a class="dropdown-item notification" href="index.php?module=notification">Aucune notification</a>');
  94. if(r.rows && r.rows.length>0) menu.html('');
  95. for (var key in r.rows) {
  96. var notification = r.rows[key];
  97. var notificationLine = $(Mustache.render($('.notification-template').html(),notification));
  98. menu.append(notificationLine);
  99. }
  100. $('.notification_menu .dropdown-menu .dropdown-divider').last().remove();
  101. menu.append('<div class="dropdown-divider"></div><a class="dropdown-item see-all-link" href="index.php?module=notification">Voir toutes les notifications</a>')
  102. menu.prepend('</div><div class="dropdown-item all-as-read" onclick="notification_user_notification_all_read(this, event);">Tout marquer comme lu</div><div class="dropdown-divider">')
  103. });
  104. }
  105. //Suppression d'élement user notification
  106. function notification_user_notification_delete(element,event,container){
  107. if(event) event.stopPropagation();
  108. if(!container) container = 'li';
  109. if(!confirm('Êtes-vous sûr de vouloir supprimer cet item ?')) return;
  110. var line = $(element).closest(container);
  111. var notifId = line.attr('data-id');
  112. $.action({
  113. action : 'notification_usernotification_delete',
  114. id : notifId
  115. },function(r){
  116. var menu = $('.notification_menu .dropdown-menu');
  117. line.remove();
  118. //Suppression ligne notif dans le dropdown
  119. $('.notification-item[data-id='+notifId+']').remove();
  120. $('.dropdown-divider[data-notification='+notifId+']', menu).remove();
  121. //Gestion affichage de la dernière notif supprimée
  122. if(!$('.dropdown-item[data-id]', menu).length){
  123. menu.html('<a class="dropdown-item notification" href="index.php?module=notification">Aucune notification pour le moment</a>');
  124. $('#notifications').append('<div class="text-center no-notification">Aucune notification pour le moment</div>');
  125. }
  126. //Gestion affichage dynamique nombre de notification
  127. var notifNbContainer = $('.notification-number');
  128. var notifNb = parseInt(notifNbContainer.text())
  129. notifNb-1 < 1 ? notifNbContainer.text(0).removeAttr('style') : notifNbContainer.text(notifNb-1);
  130. $.message('info','Notification supprimée');
  131. });
  132. }
  133. //Sauvegarde préférences notification
  134. function notification_user_save_preference(){
  135. var data = $('#notification_preference').toJson();
  136. data.action = 'notification_user_save_preference';
  137. data.categories = [];
  138. $('#notification_categories .category').each(function(i,element){
  139. element = $(element);
  140. if(element.find('input[type="checkbox"]:checked').length!=0)
  141. data.categories.push(element.attr('data-slug'));
  142. });
  143. $.action(data,function(r){
  144. $.message('success','Préférences sauvegardées');
  145. });
  146. }
  147. // /* Notification via browser */
  148. // //Request permission on page load
  149. // document.addEventListener('DOMContentLoaded', function () {
  150. // if (!Notification) return;
  151. // if (Notification.permission !== "granted") Notification.requestPermission();
  152. // });
  153. // //Émet une notification en dehors du navigateur
  154. // function emit_notification(infos) {
  155. // var url = window.location.href;
  156. // var filename = url.substring(url.lastIndexOf('/')+1);
  157. // var href = url.replace(filename,'');
  158. // if(Notification.permission !== "granted") {
  159. // Notification.requestPermission();
  160. // } else {
  161. // infos.icon = infos.icon ? infos.icon : href+'img/logo/logo.png';
  162. // infos.title = infos.title ? infos.title : 'Nouvelle notification';
  163. // infos.body = infos.body ? infos.body : 'Vous avez reçu une nouvelle notification en provenance de '+href;
  164. // var notification = new Notification(
  165. // infos.title, {
  166. // icon: infos.icon,
  167. // body: infos.body,
  168. // }
  169. // );
  170. // infos.link = infos.link ? infos.link : href+'index.php?module=notification';
  171. // notification.onclick = function() {
  172. // window.open(infos.link);
  173. // };
  174. // }
  175. // }
  176. //Notifications avancée avec utilisation d'un Service Worker
  177. // navigator.serviceWorker.register('sw.js');
  178. // function showNotification() {
  179. // Notification.requestPermission(function(result) {
  180. // if (result === 'granted') {
  181. // navigator.serviceWorker.ready.then(function(registration) {
  182. // registration.showNotification('Vibration Sample', {
  183. // body: 'Buzz! Buzz!',
  184. // icon: '../images/touch/chrome-touch-icon-192x192.png',
  185. // vibrate: [200, 100, 200, 100, 200, 100, 200],
  186. // tag: 'vibration-sample'
  187. // });
  188. // });
  189. // }
  190. // });
  191. // }