Changeset 31558

Show
Ignore:
Timestamp:
31.03.2017 19:42:22 (2 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.

Location:
main/trunk/greenstone3/web/interfaces/default
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone3/web/interfaces/default/js/javascript-global-functions.js

    r31555 r31558  
    66var HALTED = 12; 
    77 
    8 gs.functions = new Array(); 
     8gs.functions = {}; 
    99 
    1010gs.jqGet = function(id) 
     
    436436// - opts param: No function overloading in JavaScript. Can pass a custom object, however can pass opts, 
    437437// see http://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices 
    438 function callMetadataServer(callingFunction, payload, responseFunction, opts) 
     438gs.functions.callMetadataServer = function(callingFunction, payload, responseFunction, opts) 
    439439{ 
    440440 
     
    443443    // Set/remove operations will switch to synchronous AJAX, unless opt["forceSync"] is set otherwise 
    444444    var async_setting = true;  
    445     var method = "GET"; // GET was the default before 
     445    var method = "POST"; // GET was the default before 
    446446     
    447447    // Set to false if you wish to use the regular JavaScript AJAX way in gsajaxapi.js (will use payload.url) 
     
    457457    // set method = "GET" (above, but also in calling functions that specify this optional parameter!) 
    458458    // and set the default here below for _use_payload_in_data_not_url_form to false. 
    459     var _use_payload_in_data_not_url_form = false; 
     459    var _use_payload_in_data_not_url_form = true; // code will anyway set this to true for jQuery $.ajax() with POST 
    460460 
    461461    var _modifyingMeta  = false; 
     
    481481    async_setting = false; // for 'write' operations (set/remove meta), we force sequential processing of the internal operation. 
    482482 
    483     // for meta modification operatons, should we make the method=POST?????? 
    484     // method = "POST"; 
    485483    } 
    486484    // check for any overrides by calling code that knows what it's doing 
     
    580578// of the payload. Then calling functions, and the callMetadataServer() function they call, will control 
    581579// and determine which of the two forms ultimately gets used. 
    582 // UNUSED: http://stackoverflow.com/questions/8648892/convert-url-parameters-to-a-javascript-object 
    583 function getBasicDataForMetadataServer(metaServerCommand, collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, metadataPosition) { 
    584  
     580// (Constructing both URL and data structure, as I could not successfully convert old URL way to data structure 
     581// http://stackoverflow.com/questions/8648892/convert-url-parameters-to-a-javascript-object) 
     582gs.functions.getBasicDataForMetadataServer = function(metaServerCommand, collection, site, documentID, metadataName, 
     583                               metamode, metadataValue, prevMetadataValue, metadataPosition) 
     584{ 
    585585    // if we're doing set- or remove- metadata operations, 
    586586    // then we need change the data params that will make up the query string 
     
    665665 
    666666// See description for getBasicDataForMetadataServer() 
    667 function getComplexDataForMetadataServer(metaServerCommand, collection, site, docArray, metamode, where) { 
    668  
     667gs.functions.getComplexDataForMetadataServer = function(metaServerCommand, collection, site, docArray, metamode, where) 
     668{ 
    669669    var docArrayJSON = JSON.stringify(docArray); 
    670670     
     
    746746*************************/ 
    747747 
    748 // callMetadataServerURLMode("setImportMetadata", "cgi-bin/metadata-server.pl?a=set-import-metadata&c=" + collection + "&site=" + site + "&d=" + documentID + "&metaname=" + metadataName + "&metavalue=" + metadataValue + "&prevmetavalue=" + prevMetadataValue + "&metamode=" + metamode, responseFunction); 
    749  
    750748gs.functions.setImportMetadata = function(collection, site, documentID, metadataName, metadataValue, prevMetadataValue, metamode, responseFunction) 
    751749{     
    752750 
    753     callMetadataServer( 
     751    gs.functions.callMetadataServer( 
    754752    "setImportMetadata",  
    755     getBasicDataForMetadataServer("set-import-metadata", collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, null /*metapos*/),  
     753    gs.functions.getBasicDataForMetadataServer("set-import-metadata", collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, null /*metapos*/),  
    756754    responseFunction); 
    757755     
     
    764762    } 
    765763 
    766     callMetadataServer( 
     764    gs.functions.callMetadataServer( 
    767765    "setArchivesMetadata", 
    768     getBasicDataForMetadataServer("set-archives-metadata", collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, metadataPosition), 
     766    gs.functions.getBasicDataForMetadataServer("set-archives-metadata", collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, metadataPosition), 
    769767    responseFunction); 
    770768 
     
    781779    if(metadataPosition != null || prevMetadataValue != null) {  
    782780     
    783     callMetadataServer( 
     781    gs.functions.callMetadataServer( 
    784782        "setIndexMetadata", 
    785         getBasicDataForMetadataServer("set-metadata", collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, metadataPosition), 
     783        gs.functions.getBasicDataForMetadataServer("set-metadata", collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, metadataPosition), 
    786784        responseFunction); 
    787785    } 
     
    797795        // previous version of this function did not allow setting metapos or prevMetavalue 
    798796        // so leaving the behaviour the same along with function signature. 
    799         callMetadataServer( 
     797        gs.functions.callMetadataServer( 
    800798        nameArray[i], 
    801         getBasicDataForMetadataServer(functionArray[i], collection, site, documentID, metadataName, metamode, metadataValue, null /*prevMetadataValue*/, null /*metadataPosition*/), 
     799        gs.functions.getBasicDataForMetadataServer(functionArray[i], collection, site, documentID, metadataName, metamode, metadataValue, null /*prevMetadataValue*/, null /*metadataPosition*/), 
    802800        responseFunction); 
    803801    } 
     
    812810 
    813811 
    814     var payload = getComplexDataForMetadataServer("set-metadata-array", collection, site, docArray, metamode, where); 
     812    var payload = gs.functions.getComplexDataForMetadataServer("set-metadata-array", collection, site, docArray, metamode, where); 
    815813     
    816814    // set operations are generally synchronous, but allow calling function to force ajax call  
     
    822820    //console.log("cgi-bin/metadata-server.pl?"+params); 
    823821     
    824     var response = callMetadataServer("Setting metadata in "+where, payload, responseFunction, {"forceSync": forceSync, "requestMethod": "POST"}); 
     822    var response = gs.functions.callMetadataServer("Setting metadata in "+where, payload, responseFunction, {"forceSync": forceSync, "requestMethod": "POST"}); 
    825823     
    826824    return response; 
     
    837835gs.functions.getMetadataArray = function(collection, site, docArray, where, responseFunction, forceSync) 
    838836{ 
    839     var payload = getComplexDataForMetadataServer("get-metadata-array", collection, site, docArray, null /*metamode*/, where);     
     837    var payload = gs.functions.getComplexDataForMetadataServer("get-metadata-array", collection, site, docArray, null /*metamode*/, where);     
    840838 
    841839    // get operations are generally asynchronous, but allow calling function to force ajax call  
     
    847845    // http://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices 
    848846    // https://www.w3schools.com/js/js_objects.asp 
    849     var response = callMetadataServer("Getting metadata from "+where, payload, responseFunction, {"forceSync":forceSync, "requestMethod": "POST"}); 
     847    var response = gs.functions.callMetadataServer("Getting metadata from "+where, payload, responseFunction, {"forceSync":forceSync, "requestMethod": "POST"}); 
    850848 
    851849    return response; 
     
    855853gs.functions.getImportMetadata = function(collection, site, documentID, metadataName, responseFunction) 
    856854{ 
    857     var payload = getBasicDataForMetadataServer("get-import-metadata", collection, site, documentID, metadataName); 
    858     callMetadataServer("getImportMetadata", payload, function(responseText) { 
     855    var payload = gs.functions.getBasicDataForMetadataServer("get-import-metadata", collection, site, documentID, metadataName); 
     856    gs.functions.callMetadataServer("getImportMetadata", payload, function(responseText) { 
    859857        var metadata = new GSMetadata(collection, site, documentID, metadataName, null, null, responseText); 
    860858        if(responseFunction != null) 
     
    867865gs.functions.getArchivesMetadata = function(collection, site, documentID, metadataName, metadataPosition, responseFunction) 
    868866{ 
    869     var payload = getBasicDataForMetadataServer("get-archives-metadata", collection, site, documentID, metadataName, null /*metamode*/, null /*metavalue*/, null /*prevmetavalue*/, metadataPosition); 
    870  
    871     callMetadataServer("getArchivesMetadata", payload, function(responseText) { 
     867    var payload = gs.functions.getBasicDataForMetadataServer("get-archives-metadata", collection, site, documentID, metadataName, null /*metamode*/, null /*metavalue*/, null /*prevmetavalue*/, metadataPosition); 
     868 
     869    gs.functions.callMetadataServer("getArchivesMetadata", payload, function(responseText) { 
    872870    var metadata = new GSMetadata(collection, site, documentID, metadataName, null, metadataPosition, responseText); // indexPos, archivesPos, metaval (responseText) 
    873871    if(responseFunction != null) 
     
    880878gs.functions.getIndexMetadata = function(collection, site, documentID, metadataName, metadataPosition, responseFunction) 
    881879{ 
    882     var payload = getBasicDataForMetadataServer("get-metadata", collection, site, documentID, metadataName, null /*metamode*/, null /*metavalue*/, null /*prevmetavalue*/, metadataPosition); 
    883  
    884     callMetadataServer("getIndexMetadata", payload, function(responseText) { 
     880    var payload = gs.functions.getBasicDataForMetadataServer("get-metadata", collection, site, documentID, metadataName, null /*metamode*/, null /*metavalue*/, null /*prevmetavalue*/, metadataPosition); 
     881 
     882    gs.functions.callMetadataServer("getIndexMetadata", payload, function(responseText) { 
    885883    var metadata = new GSMetadata(collection, site, documentID, metadataName, metadataPosition, null, responseText); // indexPos, archivesPos, metaval (responseText) 
    886884     
     
    898896gs.functions.removeImportMetadata = function(collection, site, documentID, metadataName, metadataValue, responseFunction) 
    899897{ 
    900     callMetadataServer( 
     898    gs.functions.callMetadataServer( 
    901899    "removeImportMetadata",  
    902     getBasicDataForMetadataServer("remove-import-metadata", collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, null /*metapos*/),  
     900    gs.functions.getBasicDataForMetadataServer("remove-import-metadata", collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, null /*metapos*/),  
    903901    responseFunction); 
    904902} 
     
    910908    } 
    911909 
    912     callMetadataServer( 
     910    gs.functions.callMetadataServer( 
    913911    "removeArchiveMetadata",  
    914     getBasicDataForMetadataServer("remove-archives-metadata", collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, metadataPosition),  
     912    gs.functions.getBasicDataForMetadataServer("remove-archives-metadata", collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, metadataPosition),  
    915913    responseFunction); 
    916914} 
     
    922920    } 
    923921 
    924     callMetadataServer( 
     922    gs.functions.callMetadataServer( 
    925923    "removeIndexMetadata",  
    926     getBasicDataForMetadataServer("remove-metadata", collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, metadataPosition),  
     924    gs.functions.getBasicDataForMetadataServer("remove-metadata", collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, metadataPosition),  
    927925    responseFunction); 
    928926} 
     
    935933    for(var i = 0; i < nameArray.length; i++) 
    936934    { 
    937         callMetadataServer( 
     935        gs.functions.callMetadataServer( 
    938936        nameArray[i], 
    939         getBasicDataForMetadataServer(functionArray[i], collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, null /*metapos*/), 
     937        gs.functions.getBasicDataForMetadataServer(functionArray[i], collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, null /*metapos*/), 
    940938        responseFunction); 
    941939    } 
    942940} 
     941 
  • 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 
  • main/trunk/greenstone3/web/interfaces/default/transform/layouts/usercomments.xsl

    r31557 r31558  
    4848      </div> 
    4949       
    50       <input type="submit" id="usercommentSubmitButton" onclick="addUserComment(document.AddUserCommentForm.username.value, document.AddUserCommentForm.comment.value, document.AddUserCommentForm.d.value, document); return false;"><xsl:attribute name="value"><xsl:value-of select="util:getInterfaceText($interface_name, /page/@lang, 'usercomments.submit')"/></xsl:attribute></input> 
     50      <input type="submit" id="usercommentSubmitButton" onclick="gs.usercomments.addUserComment(document.AddUserCommentForm.username.value, document.AddUserCommentForm.comment.value, document.AddUserCommentForm.d.value, document); return false;"><xsl:attribute name="value"><xsl:value-of select="util:getInterfaceText($interface_name, /page/@lang, 'usercomments.submit')"/></xsl:attribute></input> 
    5151      <label id="usercommentfeedback"><xsl:comment>Text to prevent empty tags from becoming self-closing tags</xsl:comment></label> 
    5252