Changeset 27281

Show
Ignore:
Timestamp:
30.04.2013 20:15:30 (6 years ago)
Author:
ak19
Message:

Preliminary working version of display of existing user comments. Tested and works. Need to optimise it so that we don't repeatedly ask for each username, comment and timestamp from the archives (by calling the new getArchivesMeta function in gsajaxapi), but instead need to be able to ask for all such meta to be returned in an array of triples format.

Location:
main/trunk/greenstone2
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/macros/document.dm

    r27277 r27281  
    105105<table width=_pagewidth_ cellpadding=0 cellspacing=0 border=0> 
    106106<tr><td align=left valign=top> 
    107 _usercomment_ 
    108107_prevarrow_ 
    109108</td><td align=right valign=top> 
     
    125124####################################################################### 
    126125 
     126# associated javascript function loadUserComments is in style.dm's _globalscripts_ macro 
     127_usercomments_ { 
     128_If_(_cgiargd_, 
     129<div id="usercomments"></div> 
     130) 
     131} 
     132 
     133 
    127134# Display the add-user-comment form on actual document pages and not when browsing/searching 
    128135# This means the form should only be displayed on pages where the _cgiargd_ (the docid) is set 
     
    132139# http://stackoverflow.com/questions/8869341/ajax-form-submit-with-submit-button 
    133140 
    134 _usercomment_ { 
     141_addusercomment_ { 
    135142 
    136143_If_(_cgiargd_, 
     
    232239        doc.AddUserCommentForm.username.value = ""; 
    233240        doc.getElementById("usercommentfeedback").innerHTML = "_textcommentsubmitted_"; 
     241 
     242        // update display of existing user comments to show the newly added comment 
     243        loadUserComments(); 
    234244    \} 
    235245</script> 
     
    632642_navarrowsbottom_ 
    633643</div> 
     644 
     645<center> 
     646<table width=_pagewidth_ cellpadding=0 cellspacing=0 border=0> 
     647<tr><td align=left valign=top> 
     648<div class="commentssection"> 
     649_usercomments_ 
     650_addusercomment_ 
     651</div> 
     652</td></tr></table> 
     653</center> 
     654 
    634655_endspacer__htmlfooter_ 
    635656} 
  • main/trunk/greenstone2/macros/style.dm

    r27150 r27281  
    241241 
    242242    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    \} 
    243375  </script> 
    244376 
  • main/trunk/greenstone2/web/script/gsajaxapi.js

    r27277 r27281  
    254254    } 
    255255 
     256    // New 
     257    this.getArchivesMetadata = function(docoid,metaname,metapos) 
     258    { 
     259        var mdserver = this.metadataserverURL(); 
     260     
     261        var url = mdserver + "?a=get-archives-metadata"; 
     262        url += "&c="+collect_; 
     263        url += "&d="+docoid; 
     264        url += "&metaname=" + metaname; 
     265        if (metapos!=null) { 
     266            url += "&metapos=" + metapos; 
     267        } 
     268 
     269    //alert("In getArchivesMeta. URL: " + url) 
     270        return this.urlGetSync(url); //Once this works, make it POST 
     271    } 
     272 
    256273    this._setMetadata = function(mode,docid,metaname,metapos,metavalue,metamode) 
    257274    {