Changeset 32873

Show
Ignore:
Timestamp:
07.03.2019 20:51:46 (4 months ago)
Author:
ak19
Message:

javascript-global-functions' local function callMetadataServer() now renamed to _callMetadataServer() and takes and extra parameter BEFORE existing last parameter opts. The extra parameter is errorResponseFunction, which if not null is called on an ajax error if using jQuery mode to do ajax (errorResponseFunction is not used by gsajaxapi yet). All functions that call _callMetadataServer() now also take an extra last param, errorResponseFunction, and force it to null if undefined. It is always the last param for these public functions, so that the existing method calls from outside the file will continue to work. Tested getting and setting regular meta with the doceditor and mapeditor: saving and reloading saved data still works. Not tested set/getMetaArrays used by usercomments, but the changes haven't been drastic or complicated, so shouldn't have broken anything in those 2 functions either.

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

Legend:

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

    r32871 r32873  
    285285    var resultArray = new Array(); 
    286286     
    287     console.log("**** changesToUpdate::editableElementsInitialisationProgress = " + editableElementsInitialisationProgress); 
     287    //console.log("**** changesToUpdate::editableElementsInitialisationProgress = " + editableElementsInitialisationProgress); 
    288288     
    289289    // Only want to check for valid edited states if the editableInitStates has been fully set up: 
  • main/trunk/greenstone3/web/interfaces/default/js/gsajaxapi.js

    r31542 r32873  
    243243    // that we want to dynamically update when the async post process has finished 
    244244     
     245    // NOTE: if passing in an error callback function *separate* from success callback (i.e. if current callback param won't check xmlHttp.status == 200) 
     246    // then see https://stackoverflow.com/questions/1442425/detect-xhr-error-is-really-due-to-browser-stop-or-click-to-new-page 
     247    // and https://stackoverflow.com/questions/8866761/xmlhttprequest-ajax-error 
     248     
    245249    var typeof_callback = typeof(callback); 
    246250    if ((typeof_callback == "string") || (typeof_callback == "number") || (typeof_callback == "boolean")) { 
  • main/trunk/greenstone3/web/interfaces/default/js/javascript-global-functions.js

    r31559 r32873  
    432432// 
    433433// - 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() 
     434// AJAX to metadataserver.pl. Based on the parameters and some internal variables, _callMetadataServer() 
    435435// determines which to use. 
    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 gs.functions.callMetadataServer = function(callingFunction, payload, responseFunction, opts) 
     438// 
     439// BEWARE: 
     440// errorResponseFunction is at present only called on error if using jQuery ajax (sync/async) and not if using gsajaxapi (sync/async). 
     441gs.functions._callMetadataServer = function(callingFunction, payload, successResponseFunction, errorResponseFunction, opts) 
    439442{ 
    440443 
     
    529532        //console.log(xml); 
    530533         
    531         if(responseFunction != null) { 
     534        if(successResponseFunction != null) { 
    532535             
    533             responseFunction(response); 
     536            successResponseFunction(response); 
    534537        } 
    535538        }) 
    536539        .error(function() { 
    537         console.log("(" + callingFunction + ") Failed"); 
     540            if(errorResponseFunction != null) {  
     541                errorResponseFunction(response); 
     542            } else { 
     543                console.log("(" + callingFunction + ") Failed"); 
     544            } 
    538545        }); 
    539546    } 
     
    553560     
    554561    if(async_setting) { 
    555         gsapi.urlPostAsync(url, params, responseFunction); 
     562        gsapi.urlPostAsync(url, params, successResponseFunction); 
    556563    } else { 
    557564        ajaxResponse = gsapi.urlPostSync(url, params); 
     
    571578// Prepare the payload (data package) to transmit to metadataserver.pl over AJAX. 
    572579// These next 2 functions prepare both the URL version of the payload and the data object version of  
    573 // of the payload. Then calling functions, and the callMetadataServer() function they call, will control 
     580// of the payload. Then calling functions, and the _callMetadataServer() function they call, will control 
    574581// and determine which of the two forms ultimately gets used. 
    575582// (Constructing both URL and data structure, as I could not successfully convert old URL way to data structure 
     
    741748*************************/ 
    742749 
    743 gs.functions.setImportMetadata = function(collection, site, documentID, metadataName, metadataValue, prevMetadataValue, metamode, responseFunction) 
     750gs.functions.setImportMetadata = function(collection, site, documentID, metadataName, metadataValue, prevMetadataValue, metamode, successResponseFunction, errorResponseFunction) 
    744751{     
    745  
    746     gs.functions.callMetadataServer( 
     752    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
     753 
     754    gs.functions._callMetadataServer( 
    747755    "setImportMetadata",  
    748756    gs.functions.getBasicDataForMetadataServer("set-import-metadata", collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, null /*metapos*/),  
    749     responseFunction); 
    750      
    751 } 
    752  
    753 gs.functions.setArchivesMetadata = function(collection, site, documentID, metadataName, metadataPosition, metadataValue, prevMetadataValue, metamode, responseFunction) 
    754 { 
     757    successResponseFunction, 
     758    errorResponseFunction); 
     759     
     760} 
     761 
     762gs.functions.setArchivesMetadata = function(collection, site, documentID, metadataName, metadataPosition, metadataValue, prevMetadataValue, metamode, successResponseFunction, errorResponseFunction) 
     763{ 
     764    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
     765     
    755766    if(metadataPosition != null) { 
    756767    prevMetadataValue = null; // to force the same ultimate behaviour as in the old version of this code 
    757768    } 
    758769 
    759     gs.functions.callMetadataServer( 
     770    gs.functions._callMetadataServer( 
    760771    "setArchivesMetadata", 
    761772    gs.functions.getBasicDataForMetadataServer("set-archives-metadata", collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, metadataPosition), 
    762     responseFunction); 
    763  
    764 } 
    765  
    766 gs.functions.setIndexMetadata = function(collection, site, documentID, metadataName, metadataPosition, metadataValue, prevMetadataValue, metamode, responseFunction) 
    767 { 
     773    successResponseFunction, 
     774    errorResponseFunction); 
     775 
     776} 
     777 
     778gs.functions.setIndexMetadata = function(collection, site, documentID, metadataName, metadataPosition, metadataValue, prevMetadataValue, metamode, successResponseFunction, errorResponseFunction) 
     779{ 
     780    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
     781     
    768782    if(metadataPosition != null) { 
    769783    prevMetadataValue = null; // to force the same ultimate behaviour as in the old version of this code 
    770784    } 
    771785 
    772     // old version of this function would only call callMetadataServer if either metapos 
     786    // old version of this function would only call _callMetadataServer if either metapos 
    773787    // or prevMetaValue had a value. So sticking to the same behaviour in rewriting this function. 
    774788    if(metadataPosition != null || prevMetadataValue != null) {  
    775789     
    776     gs.functions.callMetadataServer( 
     790    gs.functions._callMetadataServer( 
    777791        "setIndexMetadata", 
    778792        gs.functions.getBasicDataForMetadataServer("set-metadata", collection, site, documentID, metadataName, metamode, metadataValue, prevMetadataValue, metadataPosition), 
    779         responseFunction); 
    780     } 
    781 } 
    782  
    783 gs.functions.setMetadata = function(collection, site, documentID, metadataName, metadataValue, metamode, responseFunction) 
    784 { 
     793        successResponseFunction, 
     794        errorResponseFunction); 
     795    } 
     796} 
     797 
     798gs.functions.setMetadata = function(collection, site, documentID, metadataName, metadataValue, metamode, successResponseFunction, errorResponseFunction) 
     799{ 
     800    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
     801     
    785802    var nameArray = ["setImportMetadata", "setArchivesMetadata", "setIndexMetadata"]; 
    786803    var functionArray = ["set-import-metadata", "set-archives-metadata", "set-metadata"]; 
     
    790807        // previous version of this function did not allow setting metapos or prevMetavalue 
    791808        // so leaving the behaviour the same along with function signature. 
    792         gs.functions.callMetadataServer( 
     809        gs.functions._callMetadataServer( 
    793810        nameArray[i], 
    794811        gs.functions.getBasicDataForMetadataServer(functionArray[i], collection, site, documentID, metadataName, metamode, metadataValue, null /*prevMetadataValue*/, null /*metadataPosition*/), 
    795         responseFunction); 
     812        successResponseFunction, 
     813        errorResponseFunction); 
    796814    } 
    797815} 
     
    802820// behaviour of calling set-metadata-array. E.g. where=import|archives|index 
    803821// THIS METHOD IS SYNCHRONOUS by default. Set forceSync to false to override this default behaviour 
    804 gs.functions.setMetadataArray = function(collection, site, docArray, metamode, where, responseFunction, forceSync)  
     822gs.functions.setMetadataArray = function(collection, site, docArray, metamode, where, successResponseFunction, forceSync, errorResponseFunction)  
    805823 
     824    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
    806825 
    807826    var payload = gs.functions.getComplexDataForMetadataServer("set-metadata-array", collection, site, docArray, metamode, where); 
     
    815834    //console.log("cgi-bin/metadata-server.pl?"+params); 
    816835     
    817     var response = gs.functions.callMetadataServer("Setting metadata in "+where, payload, responseFunction, {"forceSync": forceSync, "requestMethod": "POST"}); 
     836    var response = gs.functions._callMetadataServer("Setting metadata in "+where, payload, successResponseFunction, errorResponseFunction, {"forceSync": forceSync, "requestMethod": "POST"}); 
    818837     
    819838    return response; 
     
    828847// See description for setMetadataArray above for information about the 'where' parameter. 
    829848// THIS METHOD IS SYNCHRONOUS BY DEFAULT. Set forceSync to false to override this default behaviour 
    830 gs.functions.getMetadataArray = function(collection, site, docArray, where, responseFunction, forceSync) 
    831 { 
     849gs.functions.getMetadataArray = function(collection, site, docArray, where, successResponseFunction, forceSync, errorResponseFunction) 
     850{ 
     851    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
     852 
    832853    var payload = gs.functions.getComplexDataForMetadataServer("get-metadata-array", collection, site, docArray, null /*metamode*/, where);     
    833854 
     
    840861    // http://stackoverflow.com/questions/456177/function-overloading-in-javascript-best-practices 
    841862    // https://www.w3schools.com/js/js_objects.asp 
    842     var response = gs.functions.callMetadataServer("Getting metadata from "+where, payload, responseFunction, {"forceSync":forceSync, "requestMethod": "POST"}); 
     863    var response = gs.functions._callMetadataServer("Getting metadata from "+where, payload, successResponseFunction, errorResponseFunction, {"forceSync":forceSync, "requestMethod": "POST"}); 
    843864 
    844865    return response; 
     
    846867 
    847868 
    848 gs.functions.getImportMetadata = function(collection, site, documentID, metadataName, responseFunction) 
    849 { 
     869gs.functions.getImportMetadata = function(collection, site, documentID, metadataName, successResponseFunction, errorResponseFunction) 
     870{ 
     871    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
     872 
    850873    var payload = gs.functions.getBasicDataForMetadataServer("get-import-metadata", collection, site, documentID, metadataName); 
    851     gs.functions.callMetadataServer("getImportMetadata", payload, function(responseText) { 
     874    gs.functions._callMetadataServer("getImportMetadata", payload, function(responseText) { 
    852875        var metadata = new GSMetadata(collection, site, documentID, metadataName, null, null, responseText); 
    853         if(responseFunction != null) 
    854         { 
    855             responseFunction(metadata); 
    856         } 
    857     }); 
    858 } 
    859  
    860 gs.functions.getArchivesMetadata = function(collection, site, documentID, metadataName, metadataPosition, responseFunction) 
    861 { 
     876        if(successResponseFunction != null) 
     877        { 
     878            successResponseFunction(metadata); 
     879        } 
     880    }, 
     881    errorResponseFunction); 
     882} 
     883 
     884gs.functions.getArchivesMetadata = function(collection, site, documentID, metadataName, metadataPosition, successResponseFunction, errorResponseFunction) 
     885{ 
     886    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
     887 
    862888    var payload = gs.functions.getBasicDataForMetadataServer("get-archives-metadata", collection, site, documentID, metadataName, null /*metamode*/, null /*metavalue*/, null /*prevmetavalue*/, metadataPosition); 
    863889 
    864     gs.functions.callMetadataServer("getArchivesMetadata", payload, function(responseText) { 
    865     var metadata = new GSMetadata(collection, site, documentID, metadataName, null, metadataPosition, responseText); // indexPos, archivesPos, metaval (responseText) 
    866     if(responseFunction != null) 
    867     { 
    868         responseFunction(metadata); 
    869     } 
    870     }); 
    871 } 
    872  
    873 gs.functions.getIndexMetadata = function(collection, site, documentID, metadataName, metadataPosition, responseFunction) 
    874 { 
     890    gs.functions._callMetadataServer("getArchivesMetadata", payload, function(responseText) { 
     891        var metadata = new GSMetadata(collection, site, documentID, metadataName, null, metadataPosition, responseText); // indexPos, archivesPos, metaval (responseText) 
     892        if(successResponseFunction != null) 
     893        { 
     894            successResponseFunction(metadata); 
     895        } 
     896    }, 
     897    errorResponseFunction); 
     898} 
     899 
     900gs.functions.getIndexMetadata = function(collection, site, documentID, metadataName, metadataPosition, successResponseFunction, errorResponseFunction) 
     901{ 
     902    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
     903 
    875904    var payload = gs.functions.getBasicDataForMetadataServer("get-metadata", collection, site, documentID, metadataName, null /*metamode*/, null /*metavalue*/, null /*prevmetavalue*/, metadataPosition); 
    876905 
    877     gs.functions.callMetadataServer("getIndexMetadata", payload, function(responseText) { 
    878     var metadata = new GSMetadata(collection, site, documentID, metadataName, metadataPosition, null, responseText); // indexPos, archivesPos, metaval (responseText) 
    879      
    880     if(responseFunction != null) 
    881     { 
    882         responseFunction(metadata); 
    883     } 
    884     }); 
     906    gs.functions._callMetadataServer("getIndexMetadata", payload, function(responseText) { 
     907        var metadata = new GSMetadata(collection, site, documentID, metadataName, metadataPosition, null, responseText); // indexPos, archivesPos, metaval (responseText)        
     908        if(successResponseFunction != null) 
     909        { 
     910            successResponseFunction(metadata); 
     911        } 
     912    }, 
     913    errorResponseFunction); 
    885914} 
    886915 
     
    889918****************************/ 
    890919 
    891 gs.functions.removeImportMetadata = function(collection, site, documentID, metadataName, metadataValue, responseFunction) 
    892 { 
    893     gs.functions.callMetadataServer( 
     920gs.functions.removeImportMetadata = function(collection, site, documentID, metadataName, metadataValue, successResponseFunction, errorResponseFunction) 
     921{ 
     922    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
     923     
     924    gs.functions._callMetadataServer( 
    894925    "removeImportMetadata",  
    895926    gs.functions.getBasicDataForMetadataServer("remove-import-metadata", collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, null /*metapos*/),  
    896     responseFunction); 
    897 } 
    898  
    899 gs.functions.removeArchivesMetadata = function(collection, site, documentID, metadataName, metadataPosition, metadataValue, responseFunction) 
    900 { 
     927    successResponseFunction, 
     928    errorResponseFunction); 
     929} 
     930 
     931gs.functions.removeArchivesMetadata = function(collection, site, documentID, metadataName, metadataPosition, metadataValue, successResponseFunction, errorResponseFunction) 
     932{ 
     933    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
     934     
    901935    if(metadataPosition != null) { 
    902936    metadataValue = null; // retaining behaviour of previous version of this function removeArchivesMetadata() 
    903937    } 
    904938 
    905     gs.functions.callMetadataServer( 
     939    gs.functions._callMetadataServer( 
    906940    "removeArchiveMetadata",  
    907941    gs.functions.getBasicDataForMetadataServer("remove-archives-metadata", collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, metadataPosition),  
    908     responseFunction); 
    909 } 
    910  
    911 gs.functions.removeIndexMetadata = function(collection, site, documentID, metadataName, metadataPosition, metadataValue, responseFunction) 
    912 { 
     942    successResponseFunction, 
     943    errorResponseFunction); 
     944} 
     945 
     946gs.functions.removeIndexMetadata = function(collection, site, documentID, metadataName, metadataPosition, metadataValue, successResponseFunction, errorResponseFunction) 
     947{ 
     948    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
     949     
    913950    if(metadataPosition != null) { 
    914951    metadataValue = null; // retaining behaviour of previous version of this function removeIndexMetadata() 
    915952    } 
    916953 
    917     gs.functions.callMetadataServer( 
     954    gs.functions._callMetadataServer( 
    918955    "removeIndexMetadata",  
    919956    gs.functions.getBasicDataForMetadataServer("remove-metadata", collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, metadataPosition),  
    920     responseFunction); 
    921 } 
    922  
    923 gs.functions.removeMetadata = function(collection, site, documentID, metadataName, metadataValue, responseFunction) 
    924 { 
     957    successResponseFunction, 
     958    errorResponseFunction); 
     959} 
     960 
     961gs.functions.removeMetadata = function(collection, site, documentID, metadataName, metadataValue, successResponseFunction, errorResponseFunction) 
     962{ 
     963    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value 
     964     
    925965    var nameArray = ["removeImportMetadata", "removeArchivesMetadata", "removeIndexMetadata"]; 
    926966    var functionArray = ["remove-import-metadata", "remove-archives-metadata", "remove-metadata"]; 
     
    928968    for(var i = 0; i < nameArray.length; i++) 
    929969    { 
    930         gs.functions.callMetadataServer( 
     970        gs.functions._callMetadataServer( 
    931971        nameArray[i], 
    932972        gs.functions.getBasicDataForMetadataServer(functionArray[i], collection, site, documentID, metadataName, null /*metamode*/, metadataValue, null /*prevmetavalue*/, null /*metapos*/), 
    933         responseFunction); 
    934     } 
    935 } 
    936  
     973        successResponseFunction, 
     974        errorResponseFunction); 
     975    } 
     976} 
     977