##// END OF EJS Templates
Fixed reply links on page that trigger ajax
neko259 -
r355:d660dc4d default
parent child Browse files
Show More
@@ -1,104 +1,108 b''
1 1 function $X(path, root) {
2 2 return document.evaluate(path, root || document, null, 6, null);
3 3 }
4 4 function $x(path, root) {
5 5 return document.evaluate(path, root || document, null, 8, null).singleNodeValue;
6 6 }
7 7
8 8 function $del(el) {
9 9 if(el) el.parentNode.removeChild(el);
10 10 }
11 11
12 12 function $each(list, fn) {
13 13 if(!list) return;
14 14 var i = list.snapshotLength;
15 15 if(i > 0) while(i--) fn(list.snapshotItem(i), i);
16 16 }
17 17
18 18 function addRefLinkPreview(node) {
19 19 $each($X('.//a[starts-with(text(),">>")]', node || document), function(link) {
20 20 link.addEventListener('mouseover', showPostPreview, false);
21 21 link.addEventListener('mouseout', delPostPreview, false);
22 22 });
23 23 }
24 24
25 25 function showPostPreview(e) {
26 26 var doc = document;
27 27 //ref id
28 28 var pNum = $(this).text().match(/\d+/);
29 29
30 if (pNum.length == 0) {
31 return;
32 }
33
30 34 //position
31 35 //var x = e.clientX + (doc.documentElement.scrollLeft || doc.body.scrollLeft) - doc.documentElement.clientLeft + 1;
32 36 //var y = e.clientY + (doc.documentElement.scrollTop || doc.body.scrollTop) - doc.documentElement.clientTop;
33 37
34 38 var x = e.clientX + (doc.documentElement.scrollLeft || doc.body.scrollLeft) + 2;
35 39 var y = e.clientY + (doc.documentElement.scrollTop || doc.body.scrollTop);
36 40
37 41 var cln = doc.createElement('div');
38 42 cln.id = 'pstprev_' + pNum;
39 43 cln.className = 'post_preview post';
40 44
41 45 cln.style.cssText = 'top:' + y + 'px;' + (x < doc.body.clientWidth/2 ? 'left:' + x + 'px' : 'right:' + parseInt(doc.body.clientWidth - x + 1) + 'px');
42 46
43 47 cln.addEventListener('mouseout', delPostPreview, false);
44 48
45 49
46 50 var mkPreview = function(cln, html) {
47 51
48 52 cln.innerHTML = html;
49 53
50 54 addRefLinkPreview(cln);
51 55
52 56 //if(!$x('.//small', cln)) showRefMap(post, p_num, refMap)
53 57 };
54 58
55 59
56 60 cln.innerHTML = 'Загрузка...';
57 61
58 62 //если пост найден в дереве.
59 63 if($('div[id='+pNum+']').length > 0) {
60 64 var postdata = $('div[id='+pNum+']').html();
61 65
62 66 //TODO: временно
63 67 //funcInit(postdata);
64 68
65 69 //make preview
66 70 mkPreview(cln, postdata);
67 71 }
68 72 //ajax api
69 73 else {
70 74 $.ajax({
71 75 url: '/api/post/' + pNum
72 76 })
73 77 .success(function(data) {
74 78 // TODO get a json, not post itself
75 79 var postdata = $(data).html();
76 80
77 81 //make preview
78 82 mkPreview(cln, postdata);
79 83
80 84 })
81 85 .error(function() {
82 86 cln.innerHTML = 'Пост не найден.';
83 87 });
84 88 }
85 89
86 90 $del(doc.getElementById(cln.id));
87 91
88 92 //add preview
89 93 $('body').append(cln);
90 94 }
91 95
92 96 function delPostPreview(e) {
93 97 var el = $x('ancestor-or-self::*[starts-with(@id,"pstprev")]', e.relatedTarget);
94 98 if(!el) $each($X('.//div[starts-with(@id,"pstprev")]'), function(clone) {
95 99 $del(clone)
96 100 });
97 101 else while(el.nextSibling) $del(el.nextSibling);
98 102 }
99 103
100 104 function addPreview() {
101 105 $('.post').find('a').each(function() {
102 106 showPostPreview($(this));
103 107 });
104 108 }
General Comments 0
You need to be logged in to leave comments. Login now