Browse code

fix post_addcomment(), implement comments in the UI

Dario Rodriguez authored on 08/07/2014 21:35:57
Showing 3 changed files
... ...
@@ -93,12 +93,12 @@ div.centeredbutton {
93 93
 }
94 94
 
95 95
 div.post {
96
-        padding: 10px;
96
+        padding: 10px 10px 20px 10px;
97 97
         width: 800px;
98 98
         margin-left: auto;
99 99
         margin-right: auto;
100 100
         margin-top: 35px;
101
-        margin-bottom: 20px;
101
+        margin-bottom: 0px;
102 102
         background: #f4f4f4;
103 103
         -moz-box-shadow: 0px 0px 1px #aaa;
104 104
         -webkit-box-shadow: 0px 0px 1px #aaa;
... ...
@@ -130,7 +130,6 @@ p.authorpostnum, p.date {
130 130
 }
131 131
 
132 132
 span.author {
133
-/*        float: left;*/
134 133
         font-weight: bold;
135 134
 }
136 135
 
... ...
@@ -144,3 +143,61 @@ div.postbody {
144 143
         margin-right: 0px;
145 144
 }
146 145
 
146
+div.comments {
147
+        background: #eee;
148
+        margin: 0px;
149
+        padding: 0px 0px 0px 0px;
150
+        width: 820px;
151
+        margin-left: auto;
152
+        margin-right: auto;
153
+        border-style: solid none none none;
154
+        border-top: 1px solid #ddd;
155
+}
156
+
157
+div.comment {
158
+        border-style: none none solid none;
159
+        border-bottom: 1px solid #ddd;
160
+}
161
+
162
+p.commentbox {
163
+        margin: 0px;
164
+        padding: 0px;
165
+        width: 800px;
166
+        margin-left: auto;
167
+        margin-right: auto;
168
+}
169
+
170
+input.inputcomment {
171
+        width: 680px;
172
+        margin: 10px;
173
+        border-style: none;
174
+        color: #8080a5;
175
+}
176
+
177
+input.inputcomment:focus {
178
+        border-style: solid;
179
+        border-width: 1px;
180
+}
181
+
182
+input.inputsendcomment {
183
+        width: 70px;
184
+        margin: 10px;
185
+        border-style: solid;
186
+        border-width: 1px;
187
+        background: #eee;
188
+        padding: 0px;
189
+        color: #aaa;
190
+        -webkit-transition-duration: 0.2s;
191
+        -moz-transition-duration: 0.2s;
192
+        transition-duration: 0.2s;
193
+}
194
+
195
+input.inputsendcomment:hover, input.inputsendcomment:focus {
196
+        background: #fff;
197
+}
198
+
199
+input.inputsendcomment:active {
200
+        background: #aaa;
201
+        color: #eee;
202
+}
203
+
... ...
@@ -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) {
... ...
@@ -114,6 +114,7 @@ post_addcomment(kakumei *ka, int postnum, char *user, char *text)
114 114
         char filename[1024];
115 115
         char buf[16];
116 116
         FILE *f;
117
+        int off;
117 118
         if(ka==NULL || postnum<0 || user==NULL || text==NULL) {
118 119
                 log_write("POST","Sanity check error\n");
119 120
                 return(-1);
... ...
@@ -121,17 +122,25 @@ post_addcomment(kakumei *ka, int postnum, char *user, char *text)
121 122
         if(post_get(ka,postnum,filename,sizeof(filename))!=0)
122 123
                 return(-1);
123 124
         if((f=fopen(filename,"r+"))==NULL) {
124
-                log_write("POST","Couldn't open post file fot appending\n");
125
+                log_write("POST","Couldn't open post file for appending\n");
125 126
                 return(-1);
126 127
         }
127
-        fseek(f,-2,SEEK_END);
128
-        if(fread(buf,1,2,f)!=2 || buf[0]!=']' ||  buf[1]!='}') {
128
+        fseek(f,-5,SEEK_END);
129
+        if(fread(buf,1,5,f)!=5) {
130
+                fclose(f),f=NULL;
131
+                log_write("POST","Post file contents are too small\n");
132
+                return(-1);
133
+        }
134
+        off=(buf[4]=='\n')?1:0;
135
+        if(buf[4-off]!='}' || buf[3-off]!=']' || buf[2-off]!='\n') {
129 136
                 fclose(f),f=NULL;
130 137
                 log_write("POST","Post file contents are corrupted\n");
131 138
                 return(-1);
132 139
         }
133
-        fseek(f,-2,SEEK_END);
134
-        fprintf(f,"    { \"author\":\"%s\",\"date\":%li,\"text\":\"%s\"},\n]}",user,(long)time(NULL),text);
140
+        fseek(f,-4-off,SEEK_END);
141
+        fprintf(f,"%c%s\n    { \"author\":\"%s\",\"date\":%li,\"text\":\"%s\"}\n]}",
142
+          buf[1-off],(buf[1-off]!='[')?",":"",
143
+          user,(long)time(NULL),text);
135 144
         fclose(f),f=NULL;
136 145
         return(0);
137 146
 }