Ignore:
Timestamp:
2023-09-25T23:04:59+13:00 (9 months ago)
Author:
anupama
Message:

Added code in user_comments.js for the compound remove_metadata_array to get called. There's debug statements in this and in java-global-functions.js, that's because I'm still working on it. But I don't want the code to get lost in the upcoming move to another lab sometime this week.

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

    r38194 r38218  
    10201020}
    10211021
    1022 // metamode (such as accumulate) has no meaning when removingMetadata,
    1023 // so best not make it an option to avoid confusing callers?
    1024 gs.functions.removeMetadataArray = function(collection, site, docArray, /*metamode,*/
     1022// metamode=override is in theory supported by remove_archives and remove_import metadata
     1023// but not by remove_index and remove_live metadata. However, even in the former 2 cases,
     1024// passing in metamode=override still appears to be ineffective, see recent svn revision 38193.
     1025// And metamode accumulate prevents all removal.
     1026// So for now, this function forces metamode to null, regardless of what is passed in.
     1027// At least this ensures a more consistent effect with remove_index (and remove_live) functions.
     1028gs.functions.removeMetadataArray = function(collection, site, docArray, metamode,
    10251029            where, successResponseFunction, forceSync, errorResponseFunction)
    1026 
     1030{
     1031    alert("in gs.functions.removeMetaArray START");
     1032   
    10271033    if( typeof errorResponseFunction === 'undefined' ) { errorResponseFunction = null; } // force error callback to be defined: either null or has value
    10281034
     
    10381044   
    10391045    var response = gs.functions._callMetadataServer("Removing metadata from "+where, payload, successResponseFunction, errorResponseFunction, {"forceSync": forceSync, "requestMethod": "POST"});
    1040    
     1046
     1047    console.log("in gs.functions.removeMetaArray: " + JSON.stringify(payload));
     1048   
    10411049    return response;
    10421050}
  • main/trunk/greenstone3/web/interfaces/default/js/user_comments.js

    r38189 r38218  
    167167    //delCommentsButton.setAttribute("onclick", gs.usercomments.deleteSelectedComments);
    168168    // alternatively: https://www.w3schools.com/jsref/met_element_addeventlistener.asp
    169     delCommentsButton.addEventListener("click", gs.usercomments.deleteSelectedComments);
     169
     170    delCommentsButton.addEventListener("click", gs.usercomments.removeSelectedComments);
     171    // Arduous way: deletes each metadata individually
     172    //delCommentsButton.addEventListener("click", gs.usercomments.deleteSelectedComments);
    170173   
    171174    delButtonDiv.appendChild(delCommentsButton);
     
    238241}
    239242
     243// Uses remove array to remove multiple metadata in one ajax call
     244gs.usercomments.removeSelectedComments = function(eventObject, opt_metaname_username, opt_metaname_usertimestamp, opt_metaname_usercomment)
     245{
     246    // https://stackoverflow.com/questions/590018/getting-all-selected-checkboxes-in-an-array
     247    // https://stackoverflow.com/questions/6166763/jquery-multiple-checkboxes-array
     248
     249    var selectedComments = document.querySelectorAll('input[class=del-me-comment][type=checkbox]:checked');
     250
     251    if(selectedComments.length > 0) {   
     252   
     253    // Don't allow the user to submit further comments or delete comments
     254    // until the metadata has been updated
     255    document.getElementById("usercommentSubmitButton").disabled = true;
     256    var delCommentsButton = document.getElementById("delCommentsButton");
     257    if(delCommentsButton != undefined) {// should be defined when in this function
     258        delCommentsButton.disabled = true;
     259    }
     260
     261    // User comment meta are at topmost section of the document.
     262    // So only get the docId up to any period mark:
     263    var _docid = document.AddUserCommentForm.d.value;   
     264    var period = _docid.indexOf(".");
     265    if(period != -1) {
     266        _docid = _docid.substring(0, period);
     267    }
     268   
     269    //document.querySelectorAll('input[class=del-me-comment][type=checkbox]:checked').forEach(elem => console.log(elem.getAttribute('data-username')));
     270
     271    var metadataPositions = [];
     272    var failure = false;
     273
     274    // start deleting from end of metadata list, so we don't have to recalculate metapos each time
     275    for (var i = 0; i < selectedComments.length; i++) {
     276
     277        var metapos = selectedComments[i].getAttribute("data-metapos");
     278        metadataPositions.push(metapos); // add metapos to end of array
     279    }
     280    }   
     281   
     282    // now we have all the metapositions we need to delete
     283    // create a JSON of indicating the username, timestamp and comment metadata need to be deleted
     284    // at all those metapositions
     285   
     286    // removeMetadataArray escapes the entire JSON, so we don't individually escape the fields here
     287
     288    // For creating the JSON object that gets turned into a string, see
     289    // http://msdn.microsoft.com/en-us/library/ie/cc836459%28v=vs.94%29.aspx
     290    // http://jsfiddle.net/qmacro/W54hy/
     291   
     292    var username_rec = {
     293    metaname: opt_metaname_username || gs.usercomments.defaults.metaname_username, // e.g. "username"
     294    metapositions: metadataPositions
     295    };
     296   
     297    var timestamp_rec = {
     298    metaname: opt_metaname_usertimestamp || gs.usercomments.defaults.metaname_usertimestamp, // e.g. "usertimestamp"
     299    metavals: metadataPositions
     300    };
     301   
     302    var comment_rec = {
     303    metaname: opt_metaname_usercomment || gs.usercomments.defaults.metaname_usercomment, // e.g. "usercomment"
     304    metavals: metadataPositions
     305    };
     306   
     307    var doc_rec = {
     308    docid: _docid,
     309    metatable: [username_rec, timestamp_rec, comment_rec],
     310    //metamode: "accumulate"
     311    };
     312   
     313    var docArray = [doc_rec];
     314
     315    // Don't allow the user to submit further comments or delete any
     316    // until the metadata has been updated
     317    document.getElementById("usercommentSubmitButton").disabled = true;
     318    var delCommentsButton = document.getElementById("delCommentsButton");
     319    if(delCommentsButton != undefined) {
     320    delCommentsButton.disabled = true;
     321    }
     322
     323   
     324    gs.functions.removeMetadataArray(
     325    gs.variables["c"],
     326    gs.variables["site"],
     327    docArray,
     328    null, //metamode
     329    "import|archives|index",
     330    function(ajaxResult) { return gs.usercomments.doneRemovingMetadata(ajaxResult, metadataPositions); },
     331    false, // false for asynchronous, see comment in gs.usercomments.addUserComment()
     332    function(ajaxError) { return gs.usercomments.removeMultipleCommentsFailed(ajaxError); }
     333    );
     334   
     335
     336    //console.log("username: " + opt_metaname_username + " - " + gs.usercomments.defaults.metaname_username);
     337    console.log("metapositions to remove: " + metadataPositions);
     338    console.log("docArray:\n" + JSON.stringify(docArray));
     339}
     340
     341gs.usercomments.removeMultipleCommentsFailed = function(data) {
     342    var result = (data.responseText) ? data.responseText : data;
     343    alert("Remove failed. Got: " + result);
     344    failure = true;
     345
     346    // TODO, force a page reload so that the usercomments' state somewhat better reflects what's in DBs
     347}
     348
     349
     350// Removes single metadata at a time
    240351gs.usercomments.deleteSelectedComments = function() {
    241352    // https://stackoverflow.com/questions/590018/getting-all-selected-checkboxes-in-an-array
     
    282393                         gs.variables["site"],
    283394                         _docid,
    284                          "gs.username",
     395                         gs.usercomments.defaults.metaname_username,
    285396                         metapos,
    286397                         //"import|archives|index",
     
    295406                         gs.variables["site"],
    296407                         _docid,
    297                          "gs.usertimestamp",
     408                         gs.usercomments.defaults.metaname_usertimestamp,
    298409                         metapos,
    299410                         //"import|archives|index",
     
    308419                         gs.variables["site"],
    309420                         _docid,
    310                          "gs.usercomment",
     421                         gs.usercomments.defaults.metaname_usercomment,
    311422                         metapos,
    312423                         //"import|archives|index",
     
    322433    //console.log("modifiedMetapos: " + modifiedMetapos);
    323434
    324     gs.usercomments.doneRemovingMetadata(modifiedMetapos);
     435    gs.usercomments.doneRemovingMetadata(null, modifiedMetapos);
    325436    }
    326437}
     
    337448}
    338449
    339 gs.usercomments.doneRemovingMetadata = function(modifiedMetapos) {
     450gs.usercomments.doneRemovingMetadata = function(data, modifiedMetapos) {
     451
     452    // data is xmlHttpRequest Object if gsajaxapi is used for the ajax call.
     453    // And data is a string if jQuery AJAX was used.
     454    // Using JavaScript's feature sensing to detect which of the two we're dealing with:
     455    var result = (data.responseText) ? data.responseText : data;       
     456
     457    //alert(result);
     458    console.log("@@@ Done removing metadata:\n" + result);
     459   
    340460    var userCommentsDiv = document.getElementById("usercomments");
    341461       
     
    524644
    525645gs.usercomments.doneUpdatingMetatada = function(data, _username, _timestamp, _comment)
    526 {
     646{   
    527647    // data is xmlHttpRequest Object if gsajaxapi is used for the ajax call.
    528648    // And data is a string if jQuery AJAX was used.
Note: See TracChangeset for help on using the changeset viewer.