Browse code

Add titles to the posts

Dario Rodriguez authored on 08/07/2014 11:34:03
Showing 8 changed files
... ...
@@ -1,30 +1,65 @@
1 1
 
2
+
3
+span.title {
4
+        color: #777;
5
+        font-size: 20px;
6
+        padding: 4px 7px;
7
+}
8
+
2 9
 input.rounded {
3
-	border: 1px solid #ccc;
4
-	-moz-border-radius: 10px;
5
-	-webkit-border-radius: 10px;
6
-	border-radius: 10px;
7
-	-moz-box-shadow: 2px 2px 3px #666;
8
-	-webkit-box-shadow: 2px 2px 3px #666;
9
-	box-shadow: 2px 2px 3px #666;
10
-	font-size: 20px;
11
-	padding: 4px 7px;
12
-	outline: 0;
13
-	-webkit-appearance: none;
14
-}
15
-input.rounded:focus {
16
-	border-color: #339933;
10
+        border: 1px solid #ccc;
11
+}
12
+
13
+input.roundedtext {
14
+        border: 0px solid #ccc;
17 15
 }
18 16
 
17
+
18
+input.rounded, input.roundedtext {
19
+        -moz-border-radius: 10px;
20
+        -webkit-border-radius: 10px;
21
+        border-radius: 10px;
22
+        font-size: 20px;
23
+        padding: 4px 7px;
24
+        outline: 0;
25
+        -webkit-appearance: none;
26
+}
27
+
28
+input.rounded:focus, input.roundedtext:focus, #wysihtml5-editor:focus {
29
+        border: 1px solid #ccc;
30
+        border-color: #339933;
31
+        -moz-box-shadow: 0px 0px 4px 1px rgba(0,0,0,0.3);
32
+        -webkit-box-shadow: 0px 0px 4px 1px rgba(0,0,0,0.3);
33
+        box-shadow: 0px 0px 4px 1px rgba(0,0,0,0.3);
34
+}
35
+
36
+input.roundedtext {
37
+        width: 795px;
38
+        margin: 10px 0px 20px;
39
+        float: right
40
+}
41
+
42
+
19 43
 div.buttondiv {
20
-	text-align: center;
21
-	width: 650px;
22
-	margin-left: auto;
23
-	margin-right: auto;
24
-	margin-top: 10px;
44
+        text-align: center;
45
+        width: 650px;
46
+        margin-left: auto;
47
+        margin-right: auto;
48
+        margin-top: 10px;
25 49
 }
26 50
 
27 51
 span.buttonspan {
28
-	margin-left: auto;
29
-	margin-right: 0px;
52
+        margin-left: auto;
53
+        margin-right: 0px;
54
+}
55
+
56
+div.titlediv {
57
+        width: 810px;
58
+        margin-left: auto;
59
+        margin-right: auto;
60
+        margin-top: 10px;
61
+}
62
+
63
+#wysihtml5-editor {
64
+        margin-top: 30px;
30 65
 }
... ...
@@ -53,21 +53,26 @@
53 53
         <a data-wysihtml5-dialog-action="save">OK</a>&nbsp;<a data-wysihtml5-dialog-action="cancel">Cancel</a>
54 54
       </div>
55 55
     </div>
56
-    
56
+
57 57
     <section>
58 58
       <textarea id="wysihtml5-editor" spellcheck="false" wrap="off" autofocus placeholder="Escribe aquí ...">
59 59
       </textarea>
60 60
     </section>
61 61
     <div class='buttondiv'>
62
-	<span class='buttonspan'><input class='rounded' id='donewpost' type ='button' value='Enviar'/></span>
62
+        <span class='buttonspan'><input class='rounded' id='donewpost' type ='button' value='Enviar'/></span>
63
+    </div>
64
+
65
+    <div class='titlediv'>
66
+    <p><input id="title" class="roundedtext" placeholder="T&iacute;tulo"></input></p>
63 67
     </div>
68
+
64 69
     <script>
65 70
       var editor = new wysihtml5.Editor("wysihtml5-editor", {
66 71
         toolbar:     "wysihtml5-editor-toolbar",
67 72
         stylesheets: ["wysihtml5/reset-min.css", "wysihtml5/wysihtml5-editor.css"],
68 73
         parserRules: wysihtml5ParserRules
69 74
       });
70
-      
75
+
71 76
       editor.on("load", function() {
72 77
         var composer = editor.composer,
73 78
             h1 = editor.composer.element.querySelector("h1");
... ...
@@ -2,24 +2,31 @@
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
 donewpost.onclick = function(e) {
21
-	request("/newpost"+window.location.search+"&t="+encodeURIComponent(document.getElementById("wysihtml5-editor").value),function(r) {
22
-		window.location="/posts.html"+window.location.search;},function() {});
21
+        var requri=[];
22
+        requri.push("/newpost");
23
+        requri.push(window.location.search);
24
+        requri.push("&h=");
25
+        requri.push(encodeURIComponent(document.getElementById("title").value));
26
+        requri.push("&t=");
27
+        requri.push(encodeURIComponent(document.getElementById("wysihtml5-editor").value));
28
+        request(requri.join(""),function(r) {
29
+                window.location="/posts.html"+window.location.search;},function() {});
23 30
 };
24 31
 
25 32
 }());
