main.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. var reading = false;
  2. $(function(){
  3. /*notification_user_notification_search();*/
  4. });
  5. //CHARGEMENT DE LA PAGE
  6. function init_plugin_notification(){
  7. switch($.urlParam('page')){
  8. default:
  9. break;
  10. }
  11. $('#notifications').fill({
  12. action : 'notification_user_notification_search',
  13. unread : false,
  14. }, function(r){
  15. count_notifications();
  16. if(!r.rows)
  17. $('#notifications').append('<div class="text-center no-notification">Aucune notification</div>');
  18. });
  19. $('.categoryCheckbox').on('click', function(){
  20. $(this).closest('li.category').find('li.item input.itemCheckbox').prop('checked', $(this).prop('checked'));
  21. notification_filters();
  22. });
  23. $('.itemCheckbox').on('click', function(){
  24. notification_filters();
  25. });
  26. $('.itemLabel').on('click', function(){
  27. var itemCheckbox = $(this).closest('li.item').find('input.itemCheckbox');
  28. itemCheckbox.prop('checked', !itemCheckbox.prop('checked'));
  29. notification_filters();
  30. });
  31. $('.categoryIcon:eq(0)').trigger('click');
  32. }
  33. function init_setting_notification(){
  34. notification_search();
  35. $('#notifications-pinned').sortable_table({
  36. onSort : notification_search
  37. });
  38. }
  39. $(document).ready(function(e){
  40. notification_user_notification_search();
  41. //Positionnement icône des notifications
  42. if($(document).width() <= 767){
  43. var notifIcon = $('#loginHeader > .notification_menu').detach();
  44. $('#mainMenu > button.navbar-toggler').before(notifIcon);
  45. }
  46. //count_notifications();
  47. });
  48. // fonctions de filtres
  49. function notification_filters(){
  50. var filters = $('.categories').find('input[type=checkbox]:checked');
  51. if(filters.length > 0){
  52. $('li.notification-item').addClass('hidden');
  53. filters.each(function(){
  54. filterItem = $(this).attr('data-item');
  55. $('li.notification-item').each(function(){
  56. if($(this).attr('data-item') == filterItem) $(this).removeClass('hidden');
  57. });
  58. });
  59. }else{
  60. $('li.notification-item:not(:eq(0))').removeClass('hidden');
  61. }
  62. count_notifications();
  63. }
  64. function count_notifications(){
  65. var notifTotal = 0;
  66. $('.itemBadge').text(0);
  67. $('.notification-item.unread').each(function(){
  68. var notifItem = $(this).attr('data-item');
  69. var nbNotifItem = parseInt($('.itemBadge[data-item='+notifItem+']').text());
  70. $('.itemBadge[data-item='+notifItem+']').text(nbNotifItem+1);
  71. });
  72. $('.notifications-list .categories .category').each(function(){
  73. var category = $(this);
  74. var notifCat = 0;
  75. category.find('.item[data-category='+category.attr('data-category')+'] span.itemBadge').each(function(){
  76. notifCat += parseInt($(this).text());
  77. });
  78. category.find('.boxTitle span.categoryBadge').text(notifCat);
  79. notifTotal += parseInt(notifCat);
  80. });
  81. /* couleur sur badge */
  82. $('.notifications-list .category .badge').each(function(){
  83. var badge = $(this);
  84. (parseInt(badge.text()) > 0) ? badge.removeClass('badge-light').addClass('badge-primary') : badge.removeClass('badge-primary').addClass('badge-light');
  85. });
  86. $('#notifCounterFilter').text(parseInt(notifTotal));
  87. if(parseInt(notifTotal) > 0){
  88. $('#notifCounterFilter').removeClass('badge-light').addClass('badge-primary');
  89. $('#notifWord').text('notifications');
  90. }else{
  91. $('#notifCounterFilter').removeClass('badge-primary').addClass('badge-light');
  92. $('#notifWord').text('notification');
  93. }
  94. $('.notification-number').text(parseInt(notifTotal));
  95. if(parseInt(notifTotal) <= 0) $('.notification-number').addClass('hidden');
  96. if(parseInt(notifTotal) > 5) $('.notification-number').text('5+');
  97. (notifTotal > 0) ? $('#readState').removeClass('hidden') : $('#readState').addClass('hidden');
  98. if(parseInt($('.notification_menu .dropdown-menu').find('.notification-item[data-id]').length) <= 0){
  99. $('.notification_menu .dropdown-menu').html('<a class="no-notif dropdown-item notification" href="index.php?module=notification">Aucune notification pour le moment</a>');
  100. }else{
  101. $('.notification_menu .dropdown-menu .no-notif').addClass('hidden');
  102. }
  103. if(parseInt($('#notifications').find('.notification-item[data-id]').length) <= 0){
  104. $('#notifications').append('<div class="no-notif text-center no-notification">Aucune notification pour le moment</div>');
  105. }else{
  106. $('#notifications .no-notif').addClass('hidden');
  107. }
  108. if($('.notification-item').length > 2){
  109. $('#deleteState').removeClass('hidden');
  110. }else{
  111. $('#deleteState').addClass('hidden');
  112. }
  113. }
  114. //Changement positionnement icône des
  115. //notifs au redimensionnement de la fenêtre
  116. $(window).resize(function(event) {
  117. if($(document).width() > 767){
  118. var notifIcon = $('#mainMenu > .notification_menu').detach();
  119. $('#loginHeader').prepend(notifIcon);
  120. }
  121. if($(document).width() <= 767){
  122. var notifIcon = $('#loginHeader > .notification_menu').detach();
  123. $('#mainMenu > button.navbar-toggler').before(notifIcon);
  124. }
  125. });
  126. //Marquer comme lu/non lu une notification
  127. function notification_user_notification_toggle_read(element,event,container){
  128. if(event) event.stopPropagation();
  129. if(!container) container = 'li';
  130. var line = $(element).closest(container);
  131. var notifId = line.attr('data-id');
  132. $.action({
  133. action : 'notification_usernotification_toggle_read',
  134. id : notifId
  135. },function(r){
  136. var notifCounter = $('.notification-number');
  137. var number = $('.notification_menu .notification-number');
  138. var oldNumber = parseInt(number.text());
  139. var notif = 0;
  140. var totalNotif = 0;
  141. $('.badge-category[data-category]').each(function(){
  142. var notifCat = 0;
  143. $('span.badge-item[data-category="'+$(this).attr('data-category')+'"]').each(function(){
  144. if(r.unread[$(this).attr('data-item')] != null){
  145. $(this).removeClass('badge-light').addClass('badge-primary');
  146. notif = r.unread[$(this).attr('data-item')];
  147. }else{ // nbNotif = 0
  148. notif = 0;
  149. $(this).removeClass('badge-primary').addClass('badge-light');
  150. }
  151. $(this).text(notif);
  152. notifCat += parseInt(notif);
  153. });
  154. $(this).text(notifCat);
  155. if(notifCat!=0){
  156. if($(this).hasClass('badge-light')) $(this).removeClass('badge-light').addClass('badge-primary');
  157. }else{
  158. if($(this).hasClass('badge-primary')) $(this).removeClass('badge-primary').addClass('badge-light');
  159. }
  160. totalNotif += parseInt(notifCat);
  161. });
  162. if(totalNotif!=0){
  163. notifCounter.removeClass('hidden').html(totalNotif);
  164. if($('#totalNotif').hasClass('badge-light')) $('#totalNotif').removeClass('badge-light').addClass('badge-primary');
  165. }else{
  166. if($('#totalNotif').hasClass('badge-primary')) $('#totalNotif').removeClass('badge-primary').addClass('badge-light');
  167. }
  168. $('#totalNotif').html(totalNotif);
  169. $('#notifWord').text("notification"+((totalNotif>1) ? "s": ""));
  170. totalNotif.toString().indexOf('+') !== -1 ? notifCounter.addClass('more-unreads') : notifCounter.removeClass('more-unreads');
  171. if(!$.isNumeric(oldNumber)) oldNumber = 0;
  172. var notificationItem = $('.notification-item[data-id='+notifId+']');
  173. if(r.read){
  174. notificationItem.each(function(){
  175. if($(this).hasClass('unread')){
  176. $(this).removeClass('unread').addClass('read');
  177. }else if($(this).hasClass('notification-unread')){
  178. $(this).removeClass('notification-unread').addClass('notification-read');
  179. }
  180. });
  181. $(element).attr('title', 'Marquer comme non lu');
  182. if(oldNumber==1) number.removeAttr('style');
  183. } else {
  184. notificationItem.each(function(){
  185. if($(this).hasClass('read')){
  186. $(this).removeClass('read').addClass('unread');
  187. }else if($(this).hasClass('notification-read')){
  188. $(this).removeClass('notification-read').addClass('notification-unread');
  189. }
  190. });
  191. $(element).attr('title', 'Marquer comme lu');
  192. if(oldNumber==0) number.removeClass('hidden');
  193. }
  194. count_notifications();
  195. });
  196. }
  197. function notification_user_notification_read_all(element){
  198. event.preventDefault();
  199. event.stopPropagation();
  200. var container = $('.'+element);
  201. var ids = [];
  202. container.find('.notification-item').each(function(){
  203. if($.isNumeric($(this).data('id'))) ids.push($(this).data('id'));
  204. });
  205. $.action({
  206. action : 'notification_user_notification_read_all',
  207. id : JSON.stringify(ids)
  208. },function(r){
  209. $.each(r.read, function(index, value){
  210. if(typeof($('.notification-item[data-id='+index+']')) != 'undefined'){
  211. var notificationItem = $('.notification-item[data-id='+index+']');
  212. notificationItem.each(function(){
  213. if($(this).hasClass('unread')){
  214. $(this).removeClass('unread').addClass('read');
  215. }else if($(this).hasClass('notification-unread')){
  216. $(this).removeClass('notification-unread').addClass('notification-read');
  217. }
  218. });
  219. }
  220. });
  221. count_notifications();
  222. });
  223. }
  224. //Récupération des notifications
  225. function notification_user_notification_search(){
  226. var menu = $('.notification_menu .dropdown-menu');
  227. menu.html('<div class="w-100 text-center p-2"><i class="fas fa-spinner fa-pulse"></i> Chargement</div>');
  228. $.action({
  229. action : 'notification_user_notification_search',
  230. synthesis : true,
  231. },function(r){
  232. if(typeof r == "undefined" || ($.isArray(r) && !r.length)) return;
  233. if(typeof r.unread == "undefined"){
  234. $.message('error','Un problème a provoqué une erreur fatale',0);
  235. return;
  236. }
  237. var notifCounter = $('.notification-number');
  238. var notifCounterFilter = $('#notifCounterFilter');
  239. if(r.unread!='0') notifCounter.removeClass('hidden').html(r.unread);
  240. if(r.unread.toString().indexOf('+') !== -1) notifCounter.addClass('more-unreads');
  241. menu.html('<a class="dropdown-item notification" href="index.php?module=notification">Aucune notification</a>');
  242. if(r.rows && r.rows.length>0) menu.html('');
  243. for (var key in r.rows) {
  244. var notification = r.rows[key];
  245. var notificationLine = $(Mustache.render($('.notification-template').html(),notification));
  246. menu.append(notificationLine);
  247. }
  248. $('.notification_menu .dropdown-menu .dropdown-divider').last().remove();
  249. menu.append('<div class="dropdown-divider"></div><a class="dropdown-item see-all-link" href="index.php?module=notification">Voir toutes les notifications</a>')
  250. menu.prepend('</div><div class="dropdown-item all-as-read" onclick="notification_user_notification_read_all(\'dropdown-menu\', event);">Tout marquer comme lu</div><div class="dropdown-divider">')
  251. });
  252. count_notifications();
  253. }
  254. //Suppression d'élement user notification
  255. function notification_user_notification_delete(element,event,container){
  256. if(event) event.stopPropagation();
  257. if(!container) container = 'li';
  258. if(!confirm('Êtes-vous sûr de vouloir supprimer cet item ?')) return;
  259. var line = $(element).closest(container);
  260. var notifId = line.attr('data-id');
  261. $.action({
  262. action : 'notification_usernotification_delete',
  263. id : notifId
  264. },function(r){
  265. //Suppression ligne notif
  266. $('.notification-item[data-id='+notifId+']').remove();
  267. count_notifications();
  268. $.message('info','Notification supprimée');
  269. });
  270. }
  271. function notification_user_notification_delete_all(element){
  272. if(!confirm("Êtes-vous sûr de vouloir supprimer toutes les notifications ?")) return;
  273. event.preventDefault();
  274. event.stopPropagation();
  275. var container = $('.'+element);
  276. var ids = [];
  277. container.find('.notification-item').each(function(){
  278. if($.isNumeric($(this).data('id'))) ids.push($(this).data('id'));
  279. });
  280. $.action({
  281. action : 'notification_usernotifications_delete',
  282. id : JSON.stringify(ids)
  283. },function(r){
  284. $.each($('.notification-item[data-id]'), function(){
  285. $(this).remove();
  286. });
  287. count_notifications();
  288. });
  289. }
  290. //Sauvegarde préférences notification
  291. function notification_user_save_preference(){
  292. var data = $('#notification_preference').toJson();
  293. data.action = 'notification_user_save_preference';
  294. data.preferences = {};
  295. $('#notification_categories .category').each(function(i,elementParent){
  296. var category = $(elementParent).attr('data-slug');
  297. var methods = {};
  298. $(elementParent).find('input[type="checkbox"]').each(function(i, input){
  299. var dataMethod = $(input).attr('data-method');
  300. var isChecked = $(input).prop('checked');
  301. methods[category+'_'+dataMethod] = isChecked;
  302. });
  303. data.preferences[category] = methods;
  304. });
  305. $.action(data,function(r){
  306. $.message('success','Préférences sauvegardées');
  307. });
  308. }
  309. /** NOTIFICATION **/
  310. //Récuperation d'une liste de notification dans le tableau #notifications
  311. function notification_search(callback){
  312. $('#notifications-pinned').fill({
  313. action:'notification_search',
  314. filters : $('#filters').filters(),
  315. sort : $('#notifications-pinned').sortable_table('get')
  316. },function(response){
  317. $('.results-count span').text(response.pagination.total);
  318. if(callback!=null) callback();
  319. });
  320. }
  321. //Suppression d'élement notification
  322. function notification_delete(element){
  323. if(!confirm('Êtes vous sûr de vouloir supprimer cet item ?')) return;
  324. var line = $(element).closest('tr');
  325. $.action({
  326. action : 'notification_delete',
  327. id : line.attr('data-id')
  328. },function(r){
  329. line.remove();
  330. $.message('info','Élement supprimé');
  331. });
  332. }
  333. //Envois manuel d'une notification
  334. function notification_send(){
  335. var data = $('#notification_preference').toJson();
  336. data.action = 'notification_send';
  337. $.action(data,function(r){
  338. $.message('success','Notification envoyée');
  339. notification_search();
  340. });
  341. }