Ignore:
Timestamp:
2017-03-29T22:38:36+13:00 (7 years ago)
Author:
ak19
Message:
  1. Drastic changes to javascript-global-functions.js, specifically the functions that invoke callMetadataServer() and the code internal to callMetadataServer(). In the past, the AJAX calls to metadataserver.pl were performed with URLs and jQuery .ajax() calls, thus ending up as GET. This was corrected in a recent commit using gsajaxapi.js, to use POST, but still using URLs and not using jQuery. The callMetadataServer() function has been altered to now allow GET and POST as request method, to allow use of jQuery .ajax() and gsajaxapi.js to make the AJAX call, and to allow sending the data to be transmitted over AJAX (payload) as either URL or as a JavaScript object (data structure). Tested get-meta-array and set-meta-array calls via testing GS3 user comments, but have not tested the singleton set-meta and get-meta and remove-meta functions that are equally affected by the major code alterations. Those need to still be tested with GS3's online document editing. But even that may not exercise all the JavaScript metadataserver related functions in javascript-global-functions.js, if only a select set are ever called by the Document Editor. 2. The user_comments.js file has been modified to accept either the XMLHttpRequest object returned by gsajaxapi.js AJAX calls or the string objects returned by jQuery's .ajax() calls.
Location:
main/trunk/greenstone3/web/interfaces/default/js
Files:
2 edited

