main.js 13 KB

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