The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

171 lines
5.0KB

  1. /*
  2. * post-hover.js
  3. * https://github.com/savetheinternet/Tinyboard/blob/master/js/post-hover.js
  4. *
  5. * Released under the MIT license
  6. * Copyright (c) 2012 Michael Save <savetheinternet@tinyboard.org>
  7. * Copyright (c) 2013-2014 Marcin Łabanowski <marcin@6irc.net>
  8. * Copyright (c) 2013 Macil Tech <maciltech@gmail.com>
  9. *
  10. * Usage:
  11. * $config['additional_javascript'][] = 'js/jquery.min.js';
  12. * $config['additional_javascript'][] = 'js/post-hover.js';
  13. *
  14. */
  15. onready(function(){
  16. var dont_fetch_again = [];
  17. init_hover = function() {
  18. var $link = $(this);
  19. var id;
  20. var matches;
  21. if ($link.is('[data-thread]')) {
  22. id = $link.attr('data-thread');
  23. }
  24. else if(matches = $link.text().match(/^>>(?:>\/([^\/]+)\/)?(\d+)$/)) {
  25. id = matches[2];
  26. }
  27. else {
  28. return;
  29. }
  30. var board = $(this);
  31. while (board.data('board') === undefined) {
  32. board = board.parent();
  33. }
  34. var threadid;
  35. if ($link.is('[data-thread]')) threadid = 0;
  36. else threadid = board.attr('id').replace("thread_", "");
  37. board = board.data('board');
  38. var parentboard = board;
  39. if ($link.is('[data-thread]')) parentboard = $('form[name="post"] input[name="board"]').val();
  40. else if (matches[1] !== undefined) board = matches[1];
  41. var $post = false;
  42. var hovering = false;
  43. var hovered_at;
  44. $link.hover(function(e) {
  45. hovering = true;
  46. hovered_at = {'x': e.pageX, 'y': e.pageY};
  47. var start_hover = function($link) {
  48. if ($post.is(':visible') &&
  49. $post.offset().top >= $(window).scrollTop() &&
  50. $post.offset().top + $post.height() <= $(window).scrollTop() + $(window).height()) {
  51. // post is in view
  52. $post.addClass('highlighted');
  53. } else {
  54. var $newPost = $post.clone();
  55. $newPost.find('>.reply, >br').remove();
  56. $newPost.find('span.mentioned').remove();
  57. $newPost.find('a.post_anchor').remove();
  58. $newPost
  59. .attr('id', 'post-hover-' + id)
  60. .attr('data-board', board)
  61. .addClass('post-hover')
  62. .css('border-style', 'solid')
  63. .css('display', 'inline-block')
  64. .css('position', 'absolute')
  65. .css('font-style', 'normal')
  66. .css('z-index', '100')
  67. .css('margin-left', '1em')
  68. .addClass('reply').addClass('post')
  69. .insertAfter($link.parent())
  70. $link.trigger('mousemove');
  71. }
  72. };
  73. $post = $('[data-board="' + board + '"] div.post#reply_' + id + ', [data-board="' + board + '"]div#thread_' + id);
  74. if($post.length > 0) {
  75. start_hover($(this));
  76. } else {
  77. var url = $link.attr('href').replace(/#.*$/, '');
  78. if($.inArray(url, dont_fetch_again) != -1) {
  79. return;
  80. }
  81. dont_fetch_again.push(url);
  82. $.ajax({
  83. url: url,
  84. context: document.body,
  85. success: function(data) {
  86. var mythreadid = $(data).find('div[id^="thread_"]').attr('id').replace("thread_", "");
  87. if (mythreadid == threadid && parentboard == board) {
  88. $(data).find('div.post.reply').each(function() {
  89. if($('[data-board="' + board + '"] #' + $(this).attr('id')).length == 0) {
  90. $('[data-board="' + board + '"]#thread_' + threadid + " .post.reply:first").before($(this).hide().addClass('hidden'));
  91. }
  92. });
  93. }
  94. else if ($('[data-board="' + board + '"]#thread_'+mythreadid).length > 0) {
  95. $(data).find('div.post.reply').each(function() {
  96. if($('[data-board="' + board + '"] #' + $(this).attr('id')).length == 0) {
  97. $('[data-board="' + board + '"]#thread_' + mythreadid + " .post.reply:first").before($(this).hide().addClass('hidden'));
  98. }
  99. });
  100. }
  101. else {
  102. $(data).find('div[id^="thread_"]').hide().attr('data-cached', 'yes').prependTo('form[name="postcontrols"]');
  103. }
  104. $post = $('[data-board="' + board + '"] div.post#reply_' + id + ', [data-board="' + board + '"]div#thread_' + id);
  105. if(hovering && $post.length > 0) {
  106. start_hover($link);
  107. }
  108. }
  109. });
  110. }
  111. }, function() {
  112. hovering = false;
  113. if(!$post)
  114. return;
  115. $post.removeClass('highlighted');
  116. if($post.hasClass('hidden') || $post.data('cached') == 'yes')
  117. $post.css('display', 'none');
  118. $('.post-hover').remove();
  119. }).mousemove(function(e) {
  120. if(!$post)
  121. return;
  122. var $hover = $('#post-hover-' + id + '[data-board="' + board + '"]');
  123. if($hover.length == 0)
  124. return;
  125. var scrollTop = $(window).scrollTop();
  126. if ($link.is("[data-thread]")) scrollTop = 0;
  127. var epy = e.pageY;
  128. if ($link.is("[data-thread]")) epy -= $(window).scrollTop();
  129. var top = (epy ? epy : hovered_at['y']) - 10;
  130. if(epy < scrollTop + 15) {
  131. top = scrollTop;
  132. } else if(epy > scrollTop + $(window).height() - $hover.height() - 15) {
  133. top = scrollTop + $(window).height() - $hover.height() - 15;
  134. }
  135. $hover.css('left', (e.pageX ? e.pageX : hovered_at['x']) + 1).css('top', top);
  136. });
  137. };
  138. $('div.body a:not([rel="nofollow"])').each(init_hover);
  139. // allow to work with auto-reload.js, etc.
  140. $(document).on('new_post', function(e, post) {
  141. $(post).find('div.body a:not([rel="nofollow"])').each(init_hover);
  142. });
  143. });