From 3d00a7bdecae8d2ef377fb52c5ee8063e28a1484 Mon Sep 17 00:00:00 2001 From: Michael Save Date: Mon, 30 Apr 2012 02:59:11 +1000 Subject: [PATCH] js/post-hover.js and js/show-backlinks.js to work together --- js/post-hover.js | 64 +++++++++++++++++++++++--------------------- js/show-backlinks.js | 34 +++++++++++++++-------- 2 files changed, 56 insertions(+), 42 deletions(-) diff --git a/js/post-hover.js b/js/post-hover.js index 2d22ccf0..f358f7ff 100644 --- a/js/post-hover.js +++ b/js/post-hover.js @@ -11,49 +11,51 @@ * */ -$(document).ready(function(){ +onready(function(){ var dont_fetch_again = []; - var init_hover = function() { - var link = $(this); + init_hover = function() { + var $link = $(this); var id; - if(id = $(link).text().match(/^>>(\d+)$/)) { + if(id = $link.text().match(/^>>(\d+)$/)) { id = id[1]; } - var post = false; + var $post = false; var hovering = false; var hovered_at; - $(link).hover(function(e) { + $link.hover(function(e) { hovering = true; hovered_at = {'x': e.pageX, 'y': e.pageY}; - var start_hover = function(link) { - if(post.is(':visible') && - post.offset().top + post.height() >= $(window).scrollTop() && - post.offset().top <= $(window).scrollTop() + $(window).height() + var start_hover = function($link) { + if($post.is(':visible') && + $post.offset().top + $post.height() >= $(window).scrollTop() && + $post.offset().top <= $(window).scrollTop() + $(window).height() ) { // post is in view - post.attr('style', 'border-style: none dashed dashed none; background: ' + post.css('border-right-color')); + $post.attr('style', 'border-style: none dashed dashed none; background: ' + $post.css('border-right-color')); } else { - post.clone() + var $newPost = $post.clone(); + $newPost.find('span.mentioned').remove(); + $newPost .attr('id', 'post-hover-' + id) .addClass('post-hover') .css('position', 'absolute') .css('border-style', 'solid') .css('box-shadow', '1px 1px 1px #999') .css('display', 'block') - .insertAfter($(link).parent()); - $(link).trigger('mousemove'); + .insertAfter($link.parent()); + $link.trigger('mousemove'); } }; - post = $('div.post#reply_' + id); - if(post.length > 0) { - start_hover(this); + $post = $('div.post#reply_' + id); + if($post.length > 0) { + start_hover($(this)); } else { - var url = link.attr('href').replace(/#.*$/, ''); + var url = $link.attr('href').replace(/#.*$/, ''); if($.inArray(url, dont_fetch_again) != -1) { return; @@ -70,40 +72,40 @@ $(document).ready(function(){ }); - post = $('div.post#reply_' + id); - if(hovering && post.length > 0) { - start_hover(link); + $post = $('div.post#reply_' + id); + if(hovering && $post.length > 0) { + start_hover($link); } } }); } }, function() { hovering = false; - if(!post) + if(!$post) return; - post.attr('style', ''); - if(post.hasClass('hidden')) - post.css('display', 'none'); + $post.attr('style', ''); + if($post.hasClass('hidden')) + $post.css('display', 'none'); $('.post-hover').remove(); }).mousemove(function(e) { - if(!post) + if(!$post) return; - var hover = $('#post-hover-' + id); - if(hover.length == 0) + var $hover = $('#post-hover-' + id); + if($hover.length == 0) return; var top = (e.pageY ? e.pageY : hovered_at['y']) - 10; if(e.pageY < $(window).scrollTop() + 15) { top = $(window).scrollTop(); - } else if(e.pageY > $(window).scrollTop() + $(window).height() - hover.height() - 15) { - top = $(window).scrollTop() + $(window).height() - hover.height() - 15; + } else if(e.pageY > $(window).scrollTop() + $(window).height() - $hover.height() - 15) { + top = $(window).scrollTop() + $(window).height() - $hover.height() - 15; } - hover.css('left', (e.pageX ? e.pageX : hovered_at['x'])).css('top', top); + $hover.css('left', (e.pageX ? e.pageX : hovered_at['x'])).css('top', top); }); }; diff --git a/js/show-backlinks.js b/js/show-backlinks.js index c3659d09..ecf20482 100644 --- a/js/show-backlinks.js +++ b/js/show-backlinks.js @@ -7,32 +7,44 @@ * * Usage: * $config['additional_javascript'][] = 'js/jquery.min.js'; + * // $config['additional_javascript'][] = 'js/post-hover'; (optional; must come first) * $config['additional_javascript'][] = 'js/show-backlinks.js'; * */ -$(document).ready(function(){ - $('div.post.reply').each(function() { +onready(function(){ + var showBackLinks = function() { var reply_id = $(this).attr('id').replace(/^reply_/, ''); $(this).find('p.body a:not([rel="nofollow"])').each(function() { - var id, post, mentioned; + var id, post, $mentioned; if(id = $(this).text().match(/^>>(\d+)$/)) id = id[1]; else return; - post = $('#reply_' + id); - if(post.length == 0) + $post = $('#reply_' + id); + if($post.length == 0) return; - mentioned = post.find('p.intro span.mentioned'); - if(mentioned.length == 0) - mentioned = $('').appendTo(post.find('p.intro')); - - mentioned.append('>>' + reply_id + ''); + $mentioned = $post.find('p.intro span.mentioned'); + if($mentioned.length == 0) + $mentioned = $('').appendTo($post.find('p.intro')); + + if ($mentioned.find('a.mentioned-' + reply_id).length != 0) + return; + + var $link = $('>>' + + reply_id + ''); + $link.appendTo($mentioned) + + if (window.init_hover) { + $link.each(init_hover); + } }); - }); + }; + + $('div.post.reply').each(showBackLinks); });