... ...
@@ -81,11 +81,6 @@ div.centeredbutton {
81 81
         margin-right: auto;
82 82
 }
83 83
 
84
-p.title {
85
-        font-size: 28px;
86
-        padding: 0px 0px 20px 0px;
87
-}
88
-
89 84
 .aligned {
90 85
         list-style-type: none;
91 86
         padding-left: 30px;
... ...
@@ -102,23 +97,30 @@ div.post {
102 97
         width: 800px;
103 98
         margin-left: auto;
104 99
         margin-right: auto;
105
-	margin-top: 35px;
106
-	margin-bottom: 20px;
107
-	background: #f4f4f4;
100
+        margin-top: 35px;
101
+        margin-bottom: 20px;
102
+        background: #f4f4f4;
108 103
         -moz-box-shadow: 0px 0px 1px #aaa;
109 104
         -webkit-box-shadow: 0px 0px 1px #aaa;
110 105
         box-shadow: 0px 0px 1px #aaa;
111 106
 }
112 107
 
113 108
 div.postheader {
114
-        border: 1px solid #ddd;
115
-        padding: 4px;
116
-        margin-left: 0px;
117
-        margin-right: 0px;
118
-	background: #eee;
119
-        -moz-box-shadow: 0px 0px 1px #eee;
120
-        -webkit-box-shadow: 0px 0px 1px #eee;
121
-        box-shadow: 0px 0px 1px #eee;
109
+        border-style: none none solid none;
110
+        border-bottom: 1px solid #ddd;
111
+        margin-bottom: 10px;
112
+        padding-bottom: 10px;
113
+}
114
+
115
+p.title {
116
+        margin-top: 0px;
117
+}
118
+
119
+span.title {
120
+        font-size: 20px;
121
+        padding: 0px;
122
+        margin-top: 0px;
123
+        margin-bottom: 20px;
122 124
 }
123 125
 
124 126
 p.authorpostnum, p.date {
... ...
@@ -128,11 +130,12 @@ p.authorpostnum, p.date {
128 130
 }
129 131
 
130 132
 span.author {
131
-	float: left;
133
+/*        float: left;*/
134
+        font-weight: bold;
132 135
 }
133 136
 
134 137
 span.postnum {
135
-	float: right;
138
+        float: right;
136 139
 }
137 140
 
138 141
 div.postbody {
... ...
@@ -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">&nbsp;<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) {
... ...
@@ -372,11 +372,14 @@ http_getpost(wk *web, int connid, wk_uri *uri, void *userptr,char *user)
372 372
 wk_action
373 373
 http_newpost(wk *web, int connid, wk_uri *uri, void *userptr,char *user)
374 374
 {
375
+        char h[8192];
375 376
         char t[8192];
376 377
         kakumei *ka=(kakumei *)userptr;
378
+        if(wk_uri_copyvar(uri,"h",h,sizeof(h))==NULL)
379
+                h[0]='\0';
377 380
         if(wk_uri_copyvar(uri,"t",t,sizeof(t))==NULL)
378 381
                 t[0]='\0';
379
-        if(t[0]=='\0' || post_new(ka,user,t)!=0) {
382
+        if((t[0]=='\0' && h[0]=='\0') || post_new(ka,user,h,t)!=0) {
380 383
                 wk_serve_error(web,connid,wkerr_internal);
381 384
                 log_write("NEWP","invalid post, send error");
382 385
                 return(wkact_finished);
... ...
@@ -22,7 +22,7 @@
22 22
 #define MAXPOSTS 2000000
23 23
 
24 24
 int
25
-post_new(kakumei *ka, char *user, char *text)
25
+post_new(kakumei *ka, char *user, char *title, char *text)
26 26
 {
27 27
         int last;
28 28
         int num;
... ...
@@ -31,9 +31,9 @@ post_new(kakumei *ka, char *user, char *text)
31 31
         FILE *f;
32 32
         struct stat st;
33 33
         if(ka==NULL || user==NULL || user[0]=='\0' || text==NULL || text[0]=='\0') {
34
-		log_write("POST","sanity check error\n");
34
+                log_write("POST","sanity check error\n");
35 35
                 return(-1);
36
-	}
36
+        }
37 37
         last=post_last(ka);
38 38
         filename[0]='\0';
39 39
         for(num=last+1;num<MAXPOSTS;num++) {
... ...
@@ -43,9 +43,9 @@ post_new(kakumei *ka, char *user, char *text)
43 43
                         break; /* this one is ok: file doesn't exist */
44 44
         }
45 45
         if(filename[0]=='\0' || num>=MAXPOSTS) {
46
-		log_write("POST","couldn't generate filename or too many posts\n");
46
+                log_write("POST","couldn't generate filename or too many posts\n");
47 47
                 return(-1);
48
-	}
48
+        }
49 49
         /* create dir structure */
50 50
         mkdir(DATADIR,0700);
51 51
         mkdir(POSTSDIR,0700);
... ...
@@ -54,11 +54,12 @@ post_new(kakumei *ka, char *user, char *text)
54 54
         mkdir(dirname,0700);
55 55
         /* save the new post */
56 56
         if((f=fopen(filename,"w"))==NULL) {
57
-		log_write("POST","couldn't save post\n");
57
+                log_write("POST","couldn't save post\n");
58 58
                 return(-1);
59
-	}
59
+        }
60 60
         fprintf(f,"{\n  \"author\":\"%s\",\n",user);
61 61
         fprintf(f,"  \"date\":%li,\n",(long)time(NULL));
62
+        fprintf(f,"  \"title\":\"%s\",\n",title);
62 63
         fprintf(f,"  \"text\":\"%s\",\n",text);
63 64
         fprintf(f,"  \"comments\":[\n]}\n");
64 65
         fclose(f),f=NULL;
... ...
@@ -81,9 +82,9 @@ post_last(kakumei *ka)
81 82
         snprintf(filename,sizeof(filename)-1,"%s/%s",POSTSDIR,"last");
82 83
         filename[sizeof(filename)-1]='\0';
83 84
         if((fd=open(filename,O_RDONLY))==-1) {
84
-		log_write("POST","couldn't get last post number\n");
85
+                log_write("POST","couldn't get last post number\n");
85 86
                 return(-1);
86
-	}
87
+        }
87 88
         memset(readbuf,0,sizeof(readbuf));
88 89
         read(fd,readbuf,sizeof(readbuf)-1);
89 90
         close(fd),fd=-1;
... ...
@@ -95,15 +96,15 @@ post_get(kakumei *ka, int num, char *filename, int filenamesize)
95 96
 {
96 97
         struct stat st;
97 98
         if(ka==NULL || num<0 || filename==NULL || filenamesize<=0) {
98
-		log_write("POST","Sanity check error\n");
99
+                log_write("POST","Sanity check error\n");
99 100
                 return(-1);
100
-	}
101
+        }
101 102
         snprintf(filename,filenamesize,"%s/%04i/%04i.json",POSTSDIR,num/10000,num%10000);
102 103
         filename[filenamesize-1]='\0';
103 104
         if(stat(filename,&st)!=0) {
104
-		log_write("POST","Post file doesn't exist\n");
105
+                log_write("POST","Post file doesn't exist\n");
105 106
                 return(-1); /* file doesn't exist */
106
-	}
107
+        }
107 108
         return(0);
108 109
 }
109 110
 
... ...
@@ -114,19 +115,19 @@ post_addcomment(kakumei *ka, int postnum, char *user, char *text)
114 115
         char buf[16];
115 116
         FILE *f;
116 117
         if(ka==NULL || postnum<0 || user==NULL || text==NULL) {
117
-		log_write("POST","Sanity check error\n");
118
+                log_write("POST","Sanity check error\n");
118 119
                 return(-1);
119
-	}
120
-        if(post_get(ka,postnum,filename,sizeof(filename))!=0) 
120
+        }
121
+        if(post_get(ka,postnum,filename,sizeof(filename))!=0)
121 122
                 return(-1);
122 123
         if((f=fopen(filename,"r+"))==NULL) {
123
-		log_write("POST","Couldn't open post file fot appending\n");
124
+                log_write("POST","Couldn't open post file fot appending\n");
124 125
                 return(-1);
125
-	}
126
+        }
126 127
         fseek(f,-2,SEEK_END);
127 128
         if(fread(buf,1,2,f)!=2 || buf[0]!=']' ||  buf[1]!='}') {
128 129
                 fclose(f),f=NULL;
129
-		log_write("POST","Post file contents are corrupted\n");
130
+                log_write("POST","Post file contents are corrupted\n");
130 131
                 return(-1);
131 132
         }
132 133
         fseek(f,-2,SEEK_END);
... ...
@@ -11,7 +11,7 @@
11 11
 
12 12
 #include "kakumei.h"
13 13
 
14
-int post_new(kakumei *ka, char *user, char *text);
14
+int post_new(kakumei *ka, char *user, char *title, char *text);
15 15
 int post_last(kakumei *ka);
16 16
 int post_get(kakumei *ka, int postnum, char *filename, int filenamesize);
17 17
 int post_addcomment(kakumei *ka, int postnum, char *user, char *text);