notifs.js.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. notifimgon='<?php echo($notifs["imgon"]); ?>';
  2. notifimgoff='<?php echo($notifs["imgoff"]); ?>';
  3. function shidenotifs() {
  4. var notifc=document.getElementById('notifc');
  5. var ctrli=document.getElementById('bell');
  6. if (notifc.style.display=='block') {
  7. notifc.style.display='none';
  8. ctrli.src=notifimgoff;
  9. ctrli.title='Mostra le notifiche';
  10. } else {
  11. notifc.style.display='block';
  12. ctrli.src=notifimgon;
  13. ctrli.title='Nascondi le notifiche';
  14. }
  15. }
  16. function markread(notif) {
  17. let xhr=new XMLHttpRequest();
  18. xhr.open('GET','notifsh.php?act=markread&id='+notif.id.replace(/^notif-([0-9]+)$/,'$1'));
  19. xhr.responseType='json';
  20. xhr.send();
  21. xhr.onload=function() {
  22. let jarr=xhr.response;
  23. if (jarr['hmunseen']==0) {
  24. notifimgon='imgs/bell_on.svg';
  25. notifimgoff='imgs/bell_off.svg';
  26. bell.src=notifimgon;
  27. }
  28. if (notif.classList.contains("notifunseen")){
  29. // Notifica nella navbar
  30. notif.className='notifseen';
  31. } else if (notif.classList.contains("unseen")) {
  32. // Notifica nella pagina di gestione notifiche
  33. notif.classList.remove("unseen");
  34. notif.classList.add("seen");
  35. }
  36. };
  37. xhr.onerror=function() {
  38. alert('La richiesta è fallita.');
  39. };
  40. }
  41. function markdeleted(notif) {
  42. let xhr=new XMLHttpRequest();
  43. xhr.open('GET','notifsh.php?act=delete&id='+notif.id.replace(/^notif-([0-9]+)$/,'$1'));
  44. xhr.responseType='json';
  45. xhr.send();
  46. xhr.onload=function() {
  47. if (xhr.response['deleted']) {
  48. notif.remove();
  49. } else {
  50. alert("Il server non ha potuto cancellare la notifica " + notif.text);
  51. }
  52. };
  53. xhr.onerror=function() {
  54. alert('La richiesta è fallita.');
  55. };
  56. }
  57. function applyAllSelected(fun) {
  58. let notifs = document.querySelectorAll("div#notifs-list > div");
  59. notifs.forEach(function (notif) {
  60. let checkbox = notif.querySelector("input.seen-checkbox");
  61. if (checkbox.checked) {
  62. fun(notif);
  63. }});
  64. }
  65. lmt=<?php echo($notifs['lastmicrotime']); ?>;
  66. chunk=0;
  67. end=false;
  68. loading=false;
  69. function morenotifs() {
  70. if (!loading && !end && notifs.scrollHeight-notifs.clientHeight-notifs.scrollTop<20) {
  71. loading=true;
  72. chunk++;
  73. let xhr=new XMLHttpRequest();
  74. xhr.open('GET','notifsh.php?act=loadchunk&chunk='+chunk);
  75. xhr.responseType='json';
  76. xhr.send();
  77. xhr.onload=function() {
  78. let jarr=xhr.response;
  79. //console.log(jarr);
  80. let i=0, html='';
  81. for (i=0; i<jarr.length; i++)
  82. html+=jarr[i];
  83. notifs.innerHTML+=html;
  84. if (jarr.length<<?php echo($notifs['chunksize']); ?>)
  85. end=true;
  86. loading=false;
  87. };
  88. xhr.onerror=function() {
  89. alert('La richiesta è fallita.');
  90. };
  91. }
  92. }
  93. loadingupd=false;
  94. function updnotifs() {
  95. if (!loadingupd) {
  96. loadingupd=true;
  97. let xhr=new XMLHttpRequest();
  98. xhr.open('GET','notifsh.php?act=loadnew&lmt='+lmt);
  99. xhr.responseType='json';
  100. xhr.send();
  101. xhr.onload=function() {
  102. let jarr=xhr.response;
  103. //console.log(jarr);
  104. let i=0, html='';
  105. for (i=0; i<jarr['buf'].length; i++)
  106. html+=jarr['buf'][i];
  107. notifs.innerHTML=html+notifs.innerHTML;
  108. lmt=jarr['newlmt'];
  109. if (jarr['newunread']) {
  110. notifimgoff='imgs/bell_act_on.svg';
  111. notifimgon='imgs/bell_act_off.svg';
  112. (notifc.style.display=='block') ? bell.src=notifimgoff : bell.src=notifimgon;
  113. }
  114. loadingupd=false;
  115. };
  116. xhr.onerror=function() {
  117. alert('La richiesta è fallita.');
  118. };
  119. } else {
  120. console.log('Già sto caricando...');
  121. }
  122. }
  123. updint=setInterval(updnotifs,3000);