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