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