##// END OF EJS Templates
Show notifications directly above the reflink unrelated to the cursor position
neko259 -
r1456:35599bce default
parent child Browse files
Show More
@@ -1,119 +1,120 b''
1 function $X(path, root) {
1 function $X(path, root) {
2 return document.evaluate(path, root || document, null, 6, null);
2 return document.evaluate(path, root || document, null, 6, null);
3 }
3 }
4 function $x(path, root) {
4 function $x(path, root) {
5 return document.evaluate(path, root || document, null, 8, null).singleNodeValue;
5 return document.evaluate(path, root || document, null, 8, null).singleNodeValue;
6 }
6 }
7
7
8 function $del(el) {
8 function $del(el) {
9 if(el) el.parentNode.removeChild(el);
9 if(el) el.parentNode.removeChild(el);
10 }
10 }
11
11
12 function $each(list, fn) {
12 function $each(list, fn) {
13 if(!list) return;
13 if(!list) return;
14 var i = list.snapshotLength;
14 var i = list.snapshotLength;
15 if(i > 0) while(i--) fn(list.snapshotItem(i), i);
15 if(i > 0) while(i--) fn(list.snapshotItem(i), i);
16 }
16 }
17
17
18 function mkPreview(cln, html) {
18 function mkPreview(cln, html) {
19 cln.innerHTML = html;
19 cln.innerHTML = html;
20
20
21 addScriptsToPost($(cln));
21 addScriptsToPost($(cln));
22 }
22 }
23
23
24 function isElementInViewport (el) {
24 function isElementInViewport (el) {
25 //special bonus for those using jQuery
25 //special bonus for those using jQuery
26 if (typeof jQuery === "function" && el instanceof jQuery) {
26 if (typeof jQuery === "function" && el instanceof jQuery) {
27 el = el[0];
27 el = el[0];
28 }
28 }
29
29
30 var rect = el.getBoundingClientRect();
30 var rect = el.getBoundingClientRect();
31
31
32 return (
32 return (
33 rect.top >= 0 &&
33 rect.top >= 0 &&
34 rect.left >= 0 &&
34 rect.left >= 0 &&
35 rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
35 rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) && /*or $(window).height() */
36 rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
36 rect.right <= (window.innerWidth || document.documentElement.clientWidth) /*or $(window).width() */
37 );
37 );
38 }
38 }
39
39
40 function addRefLinkPreview(node) {
40 function addRefLinkPreview(node) {
41 $each($X('.//a[starts-with(text(),">>")]', node || document), function(link) {
41 $each($X('.//a[starts-with(text(),">>")]', node || document), function(link) {
42 link.addEventListener('mouseover', showPostPreview, false);
42 link.addEventListener('mouseover', showPostPreview, false);
43 link.addEventListener('mouseout', delPostPreview, false);
43 link.addEventListener('mouseout', delPostPreview, false);
44 });
44 });
45 }
45 }
46
46
47 function showPostPreview(e) {
47 function showPostPreview(e) {
48 var doc = document;
48 var doc = document;
49 //ref id
49
50 var pNum = $(this).text().match(/\d+/);
50 var reflink = $(this);
51 var pNum = reflink.text().match(/\d+/);
51
52
52 if (pNum == null || pNum.length == 0) {
53 if (pNum == null || pNum.length == 0) {
53 return;
54 return;
54 }
55 }
55
56
56 var post = $('#' + pNum);
57 var post = $('#' + pNum);
57 if (post.length > 0 && isElementInViewport(post)) {
58 if (post.length > 0 && isElementInViewport(post)) {
58 post.addClass('highlight');
59 post.addClass('highlight');
59 } else {
60 } else {
60 var x = e.clientX + (doc.documentElement.scrollLeft || doc.body.scrollLeft) + 2;
61 var x = reflink.offset().left;
61 var y = e.clientY + (doc.documentElement.scrollTop || doc.body.scrollTop);
62 var y = reflink.offset().top;
62
63
63 var cln = doc.createElement('div');
64 var cln = doc.createElement('div');
64 cln.id = 'pstprev_' + pNum;
65 cln.id = 'pstprev_' + pNum;
65 cln.className = 'post_preview';
66 cln.className = 'post_preview';
66
67
67 cln.style.cssText = 'top:' + y + 'px;' + (x < doc.body.clientWidth/2 ? 'left:' + x + 'px' : 'right:' + parseInt(doc.body.clientWidth - x + 1) + 'px');
68 cln.style.cssText = 'top:' + y + 'px;' + (x < doc.body.clientWidth/2 ? 'left:' + x + 'px' : 'right:' + parseInt(doc.body.clientWidth - x + 1) + 'px');
68
69
69 cln.addEventListener('mouseout', delPostPreview, false);
70 cln.addEventListener('mouseout', delPostPreview, false);
70
71
71 cln.innerHTML = "<div class=\"post\">" + gettext('Loading...') + "</div>";
72 cln.innerHTML = "<div class=\"post\">" + gettext('Loading...') + "</div>";
72
73
73 if(post.length > 0) {
74 if(post.length > 0) {
74 var postdata = post.clone().wrap("<div/>").parent().html();
75 var postdata = post.clone().wrap("<div/>").parent().html();
75
76
76 mkPreview(cln, postdata);
77 mkPreview(cln, postdata);
77 } else {
78 } else {
78 $.ajax({
79 $.ajax({
79 url: '/api/post/' + pNum + '/?truncated'
80 url: '/api/post/' + pNum + '/?truncated'
80 })
81 })
81 .success(function(data) {
82 .success(function(data) {
82 var postdata = $(data).wrap("<div/>").parent().html();
83 var postdata = $(data).wrap("<div/>").parent().html();
83
84
84 //make preview
85 //make preview
85 mkPreview(cln, postdata);
86 mkPreview(cln, postdata);
86
87
87 })
88 })
88 .error(function() {
89 .error(function() {
89 cln.innerHTML = "<div class=\"post\">"
90 cln.innerHTML = "<div class=\"post\">"
90 + gettext('Post not found') + "</div>";
91 + gettext('Post not found') + "</div>";
91 });
92 });
92 }
93 }
93
94
94 $del(doc.getElementById(cln.id));
95 $del(doc.getElementById(cln.id));
95
96
96 //add preview
97 //add preview
97 $(cln).fadeIn(200);
98 $(cln).fadeIn(200);
98 $('body').append(cln);
99 $('body').append(cln);
99 }
100 }
100 }
101 }
101
102
102 function delPostPreview(e) {
103 function delPostPreview(e) {
103 var el = $x('ancestor-or-self::*[starts-with(@id,"pstprev")]', e.relatedTarget);
104 var el = $x('ancestor-or-self::*[starts-with(@id,"pstprev")]', e.relatedTarget);
104 if(!el) {
105 if(!el) {
105 $each($X('.//div[starts-with(@id,"pstprev")]'), function(clone) {
106 $each($X('.//div[starts-with(@id,"pstprev")]'), function(clone) {
106 $del(clone)
107 $del(clone)
107 });
108 });
108 } else {
109 } else {
109 while(el.nextSibling) $del(el.nextSibling);
110 while(el.nextSibling) $del(el.nextSibling);
110 }
111 }
111
112
112 $('.highlight').removeClass('highlight');
113 $('.highlight').removeClass('highlight');
113 }
114 }
114
115
115 function addPreview() {
116 function addPreview() {
116 $('.post').find('a').each(function() {
117 $('.post').find('a').each(function() {
117 showPostPreview($(this));
118 showPostPreview($(this));
118 });
119 });
119 }
120 }
General Comments 0
You need to be logged in to leave comments. Login now