Changeset 27313 for main/trunk/greenstone2/macros/style.dm
- Timestamp:
- 2013-05-07T19:32:42+12:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/macros/style.dm
r27310 r27313 264 264 doc_id = doc_id.substring(0, period); 265 265 \} 266 267 var metapos=0; // counter variable 268 var username = null; 269 var timestamp = null; 270 var comment = null; 271 272 var comments_arraymap = []; // array of comment records with timestamp, username and comment fields 273 274 // First retrieve all the usercomments (usernames, associated usertimestamps and usercomments) 275 // from the archives folder, where it is arranged easier than in index because it's in sequence already. 276 277 username = gsapi.getDocumentMetadata(doc_id, "username", metapos); 278 279 // Keep looping, retrieving username, comment and timestamp 280 // Store each triplet in the comments array. Stop when there's no further usernames in archives 281 282 while(username) \{ //&& !username.indexOf("ERROR") == -1) \{ 283 284 var errorIndex = username.indexOf("ERROR"); 285 if(errorIndex != -1) \{ 286 var endIndex = username.indexOf("("); 287 var error = username.substring(errorIndex,endIndex); 288 var errormessage=document.createTextNode("Error retrieving comments. " + error); 289 usercommentdiv.appendChild(errormessage); 290 //alert(username); 291 break; 292 \} 293 294 timestamp = gsapi.getDocumentMetadata(doc_id, "usertimestamp", metapos); 295 comment = gsapi.getDocumentMetadata(doc_id, "usercomment", metapos); 296 297 //alert("username: " + username + "\\ntime: " + timestamp + "\\ncomment: " + comment); 298 299 // array of records (arraymap) 300 // http://stackoverflow.com/questions/8190658/sorting-objects-in-a-javascript-array-by-a-key-value 301 // http://stackoverflow.com/questions/4969121/in-javascript-is-there-an-easy-way-to-sort-key-value-pairs-by-the-value-and-re 302 // http://stackoverflow.com/questions/6298169/how-to-create-a-map-object-in-a-javascript 303 304 // object with member vars t, u and c. 305 var comment_record = \{ 306 t: timestamp, 307 u: username, 308 c: comment 309 \}; 310 311 comments_arraymap.push(comment_record); 312 313 metapos++; 314 username = gsapi.getDocumentMetadata(doc_id, "username", metapos); 266 267 var username_rec = \{ 268 metaname: "username", 269 metapos: "all" 270 \}; 271 272 var timestamp_rec = \{ 273 metaname: "usertimestamp", 274 metapos: "all" 275 \}; 276 277 var comment_rec = \{ 278 metaname: "usercomment", 279 metapos: "all" 280 \}; 281 282 var doc_rec = \{ 283 docid: doc_id, 284 metatable: [username_rec, timestamp_rec, comment_rec] 285 \}; 286 287 var docArray = [doc_rec]; 288 //alert(JSON.stringify(docArray)); 289 290 var json_result_str = gsapi.getMetadataArray(docArray, "index"); 291 // alert(json_result_str); 292 var result = JSON.parse(json_result_str); 293 // result contains only one docrec (result[0]), since we asked for the usercomments of one docid 294 var metatable = result[0].metatable; 295 // alert(JSON.stringify(metatable)); 296 297 var i = 0; 298 var looping = true; 299 var print_heading = true; 300 301 // metatable[0] = list of usernames, metatable[1] = list of timestamps, metatable[2] = list of comments 302 // the 3 lists/arrays should be of even length. Assuming this, loop as long as there's another username 303 while(looping) \{ 304 var metaval_rec = metatable[0].metavals[i]; 305 if(metaval_rec == undefined) \{ 306 looping = false; 307 \} 308 else \{ 309 310 if(print_heading) \{ 311 var heading=document.createElement("div"); 312 var attr=document.createAttribute("class"); 313 attr.nodeValue="usercommentheading"; 314 heading.setAttributeNode(attr); 315 var txt=document.createTextNode("_text-usercomments-section_"); 316 heading.appendChild(txt); 317 usercommentdiv.appendChild(heading); 318 319 print_heading = false; 320 \} 321 322 var username = metaval_rec.metavalue; 323 var timestamp = metatable[1].metavals[i].metavalue; 324 var comment = metatable[2].metavals[i].metavalue; 325 326 //alert("Comment: " + username + " " + timestamp + " " + comment); 327 328 // No need to sort by time, as the meta are already stored sorted 329 // and hence retrieved in the right order by using the i (metapos) counter 330 // If sorting the array of comment records, which would be by timestamp, see 331 // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort 332 333 // for each usercomment, create a child div with the username, timestamp and comment 334 displayInUserCommentList(usercommentdiv, username, timestamp, comment); 335 336 i++; 337 \} 315 338 \} 316 339 317 // No need to sort by time,318 // as the array is already stored sorted and hence retrieved in the right order by using metapos counter319 // If sorting the array of comment records, which would be by timestamp, see320 // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort321 322 //comments_arraymap.sort(function(rec1, rec2) \{323 // return rec1.t-rec2.t; // sort by timestamp324 //\});325 326 //alert("Found " + metapos + " number of comments in the archives folder for doc " + doc_id);327 328 // create the output HTML for all comments in one go now329 // (we do this here for all values together to hopefully avoid reloading the page for each comment)330 331 if(metapos > 0) \{332 var heading=document.createElement("div");333 var attr=document.createAttribute("class");334 attr.nodeValue="usercommentheading";335 heading.setAttributeNode(attr);336 var txt=document.createTextNode("_text-usercomments-section_");337 heading.appendChild(txt);338 usercommentdiv.appendChild(heading);339 \}340 341 for(var i = 0; i < metapos; i++) \{342 var comment_record = comments_arraymap[i];343 344 username = comment_record.u;345 timestamp = comment_record.t;346 comment = comment_record.c;347 348 // for each usercomment, create a child div with the username, timestamp and comment349 displayInUserCommentList(usercommentdiv, username, timestamp, comment);350 \}351 352 353 340 \} 341 354 342 355 343 function displayInUserCommentList(usercommentdiv, username, timestamp, comment) \{
Note:
See TracChangeset
for help on using the changeset viewer.