Changeset 33108

Show
Ignore:
Timestamp:
24.05.2019 23:42:14 (3 weeks ago)
Author:
ak19
Message:

Refactoring to remove repeated code blocks. Tested Delete Selected and Delete All berries once again.

Files:
1 modified

Legend:

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

    r33107 r33108  
    2424 
    2525    // https://stackoverflow.com/questions/386281/how-to-implement-select-all-check-box-in-html 
    26     //https://stackoverflow.com/questions/590018/getting-all-selected-checkboxes-in-an-array 
    27     //https://www.w3schools.com/jsref/met_document_queryselectorall.asp 
    28     //https://www.w3schools.com/cssref/css_selectors.asp 
    29     var selectedCheckboxList = document.querySelectorAll('input[name=select-berry-checkbox]:checked'); 
    30     if(selectedCheckboxList.length == 0) return; // nothing selected, so nothing to delete 
    31    
    32     doDelete(selectedCheckboxList); 
    33 } 
    34  
    35 function deleteAll() { 
    36  
    37     if(docList.length == 0) return; // nothing to delete 
    38      
    39     var  delurl = delurlPath; // var delurlPath is declared in ygDDPlayer.js. 
    40     // Just need to append each doc id separated by |, but this character needs to be URL encoded, 
    41     // else the delete doesn't work. 
    42  
    43     for(var i = 0; i < docList.length; i++) { 
    44     var doc = docList[i]; 
    45     var doc_id = doc['collection']+":"+ doc['name']; 
    46  
    47     if((i+1) == docList.length) { // if it's the last id to process, don't append separator 
    48         delurl += doc_id; 
    49     } else { // there's more ids to process, so append separator 
    50         delurl += doc_id + "%7C"; // url-encoded version of | 
    51     } 
    52     } 
    53  
    54     // The following is a modified version of methods internal to 
    55     // ygDDPlayer.js's ygDDPlayer.prototype.onDragDrop 
    56     var delSuccess = function(o){  
    57     var result = o.responseXML; 
    58      
    59     docList.length = 0; // https://www.jstips.co/en/javascript/two-ways-to-empty-an-array/   
    60      
    61     var content =  YAHOO.util.Dom.get('berryBasketContent'); 
    62     while (content.hasChildNodes()) { 
    63         content.removeChild(content.firstChild); 
    64     }   
    65     content.appendChild(document.createTextNode('Your berry basket is empty.')); 
    66      
    67     var trashbin =  YAHOO.util.Dom.get('trashbin'); 
    68     if ( trashbin !=null){ 
    69         trashbin.style.background = 'url("interfaces/default/images/trash-full.png") 0 0 no-repeat'; 
    70     } 
    71  
    72     var delall_checkbox = YAHOO.util.Dom.get('delall-checkbox'); 
    73     delall_checkbox.checked = false; // deselect the delete-all checkbox 
    74     } 
    75  
    76     var delFailure = function(o){ alert("Deletion failed" + o);} 
    77      
    78     var delcallback = { 
    79     success:delSuccess, 
    80     failure:delFailure,   
    81     argument:null // supposed to be the ygDDPlayer object, but don't have a ref to it here, so trying null 
    82     } 
    83     // request_type defaults to GET, which is what's used for add and del, see ygDDPlayer.js. 
    84     YAHOO.util.Connect.asyncRequest(request_type, delurl , delcallback); 
    85      
    86 } 
    87  
    88  
    89 function doDelete(selectedList) { // given list of selected checkboxes 
    90      
     26    // https://stackoverflow.com/questions/590018/getting-all-selected-checkboxes-in-an-array 
     27    // https://www.w3schools.com/jsref/met_document_queryselectorall.asp 
     28    // https://www.w3schools.com/cssref/css_selectors.asp 
     29    var selectedList = document.querySelectorAll('input[name=select-berry-checkbox]:checked'); 
     30    if(selectedList.length == 0) return; // nothing selected, so nothing to delete 
     31 
     32    // if all berries selected for deletion, can optimise 
     33    if(selectedList.length === docList.length) { 
     34    deleteAll(); 
     35    return; // done! 
     36    } 
     37     
     38    // otherwise selected list of berries is a proper subset of total berries (berries in docList)     
    9139    var idsToDelete = []; 
    9240 
     
    11159     
    11260    } 
    113      
     61 
     62    var delAll = false; 
     63    doDelete(delAll, delurl, selectedList, idsToDelete); 
     64} 
     65 
     66function deleteAll() { 
     67 
     68    if(docList.length == 0) return; // nothing to delete 
     69     
     70    var  delurl = delurlPath; // var delurlPath is declared in ygDDPlayer.js. 
     71    // Just need to append each doc id separated by |, but this character needs to be URL encoded, 
     72    // else the delete doesn't work. 
     73 
     74    for(var i = 0; i < docList.length; i++) { 
     75    var doc = docList[i]; 
     76    var doc_id = doc['collection']+":"+ doc['name']; 
     77 
     78    if((i+1) == docList.length) { // if it's the last id to process, don't append separator 
     79        delurl += doc_id; 
     80    } else { // there's more ids to process, so append separator (in URL encoded form!) 
     81        delurl += doc_id + "%7C"; // url-encoded version of | 
     82    } 
     83    } 
     84     
     85    var delAll = true; 
     86    doDelete(delAll, delurl, null, null); 
     87} 
     88 
     89 
     90function doDelete(deleteAll, delurl, selectedList, idsToDelete) { // given list of selected checkboxes 
     91 
    11492    // The following is a modified version of methods internal to 
    11593    // ygDDPlayer.js's ygDDPlayer.prototype.onDragDrop 
    11694    var delSuccess = function(o) { 
    11795    var result = o.responseXML; 
    118  
    119     var deleteAll = false; 
    120     if(selectedList.length === docList.length) { // deleting all berries, can optimise 
    121         deleteAll = true; 
    122     } 
    123     else { // Not deleting all berries, just a subset 
     96     
     97    if(!deleteAll) { // then we're given a selection to delete: Not deleting all berries, just a subset 
    12498        // Remove id of selected doc to be deleted from docList.         
    12599        // Minor optimisation to double for loop, dependent on ordering of selected berries being 
     
    139113        } 
    140114        } 
    141  
     115         
    142116        // remove the selected documents' HTML display elements 
    143117        var berryDocsList = YAHOO.util.Dom.get('berryDocsList'); // ordered list item containing the berries     
     
    148122        } 
    149123    } 
    150  
    151     // if all docs are deleted, then display "berry basket is empty" message 
    152     if (deleteAll || !berryDocsList.hasChildNodes()) {       
    153         if(deleteAll) { 
    154         // if deleting all docs, just use the easy way to empty the docList array 
    155         docList.length = 0; // https://www.jstips.co/en/javascript/two-ways-to-empty-an-array/ 
    156         } 
     124     
     125 
     126    // if all docs are deleted by this stage, then display "berry basket is empty" message 
     127    if (deleteAll || !berryDocsList.hasChildNodes()) { // 2nd clause no longer needed?, then this just becomes an else against the first if(!deleteAll) test 
     128         
     129        // if deleting all docs, just use the easy way to empty the docList array 
     130        docList.length = 0; // https://www.jstips.co/en/javascript/two-ways-to-empty-an-array/ 
    157131 
    158132        // Removing all child nodes (done one at a time) is more optimal 
     
    171145    } 
    172146 
    173     // deselect the delete-all and delete-selected checkboxes 
     147    // Ensure the delete-all and delete-selected checkboxes are deselected 
    174148    YAHOO.util.Dom.get('delall-checkbox').checked = false;  
    175149    YAHOO.util.Dom.get('delselected-checkbox').checked = false;