Ignore:
Timestamp:
2017-03-31T19:42:22+13:00 (7 years ago)
Author:
ak19
Message:
  1. gs.functions should not be declared as new Array() as its array features are not being used (and if it were an array, declare using the array literal notation =[] which is faster). gs.functions is now declared as a JavaScript object since that's how it's being used with functions getting attached to gs.functions as properties. 2. callMetadataServer and helper functions are now getting attached to the gs.functions object by being declared as gs.functions.callMetadataServer, instead of being global functions attached to the browser window object. 3. Similarly, user_comments.js attaches its functions to gs.usercomments rather than leaving them global as before.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/web/interfaces/default/js/user_comments.js

    r31547 r31558  
    55// http://stackoverflow.com/questions/8714472/cant-pass-event-to-addeventlistener-closure-issue
    66// https://www.sitepoint.com/demystifying-javascript-closures-callbacks-iifes/
     7// http://stackoverflow.com/questions/4869712/new-without-delete-on-same-variable-in-javascript
     8// http://stackoverflow.com/questions/7375120/why-is-arr-faster-than-arr-new-array
     9// http://stackoverflow.com/questions/874205/what-is-the-difference-between-an-array-and-an-object
     10// http://stackoverflow.com/questions/33514915/what-s-the-difference-between-and-while-declaring-a-javascript-array
     11// http://www.nfriedly.com/techblog/2009/06/advanced-javascript-objects-arrays-and-array-like-objects/
    712
    813/***************
    914* USER COMMENTS
    1015****************/
     16
     17gs.usercomments = {};
     18
    1119// http://stackoverflow.com/questions/6312993/javascript-seconds-to-time-with-format-hhmmss
    1220// Call as: alert(timestamp.printTime());
    13 function formatTime(timestamp) {
     21gs.usercomments.formatTime = function(timestamp) {
    1422    var int_timestamp    = parseInt(timestamp, 10); // don't forget the second param
    1523    var date = new Date(int_timestamp);
     
    1725}
    1826
    19 function loadUserComments() {
     27gs.usercomments.loadUserComments = function() {
    2028
    2129    // don't bother loading comments if we're not on a document page (in which case there's no docid)
     
    7482    //var json_result_str = gs.functions.getMetadataArray(gs.variables["c"], gs.variables["site"], docArray, "index");
    7583
    76     gs.functions.getMetadataArray(gs.variables["c"], gs.variables["site"], docArray, "index", loadedUserComments, false); // false for asynchronous
    77 }
    78 
    79 function loadedUserComments(data)
     84    gs.functions.getMetadataArray(gs.variables["c"], gs.variables["site"], docArray, "index", gs.usercomments.loadedUserComments, false); // false for asynchronous
     85}
     86
     87gs.usercomments.loadedUserComments = function(data)
    8088{
    8189    // don't bother displaying comments if we're not on a document page
     
    141149       
    142150        // for each usercomment, create a child div with the username, timestamp and comment
    143         displayInUserCommentList(usercommentdiv, username, timestamp, comment);
     151        gs.usercomments.displayInUserCommentList(usercommentdiv, username, timestamp, comment);
    144152       
    145153        i++;
     
    155163
    156164
    157 function displayInUserCommentList(usercommentdiv, username, timestamp, comment) {
     165gs.usercomments.displayInUserCommentList = function(usercommentdiv, username, timestamp, comment) {
    158166   
    159167    //alert("Comment: " + username + " " + timestamp + " " + comment);
     
    174182   
    175183    divgroup.appendChild(divtime);
    176     txt=document.createTextNode(formatTime(timestamp)); // format timestamp for date/time display
     184    txt=document.createTextNode(gs.usercomments.formatTime(timestamp)); // format timestamp for date/time display
    177185    divtime.appendChild(txt);
    178186   
     
    193201// Unused. Replaced in favour of call to escape() in setMetaArray function that calls urlPostSync
    194202// http://stackoverflow.com/questions/6020714/escape-html-using-jquery
    195 function safeHTML(str) {
     203gs.usercomments.safeHTML = function(str) {
    196204    return str.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace('"',"&quot;").replace("'","&#x27;").replace("/", "&#x2F;"); //"\\""
    197 } 
    198 
    199 
    200 function addUserComment(_username, _comment, _docid, doc) {
     205}
     206
     207
     208gs.usercomments.addUserComment = function(_username, _comment, _docid, doc) {
    201209   
    202210    // don't add empty strings for name/comment     
     
    287295   
    288296    //var result = gs.functions.setMetadataArray(gs.variables["c"], gs.variables["site"], docArray, "accumulate", "import|archives|index");
    289     gs.functions.setMetadataArray(gs.variables["c"], gs.variables["site"], docArray, "accumulate", "import|archives|index", function(ajaxResult) { return doneUpdatingMetatada(ajaxResult, _username, _timestamp, _comment); }, false); // false for asynchronous,
     297    gs.functions.setMetadataArray(gs.variables["c"],
     298    gs.variables["site"],
     299    docArray, "accumulate",
     300    "import|archives|index",
     301    function(ajaxResult) { return gs.usercomments.doneUpdatingMetatada(ajaxResult, _username, _timestamp, _comment); },
     302    false); // false for asynchronous,
    290303    // this is ok since we're disabling the comment submit button, so no further set-meta-array calls can be
    291304    // made until the ajax call returns and the callback is called which re-enables the submit button
     
    296309}
    297310
    298 function doneUpdatingMetatada(data, _username, _timestamp, _comment)
     311gs.usercomments.doneUpdatingMetatada = function(data, _username, _timestamp, _comment)
    299312{
    300313
     
    339352    var usercommentdiv = document.getElementById("usercomments");
    340353    if(usercommentdiv != undefined) {
    341         displayInUserCommentList(usercommentdiv, _username, _timestamp, _comment);
     354        gs.usercomments.displayInUserCommentList(usercommentdiv, _username, _timestamp, _comment);
    342355        }
    343356    }
     
    348361}
    349362
    350 function commentAreaSetup() {
    351     loadUserComments();
     363gs.usercomments.commentAreaSetup = function() {
     364    gs.usercomments.loadUserComments();
    352365
    353366    //$("div#commentarea").html("<textarea required=\"required\" name=\"comment\" rows=\"10\" cols=\"64\" placeholder=\"Add your comment here...\"></textarea>");
     
    361374// This way we ensure we don't replace any other onLoad() functions, but append the loadUserComments()
    362375// function to the existing set of eventhandlers called onDocReady
    363 $(document).ready(commentAreaSetup);
    364 
     376$(document).ready(gs.usercomments.commentAreaSetup);
     377
Note: See TracChangeset for help on using the changeset viewer.