notifs.js.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. notif.className='notifseen';
  23. let jarr=xhr.response;
  24. if (jarr['hmunseen']==0) {
  25. notifimgon='imgs/bell_on.svg';
  26. notifimgoff='imgs/bell_off.svg';
  27. bell.src=notifimgon;
  28. }
  29. };
  30. xhr.onerror=function() {
  31. alert('La richiesta è fallita.');
  32. };
  33. }
  34. lmt=<?php echo($notifs['lastmicrotime']); ?>;
  35. chunk=0;
  36. end=false;
  37. loading=false;
  38. function morenotifs() {
  39. if (!loading && !end && notifs.scrollHeight-notifs.clientHeight-notifs.scrollTop<20) {
  40. loading=true;
  41. chunk++;
  42. let xhr=new XMLHttpRequest();
  43. xhr.open('GET','notifsh.php?act=loadchunk&chunk='+chunk);
  44. xhr.responseType='json';
  45. xhr.send();
  46. xhr.onload=function() {
  47. let jarr=xhr.response;
  48. //console.log(jarr);
  49. let i=0, html='';
  50. for (i=0; i<jarr.length; i++)
  51. html+=jarr[i];
  52. notifs.innerHTML+=html;
  53. if (jarr.length<<?php echo($notifs['chunksize']); ?>)
  54. end=true;
  55. loading=false;
  56. };
  57. xhr.onerror=function() {
  58. alert('La richiesta è fallita.');
  59. };
  60. }
  61. }
  62. loadingupd=false;
  63. function updnotifs() {
  64. if (!loadingupd) {
  65. loadingupd=true;
  66. let xhr=new XMLHttpRequest();
  67. xhr.open('GET','notifsh.php?act=loadnew&lmt='+lmt);
  68. xhr.responseType='json';
  69. xhr.send();
  70. xhr.onload=function() {
  71. let jarr=xhr.response;
  72. //console.log(jarr);
  73. let i=0, html='';
  74. for (i=0; i<jarr['buf'].length; i++)
  75. html+=jarr['buf'][i];
  76. notifs.innerHTML=html+notifs.innerHTML;
  77. lmt=jarr['newlmt'];
  78. if (jarr['newunread']) {
  79. notifimgoff='imgs/bell_act_on.svg';
  80. notifimgon='imgs/bell_act_off.svg';
  81. (notifc.style.display=='block') ? bell.src=notifimgoff : bell.src=notifimgon;
  82. }
  83. loadingupd=false;
  84. };
  85. xhr.onerror=function() {
  86. alert('La richiesta è fallita.');
  87. };
  88. } else {
  89. console.log('Già sto caricando...');
  90. }
  91. }
  92. updint=setInterval(updnotifs,3000);