Changeset 27281 for main/trunk/greenstone2/macros/style.dm
- Timestamp:
- 2013-04-30T20:15:30+12:00 (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
main/trunk/greenstone2/macros/style.dm
r27150 r27281 241 241 242 242 var gsapi = new GSAjaxAPI("_gwcgi_","_cgiargc_"); 243 244 // http://stackoverflow.com/questions/6312993/javascript-seconds-to-time-with-format-hhmmss 245 // Call as: alert(timestamp.printTime()); 246 String.prototype.printTime = function () \{ 247 var timestamp = parseInt(this, 10); // don't forget the second param 248 var date = new Date(timestamp); 249 return date.toLocaleDateString() + " " + date.toLocaleTimeString(); 250 \} 251 252 253 function loadUserComments() \{ 254 255 // don't bother loading comments if we're not on a document page (in which case there's no usercommentdiv) 256 var usercommentdiv = document.getElementById("usercomments"); 257 if(usercommentdiv == undefined || usercommentdiv == null) \{ 258 return; 259 \} 260 261 // else, if we have a usercommentdiv, we would have a docid. Get toplevel section of the docid 262 var doc_id = "_cgiargd_"; //escape("_cgiargd_"); 263 var period = doc_id.indexOf("."); 264 if(period != -1) \{ 265 doc_id = doc_id.substring(0, period); 266 \} 267 268 var metapos=0; // counter variable 269 var username = null; 270 var timestamp = null; 271 var comment = null; 272 273 var comments_arraymap = []; // array of comment records with timestamp, username and comment fields 274 275 // First retrieve all the usercomments (usernames, associated usertimestamps and usercomments) 276 // from the archives folder, where it is arranged easier than in index because it's in sequence already. 277 278 username = gsapi.getArchivesMetadata(doc_id, "username", metapos); 279 280 // Keep looping, retrieving username, comment and timestamp 281 // Store each triplet in the comments array. Stop when there's no further usernames in archives 282 283 while(username) \{ 284 285 timestamp = gsapi.getArchivesMetadata(doc_id, "usertimestamp", metapos); 286 comment = gsapi.getArchivesMetadata(doc_id, "usercomment", metapos); 287 288 //alert("username: " + username + "\\ntime: " + timestamp + "\\ncomment: " + comment); 289 290 // array of records (arraymap) 291 // http://stackoverflow.com/questions/8190658/sorting-objects-in-a-javascript-array-by-a-key-value 292 // http://stackoverflow.com/questions/4969121/in-javascript-is-there-an-easy-way-to-sort-key-value-pairs-by-the-value-and-re 293 // http://stackoverflow.com/questions/6298169/how-to-create-a-map-object-in-a-javascript 294 295 // object with member vars t, u and c. 296 var comment_record = \{ 297 t: timestamp, 298 u: username, 299 c: comment 300 \}; 301 302 comments_arraymap.push(comment_record); 303 304 metapos++; 305 username = gsapi.getArchivesMetadata(doc_id, "username", metapos); 306 \} 307 308 // No need to sort by time, 309 // as the array is already stored sorted and hence retrieved in the right order by using metapost counter 310 // If sorting the array of comment records, which would be by timestamp, see 311 // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort 312 313 //comments_arraymap.sort(function(rec1, rec2) \{ 314 // return rec1.t-rec2.t; // sort by timestamp 315 //\}); 316 317 //alert("Found " + metapos + " number of comments in the archives folder for doc " + doc_id); 318 319 // create the output HTML for all comments in one go now 320 // (we do this here altogether to hopefully avoid reloading the page for each comment) 321 322 for(var i = 0; i < metapos; i++) \{ 323 324 var comment_record = comments_arraymap[i]; 325 326 username = comment_record.u; 327 timestamp = comment_record.t; 328 comment = comment_record.c; 329 330 // for each usercomment, create a child div with the username, timestamp and comment 331 332 var divgroup=document.createElement("div"); 333 var attr=document.createAttribute("class"); 334 attr.nodeValue="usercomment"; 335 divgroup.setAttributeNode(attr); 336 337 var divuser=document.createElement("div"); 338 var divtime=document.createElement("div"); 339 var divcomment=document.createElement("div"); 340 341 342 divgroup.appendChild(divuser); 343 var txt=document.createTextNode(username); 344 divuser.appendChild(txt); 345 346 divgroup.appendChild(divtime); 347 txt=document.createTextNode(timestamp.printTime()); // formate timestamp for date/time display 348 divtime.appendChild(txt); 349 350 divgroup.appendChild(divcomment); 351 txt=document.createTextNode(comment); 352 divcomment.appendChild(txt); 353 354 usercommentdiv.appendChild(divgroup); 355 \} 356 357 358 \} 359 360 361 // http://stackoverflow.com/questions/807878/javascript-that-executes-after-page-load 362 // ensure we don't replace any other onLoad() functions, but append the loadUserComments() 363 // function to the existing onLoad handlers 364 365 if(window.onload) {\ 366 var curronload = window.onload; 367 var newonload = function() {\ 368 curronload(); 369 loadUserComments(); 370 \}; 371 window.onload = newonload; 372 \} else {\ 373 window.onload = loadUserComments; 374 \} 243 375 </script> 244 376
Note:
See TracChangeset
for help on using the changeset viewer.