source: main/trunk/greenstone3/web/interfaces/default/js/user_comments.js@ 31537

Last change on this file since 31537 was 31537, checked in by ak19, 7 years ago

First commit for getting user comments working for GS3. It all works, but there's debugging statements still and haven't cleaned up commented out code. After that, would like to make POST rather than GET AJAX calls, so more refactoring required. 1. config_format.xsl is just minor spelling corrections. 2. header.xsl has a new function generateLogoutURL. 3. document.xsl imports and calls new usercomments.xsl to set up the user comments area. 4. New usercomments.js is imported by new usercomments.xsl, and sets up the interaction of the usercomments area. 5. javascript-global-functions.js now contains setMetadataArray and getMetadataArray functions to parallel what GS2 used in gsajaxapi.js, but for GS3 need to go through GS2Construct.processModifyMetadata() service in java code. 5. GS2Construct.java does different checking for users adding user comments versus users doing document editing. For the latter, the user needs to have editing permissions for the document. But any user is allowed to add comments on any accessible document. But ModifyMetadata should not allow any other metadata to be modified other than meta fields. 6. New language strings for usercomment area and GS2Construct errors in the 2 changed properties files.

File size: 11.3 KB
Line 
1// http://toddmotto.com/avoiding-anonymous-javascript-functions
2// https://toddmotto.com/everything-you-wanted-to-know-about-javascript-scope/
3// pass event to addEventListener onmouseover
4// http://stackoverflow.com/questions/256754/how-to-pass-arguments-to-addeventlistener-listener-function
5// http://stackoverflow.com/questions/8714472/cant-pass-event-to-addeventlistener-closure-issue
6
7
8/***************
9* USER COMMENTS
10****************/
11// http://stackoverflow.com/questions/6312993/javascript-seconds-to-time-with-format-hhmmss
12// Call as: alert(timestamp.printTime());
13function formatTime(timestamp) {
14 var int_timestamp = parseInt(timestamp, 10); // don't forget the second param
15 var date = new Date(int_timestamp);
16 return date.toLocaleDateString() + " " + date.toLocaleTimeString();
17}
18
19function loadUserComments() {
20 /*
21
22 if(gs.variables) {
23 var listing = "";
24 for (prop in gs.variables) {
25 listing = listing + prop.toString() + ": " + gs.variables[prop] + "\n";
26 }
27 alert ("gs variables:\n" + listing);
28 } else {
29 alert("LOADING USER COMMENTS - no gs.variables set");
30 }
31 */
32
33 // don't bother loading comments if we're not on a document page (in which case there's no usercommentdiv)
34 var usercommentdiv = document.getElementById("usercomments");
35 if(usercommentdiv == undefined || usercommentdiv == null) {
36 return;
37 }
38
39 // else, if we have a usercommentdiv, we would have a docid. Get toplevel section of the docid
40 var doc_id = gs.variables["d"]; ///"_cgiargdJssafe_"; //escape("cgiargd");
41 var period = doc_id.indexOf(".");
42 if(period != -1) {
43 doc_id = doc_id.substring(0, period);
44 }
45
46 var username_rec = {
47 metaname: "username",
48 metapos: "all"
49 };
50
51 var timestamp_rec = {
52 metaname: "usertimestamp",
53 metapos: "all"
54 };
55
56 var comment_rec = {
57 metaname: "usercomment",
58 metapos: "all"
59 };
60
61 var doc_rec = {
62 docid: doc_id,
63 metatable: [username_rec, timestamp_rec, comment_rec]
64 };
65
66 var docArray = [doc_rec];
67 //alert(JSON.stringify(docArray));
68
69 var json_result_str = gs.functions.getMetadataArray(gs.variables["c"], gs.variables["site"], docArray, "index");
70 // alert(json_result_str);
71 var result = JSON.parse(json_result_str);
72 // result contains only one docrec (result[0]), since we asked for the usercomments of one docid
73 var metatable = result[0].metatable;
74 // alert(JSON.stringify(metatable));
75
76 var i = 0;
77 var looping = true;
78 var print_heading = false; // UNUSED SECTION if(print_heading) below
79
80 // metatable[0] = list of usernames, metatable[1] = list of timestamps, metatable[2] = list of comments
81 // the 3 lists/arrays should be of even length. Assuming this, loop as long as there's another username
82 while(looping) {
83 var metaval_rec = metatable[0].metavals[i];
84 if(metaval_rec == undefined) {
85 looping = false;
86 }
87 else {
88
89// BEGIN UNUSED
90 if(print_heading) {
91 var heading=document.createElement("div");
92 var attr=document.createAttribute("class");
93 attr.nodeValue="usercommentheading";
94 heading.setAttributeNode(attr);
95 var txt=document.createTextNode(gs.variables["textusercommentssection"]); ///"_textusercommentssection_");
96 heading.appendChild(txt);
97 usercommentdiv.appendChild(heading);
98
99 print_heading = false;
100 }
101// END UNUSED
102
103 var username = metaval_rec.metavalue;
104 var timestamp = metatable[1].metavals[i].metavalue;
105 var comment = metatable[2].metavals[i].metavalue;
106
107 //alert("Comment: " + username + " " + timestamp + " " + comment);
108
109 // No need to sort by time, as the meta are already stored sorted
110 // and hence retrieved in the right order by using the i (metapos) counter
111 // If sorting the array of comment records, which would be by timestamp, see
112 // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort
113
114 // for each usercomment, create a child div with the username, timestamp and comment
115 displayInUserCommentList(usercommentdiv, username, timestamp, comment);
116
117 i++;
118 }
119 }
120
121}
122
123
124function displayInUserCommentList(usercommentdiv, username, timestamp, comment) {
125
126 //alert("Comment: " + username + " " + timestamp + " " + comment);
127
128 var divgroup=document.createElement("div");
129 var attr=document.createAttribute("class");
130 attr.nodeValue="usercomment";
131 divgroup.setAttributeNode(attr);
132
133 var divuser=document.createElement("div");
134 var divtime=document.createElement("div");
135 var divcomment=document.createElement("div");
136
137
138 divgroup.appendChild(divuser);
139 var txt=document.createTextNode(username);
140 divuser.appendChild(txt);
141
142 divgroup.appendChild(divtime);
143 txt=document.createTextNode(formatTime(timestamp)); // format timestamp for date/time display
144 divtime.appendChild(txt);
145
146 // any quotes and colons in the fields would have been protected for transmitting as JSON
147 // so decode their entity values
148 comment = comment.replace(/"/gmi, '"');
149 comment = comment.replace(/&58;/gmi, ':');
150
151 divgroup.appendChild(divcomment);
152 txt=document.createTextNode(comment);
153 divcomment.appendChild(txt);
154
155 usercommentdiv.appendChild(divgroup);
156
157}
158
159
160// Unused. Replaced in favour of call to escape() in setMetaArray function that calls urlPostSync
161// http://stackoverflow.com/questions/6020714/escape-html-using-jquery
162function safeHTML(str) {
163 return str.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace('"',"&quot;").replace("'","&#x27;").replace("/", "&#x2F;"); //"\\""
164}
165
166
167function addUserComment(_username, _comment, _docid, doc) {
168
169 // don't add empty strings for name/comment
170
171 // http://stackoverflow.com/questions/498970/how-do-i-trim-a-string-in-javascript
172 //var trimmed_username=_username.replace(/^\s+|\s+$/g, '');
173 var trimmed_comment = _comment.replace(/^\s+|\s+$/g, '');
174
175 if(!trimmed_comment) { // || !trimmed_username
176 doc.AddUserCommentForm.comment.value = "";
177 //doc.AddUserCommentForm.username.value = "";
178 doc.getElementById("usercommentfeedback").innerHTML = gs.variables["textisempty"]; ///"_textisempty_";
179 return;
180 }
181
182 // Need to the add user comment meta of username, timestamp and comment to the
183 // topmost section of the document. So only get the docId up to any period mark:
184 var period = _docid.indexOf(".");
185 if(period != -1) {
186 _docid = _docid.substring(0, period);
187 }
188
189
190 // Want to store username, timestamp and comment in import/metadata.xml, archives/doc.xml
191 // and index/col.gdb.
192
193 // For getting the current time, see
194 // http://stackoverflow.com/questions/3830244/get-current-date-time-in-seconds
195 var _timestamp = new Date().getTime(); // div by 1000 to get seconds. valueOf() may return string
196
197 //alert("username:" + _username
198 //+ "\\ncomment: " + _comment
199 //+ "\\ncollection: " + collection
200 //+ "\\ndocid: " + _docid
201 //+ "\\ntimestamp: " + _timestamp);
202
203
204 // Entity encode the values before storing (at least <, >, forward slash.
205 // And single and double quote, ampersand)
206 // http://stackoverflow.com/questions/6020714/escape-html-using-jquery
207 // setMetadataArray escapes the entire JSON, is that better than escaping individually here?
208 //_docid = escape(_docid);
209 //_timestamp = escape(_timestamp);
210 //_username = escape(_username); //safeHTML(_username);
211 //_comment = escape(_comment); //safeHTML(_comment);
212
213 // Use this if making individual api calls to set username meta, then timestamp then comment meta
214 // GSAPI already knows the collection
215 //gsapi.setMetadata(_docid, "username", null, _username, "accumulate", "import|archives|index");
216 //gsapi.setMetadata(_docid, "usertimestamp", null, _timestamp, "accumulate", "import|archives|index");
217 //gsapi.setMetadata(_docid, "usercomment", null, _comment, "accumulate", "import|archives|index");
218
219
220 // Use the new JSON metatable format to set username, timestamp and comment meta for docid in one go
221
222 // For creating the JSON object that gets turned into a string, see
223 // http://msdn.microsoft.com/en-us/library/ie/cc836459%28v=vs.94%29.aspx
224 // http://jsfiddle.net/qmacro/W54hy/
225
226 var username_rec = {
227 metaname: "username",
228 metavals: [_username]
229 };
230
231 var timestamp_rec = {
232 metaname: "usertimestamp",
233 metavals: [_timestamp]
234 };
235
236 var comment_rec = {
237 metaname: "usercomment",
238 metavals: [_comment]
239 };
240
241 var doc_rec = {
242 docid: _docid,
243 metatable: [username_rec, timestamp_rec, comment_rec],
244 metamode: "accumulate"
245 };
246
247 var docArray = [doc_rec];
248
249 //alert(JSON.stringify(docArray));
250
251 // GSAPI already knows the collection
252 var result = gs.functions.setMetadataArray(gs.variables["c"], gs.variables["site"], docArray, "accumulate", "import|archives|index");
253
254 // clear the comment field as it has now been submitted, but not the username field
255 // as the user is logged in, so they should be able to commit again under their username.
256 doc.AddUserCommentForm.comment.value = "";
257
258 // check for locked collection error
259 var errorIndex = result.indexOf("ERROR");
260 // check for any error discovered on the server side
261 var responseErrorIndex = result.indexOf("<error");
262
263 if(errorIndex != -1) {
264 var endIndex = result.indexOf("\\n");
265 var error = result.substring(errorIndex,endIndex);
266 errormessage="ERROR: Unable to add comment. " + error;
267 doc.getElementById("usercommentfeedback").innerHTML = errormessage;
268 //alert("Result: " + result);
269 }
270 else if (responseErrorIndex != -1) {
271 var endIndex = result.indexOf("</error>");
272 var startIndex = result.indexOf(">", responseErrorIndex+1);
273 var error = result.substring(startIndex+1,endIndex);
274 errormessage="ERROR: Unable to add comment. " + error;
275 doc.getElementById("usercommentfeedback").innerHTML = errormessage;
276 //alert("Result: " + result);
277 }
278 else { // success!
279 doc.getElementById("usercommentfeedback").innerHTML = gs.variables["textcommentsubmitted"]; ///"_textcommentsubmitted_";
280
281 // update display of existing user comments to show the newly added comment
282 var usercommentdiv = document.getElementById("usercomments");
283 if(usercommentdiv != undefined) {
284 displayInUserCommentList(usercommentdiv, _username, _timestamp, _comment);
285 }
286 }
287}
288
289function commentAreaSetup() {
290 loadUserComments();
291 //$("div#commentarea").html("<textarea required=\"required\" name=\"comment\" rows=\"10\" cols=\"64\" placeholder=\"Add your comment here...\"></textarea>");
292 $("div#commentarea").html('<textarea required="required" name="comment" rows="10" cols="64" placeholder="Add your comment here..."></textarea>');
293
294}
295
296
297// "Handlers added via $(document).ready() don't overwrite each other, but rather execute in turn"
298// as explained at http://stackoverflow.com/questions/15564029/adding-to-window-onload-event
299
300$(document).ready(commentAreaSetup);
301
302/*
303// http://stackoverflow.com/questions/807878/javascript-that-executes-after-page-load
304// ensure we don't replace any other onLoad() functions, but append the loadUserComments()
305// function to the existing onLoad handlers
306
307if(window.onload) {
308 var curronload = window.onload;
309 var newonload = function() {
310 curronload();
311 loadUserComments();
312 };
313 window.onload = newonload;
314} else {
315 window.onload = loadUserComments;
316}
317*/
Note: See TracBrowser for help on using the repository browser.