2
0

topic.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. "use strict";
  2. function opennamu_thread_delete() {
  3. let lang_data = new FormData();
  4. lang_data.append('data', 'delete');
  5. fetch('/api/lang', {
  6. method : 'POST',
  7. body : lang_data,
  8. }).then(function(res) {
  9. return res.json();
  10. }).then(function(lang) {
  11. lang = lang["data"];
  12. let check_list = [];
  13. let check_list_str = '';
  14. for(let for_a = 0; for_a < document.getElementsByClassName("opennamu_blind_button").length; for_a++) {
  15. let id = document.getElementsByClassName("opennamu_blind_button")[for_a].id;
  16. id = id.replace(/^opennamu_blind_/, '');
  17. id = id.split('_');
  18. let checked = document.getElementsByClassName("opennamu_blind_button")[for_a].checked;
  19. if(checked) {
  20. check_list.push([id[0], id[1]]);
  21. check_list_str += '#' + id[1] + ' ';
  22. }
  23. }
  24. let check = confirm(check_list_str + lang[0]);
  25. if(check === true) {
  26. for(let for_a = 0; for_a < check_list.length; for_a++) {
  27. fetch("/thread/" + check_list[for_a][0] + '/comment/' + check_list[for_a][1] + '/delete', { method : 'POST' });
  28. }
  29. if(check_list.length > 0) {
  30. history.go(0);
  31. }
  32. }
  33. });
  34. }
  35. function opennamu_thread_blind() {
  36. let do_true = 0;
  37. for(let for_a = 0; for_a < document.getElementsByClassName("opennamu_blind_button").length; for_a++) {
  38. let id = document.getElementsByClassName("opennamu_blind_button")[for_a].id;
  39. id = id.replace(/^opennamu_blind_/, '');
  40. id = id.split('_');
  41. let checked = document.getElementsByClassName("opennamu_blind_button")[for_a].checked;
  42. if(checked) {
  43. fetch("/thread/" + id[0] + '/comment/' + id[1] + '/blind', { method : 'GET' });
  44. do_true = 1;
  45. }
  46. }
  47. if(do_true === 1) {
  48. history.go(0);
  49. }
  50. }
  51. function opennamu_get_thread_ui(user_id, date, data, code, color = '', blind = '', add_style = '', topic_num = '') {
  52. let color_b, class_b;
  53. if(blind === 'O') {
  54. color_b = data === '' ? 'opennamu_comment_blind' : 'opennamu_comment_blind_admin';
  55. class_b = 'opennamu_comment_blind_js opennamu_list_hidden';
  56. } else {
  57. color_b = 'opennamu_comment_blind_not';
  58. class_b = '';
  59. }
  60. let admin_check_box = ''
  61. if(topic_num != '') {
  62. admin_check_box = '<input type="checkbox" class="opennamu_blind_button" id="opennamu_blind_' + topic_num + '_' + code + '">';
  63. }
  64. return `
  65. <span class="` + class_b + `">
  66. <table class="opennamu_comment" style="` + add_style + `">
  67. <tr>
  68. <td class="opennamu_comment_color_` + color + `">
  69. ` + admin_check_box + `
  70. <a href="#thread_shortcut" id="` + code + `">#` + code + `</a>
  71. ` + user_id + `
  72. <span style="float: right;">` + date + `</span>
  73. </td>
  74. </tr>
  75. <tr>
  76. <td class="` + color_b + ` opennamu_comment_data_main" id="thread_` + code + `">
  77. ` + data + `
  78. </td>
  79. </tr>
  80. </table>
  81. <hr class="main_hr">
  82. </span>
  83. `;
  84. }
  85. function opennamu_get_new_thread(topic_num = "", thread_num = "") {
  86. let get_thread = setInterval(function() {
  87. if(!document.getElementById('opennamu_default_thread_render_' + thread_num)) {
  88. opennamu_get_thread(topic_num, "", thread_num);
  89. } else {
  90. opennamu_get_new_thread(topic_num, String(Number(thread_num) + 1));
  91. clearInterval(get_thread);
  92. }
  93. }, 3000);
  94. }
  95. function opennamu_get_thread(topic_num = "", do_type = "", thread_num = "") {
  96. let url, to_obj, color;
  97. if(do_type === "top") {
  98. url = "/api/thread/" + topic_num + "/top";
  99. to_obj = 'opennamu_top_thread';
  100. color = 'red';
  101. } else {
  102. if(thread_num === "") {
  103. url = "/api/thread/" + topic_num;
  104. } else {
  105. url = "/api/thread/" + topic_num + "/" + thread_num + "/" + thread_num;
  106. }
  107. to_obj = 'opennamu_main_thread';
  108. color = 'default';
  109. }
  110. fetch(url).then(function(res) {
  111. return res.json();
  112. }).then(function(data) {
  113. if(data["data"].length !== 0) {
  114. let end_data = '';
  115. let end_render = [];
  116. let lang = data["language"];
  117. data = data["data"];
  118. let first = '';
  119. for(let for_a = 0; for_a < data.length; for_a++) {
  120. if(first === '') {
  121. first = data[for_a]["ip"];
  122. }
  123. let real_color = color;
  124. if(color !== 'red') {
  125. if(data[for_a]["blind"] === '1') {
  126. real_color = 'blue';
  127. } else if(first === data[for_a]["ip"]) {
  128. real_color = 'green';
  129. } else {
  130. real_color = 'default';
  131. }
  132. }
  133. let date = '<a href="/thread/' + topic_num + '/comment/' + data[for_a]["id"] + '/tool">(' + lang["tool"] + ')</a> ' + data[for_a]["date"];
  134. let render_data = data[for_a]["data"] !== "" ? data[for_a]["data"] : "[br]";
  135. end_data += opennamu_get_thread_ui(
  136. data[for_a]["ip_render"],
  137. date,
  138. '<div class="opennamu_comment_scroll" id="opennamu_' + color + '_thread_render_' + data[for_a]["id"] + '">' + opennamu_xss_filter(render_data) + '</div>',
  139. data[for_a]["id"],
  140. real_color,
  141. data[for_a]["blind"],
  142. '',
  143. topic_num
  144. )
  145. end_render.push([
  146. render_data,
  147. data[for_a]["id"]
  148. ]);
  149. }
  150. if(do_type === "" && thread_num === "") {
  151. opennamu_get_new_thread(topic_num, String(Number(data[data.length - 1]["id"]) + 1));
  152. }
  153. document.getElementById(to_obj).innerHTML += end_data;
  154. for(let for_a = 0; for_a < end_render.length; for_a++) {
  155. let observer = new IntersectionObserver(entries => {
  156. entries.forEach(entry => {
  157. if(entry.isIntersecting) {
  158. opennamu_do_render(
  159. 'opennamu_' + color + '_thread_render_' + end_render[for_a][1],
  160. end_render[for_a][0],
  161. '',
  162. 'thread'
  163. );
  164. observer.unobserve(entry.target);
  165. }
  166. });
  167. });
  168. observer.observe(document.getElementById('opennamu_' + color + '_thread_render_' + end_render[for_a][1]));
  169. }
  170. }
  171. });
  172. }