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.

179 lines
5.2KB

  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($.contains($post[0], $link[0])) {
  49. // link links to itself or to op; ignore
  50. }
  51. else if ($post.is(':visible') &&
  52. $post.offset().top >= $(window).scrollTop() &&
  53. $post.offset().top + $post.height() <= $(window).scrollTop() + $(window).height()) {
  54. // post is in view
  55. $post.addClass('highlighted');
  56. } else {
  57. var $newPost = $post.clone();
  58. $newPost.find('>.reply, >br').remove();
  59. //$newPost.find('span.mentioned').remove();
  60. $newPost.find('a.post_anchor').remove();
  61. $newPost
  62. .attr('id', 'post-hover-' + id)
  63. .attr('data-board', board)
  64. .addClass('post-hover')
  65. .css('border-style', 'solid')
  66. .css('display', 'inline-block')
  67. .css('position', 'absolute')
  68. .css('font-style', 'normal')
  69. .css('z-index', '100')
  70. .css('margin-left', '1em')
  71. .addClass('reply').addClass('post')
  72. .insertAfter($link.parent())
  73. $link.trigger('mousemove');
  74. }
  75. };
  76. $post = $('[data-board="' + board + '"] div.post#reply_' + id + ', [data-board="' + board + '"]div#thread_' + id);
  77. if($post.length > 0) {
  78. start_hover($(this));
  79. } else {
  80. var url = $link.attr('href').replace(/#.*$/, '');
  81. if($.inArray(url, dont_fetch_again) != -1) {
  82. return;
  83. }
  84. dont_fetch_again.push(url);
  85. $.ajax({
  86. url: url,
  87. context: document.body,
  88. success: function(data) {
  89. var mythreadid = $(data).find('div[id^="thread_"]').attr('id').replace("thread_", "");
  90. if (mythreadid == threadid && parentboard == board) {
  91. $(data).find('div.post.reply').each(function() {
  92. if($('[data-board="' + board + '"] #' + $(this).attr('id')).length == 0) {
  93. $('[data-board="' + board + '"]#thread_' + threadid + " .post.reply:first").before($(this).hide().addClass('hidden'));
  94. }
  95. });
  96. }
  97. else if ($('[data-board="' + board + '"]#thread_'+mythreadid).length > 0) {
  98. $(data).find('div.post.reply').each(function() {
  99. if($('[data-board="' + board + '"] #' + $(this).attr('id')).length == 0) {
  100. $('[data-board="' + board + '"]#thread_' + mythreadid + " .post.reply:first").before($(this).hide().addClass('hidden'));
  101. }
  102. });
  103. }
  104. else {
  105. $(data).find('div[id^="thread_"]').hide().attr('data-cached', 'yes').prependTo('form[name="postcontrols"]');
  106. }
  107. $post = $('[data-board="' + board + '"] div.post#reply_' + id + ', [data-board="' + board + '"]div#thread_' + id);
  108. if(hovering && $post.length > 0) {
  109. start_hover($link);
  110. }
  111. }
  112. });
  113. }
  114. }, function() {
  115. hovering = false;
  116. if(!$post)
  117. return;
  118. $post.removeClass('highlighted');
  119. if($post.hasClass('hidden') || $post.data('cached') == 'yes')
  120. $post.css('display', 'none');
  121. $('.post-hover').remove();
  122. }).mousemove(function(e) {
  123. if(!$post)
  124. return;
  125. var $hover = $('#post-hover-' + id + '[data-board="' + board + '"]');
  126. if($hover.length == 0)
  127. return;
  128. var scrollTop = $(window).scrollTop();
  129. if ($link.is("[data-thread]")) scrollTop = 0;
  130. var epy = e.pageY;
  131. if ($link.is("[data-thread]")) epy -= $(window).scrollTop();
  132. var top = (epy ? epy : hovered_at['y']) - 10;
  133. if(epy < scrollTop + 15) {
  134. top = scrollTop;
  135. } else if(epy > scrollTop + $(window).height() - $hover.height() - 15) {
  136. top = scrollTop + $(window).height() - $hover.height() - 15;
  137. }
  138. var hovery = e.pageY ? e.pageY : hovered_at['y'];
  139. if ( ( hovery - top) > 20){
  140. top = hovery;
  141. }
  142. $hover.css('left', (e.pageX ? e.pageX : hovered_at['x']) + 1).css('top', top);
  143. });
  144. };
  145. $('div.body a:not([rel="nofollow"])').each(init_hover);
  146. // allow to work with auto-reload.js, etc.
  147. $(document).on('new_post', function(e, post) {
  148. $(post).find('div.body a:not([rel="nofollow"])').each(init_hover);
  149. });
  150. });