1- var pagemaker = require ( './../js/ pagemaker' ) ;
2- var slh = require ( "./../js/ SQLiteHelper.js" ) ;
1+ var pagemaker = require ( 'pagemaker' ) ;
2+ var slh = require ( ' SQLiteHelper' ) ;
33var url = require ( 'url' ) ;
4+ var upops = require ( 'uploadOps' ) ;
45
56exports . getReq = function ( request , response ) {
6- send ( response , url . parse ( request . url , true ) . query [ "taskid" ] ) ;
7+ send ( response , request , url . parse ( request . url , true ) . query [ "taskid" ] ) ;
78}
89
9- exports . postReq = function ( request , response ) {
10- //TODO: Read incoming post data and store in in the database
11- send ( response , url . parse ( request . url , true ) . query [ "taskid" ] ) ;
10+ exports . postReq = function ( request , response , dataBuffer ) {
11+ var parsed = upops . parseMultipartFormdata ( dataBuffer ) ;
12+
13+ //Add comment to the database (truncated to 500 characters)
14+ slh . addComment ( parsed [ "thecomment" ] . toString ( ) . substring ( 0 , 1024 ) , parsed [ "taskid" ] . toString ( ) ,
15+ parsed [ "email" ] . toString ( ) , function ( obj ) {
16+ if ( obj . status != 0 ) {
17+ //TODO: Send a 500 error page
18+ console . log ( "Error saving comment: " + obj . detail ) ;
19+ return ;
20+ }
21+ else {
22+ // Once comment has been added, display the comments for the task
23+ send ( response , request , url . parse ( request . url , true ) . query [ "taskid" ] ) ;
24+ }
25+ } ) ;
1226}
1327
14- send = function ( response , taskId ) {
28+ send = function ( response , request , taskId ) {
1529 response . writeHead ( 200 , { 'Content-Type' : 'text/html' } ) ;
1630
1731 // If taskid isn't set, return an error
1832 if ( taskId == undefined ) {
19- var page = new StandardPage ( ) ;
33+ var page = new StandardPage ( request ) ;
2034
2135 page . addContent ( "<div><span>Error: No Task ID defined.</span></div>" +
2236 "<br />" ) ;
@@ -34,46 +48,70 @@ send = function (response, taskId) {
3448 return ;
3549 }
3650
37- var page = new StandardPage ( ) ;
51+ var page = new StandardPage ( request ) ;
3852 page . setTitle ( "Comments for task " + taskId ) ;
39-
40- // Add comment form
41- page . addContent ( "<div><span>Enter a comment:</span></div>" +
42- "<br />" ) ;
43-
44- page . addContent ( "<form action='comments.js' method='post'>" +
45- "<textarea name='thecomment' rows='4' cols='50'></textarea><br />" +
46- "<input type='hidden' name='taskid' value=" + taskId + ">" +
47- "<input type='submit' value='Save'>" +
48- "</form>" ) ;
49-
53+
54+ var cookies = upops . parseCookies ( request . headers ) ;
55+
56+ // Show add comement box if user is logged in
57+ if ( cookies . Email ) {
58+ // Add comment form
59+ page . addContent ( "<div><span>Enter a comment:</span></div>" +
60+ "<br />" ) ;
61+
62+ page . addContent ( "<script type='text/javascript'>" +
63+ "function validate(form) {" +
64+ "var c = document.getElementById('thecomment');" +
65+ "if(!/\\S/.test(c.value)){" +
66+ "window.alert('Please enter a comment');" +
67+ "return;" +
68+ "}" +
69+ "else if(c.value.length > '1024'){" +
70+ "window.alert('This comment exceeds the maximum length of 500 characters.');" +
71+ "return;" +
72+ "}" +
73+ "form.form.submit();" +
74+ "}" +
75+ "</script>" ) ;
76+
77+ page . addContent ( "<form action='comments?taskid=" + taskId + "' method='post' enctype='multipart/form-data'>" +
78+ "<textarea name='thecomment' id='thecomment' rows='4' cols='50'></textarea><br />" +
79+ "<input type='hidden' name='taskid' value='" + taskId + "'>" +
80+ "<input type='hidden' name='email' value='" + cookies . Email + "'>" +
81+ "<button type='button' class='rounded' id='enter' onclick='validate(this)'>" +
82+ "<span>Save</span>" +
83+ "</button>" +
84+ "</form>" ) ;
85+ }
86+ else {
87+ page . addContent ( "<div><span style='font-weight:bold'>Login to add a comment.</span></div>" +
88+ "<br />" ) ;
89+ }
90+
5091 var numComments = obj . rows . length ;
5192
5293 // If there are no comments for the task, say so
5394 if ( numComments == 0 ) {
5495 page . addContent ( "<div><span>There are currently no comments for this task.</span></div>" +
5596 "<br />" ) ;
5697 }
57- // Otherwise, display all comments
98+ // Otherwise, display all comments (newest on top)
5899 else {
59- for ( i = 0 ; i < numComments ; i ++ ) {
60- page . addContent ( "<div>" +
100+ for ( i = numComments - 1 ; i >= 0 ; i -- ) {
101+ page . addContent ( "<div class='commentBox' >" +
61102 "<span>" + obj . rows [ i ] . email + " says: </span>" +
62103 "<br />" +
104+ "<span class='timeStamp'>" + obj . rows [ i ] . created + "</span>" +
105+ "<br /><br />" +
63106 "<span>" + obj . rows [ i ] . thecomment + "</span>" +
64107 "</div>" +
65- "<br />" ) ;
108+ "<br /><br /> " ) ;
66109 }
67110 }
68111
69112 page . standardMenus ( ) ;
70-
71113 response . write ( page . toHTML ( ) ) ;
72-
73114 response . end ( ) ;
74-
75115 } ) ;
76-
77116 }
78-
79- }
117+ }
0 commit comments