Legend:

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

    r31545 r31547  
    422422}
    423423
    424 // No function overloading in JavaScript. Can pass a custom object, however, see
    425 // http://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices
    426 function callMetadataServerGET(callingFunction, url, responseFunction, opts)
    427 {
    428     var async_setting = true; // Internal processing of 'read' operations (get meta) is not order dependent
    429 
     424// This method performs an AJAX call after working out, based on parameters and internal decision-making code,
     425// if it's using GET or POST,
     426// asynchronous or synchronous AJAX,
     427// jQuery's .ajax() method or gsajaxapi.js' regular JavaScript way of calling AJAX (necessary functions
     428// now ported from GS2 to GS3)
     429// and whether it needs to transmit the payload in URL or data structure (Java object) form.
     430// In the past, the AJAX calls to metadataserver.pl only dealt with URLs and used jQuery .ajax(). As a
     431// consequence of this particular combination, the calls in the past were all GET operations.
     432//
     433// - payload param: contains both the URL form and the data object form of the package to transmit over
     434// AJAX to metadataserver.pl. Based on the parameters and some internal variables, callMetadataServer()
     435// determines which to use.
     436// - opts param: No function overloading in JavaScript. Can pass a custom object, however can pass opts,
     437// see http://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices
     438function callMetadataServer(callingFunction, payload, responseFunction, opts)
     439{
     440
     441    // async AJAX by default for get operations: because internal processing of 'read' operations (get meta)
     442    // is not order dependent.
     443    // Set/remove operations will switch to synchronous AJAX, unless opt["forceSync"] is set otherwise
     444    var async_setting = true;
     445    var method = "GET"; // GET was the default before
     446   
     447    // Set to false if you wish to use the regular JavaScript AJAX way in gsajaxapi.js (will use payload.url)
     448    // Set to true if using jQuery AJAX (will use payload.data).
     449    var _use_jQuery_ajax_not_gsajaxapi = true;
     450
     451    // _use_payload_in_data_not_url_form is determined based on vars method and _use_jQuery_ajax_not_gsajaxapi
     452    // If using AJAX with payload data (with jQuery) rather than using URLs containing data (payload in url):
     453    // using data will allow us to use jQuery to POST stuff too.
     454    // For gsajaxapi, payload to be transmitted over AJAX must be in URL form, whether GET or POST.
     455    // For jQuery, AJAX calls ended up as GET when the payload is in URL form.
     456    // Default used to be payload in url form. To get the default back,
     457    // set method = "GET" (above, but also in calling functions!) and set the default here below
     458    // for _use_payload_in_data_not_url_form to false.   
     459    var _use_payload_in_data_not_url_form = false;
     460
     461    var _modifyingMeta  = false;
     462
     463    var url = payload["url"]; // for jQuery GET, and for GET and POST using JavaScript AJAX
     464    var data = payload["data"]; // for jQuery POST
     465
     466
     467    // check for any caller overrides
     468    if(opts != null) {
     469    //if(opts["use_payload_in_data_not_url_form"] != null) {
     470      //  _use_payload_in_data_not_url_form = opts["use_payload_in_data_not_url_form"];
     471    //}
     472    if(opts["requestMethod"] != null) {
     473        method = opts["requestMethod"];
     474    }
     475    }
     476   
     477    // sync or async? Generally, synchronous AJAX for set-meta operations, and asynchronous for get-meta ops
     478    var metaServerCommand = (data["s1.a"] == null) ? data["a"] : data["s1.a"];
     479    if(metaServerCommand.indexOf("set-") != -1 || metaServerCommand.indexOf("remove-") != -1) {
     480    _modifyingMeta  = true;
     481    async_setting = false; // for 'write' operations (set/remove meta), we force sequential processing of the internal operation.
     482
     483    // for meta modification operatons, should we make the method=POST??????
     484    // method = "POST";
     485    }
     486    // check for any overrides by calling code that knows what it's doing
     487    if (opts != null && opts["forceSync"] != null) {
     488    async_setting = (!opts["forceSync"]);
     489    }
     490
     491    if(_use_jQuery_ajax_not_gsajaxapi) {
     492    if(method == "POST") {
     493        _use_payload_in_data_not_url_form = true;
     494    }  // if GET, can use payload in URL form or in data form for jQuery AJAX
     495    // to put it another way: can't do jQuery POST operations with payload in URL form
     496
     497    } else { // using gsajaxapi.js, which only accepts the payload in URL form, whether GET or POST
     498    _use_payload_in_data_not_url_form = false;
     499    }
     500
     501    // use the URL form or the data form to transmit the payload over AJAX?
     502    // Payload in data form implies jQuery AJAX, not gsajaxapi calls,
     503    // since we can't use gsajaxapi.js AJAX GET/POST calls without payload in URL form
     504    if(_use_payload_in_data_not_url_form) { // using data payload to do AJAX (regardless of request method)
     505   
     506    //method = "POST";
     507
     508    // for get-meta operations, go directly through metadata-server.pl
     509    // for set-meta ops, should go via GS3 authentication, which is off the GS3 library servlet
     510    url = (_modifyingMeta) ? gs.xsltParams.library_name : "cgi-bin/metadata-server.pl";     
     511
     512    } else { // uses data encoded into URL, rather than a data structure.
     513    data = null; // we're using the URL as payload, don't duplicate the payload to be transmitted into data
     514   
     515    url = payload["url"]; // payload["url"] contains the URL + data encoded in URL form
     516    // URL is already correct for get-meta vs meta-modification operations.
     517    // For meta-modification ops, it will through GS3 authentication first rather than metadata-server.pl
     518   
     519    }
     520
     521    // finally, can do the AJAX call
     522
     523    console.log("*** Away to call: " + url);
     524    var ajaxResponse = async_setting ? "*** No response received yet, async ajax request" : null;
     525
     526
     527    if(_use_jQuery_ajax_not_gsajaxapi) {
     528    // ajax calls default to using method GET, we want to do POST operations for get-meta and set-meta requests
     529    // since get-meta-array and especially set-meta-array can be large, e.g. for user comments.
     530    $.ajax({url: url, async: async_setting, type: method, data: data})
     531        .success(function(response) {
     532        ajaxResponse = response;
     533        console.log("** (" + callingFunction + ") Response received from server: " + ajaxResponse);
     534       
     535        //var xml = $.parseXML(response);
     536        //console.log(xml);
     537       
     538        if(responseFunction != null) {
     539           
     540            responseFunction(response);
     541        }
     542        })
     543        .error(function() {
     544        console.log("(" + callingFunction + ") Failed");
     545        });
     546    }
     547    else {
     548    // USES GSAJAXAPI.JS to do AJAX. In this case, the payload must be in URL form
     549   
     550    var splitURL = url.split("?");
     551    url = splitURL[0]; // base URL
     552    var params = splitURL[1]; // query-string
     553
     554    // Don't need to delete objects created with 'new' in JavaScript. Garbage collection will do it.
     555    // http://stackoverflow.com/questions/4869712/new-without-delete-on-same-variable-in-javascript
     556    var gsapi = new GSAjaxAPI(url);
     557   
     558    // ajax calls default to using method GET, we want to do POST operations for get-meta and set-meta requests
     559    // since get-meta-array and especially set-meta-array can be large, e.g. for user comments.
     560   
     561    if(async_setting) {
     562        gsapi.urlPostAsync(url, params, responseFunction);
     563    } else {
     564        ajaxResponse = gsapi.urlPostSync(url, params);
     565        ajaxResponse = ajaxResponse;
     566    }
     567   
     568    console.log("*** (" + callingFunction + ") Response from server: " + ajaxResponse);
     569
     570    }
     571   
     572    console.log("*** Finished ajax call to: " + url);   
     573    console.log("*** Got response: " + ajaxResponse);
     574
     575    return ajaxResponse;
     576}
     577
     578// Prepare the payload (data package) to transmit to metadataserver.pl over AJAX.
     579// These next 2 functions prepare both the URL version of the payload and the data object version of
     580// of the payload. Then calling functions, and the callMetadataServer() function they call, will control
     581// and determine which of the two forms ultimately gets used.
     582// UNUSED: http://stackoverflow.com/questions/8648892/convert-url-parameters-to-a-javascript-object
     583function getBasicDataForMetadataServer(metaServerCommand, collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, metaPosition) {
     584
     585    // if we're doing set- or remove- metadata operations,
     586    // then we need change the data params that will make up the query string
     587    // to make sure we go through GS3's authentication
     588    // 1. prefix meta names with s1,
     589    // 2. use s1.collection for collectionName since c is a special param name for GS2Construct
     590    // 3. Additional parameters for rerouting through Authentication: a=g&rt=r&ro=1&s=ModifyMetadata
     591
     592    var modifyingMeta  = false;
     593    var prefix = "";
     594    var colPropName = "c";
     595    var baseURL = "cgi-bin/metadata-server.pl?";
     596
     597    // if we need authentication:
     598    if(metaServerCommand.indexOf("set-") != -1 || metaServerCommand.indexOf("remove-") != -1) {
     599    modifyingMeta  = true;
     600    prefix = "s1.";
     601    colPropName = prefix+"collection"; // "s1.collection"
     602    baseURL = gs.xsltParams.library_name + "?a=g&rt=r&ro=1&s=ModifyMetadata&";
     603    }
     604
     605
     606    // 1. when using jQuery to POST, need to invoke AJAX with a data structure rather than a URL
     607    var data = {};
     608
     609    // customizable portion of ajax call
     610    data[prefix+"a"] = metaServerCommand;
     611    data[colPropName] = collection;
     612    data[prefix+"site"] = site;
     613    data[prefix+"d"] = documentID;
     614    data[prefix+"metaname"] = metadataName;
     615    data[prefix+"metapos"] = metadataPosition;
     616    data[prefix+"metavalue"] = metadataValue;
     617    data[prefix+"prevmetavalue"] = prevMetadataValue;
     618    data[prefix+"metamode"] = metamode;
     619
     620    if(modifyingMeta) {
     621    // fixed portion of url: add the a=g&rt=r&ro=1&s=ModifyMetadata part of the GS3 URL for
     622    // going through authentication. Don't prefix "s1." to these!
     623    data["a"] = "g";
     624    data["rt"] = "r";
     625    data["ro"] = "1";
     626    data["s"] = "ModifyMetadata";
     627    }
     628
     629    // 2. Construct the URL version of the metadata-server.pl operation:
     630    // for GET requests, the URL can contain the data.
     631    // Regular JavaScript AJAX code in gsajaxapi.js can also POST data in URL form, but not jQuery's .ajax().
     632
     633       
    430634    // If doing set- or remove- (not get-) metadata, then rewrite URLs to call GS2Construct's ModfiyMetadata service instead (which will ensure this only works when authenticated).
    431635    // From:
     
    434638    // <gs3server>/library?a=g&rt=r&ro=1&s=ModifyMetadata&s1.a=set-archives-metadata&s1.collection=smallcol&s1.site=localsite&s1.d=HASH01454f31011f6b6b26eaf8d7&s1.metaname=Title&s1.metavalue=Moo&s1.prevmetavalue=Blabla&s1.metamode=override
    435639
    436     // if we're doing a set- or remove- metadata operations, then we'll be changing the URL to make sure we go through GS3's authentication
    437     if(url.indexOf("set-") != -1 || url.indexOf("remove-") != -1) {
    438    
    439     url = url.replace("&c=",  "&collection="); // c is a special param name for GS2Construct
    440     url = url.replace(/(&|\?)([^=]*=)/g, "$1"+"s1.$2"); // prefix param names with "s1."
    441     url = url.replace("cgi-bin/metadata-server.pl?",  gs.xsltParams.library_name + "?a=g&rt=r&ro=1&s=ModifyMetadata&");
    442    
    443     //console.log("@@@@@ URL is " + url);
    444 
    445     async_setting = false; // for 'write' operations (set/remove meta), we force sequential processing of the internal operation.
    446 
    447     } // otherwise, such as for get- metadata operation, we proceed as before, which will not require authentication
    448 
    449     if (opts != null && opts["forceSync"] != null) {
    450     async_setting = (!opts["forceSync"]);
    451     }
    452 
    453     console.log("Away to call: " + url);
    454     var ajaxResponse = null;
    455 
    456     $.ajax(url, {async: async_setting})
    457     .success(function(response)
    458     {
    459         console.log("(" + callingFunction + ") Response received from server: " + ajaxResponse);
    460 
    461         ajaxResponse = response;
    462 
    463         //var xml = $.parseXML(response);
    464         //console.log(xml);
    465        
    466         if(responseFunction != null)
    467         {
    468            
    469             responseFunction(response);
    470         }
    471     })
    472     .error(function()
    473     {
    474         console.log("(" + callingFunction + ") Failed");
    475     });
    476 
    477     console.log("Finished ajax call to: " + url);
    478    
    479     console.log("Got response: " + ajaxResponse);
    480     return ajaxResponse;
    481 }
    482 
    483 
    484 // No function overloading in JavaScript. Can pass a custom object, however, see
    485 // http://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices
    486 function callMetadataServer(callingFunction, url, responseFunction, opts)
    487 {
    488     var async_setting = true; // Internal processing of 'read' operations (get meta) is not order dependent
    489 
    490     // If doing set- or remove- (not get-) metadata, then rewrite URLs to call GS2Construct's ModfiyMetadata service instead (which will ensure this only works when authenticated).
    491     // From:
    492     // <gs3server>/cgi-bin/metadata-server.pl?a=set-archives-metadata&c=smallcol&site=localsite&d=HASH01454f31011f6b6b26eaf8d7&metaname=Title&metavalue=Moo&prevmetavalue=Blabla&metamode=override
    493     // To:
    494     // <gs3server>/library?a=g&rt=r&ro=1&s=ModifyMetadata&s1.a=set-archives-metadata&s1.collection=smallcol&s1.site=localsite&s1.d=HASH01454f31011f6b6b26eaf8d7&s1.metaname=Title&s1.metavalue=Moo&s1.prevmetavalue=Blabla&s1.metamode=override
    495 
    496     // if we're doing a set- or remove- metadata operations, then we'll be changing the URL to make sure we go through GS3's authentication
    497     if(url.indexOf("set-") != -1 || url.indexOf("remove-") != -1) {
    498    
    499     url = url.replace("&c=",  "&collection="); // c is a special param name for GS2Construct
    500     url = url.replace(/(&|\?)([^=]*=)/g, "$1"+"s1.$2"); // prefix param names with "s1."
    501     url = url.replace("cgi-bin/metadata-server.pl?",  gs.xsltParams.library_name + "?a=g&rt=r&ro=1&s=ModifyMetadata&");
    502    
    503     //console.log("@@@@@ URL is " + url);
    504 
    505     async_setting = false; // for 'write' operations (set/remove meta), we force sequential processing of the internal operation.
    506 
    507     } // otherwise, such as for get- metadata operation, we proceed as before, which will not require authentication
    508 
    509     if (opts != null) {
    510     if(opts["forceSync"] != null) {
    511         async_setting = (!opts["forceSync"]);
    512     }
    513     }
    514 
    515     console.log("Away to call: " + url);
    516     var ajaxResponse = "No response received yet, async ajax request";
    517 
    518     var splitURL = url.split("?");
    519     url = splitURL[0];
    520     var params = splitURL[1];
    521     var gsapi = new GSAjaxAPI(url);
    522 
    523     // ajax calls default to using method GET, we want to do POST operations for get-meta and set-meta requests
    524     // since get-meta-array and especially set-meta-array can be large, e.g. for user comments.
    525 
    526     if(async_setting) {
    527     gsapi.urlPostAsync(url, params, responseFunction);
    528     } else {
    529     ajaxResponse = gsapi.urlPostSync(url, params); 
    530     }
    531 
    532     console.log("(" + callingFunction + ") Response received from server: " + ajaxResponse);
    533 
    534     console.log("Finished ajax call to: " + url);
    535    
    536     console.log("Got response: " + ajaxResponse);
    537     return ajaxResponse;
    538 }
    539 
     640    var extraParams = "";
     641   
     642    if(metadataValue != null) {
     643    extraParams += "&"+prefix+"metavalue=" + metadataValue;
     644    }
     645   
     646    if(metadataPosition != null)
     647    {
     648    extraParams += "&"+prefix+"metapos=" + metadataPosition;       
     649    }
     650   
     651    if(prevMetadataValue != null) {
     652    extraParams += "&"+prefix+"prevmetavalue=" + prevMetadataValue;
     653    }
     654   
     655    var url = baseURL + prefix+"a=" + metaServerCommand + "&"+colPropName+"=" + collection + "&"+prefix+"site=" + site + "&"+prefix+"d=" + documentID + "&"+prefix+"metaname=" + metadataName + extraParams + "&"+prefix+"metamode=" + metamode;
     656
     657    // 3. Return both the constructed url & data variants of the payload to be transmitted over ajax
     658    var payload = {
     659    url: url,   
     660    data: data
     661    };
     662
     663    return payload;
     664}
     665
     666// See description for getBasicDataForMetadataServer()
     667function getComplexDataForMetadataServer(metaServerCommand, collection, site, docArray, metamode, where) {
     668
     669    var docArrayJSON = JSON.stringify(docArray);
     670   
     671    // if we're doing set- or remove- metadata operations,
     672    // then we need change the data params that will make up the query string
     673    // to make sure we go through GS3's authentication
     674    // 1. prefix meta names with s1,
     675    // 2. use s1.collection for collectionName since c is a special param name for GS2Construct
     676    // 3. Additional parameters for rerouting through Authentication: a=g&rt=r&ro=1&s=ModifyMetadata
     677
     678    var modifyingMeta  = false;
     679    var prefix = "";
     680    var colPropName = "c";
     681    var baseURL = "cgi-bin/metadata-server.pl?";
     682
     683    // if we need authentication:
     684    if(metaServerCommand.indexOf("set-") != -1 || metaServerCommand.indexOf("remove-") != -1) {
     685    modifyingMeta  = true;
     686    prefix = "s1.";
     687    colPropName = prefix+"collection"; // "s1.collection"
     688    baseURL = gs.xsltParams.library_name + "?a=g&rt=r&ro=1&s=ModifyMetadata&";
     689    }
     690
     691    // 1. when using jQuery to POST, need to invoke AJAX with a data structure rather than a URL
     692    var data = {};
     693
     694    // customizable portion of ajax call
     695    data[prefix+"a"] = metaServerCommand;
     696    data[colPropName] = collection;
     697    data[prefix+"site"] = site;
     698    data[prefix+"json"] = docArrayJSON;
     699   
     700    if(where != null) {
     701    data[prefix+"where"] = where;
     702    }
     703    if (metamode!=null) {
     704    data[prefix+"metamode"] = metamode;
     705    }
     706
     707    if(modifyingMeta) {
     708    // fixed portion of url: add the a=g&rt=r&ro=1&s=ModifyMetadata part of the GS3 URL for
     709    // going through authentication. Don't prefix "s1." to these!
     710    data["a"] = "g";
     711    data["rt"] = "r";
     712    data["ro"] = "1";
     713    data["s"] = "ModifyMetadata";
     714    }
     715   
     716
     717    // 2. URL for when doing AJAX in URL mode. GET with jQuery allows the data to be part of the URL, but
     718    // not jQuery POST. But our regular JavaScript AJAX code in gsajaxapi.js allows GET and POST with URLs
     719    // containing the data.   
     720   
     721    var params = prefix+"a=" + escape(metaServerCommand); //"a=set-metadata-array";
     722    if(where != null) {
     723    params += "&"+prefix+"where=" + escape(where); // if where not specified, meta-server will default to setting index meta
     724    //} else {
     725    //    params += "&"+prefix+"where=import|archives|index";
     726    }
     727    params += "&"+colPropName+"="+escape(collection);
     728    params += "&"+prefix+"site="+escape(site);
     729    params += "&"+prefix+"json="+escape(docArrayJSON);
     730   
     731    if (metamode!=null) {
     732    params += "&"+prefix+"metamode=" + escape(metamode);
     733    }
     734
     735    // 3. Return both the constructed url & data variants of the payload to be transmitted over ajax
     736    var payload = {
     737    url: baseURL + params,   
     738    data: data
     739    };
     740
     741    return payload;
     742}
    540743
    541744/*************************
     
    543746*************************/
    544747
     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
    545750gs.functions.setImportMetadata = function(collection, site, documentID, metadataName, metadataValue, prevMetadataValue, metamode, responseFunction)
    546 {
    547     callMetadataServer("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);
     751{   
     752
     753    callMetadataServer(
     754    "setImportMetadata",
     755    getBasicDataForMetadataServer("set-import-metadata", collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, null /*metapos*/),
     756    responseFunction);
     757   
    548758}
    549759
    550760gs.functions.setArchivesMetadata = function(collection, site, documentID, metadataName, metadataPosition, metadataValue, prevMetadataValue, metamode, responseFunction)
    551761{
    552     if(metadataPosition != null)
    553     {
    554         callMetadataServer("setArchivesMetadata", "cgi-bin/metadata-server.pl?a=set-archives-metadata&c=" + collection + "&site=" + site + "&d=" + documentID + "&metaname=" + metadataName + "&metapos=" + metadataPosition + "&metavalue=" + metadataValue + "&metamode=" + metamode, responseFunction);
    555     }
    556     else if(prevMetadataValue != null)
    557     {
    558         callMetadataServer("setArchivesMetadata", "cgi-bin/metadata-server.pl?a=set-archives-metadata&c=" + collection + "&site=" + site + "&d=" + documentID + "&metaname=" + metadataName + "&metavalue=" + metadataValue + "&prevmetavalue=" + prevMetadataValue + "&metamode=" + metamode, responseFunction);
    559     }
    560     else
    561     {
    562         callMetadataServer("setArchivesMetadata", "cgi-bin/metadata-server.pl?a=set-archives-metadata&c=" + collection + "&site=" + site + "&d=" + documentID + "&metaname=" + metadataName + "&metavalue=" + metadataValue + "&metamode=" + metamode, responseFunction);
    563     }
     762    if(metadataPosition != null) {
     763    prevMetadataValue = null; // to force the same ultimate behaviour as in the old version of this code
     764    }
     765
     766    callMetadataServer(
     767    "setArchivesMetadata",
     768    getBasicDataForMetadataServer("set-archives-metadata", collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, metadataPosition),
     769    responseFunction);
     770
    564771}
    565772
    566773gs.functions.setIndexMetadata = function(collection, site, documentID, metadataName, metadataPosition, metadataValue, prevMetadataValue, metamode, responseFunction)
    567774{
    568     if(metadataPosition != null)
    569     {
    570         callMetadataServer("setIndexMetadata", "cgi-bin/metadata-server.pl?a=set-metadata&c=" + collection + "&site=" + site + "&d=" + documentID + "&metaname=" + metadataName + "&metapos=" + metadataPosition + "&metavalue=" + metadataValue + "&metamode=" + metamode, responseFunction);
    571     }
    572     else if(prevMetadataValue != null)
    573     {
    574         callMetadataServer("setIndexMetadata", "cgi-bin/metadata-server.pl?a=set-metadata&c=" + collection + "&site=" + site + "&d=" + documentID + "&metaname=" + metadataName + "&metavalue=" + metadataValue + "&prevmetavalue=" + prevMetadataValue + "&metamode=" + metamode, responseFunction);
    575     }
     775    if(metadataPosition != null) {
     776    prevMetadataValue = null; // to force the same ultimate behaviour as in the old version of this code
     777    }
     778
     779    // old version of this function would only call callMetadataServer if either metapos
     780    // or prevMetaValue had a value. So sticking to the same behaviour in rewriting this function.
     781    if(metadataPosition != null || prevMetadataValue != null) {
     782   
     783    callMetadataServer(
     784        "setIndexMetadata",
     785        getBasicDataForMetadataServer("set-metadata", collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, metadataPosition),
     786        responseFunction);
     787    }
    576788}
    577789
     
    583795    for(var i = 0; i < nameArray.length; i++)
    584796    {
    585         callMetadataServer(nameArray[i], "cgi-bin/metadata-server.pl?a=" + functionArray[i] + "&c=" + collection + "&site=" + site + "&d=" + documentID + "&metaname=" + metadataName + "&metavalue=" + metadataValue + "&metamode=" + metamode, responseFunction);
     797        // previous version of this function did not allow setting metapos or prevMetavalue
     798        // so leaving the behaviour the same along with function signature.
     799        callMetadataServer(
     800        nameArray[i],
     801        getBasicDataForMetadataServer(functionArray[i], collection, site, documentID, metadataName, metamode, metadataValue, null /*prevMetadataValue*/, null /*metadataPosition*/),
     802        responseFunction);
    586803    }
    587804}
     
    591808// separated by |. If null, it is assumed to be index which is the original default
    592809// behaviour of calling set-metadata-array. E.g. where=import|archives|index
    593 // THIS METHOD IS SYNCHRONOUS
     810// THIS METHOD IS SYNCHRONOUS by default. Set forceSync to false to override this default behaviour
    594811gs.functions.setMetadataArray = function(collection, site, docArray, metamode, where, responseFunction, forceSync)
    595 {
    596     var docArrayJSON = JSON.stringify(docArray);
    597    
    598     var params = "a=" + escape("set-metadata-array"); //"a=set-metadata-array";
    599     if(where != null) {
    600     params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
    601     //} else {
    602     //    params += "&where=import|archives|index";
    603     }
    604     params += "&c="+escape(collection);
    605     params += "&site="+escape(site);
    606     params += "&json="+escape(docArrayJSON);
    607    
    608     if (metamode!=null) {
    609     params += "&metamode=" + escape(metamode);
    610     }
     812
     813
     814    var payload = getComplexDataForMetadataServer("set-metadata-array", collection, site, docArray, metamode, where);
    611815   
    612816    // set operations are generally synchronous, but allow calling function to force ajax call
     
    615819    forceSync = true;
    616820    }
    617 
    618     var response = callMetadataServer("Setting metadata in "+where, "cgi-bin/metadata-server.pl?"+params, responseFunction, {"forceSync": forceSync});
    619 
     821   
     822    //console.log("cgi-bin/metadata-server.pl?"+params);
     823   
     824    var response = callMetadataServer("Setting metadata in "+where, payload, responseFunction, {"forceSync": forceSync, "requestMethod": "POST"});
     825   
    620826    return response;
    621     // return this.urlPostSync(mdserver,params); // gsajaxapi.js version for GS2
    622827}
    623828
     
    632837gs.functions.getMetadataArray = function(collection, site, docArray, where, responseFunction, forceSync)
    633838{
    634     var docArrayJSON = JSON.stringify(docArray);
    635    
    636     var params = "a=" + escape("get-metadata-array"); //"a=set-metadata-array";
    637     if(where != null) {
    638     params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta
    639     //} else {
    640     //    params += "&where=import|archives|index";
    641     }
    642     params += "&c="+escape(collection);
    643     params += "&site="+escape(site);
    644     params += "&json="+escape(docArrayJSON);
    645        
     839    var payload = getComplexDataForMetadataServer("get-metadata-array", collection, site, docArray, null /*metamode*/, where);   
     840
    646841    // get operations are generally asynchronous, but allow calling function to force ajax call
    647     // to be synchronous or not. Default is synchronous, as it was for GS2
     842    // to be synchronous or not. Default for get-metadata-array is synchronous, as it was for GS2
    648843    if(forceSync == null) {
    649844    forceSync = true;
     
    652847    // http://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices
    653848    // https://www.w3schools.com/js/js_objects.asp
    654     var response = callMetadataServer("Getting metadata from "+where, "cgi-bin/metadata-server.pl?"+params, responseFunction, {"forceSync":forceSync});
     849    var response = callMetadataServer("Getting metadata from "+where, payload, responseFunction, {"forceSync":forceSync, "requestMethod": "POST"});
    655850
    656851    return response;
    657     //return this.urlPostSync(mdserver,params); // gsajaxapi.js version for GS2
    658852}
    659853
     
    661855gs.functions.getImportMetadata = function(collection, site, documentID, metadataName, responseFunction)
    662856{
    663     var url = "cgi-bin/metadata-server.pl?a=get-import-metadata&c=" + collection + "&site=" + site + "&d=" + documentID + "&metaname=" + metadataName;
    664     callMetadataServer("getImportMetadata", url, function(responseText)
    665     {
     857    var payload = getBasicDataForMetadataServer("get-import-metadata", collection, site, documentID, metadataName);
     858    callMetadataServer("getImportMetadata", payload, function(responseText) {
    666859        var metadata = new GSMetadata(collection, site, documentID, metadataName, null, null, responseText);
    667860        if(responseFunction != null)
     
    672865}
    673866
    674 gs.functions.getArchivesMetadata = function(collection, site, documentID, metadataName, metadataPosition,  responseFunction)
    675 {
    676     var url = "cgi-bin/metadata-server.pl?a=get-archives-metadata&c=" + collection + "&site=" + site + "&d=" + documentID + "&metaname=" + metadataName;
    677     if(metadataPosition != null)
    678     {
    679         url += "&metapos=" + metadataPosition;
    680     }
    681 
    682     callMetadataServer("getArchivesMetadata", url, function(responseText)
    683     {
    684         var metadata = new GSMetadata(collection, site, documentID, metadataName, null, metadataPosition, responseText); // indexPos, archivesPos, metaval (responseText)
    685         if(responseFunction != null)
    686         {
    687             responseFunction(metadata);
    688         }
    689     });
     867gs.functions.getArchivesMetadata = function(collection, site, documentID, metadataName, metadataPosition, responseFunction)
     868{
     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) {
     872    var metadata = new GSMetadata(collection, site, documentID, metadataName, null, metadataPosition, responseText); // indexPos, archivesPos, metaval (responseText)
     873    if(responseFunction != null)
     874    {
     875        responseFunction(metadata);
     876    }
     877    });
    690878}
    691879
    692880gs.functions.getIndexMetadata = function(collection, site, documentID, metadataName, metadataPosition, responseFunction)
    693881{
    694     var url = "cgi-bin/metadata-server.pl?a=get-metadata&c=" + collection + "&site=" + site + "&d=" + documentID + "&metaname=" + metadataName;
    695     if(metadataPosition != null)
    696     {
    697         url += "&metapos=" + metadataPosition;
    698     }
    699 
    700     callMetadataServer("getIndexMetadata", url, function(responseText)
    701     {
    702         var metadata = new GSMetadata(collection, site, documentID, metadataName, metadataPosition, null, responseText); // indexPos, archivesPos, metaval (responseText)
    703        
    704         if(responseFunction != null)
    705         {
    706             responseFunction(metadata);
    707         }
    708     });
     882    var payload = getBasicDataForMetadataServer("get-metadata", collection, site, documentID, metadataName, null /*metamode*/, null /*metavalue*/, null /*prevmetavalue*/, metadataPosition);
     883
     884    callMetadataServer("getIndexMetadata", payload, function(responseText) {
     885    var metadata = new GSMetadata(collection, site, documentID, metadataName, metadataPosition, null, responseText); // indexPos, archivesPos, metaval (responseText)
     886   
     887    if(responseFunction != null)
     888    {
     889        responseFunction(metadata);
     890    }
     891    });
    709892}
    710893
     
    715898gs.functions.removeImportMetadata = function(collection, site, documentID, metadataName, metadataValue, responseFunction)
    716899{
    717     callMetadataServer("removeImportMetadata", "cgi-bin/metadata-server.pl?a=remove-import-metadata&c=" + collection + "&site=" + site + "&d=" + documentID + "&metavalue=" + metadataValue + "&metaname=" + metadataName, responseFunction);
     900    callMetadataServer(
     901    "removeImportMetadata",
     902    getBasicDataForMetadataServer("remove-import-metadata", collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, null /*metapos*/),
     903    responseFunction);
    718904}
    719905
    720906gs.functions.removeArchivesMetadata = function(collection, site, documentID, metadataName, metadataPosition, metadataValue, responseFunction)
    721 {   
    722     if(metadataPosition != null)
    723     {
    724         callMetadataServer("removeArchiveMetadata", "cgi-bin/metadata-server.pl?a=remove-archives-metadata&c=" + collection + "&site=" + site + "&d=" + documentID + "&metaname=" + metadataName + "&metapos=" + metadataPosition, responseFunction);
    725     }
    726     else if(metadataValue != null)
    727     {
    728         callMetadataServer("removeArchiveMetadata", "cgi-bin/metadata-server.pl?a=remove-archives-metadata&c=" + collection + "&site=" + site + "&d=" + documentID  + "&metavalue=" + metadataValue + "&metaname=" + metadataName, responseFunction);
    729     }
     907{
     908    if(metadataPosition != null) {
     909    metadataValue = null; // retaining behaviour of previous version of this function removeArchivesMetadata()
     910    }
     911
     912    callMetadataServer(
     913    "removeArchiveMetadata",
     914    getBasicDataForMetadataServer("remove-archives-metadata", collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, metadataPosition),
     915    responseFunction);
    730916}
    731917
    732918gs.functions.removeIndexMetadata = function(collection, site, documentID, metadataName, metadataPosition, metadataValue, responseFunction)
    733919{
    734     if(metadataPosition != null)
    735     {
    736         callMetadataServer("removeIndexMetadata", "cgi-bin/metadata-server.pl?a=remove-metadata&c=" + collection + "&site=" + site + "&d=" + documentID + "&metaname=" + metadataName + "&metapos=" + metadataPosition, responseFunction);
    737     }
    738     else if(metadataValue != null)
    739     {
    740         callMetadataServer("removeIndexMetadata", "cgi-bin/metadata-server.pl?a=remove-metadata&c=" + collection + "&site=" + site + "&d=" + documentID  + "&metavalue=" + metadataValue + "&metaname=" + metadataName, responseFunction);
    741     }
     920    if(metadataPosition != null) {
     921    metadataValue = null; // retaining behaviour of previous version of this function removeIndexMetadata()
     922    }
     923
     924    callMetadataServer(
     925    "removeIndexMetadata",
     926    getBasicDataForMetadataServer("remove-metadata", collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, metadataPosition),
     927    responseFunction);
    742928}
    743929
     
    749935    for(var i = 0; i < nameArray.length; i++)
    750936    {
    751         callMetadataServer(nameArray[i], "cgi-bin/metadata-server.pl?a=" + functionArray[i] + "&c=" + collection + "&site=" + site + "&d=" + documentID + "&metavalue=" + metadataValue + "&metaname=" + metadataName, responseFunction);
    752     }
    753 }
     937        callMetadataServer(
     938        nameArray[i],
     939        getBasicDataForMetadataServer(functionArray[i], collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, null /*metapos*/),
     940        responseFunction);
     941    }
     942}
  • main/trunk/greenstone3/web/interfaces/default/js/user_comments.js

    r31543 r31547  
    7777}
    7878
    79 function loadedUserComments(xmlHttpObj)
     79function loadedUserComments(data)
    8080{
    8181    // don't bother displaying comments if we're not on a document page
     
    8888    }
    8989
    90     var json_result_str = xmlHttpObj.responseText;
     90    // data is xmlHttpRequest Object if gsajaxapi is used for the ajax call.
     91    // And data is a string if jQuery AJAX was used.
     92    // Using JavaScript's feature sensing to detect which of the two we're dealing with:
     93    var json_result_str = (data.responseText) ? data.responseText : data;
     94        // http://stackoverflow.com/questions/6286542/how-can-i-check-if-a-var-is-a-string-in-javascript
     95        //var json_result_str = (typeof data !== 'string') ? data.responseText : data;
     96        //alert("Type of ajax get result: " + typeof (data));
     97
    9198    //  alert(json_result_str);
    9299    console.log("Got to display: " + json_result_str);
     
    280287   
    281288    //var result = gs.functions.setMetadataArray(gs.variables["c"], gs.variables["site"], docArray, "accumulate", "import|archives|index");
    282     gs.functions.setMetadataArray(gs.variables["c"], gs.variables["site"], docArray, "accumulate", "import|archives|index", function(xmlHttpObj) { return doneUpdatingMetatada(xmlHttpObj, _username, _timestamp, _comment); }, false); // false for asynchronous,
     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,
    283290    // this is ok since we're disabling the comment submit button, so no further set-meta-array calls can be
    284291    // made until the ajax call returns and the callback is called which re-enables the submit button
     
    289296}
    290297
    291 function doneUpdatingMetatada(xmlHttpObj, _username, _timestamp, _comment)
     298function doneUpdatingMetatada(data, _username, _timestamp, _comment)
    292299{
    293     var result = xmlHttpObj.responseText;
     300
     301    // data is xmlHttpRequest Object if gsajaxapi is used for the ajax call.
     302    // And data is a string if jQuery AJAX was used.
     303    // Using JavaScript's feature sensing to detect which of the two we're dealing with:
     304    var result = (data.responseText) ? data.responseText : data;       
     305        // http://stackoverflow.com/questions/6286542/how-can-i-check-if-a-var-is-a-string-in-javascript
     306        //var result = (typeof data !== 'string') ? data.responseText : data;
     307        // alert("Type of ajax set result: " + typeof(data));
     308
    294309    //alert("Received post response to setMeta: " + result); // just the HTML page
    295310
Note: See TracChangeset for help on using the changeset viewer.