The version of vichan running on lainchan.org
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

115 lines
3.8KB

  1. /*
  2. * thread-stats.js
  3. * - Adds statistics of the thread below the posts area
  4. * - Shows ID post count beside each postID on hover
  5. *
  6. * Usage:
  7. * $config['additional_javascript'][] = 'js/jquery.min.js';
  8. * $config['additional_javascript'][] = 'js/thread-stats.js';
  9. */
  10. if (active_page == 'thread') {
  11. $(document).ready(function(){
  12. //check if page uses unique ID
  13. var IDsupport = ($('.poster_id').length > 0);
  14. var thread_id = (document.location.pathname + document.location.search).split('/');
  15. thread_id = thread_id[thread_id.length -1].split('+')[0].split('.')[0];
  16. $('form[name="postcontrols"] > .delete')
  17. .first()
  18. .before('<div id="thread_stats"></div>');
  19. var el = $('#thread_stats');
  20. el.prepend('Page <span id="thread_stats_page">?</span>');
  21. if (IDsupport){
  22. el.prepend('<span id="thread_stats_uids">0</span> UIDs |&nbsp;');
  23. }
  24. el.prepend('<span id="thread_stats_images">0</span> images |&nbsp;');
  25. el.prepend('<span id="thread_stats_posts">0</span> replies |&nbsp;');
  26. delete el;
  27. function update_thread_stats(){
  28. var op = $('#thread_'+ thread_id +' > div.post.op:not(.post-hover):not(.inline)').first();
  29. var replies = $('#thread_'+ thread_id +' > div.post.reply:not(.post-hover):not(.inline)');
  30. // post count
  31. $('#thread_stats_posts').text(replies.length);
  32. // image count
  33. $('#thread_stats_images').text(replies.filter(function(){
  34. return $(this).find('> .files').text().trim() != false;
  35. }).length);
  36. // unique ID count
  37. if (IDsupport) {
  38. var opID = op.find('> .intro > .poster_id').text();
  39. var ids = {};
  40. replies.each(function(){
  41. var cur = $(this).find('> .intro > .poster_id');
  42. var curID = cur.text();
  43. if (ids[curID] === undefined) {
  44. ids[curID] = 0;
  45. }
  46. ids[curID]++;
  47. });
  48. if (ids[opID] === undefined) {
  49. ids[opID] = 0;
  50. }
  51. ids[opID]++;
  52. var cur = op.find('>.intro >.poster_id');
  53. cur.find('+.posts_by_id').remove();
  54. cur.after('<span class="posts_by_id"> ('+ ids[cur.text()] +')</span>');
  55. replies.each(function(){
  56. cur = $(this).find('>.intro >.poster_id');
  57. cur.find('+.posts_by_id').remove();
  58. cur.after('<span class="posts_by_id"> ('+ ids[cur.text()] +')</span>');
  59. });
  60. var size = function(obj) {
  61. var size = 0, key;
  62. for (key in obj) {
  63. if (obj.hasOwnProperty(key)) size++;
  64. }
  65. return size;
  66. };
  67. $('#thread_stats_uids').text(size(ids));
  68. }
  69. $.getJSON('//'+ document.location.host +'/'+ board_name +'/threads.json').success(function(data){
  70. var found, page = '???';
  71. for (var i=0;data[i];i++){
  72. var threads = data[i].threads;
  73. for (var j=0; threads[j]; j++){
  74. if (parseInt(threads[j].no) == parseInt(thread_id)) {
  75. page = data[i].page +1;
  76. found = true;
  77. break;
  78. }
  79. }
  80. if (found) break;
  81. }
  82. $('#thread_stats_page').text(page);
  83. if (!found) $('#thread_stats_page').css('color','red');
  84. else $('#thread_stats_page').css('color','');
  85. });
  86. }
  87. // load the current page the thread is on.
  88. // uses ajax call so it gets loaded on a delay (depending on network resources available)
  89. var thread_stats_page_timer = setInterval(function(){
  90. $.getJSON('//'+ document.location.host +'/'+ board_name +'/threads.json').success(function(data){
  91. var found, page = '???';
  92. for (var i=0;data[i];i++){
  93. var threads = data[i].threads;
  94. for (var j=0; threads[j]; j++){
  95. if (parseInt(threads[j].no) == parseInt(thread_id)) {
  96. page = data[i].page +1;
  97. found = true;
  98. break;
  99. }
  100. }
  101. if (found) break;
  102. }
  103. $('#thread_stats_page').text(page);
  104. if (!found) $('#thread_stats_page').css('color','red');
  105. else $('#thread_stats_page').css('color','');
  106. });
  107. },30000);
  108. $('body').append('<style>.posts_by_id{display:none;}.poster_id:hover+.posts_by_id{display:initial}</style>');
  109. update_thread_stats();
  110. $('#update_thread').click(update_thread_stats);
  111. $(document).on('new_post',update_thread_stats);
  112. });
  113. }