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/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
Note: See TracChangeset for help on using the changeset viewer.