... | ... |
@@ -17,8 +17,16 @@ function request(querytext, func, errorfunc) { |
17 | 17 |
req.send(); |
18 | 18 |
} |
19 | 19 |
|
20 |
+function getvar(name) { |
|
21 |
+ var re=new RegExp("[?&]"+name+"=\([^&]*\)"); |
|
22 |
+ var value=window.location.search.match(re); |
|
23 |
+ if(value===null || value.length<2) |
|
24 |
+ return(name+"="); |
|
25 |
+ return(name+"="+value[1]); |
|
26 |
+} |
|
27 |
+ |
|
20 | 28 |
gotonewpost.onclick = function(e) { |
21 |
- window.location="/newpost.html"+window.location.search |
|
29 |
+ window.location="/newpost.html?"+getvar("s"); |
|
22 | 30 |
}; |
23 | 31 |
|
24 | 32 |
document.getElementById("posts").innerHTML='Loading posts...'; |
... | ... |
@@ -55,15 +63,16 @@ function changepost(postnum,data) { |
55 | 63 |
for(var i in p.comments) { |
56 | 64 |
var c=p.comments[i]; |
57 | 65 |
comments.push('<div class="comment">'); |
58 |
- comments.push('<p class="commentheader">'); |
|
66 |
+ comments.push('<div class="commentheader">'); |
|
59 | 67 |
var commentdate=new Date(0); |
60 | 68 |
commentdate.setUTCSeconds(parseInt(c.date)); |
69 |
+ comments.push('<span class="commentauthor">'); |
|
61 | 70 |
comments.push(decodeURIComponent(c.author)); |
62 |
- comments.push(" "); |
|
71 |
+ comments.push('</span> <span class="commentdate">'); |
|
63 | 72 |
comments.push(commentdate.toString()); |
64 |
- comments.push('</p><p>'); |
|
73 |
+ comments.push('</span></div><div class="commenttext">'); |
|
65 | 74 |
comments.push(decodeURIComponent(c.text)); |
66 |
- comments.push('</p>'); |
|
75 |
+ comments.push('</div>'); |
|
67 | 76 |
comments.push('</div>'); |
68 | 77 |
} |
69 | 78 |
comments.push('<div class="commentbox"><p class="commentbox">'); |
... | ... |
@@ -73,8 +82,8 @@ function changepost(postnum,data) { |
73 | 82 |
postsarray[toppost-postnum]='<div class="post">'+header.join("")+body.join("")+'</div>'+comments.join("") /*+JSON.stringify(p) */; |
74 | 83 |
functionsarray[toppost-postnum]=function() { |
75 | 84 |
var requri=[]; |
76 |
- requri.push("/newcomment"); |
|
77 |
- requri.push(window.location.search); |
|
85 |
+ requri.push("/newcomment?"); |
|
86 |
+ requri.push(getvar("s")); |
|
78 | 87 |
requri.push("&n="+postnum); |
79 | 88 |
requri.push("&t="); |
80 | 89 |
requri.push(encodeURIComponent(document.getElementById("comment"+(toppost-postnum)).value)); |
... | ... |
@@ -107,7 +116,7 @@ function displayposts() { |
107 | 116 |
} |
108 | 117 |
|
109 | 118 |
function refreshpost(postnum) { |
110 |
- request("/getpost"+window.location.search+"&n="+postnum, function(r1) { |
|
119 |
+ request("/getpost?"+getvar("s")+"&n="+postnum, function(r1) { |
|
111 | 120 |
changepost(postnum,r1); |
112 | 121 |
displayposts(); |
113 | 122 |
},function() { |
... | ... |
@@ -115,12 +124,79 @@ function refreshpost(postnum) { |
115 | 124 |
}); |
116 | 125 |
} |
117 | 126 |
|
127 |
+function refreshprevnext() { |
|
128 |
+ var nav=[]; |
|
129 |
+ var curpage; |
|
130 |
+ var totalpages; |
|
131 |
+ var inc; |
|
132 |
+ var maxinc=(Number(lastpost)/postsperpage)/2; |
|
133 |
+ nav.push('<span class="paginas">Página:'); |
|
134 |
+ curpage=Math.floor(((Number(lastpost)-Number(toppost))/postsperpage))+1; |
|
135 |
+ totalpages=Math.floor((Number(lastpost)-1)/postsperpage)+1; |
|
136 |
+ for(var page=1;(Number(lastpost)-(page-1)*postsperpage)>0;page+=inc) { |
|
137 |
+ if(Number(toppost)<=(Number(lastpost)-(page-1)*postsperpage) && |
|
138 |
+ Number(toppost)>(Number(lastpost)-(page)*postsperpage)) { |
|
139 |
+ nav.push(" "+page); |
|
140 |
+ } else { |
|
141 |
+ nav.push(' <a href="'); |
|
142 |
+ nav.push('/posts.html?'); |
|
143 |
+ nav.push(getvar("s")); |
|
144 |
+ if(page>1) { |
|
145 |
+ nav.push('&p='); |
|
146 |
+ nav.push(Number(toppost)-(page-1)*postsperpage); |
|
147 |
+ } |
|
148 |
+ nav.push('" class="pagenum">'); |
|
149 |
+ nav.push(page); |
|
150 |
+ nav.push('</a>'); |
|
151 |
+ } |
|
152 |
+ if(Math.abs(curpage-page)<5) { |
|
153 |
+ inc=1; |
|
154 |
+ } else { |
|
155 |
+ inc=Math.floor((Math.abs(curpage-page)/totalpages)*maxinc); |
|
156 |
+ inc=(inc<=0)?1:inc; |
|
157 |
+ if(page<totalpages && (page+inc)>totalpages) |
|
158 |
+ inc=totalpages-page; |
|
159 |
+ } |
|
160 |
+ } |
|
161 |
+ nav.push('</span> '); |
|
162 |
+ nav.push('<span class="newerolder">'); |
|
163 |
+ if(toppost<lastpost) { |
|
164 |
+ nav.push(' <a href="'); |
|
165 |
+ nav.push('/posts.html?'); |
|
166 |
+ nav.push(getvar("s")); |
|
167 |
+ if((Number(toppost)+Number(postsperpage))<lastpost) { |
|
168 |
+ nav.push('&p='); |
|
169 |
+ nav.push(Number(toppost)+Number(postsperpage)); |
|
170 |
+ } |
|
171 |
+ nav.push('" class="pagenum">Posts más nuevos</a>'); |
|
172 |
+ } else |
|
173 |
+ nav.push("Posts más nuevos"); |
|
174 |
+ nav.push(" | "); |
|
175 |
+ if((Number(toppost)-Number(postsperpage))>0) { |
|
176 |
+ nav.push(' <a href="'); |
|
177 |
+ nav.push('/posts.html?'); |
|
178 |
+ nav.push(getvar("s")); |
|
179 |
+ nav.push('&p='); |
|
180 |
+ nav.push(Number(toppost)-Number(postsperpage)); |
|
181 |
+ nav.push('" class="pagenum">Posts más antiguos</a>'); |
|
182 |
+ } else |
|
183 |
+ nav.push('Posts más antiguos'); |
|
184 |
+ nav.push('</span>'); |
|
185 |
+ document.getElementById("prevnext").innerHTML=nav.join(""); |
|
186 |
+} |
|
118 | 187 |
|
119 |
-request("/lastpost"+window.location.search,function(r) { |
|
188 |
+request("/lastpost?"+getvar("s"),function(r) { |
|
189 |
+ var p; |
|
120 | 190 |
lastpost=r; |
121 |
- toppost=r; |
|
191 |
+ toppost=r |
|
192 |
+ p=getvar("p"); |
|
193 |
+ if(p.length>2) |
|
194 |
+ toppost=Number(p.slice(2)); |
|
195 |
+ if(toppost<0 || toppost>lastpost) |
|
196 |
+ toppost=lastpost; |
|
197 |
+ refreshprevnext(); |
|
122 | 198 |
for(var i=0;i<postsperpage;i++) |
123 |
- refreshpost(r-i); |
|
199 |
+ refreshpost(toppost-i); |
|
124 | 200 |
},function() { |
125 | 201 |
document.getElementById("posts").innerHTML='There are no posts'; |
126 | 202 |
}); |
... | ... |
@@ -24,6 +24,7 @@ gotonewpost.onclick = function(e) { |
24 | 24 |
document.getElementById("posts").innerHTML='Loading posts...'; |
25 | 25 |
|
26 | 26 |
var postsarray=[]; |
27 |
+var functionsarray=[]; |
|
27 | 28 |
var lastpost=-1; |
28 | 29 |
var toppost=-1; |
29 | 30 |
var postsperpage=5; |
... | ... |
@@ -34,6 +35,7 @@ function changepost(postnum,data) { |
34 | 35 |
postdate.setUTCSeconds(parseInt(p.date)); |
35 | 36 |
var header=[]; |
36 | 37 |
var body=[]; |
38 |
+ var comments=[]; |
|
37 | 39 |
header.push('<div class="postheader">'); |
38 | 40 |
header.push('<p class="title"><span class="title">'); |
39 | 41 |
header.push(decodeURIComponent(p.title)); |
... | ... |
@@ -49,11 +51,42 @@ function changepost(postnum,data) { |
49 | 51 |
body.push('<div class="postbody">'); |
50 | 52 |
body.push(decodeURIComponent(p.text)); |
51 | 53 |
body.push('</div>'); |
52 |
- postsarray[toppost-postnum]='<div class="post">'+header.join("")+body.join("")+'</div>' /*+JSON.stringify(p) */; |
|
54 |
+ comments.push('<div class="comments">'); |
|
55 |
+ for(var i in p.comments) { |
|
56 |
+ var c=p.comments[i]; |
|
57 |
+ comments.push('<div class="comment">'); |
|
58 |
+ comments.push('<p class="commentheader">'); |
|
59 |
+ var commentdate=new Date(0); |
|
60 |
+ commentdate.setUTCSeconds(parseInt(c.date)); |
|
61 |
+ comments.push(decodeURIComponent(c.author)); |
|
62 |
+ comments.push(" "); |
|
63 |
+ comments.push(commentdate.toString()); |
|
64 |
+ comments.push('</p><p>'); |
|
65 |
+ comments.push(decodeURIComponent(c.text)); |
|
66 |
+ comments.push('</p>'); |
|
67 |
+ comments.push('</div>'); |
|
68 |
+ } |
|
69 |
+ comments.push('<div class="commentbox"><p class="commentbox">'); |
|
70 |
+ comments.push('<input type="text" class="inputcomment" id="comment'+(toppost-postnum)+'" placeholder="Escribir comentario..."/>'); |
|
71 |
+ comments.push('<input type="button" class="inputsendcomment" id="commentbutton'+(toppost-postnum)+'" value="Enviar"/>'); |
|
72 |
+ comments.push('</p></div></div>'); |
|
73 |
+ postsarray[toppost-postnum]='<div class="post">'+header.join("")+body.join("")+'</div>'+comments.join("") /*+JSON.stringify(p) */; |
|
74 |
+ functionsarray[toppost-postnum]=function() { |
|
75 |
+ var requri=[]; |
|
76 |
+ requri.push("/newcomment"); |
|
77 |
+ requri.push(window.location.search); |
|
78 |
+ requri.push("&n="+postnum); |
|
79 |
+ requri.push("&t="); |
|
80 |
+ requri.push(encodeURIComponent(document.getElementById("comment"+(toppost-postnum)).value)); |
|
81 |
+ request(requri.join(""),function(r) { |
|
82 |
+ refreshpost(postnum); |
|
83 |
+ },function() {}); |
|
84 |
+ } |
|
53 | 85 |
} |
54 | 86 |
|
55 | 87 |
function clearpost(postnum) { |
56 | 88 |
postsarray[toppost-postnum]=undefined; |
89 |
+ functionsarray[toppost-postnum]=undefined; |
|
57 | 90 |
} |
58 | 91 |
|
59 | 92 |
function displayposts() { |
... | ... |
@@ -66,6 +99,11 @@ function displayposts() { |
66 | 99 |
if(contents.length==0) |
67 | 100 |
contents[0]='No posts loaded'; |
68 | 101 |
document.getElementById("posts").innerHTML=contents.join("\n"); |
102 |
+ for(var i=0;i<postsperpage;i++) { |
|
103 |
+ if(functionsarray[i]==undefined) |
|
104 |
+ continue; |
|
105 |
+ document.getElementById("commentbutton"+i).onclick=functionsarray[i]; |
|
106 |
+ } |
|
69 | 107 |
} |
70 | 108 |
|
71 | 109 |
function refreshpost(postnum) { |
... | ... |
@@ -32,11 +32,24 @@ function changepost(postnum,data) { |
32 | 32 |
var p=JSON.parse(data); |
33 | 33 |
var postdate=new Date(0); |
34 | 34 |
postdate.setUTCSeconds(parseInt(p.date)); |
35 |
- var header=""; |
|
36 |
- var body=""; |
|
37 |
- header='<div class="postheader"><p class="authorpostnum"> <span class="author">Autor: '+p.author+'</span><span class="postnum">#'+postnum+'</span></p><p class="date">Fecha: '+postdate.toString()+'</p></div>'; |
|
38 |
- body='<div class="postbody">'+decodeURIComponent(p.text)+'</div>'; |
|
39 |
- postsarray[toppost-postnum]='<div class="post">'+header+body+'</div>' /*+JSON.stringify(p) */; |
|
35 |
+ var header=[]; |
|
36 |
+ var body=[]; |
|
37 |
+ header.push('<div class="postheader">'); |
|
38 |
+ header.push('<p class="title"><span class="title">'); |
|
39 |
+ header.push(decodeURIComponent(p.title)); |
|
40 |
+ header.push('</span><span class="postnum">#'); |
|
41 |
+ header.push(postnum); |
|
42 |
+ header.push('</span></p>'); |
|
43 |
+ header.push('<p class="authorpostnum">Por <span class="author">'); |
|
44 |
+ header.push(p.author); |
|
45 |
+ header.push('</span> el <span class="date">'); |
|
46 |
+ header.push(postdate.toString()); |
|
47 |
+ header.push('</span></p>'); |
|
48 |
+ header.push('</div>') |
|
49 |
+ body.push('<div class="postbody">'); |
|
50 |
+ body.push(decodeURIComponent(p.text)); |
|
51 |
+ body.push('</div>'); |
|
52 |
+ postsarray[toppost-postnum]='<div class="post">'+header.join("")+body.join("")+'</div>' /*+JSON.stringify(p) */; |
|
40 | 53 |
} |
41 | 54 |
|
42 | 55 |
function clearpost(postnum) { |
... | ... |
@@ -2,35 +2,76 @@ |
2 | 2 |
"use strict"; |
3 | 3 |
|
4 | 4 |
function request(querytext, func, errorfunc) { |
5 |
- var req = new XMLHttpRequest(); |
|
6 |
- req.onreadystatechange = function() { |
|
7 |
- if (req.readyState !== 4) { |
|
8 |
- return; |
|
9 |
- } |
|
10 |
- if (req.status !== 200) { |
|
11 |
- errorfunc(); |
|
12 |
- return; |
|
13 |
- } |
|
14 |
- func(req.responseText); |
|
15 |
- }; |
|
16 |
- req.open("GET",querytext); |
|
17 |
- req.send(); |
|
5 |
+ var req = new XMLHttpRequest(); |
|
6 |
+ req.onreadystatechange = function() { |
|
7 |
+ if (req.readyState !== 4) { |
|
8 |
+ return; |
|
9 |
+ } |
|
10 |
+ if (req.status !== 200) { |
|
11 |
+ errorfunc(); |
|
12 |
+ return; |
|
13 |
+ } |
|
14 |
+ func(req.responseText); |
|
15 |
+ }; |
|
16 |
+ req.open("GET",querytext); |
|
17 |
+ req.send(); |
|
18 | 18 |
} |
19 | 19 |
|
20 | 20 |
gotonewpost.onclick = function(e) { |
21 |
- window.location="/newpost.html"+window.location.search |
|
21 |
+ window.location="/newpost.html"+window.location.search |
|
22 | 22 |
}; |
23 | 23 |
|
24 | 24 |
document.getElementById("posts").innerHTML='Loading posts...'; |
25 | 25 |
|
26 |
+var postsarray=[]; |
|
27 |
+var lastpost=-1; |
|
28 |
+var toppost=-1; |
|
29 |
+var postsperpage=5; |
|
30 |
+ |
|
31 |
+function changepost(postnum,data) { |
|
32 |
+ var p=JSON.parse(data); |
|
33 |
+ var postdate=new Date(0); |
|
34 |
+ postdate.setUTCSeconds(parseInt(p.date)); |
|
35 |
+ var header=""; |
|
36 |
+ var body=""; |
|
37 |
+ header='<div class="postheader"><p class="authorpostnum"> <span class="author">Autor: '+p.author+'</span><span class="postnum">#'+postnum+'</span></p><p class="date">Fecha: '+postdate.toString()+'</p></div>'; |
|
38 |
+ body='<div class="postbody">'+decodeURIComponent(p.text)+'</div>'; |
|
39 |
+ postsarray[toppost-postnum]='<div class="post">'+header+body+'</div>' /*+JSON.stringify(p) */; |
|
40 |
+} |
|
41 |
+ |
|
42 |
+function clearpost(postnum) { |
|
43 |
+ postsarray[toppost-postnum]=undefined; |
|
44 |
+} |
|
45 |
+ |
|
46 |
+function displayposts() { |
|
47 |
+ var contents=[] |
|
48 |
+ for(var i=0;i<postsperpage;i++) { |
|
49 |
+ if(postsarray[i]==undefined) |
|
50 |
+ continue; |
|
51 |
+ contents.push(postsarray[i]); |
|
52 |
+ } |
|
53 |
+ if(contents.length==0) |
|
54 |
+ contents[0]='No posts loaded'; |
|
55 |
+ document.getElementById("posts").innerHTML=contents.join("\n"); |
|
56 |
+} |
|
57 |
+ |
|
58 |
+function refreshpost(postnum) { |
|
59 |
+ request("/getpost"+window.location.search+"&n="+postnum, function(r1) { |
|
60 |
+ changepost(postnum,r1); |
|
61 |
+ displayposts(); |
|
62 |
+ },function() { |
|
63 |
+ clearpost(postnum); |
|
64 |
+ }); |
|
65 |
+} |
|
66 |
+ |
|
67 |
+ |
|
26 | 68 |
request("/lastpost"+window.location.search,function(r) { |
27 |
- request("/getpost"+window.location.search+"&n="+r, function(r1) { |
|
28 |
- document.getElementById("posts").innerHTML=r1; |
|
29 |
- },function() { |
|
30 |
- document.getElementById("posts").innerHTML='Error getting posts'; |
|
31 |
- }); |
|
69 |
+ lastpost=r; |
|
70 |
+ toppost=r; |
|
71 |
+ for(var i=0;i<postsperpage;i++) |
|
72 |
+ refreshpost(r-i); |
|
32 | 73 |
},function() { |
33 |
- document.getElementById("posts").innerHTML='There are no posts'; |
|
74 |
+ document.getElementById("posts").innerHTML='There are no posts'; |
|
34 | 75 |
}); |
35 | 76 |
|
36 | 77 |
}()); |
... | ... |
@@ -24,11 +24,11 @@ gotonewpost.onclick = function(e) { |
24 | 24 |
document.getElementById("posts").innerHTML='Loading posts...'; |
25 | 25 |
|
26 | 26 |
request("/lastpost"+window.location.search,function(r) { |
27 |
- request("/getpost"+window.location.search+"n="+r, function(r1) { |
|
27 |
+ request("/getpost"+window.location.search+"&n="+r, function(r1) { |
|
28 | 28 |
document.getElementById("posts").innerHTML=r1; |
29 | 29 |
},function() { |
30 | 30 |
document.getElementById("posts").innerHTML='Error getting posts'; |
31 |
- } |
|
31 |
+ }); |
|
32 | 32 |
},function() { |
33 | 33 |
document.getElementById("posts").innerHTML='There are no posts'; |
34 | 34 |
}); |
... | ... |
@@ -21,4 +21,16 @@ gotonewpost.onclick = function(e) { |
21 | 21 |
window.location="/newpost.html"+window.location.search |
22 | 22 |
}; |
23 | 23 |
|
24 |
+document.getElementById("posts").innerHTML='Loading posts...'; |
|
25 |
+ |
|
26 |
+request("/lastpost"+window.location.search,function(r) { |
|
27 |
+ request("/getpost"+window.location.search+"n="+r, function(r1) { |
|
28 |
+ document.getElementById("posts").innerHTML=r1; |
|
29 |
+ },function() { |
|
30 |
+ document.getElementById("posts").innerHTML='Error getting posts'; |
|
31 |
+ } |
|
32 |
+},function() { |
|
33 |
+ document.getElementById("posts").innerHTML='There are no posts'; |
|
34 |
+}); |
|
35 |
+ |
|
24 | 36 |
}()); |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,24 @@ |
1 |
+(function () { |
|
2 |
+"use strict"; |
|
3 |
+ |
|
4 |
+function request(querytext, func, errorfunc) { |
|
5 |
+ var req = new XMLHttpRequest(); |
|
6 |
+ req.onreadystatechange = function() { |
|
7 |
+ if (req.readyState !== 4) { |
|
8 |
+ return; |
|
9 |
+ } |
|
10 |
+ if (req.status !== 200) { |
|
11 |
+ errorfunc(); |
|
12 |
+ return; |
|
13 |
+ } |
|
14 |
+ func(req.responseText); |
|
15 |
+ }; |
|
16 |
+ req.open("GET",querytext); |
|
17 |
+ req.send(); |
|
18 |
+} |
|
19 |
+ |
|
20 |
+gotonewpost.onclick = function(e) { |
|
21 |
+ window.location="/newpost.html"+window.location.search |
|
22 |
+}; |
|
23 |
+ |
|
24 |
+}()); |