Ignore:
Timestamp:
2023-03-21T16:28:23+13:00 (15 months ago)
Author:
kjdon
Message:

tidy up of favourites

File:
1 edited

Legend:

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

    r37550 r37553  
    1 // The default link type in the basket - "document" = greenstone version of the document, "source" = link to source file eg pdf.
    2 var default_link_type = "document"; // "source" or "document"
    3 // use the appropriate one of these to override the default for particular collections.
    4 var source_link_collections = new Array(); // or add list of collections like ["pdfberry", "mgppdemo"];
    5 var document_link_collections = new Array(); // or add list of collections as above.
    6 //these are the default metadata items used by favourite baskets.
    7 var default_metas = ["Title", "root_Title", "root_assocfilepath", "root_srclinkFile", "nodeID", "collection", "Date"];
    8 
     1// a list of doc ids
    92var docList = new Array();
    10 var urlonly = false;
     3// a corresponding list of plain text output for each doc
     4var docPlainList = new Array();
     5
    116var mailinfo = new Array();
    127mailinfo['address'] = gs.text.favourites.to;
     
    149mailinfo['bcc'] = gs.text.favourites.bcc;
    1510mailinfo['subject'] = gs.text.favourites.subject;
    16 var textwin;
    17 var mailwin;
    18 
    19 var options = ['fullview', 'textview', 'email'];
     11
    2012var delurlPath = gs.xsltParams.library_name + "?a=pr&rt=r&ro=1&s=DeleteFavourites&c=&o=XML&hhf="+makeURLComponentSafe("[{\"name\":\"Cache-Control\", \"value\":\"no-cache\"}]")+"&s1.items=";
    2113var request_type = "GET"; // the default
    2214
    23 
     15var emailVisible = false;
     16function showAndHideEmailForm() {
     17    var emaildiv = document.getElementById("emailForm");
     18    if (emailVisible) {
     19        emailVisible = false;
     20        emaildiv.setAttribute("style","display:none;");
     21    } else {
     22        emailVisible = true;
     23        var textarea = document.getElementById("pretextarea");
     24        textarea.value = outputPlain();
     25        emaildiv.setAttribute("style","display:block;");
     26    }
     27}
     28
     29function copyToClip() {
     30    function listener(e) {
     31        var str = outputPlain();
     32    e.clipboardData.setData("text/plain", str);
     33    e.preventDefault();
     34    }
     35    document.addEventListener("copy", listener);
     36    document.execCommand("copy");
     37    document.removeEventListener("copy", listener);
     38};
     39
     40function outputPlain() {
     41    var output = "";
     42    for(var i = 0; i < docPlainList.length; i++) {
     43        output += docPlainList[i] + "\n\n"
     44    }
     45    return output;
     46}
     47
     48function mailTo() {
     49    var url = "mailto:?Subject="+gs.text.favourites.default_subject+"&body=";
     50    var content = outputPlain();
     51    content = content.replaceAll("\n", "%0D%0A");
     52    console.log("output="+content);
     53    window.location.href= url+content;
     54
     55   
     56
     57}
     58// called by clicking the delete All button
    2459function deleteAll() {
    2560
     
    3166
    3267    for(var i = 0; i < docList.length; i++) {
    33     //var doc = docList[i];
    34     //var doc_id = doc['collection']+":"+ doc['name'];
    3568        var doc_id = docList[i];
    3669    if((i+1) == docList.length) { // if it's the last id to process, don't append separator
     
    4578}
    4679
     80// called when you click the star in the favourites list
    4781function deleteFavouriteFromCheckout(el)
    4882{
     
    5892}
    5993
     94// do the actual deletion, or all or some of the list
    6095function doDelete(deleteAll, delurl, selectedList, idsToDelete) { // given list of selected checkboxes
    6196
    62     // The following is a modified version of methods internal to
    63     // ygDDPlayer.js's ygDDPlayer.prototype.onDragDrop
    6497    var delSuccess = function(o) {
    6598    var result = o.responseXML;
    6699   
    67     if(!deleteAll) { // then we're given a selection to delete: Not deleting all berries, just a subset
     100    if(!deleteAll) { // then we're given a selection to delete: Not deleting all items, just a subset
    68101        // Remove id of selected doc to be deleted from docList.       
    69102        // Minor optimisation to double for loop, dependent on ordering of selected berries being
     
    72105        var searchForNextSelectedIdFromIndex = idsToDelete.length-1;
    73106        for (var i = docList.length - 1; i >= 0; i--) {
    74         //var berry = docList[i];
    75         //var berry_id = berry['collection'] + ":" + berry['name'];
    76         var berry_id = docList[i];     
     107        var doc_id = docList[i];       
    77108        for(var j = searchForNextSelectedIdFromIndex; j >= 0; j--) {
    78             if(idsToDelete[j] == berry_id) {
     109            if(idsToDelete[j] == doc_id) {
    79110            docList.splice(i, 1); // i indexes into docList, delete element i from docList
     111                        docPlainList.splice(i,1); // also remove the plain description
    80112            searchForNextSelectedIdFromIndex = j-1;
    81113            break;
     
    85117       
    86118        // remove the selected documents' HTML display elements
    87         var berryDocsList = YAHOO.util.Dom.get('berryDocsList'); // ordered list item containing the berries   
     119        var favouriteDocsList = document.getElementById('favouriteDocsList'); // ordered list item containing the berries   
    88120        for(var i = 0; i < selectedList.length; i++) {
    89121        var li = selectedList[i].parentNode; // list item parent of checkbox
    90122        // remove the list item from its containing orderedList     
    91         berryDocsList.removeChild(li);
     123        favouriteDocsList.removeChild(li);
    92124        }
    93125    }
    94    
    95 
    96     // if all docs are deleted by this stage, then display "berry basket is empty" message
    97     if (deleteAll || !berryDocsList.hasChildNodes()) { // 2nd clause no longer needed?, then this just becomes an else against the first if(!deleteAll) test
     126
     127
     128
     129    // if all docs are deleted by this stage, then display no favourites message
     130    if (deleteAll || favouriteDocsList.getElementsByTagName("li").length==0) { // 2nd clause no longer needed?, then this just becomes an else against the first if(!deleteAll) test
    98131       
    99132        // if deleting all docs, just use the easy way to empty the docList array
    100133        docList.length = 0; // https://www.jstips.co/en/javascript/two-ways-to-empty-an-array/
    101 
     134            docPlainList.length = 0;
    102135        // Removing all child nodes (done one at a time) is more optimal
    103136        // than setting innerHTML to empty string, see
    104137        // https://stackoverflow.com/questions/3955229/remove-all-child-elements-of-a-dom-node-in-javascript
    105         var content =  YAHOO.util.Dom.get('berryBasketContent');
     138            // this removes the empty ul, rather than the docs themselves which was done above
     139        var content =  document.getElementById('favouritesContent');
    106140        while (content.hasChildNodes()) {
    107141        content.removeChild(content.firstChild);
    108142        } 
    109143        content.appendChild(emptyBasketText());
    110         var berryBasketDelOptions = YAHOO.util.Dom.get('delOptions');
    111         if (berryBasketDelOptions != null) {
    112         berryBasketDelOptions.setAttribute("style","display:none;");
    113         }
    114 
    115         var trashbin =  YAHOO.util.Dom.get('trashbin');
    116         if ( trashbin !=null){
    117         trashbin.style.background = 'url("interfaces/default/images/trash-full.png") 0 0 no-repeat';
    118         }
    119     }
    120 
    121     var have_checkboxes = YAHOO.util.Dom.get('select-all-checkbox'); // just pick one for the test
    122     if (have_checkboxes) {
    123         // Ensure the select-all, delete-all and delete-selected checkboxes are deselected
    124         YAHOO.util.Dom.get('select-all-checkbox').checked = false;
    125         YAHOO.util.Dom.get('delete-selected-checkbox').checked = false;
    126         YAHOO.util.Dom.get('delete-all-checkbox').checked = false;
    127     }
     144
     145    }
     146        // reset the plain doclist
     147        var textarea = document.getElementById("pretextarea");
     148        textarea.value = outputPlain();
     149
    128150    }
    129151
     
    142164
    143165
    144 // switch between Full, Text and Email views
    145 function navigate(e){
    146    
    147     var target = this;
    148 
    149     if ( target.id.toLowerCase() == '' ) {
    150         target = target.parentNode;
    151     }
    152 
    153     if (target.id.toLowerCase() == 'fullview'){
    154         berryCheckoutHighlight( 'fullview' );
    155         showFullView();
    156     }
    157 
    158     if (target.id.toLowerCase() == 'textview'){
    159         berryCheckoutHighlight( 'textview' );
    160         showTextView();
    161     }
    162 
    163     if (target.id.toLowerCase() == 'email'){
    164         berryCheckoutHighlight( 'email' );
    165         showEmail();
    166     }
    167 
    168     if (target.id.toLowerCase() == 'sendmail'){
    169         sendMail();
    170     }
    171 
    172     if (target.id.toLowerCase() == 'urlcheck' && urlonly){
    173         var urlcheck = YAHOO.util.Dom.get('urlcheck');
    174         urlcheck.src = 'interfaces/default/images/check3.gif';
    175         var parea =YAHOO.util.Dom.get('pretextarea');
    176         urlonly = false;
    177        
    178         this.value=gs.text.favourites.url_only;
    179        
    180         populateUrlsAndMetadata(parea);
    181         return;
    182     }
    183 
    184     if (target.id.toLowerCase() == 'urlcheck' && !urlonly ){
    185         var urlcheck = YAHOO.util.Dom.get('urlcheck');
    186         urlcheck.src = 'interfaces/default/images/check4.gif';
    187         var parea =YAHOO.util.Dom.get('pretextarea');
    188         populateUrls(parea);
    189         urlonly = true;
    190        
    191         this.value=gs.text.favourites.url_and_metadata;
    192        
    193         return;
    194     }
    195 
    196     if (target.id.toLowerCase() == 'extextview' ){
    197         if (textwin != null){
    198             textwin.close();
    199         }
    200 
    201         textwin = window.open("","Berry basket plain text view","status=1,width=450,height=300");
    202         textwin.moveTo(0,0);
    203         var content = document.createElement('div');
    204         buildPreview(content);
    205         var body = textwin.document.getElementsByTagName('body')[0];
    206         body.appendChild(content);
    207         var prearea = textwin.document.getElementsByTagName('textarea')[0];
    208         prearea.cols = '55';
    209         prearea.rows = '15';
    210     }
    211 
    212     if (target.id.toLowerCase() == 'exemail' ){
    213         if (mailwin != null){
    214             mailwin.close();
    215         }
    216         mailwin = window.open("","Berry basket mail to a friend","status=1,width=450,height=350");
    217         mailwin.moveTo(0,0);
    218         var content = document.createElement('div');
    219         getEmailContent(content);
    220         var body = mailwin.document.getElementsByTagName('body')[0];
    221         body.appendChild(content);
    222         var prearea = mailwin.document.getElementsByTagName('textarea')[0];
    223         prearea.cols = '50';
    224         prearea.rows = '11';
    225     }
    226 }
    227 
    228 function pageLoad(){
    229     for(var j = 0; j < options.length; j++)
    230     {
    231         var ele = document.getElementById(options[j]);
    232         YAHOO.util.Event.addListener(ele, 'click', navigate);
    233     }
    234    
    235     showFullView();
    236 }
    237 
    238 function showFullView() {
    239     showFullViewFavourites();
    240 }
    241 
    242166function emptyBasketText() {
    243167    return document.createTextNode(gs.text.favourites.empty_basket);
     
    245169
    246170
    247 function showFullViewFavourites(){
    248 
    249     //var content =  YAHOO.util.Dom.get('berryBasketContentXX');
    250     //var fullview =  YAHOO.util.Dom.get('fullview');
    251     //berryCheckoutPageClear();
    252 
    253         var berryBasketDelOptions = YAHOO.util.Dom.get('delOptions');
    254     if (docList.length == 0){
    255         content//.appendChild(emptyBasketText());
    256         if (berryBasketDelOptions != null) {
    257         berryBasketDelOptions.setAttribute("style","display:none;");
    258         }
    259 
    260         return;
    261     }
    262         if (berryBasketDelOptions != null) {
    263        berryBasketDelOptions.setAttribute("style","display:block;");
    264     }
    265 
    266     var share_anchor = YAHOO.util.Dom.get('shareAnchor');
    267     var plain_text = "";
    268     var dlist = document.createElement('div');
    269     //content.appendChild(dlist);
    270     var ul = document.createElement('ul');
    271     dlist.appendChild(ul);
    272 
    273     ul.setAttribute("id", "berryDocsList");
    274         ul.setAttribute("style","list-style: none;");
    275 
    276     for (var i in docList){
    277         //var doc = docList[i];
    278         var li = document.createElement('li');
    279             var img = document.createElement('img');
    280         var text ="";
    281 
    282         //var doc_id = doc['collection']+":"+ doc['name'];
    283         var doc_id = docList[i];
    284        
    285         img.setAttribute("src", gs.variables.selected_favourite_icon_url);
    286         img.setAttribute("id", doc_id);
    287         img.setAttribute("height", "20px");
    288         img.setAttribute("width", "20px");
    289             img.setAttribute("style","padding-right: 5px;"); // **** better to do this with CSS
    290         img.setAttribute("onClick", "deleteFavouriteFromCheckout(this)");
    291         li.appendChild(img);
    292 
    293         //generateDocDisplay(li, doc, doc_id);
    294             plain_text += generateMailEntry(doc, doc_id)+"%0D%0A%0D%0A";
    295         li.className = 'berrydoc';
    296         ul.appendChild(li);
    297     }
    298 
    299     var mailto = "mailto:?Subject=Favourites&body="+plain_text;
    300     share_anchor.setAttribute("href", mailto);
    301 }
    302 
    303 function generateMailEntry(doc) {
    304 
    305     var url=generateURL(doc);
    306     var title = doc['Title'];
    307     return "Title: "+title+"%0D%0AURL:"+url;
    308 
    309 }
    310 function generateDocDisplay(li, doc, doc_id) {
    311     var a = document.createElement('a');
    312     var text="";
    313     a.href=generateURL(doc);
    314     a.appendChild(document.createTextNode(doc['Title']));
    315 
    316     if (doc['root_Title']){
    317     li.appendChild(document.createTextNode(doc['root_Title']+": "));
    318     }
    319 
    320     li.appendChild(a);
    321     li.appendChild(document.createTextNode(" ("+doc['collection']+")"));
    322     var metadata = "";
    323     for (var metaItem in doc) {
    324     if ( !default_metas.includes(metaItem)){
    325         metadata += " "+metaItem+": "+ doc[metaItem]+" ";
    326     }
    327     }
    328     text +=metadata;
    329     li.appendChild(document.createTextNode(text));
    330 
    331 }
    332 
    333 function showTextView(){
    334 
    335     var content = YAHOO.util.Dom.get('berryBasketContent');
    336     var textview = YAHOO.util.Dom.get('textview');
    337 
    338     berryCheckoutPageClear();
    339     if (docList.length == 0){
    340         content.appendChild(emptyBasketText());
    341         return;
    342     }
    343     buildPreview(content);
    344 
    345 }
    346 
    347 function getEmailContent(content){
    348     var item ;
    349     var tr;
    350     var td;
    351     var input;
    352 
    353     table = document.createElement('table');
    354     table.setAttribute("class","mailtable");
    355 
    356     for (item in mailinfo){
    357         tr = document.createElement('tr');
    358         td = document.createElement('td');
    359         td.setAttribute("class","mailitem");
    360         td.appendChild(document.createTextNode(mailinfo[item]));
    361         tr.appendChild(td);
    362         td = document.createElement('td');
    363         input = document.createElement('input');
    364         input.setAttribute("id", item);
    365         input.setAttribute("class", "mailinput");
    366         if(item === "address") {
    367             input.setAttribute("type", "email"); // https://html5-tutorial.net/form-validation/validating-email/
    368             input.required = true; // https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
    369         } else {
    370             input.setAttribute("type", "text");
    371         }
    372         td.appendChild(input);
    373         tr.appendChild(td);
    374         table.appendChild(tr);
    375     }
    376 
    377     // an empty line
    378     tr = document.createElement('tr');
    379     td = document.createElement('td');
    380     td.appendChild(document.createElement('br'));
    381     tr.appendChild(td);
    382     table.appendChild(tr);
    383 
    384     content.appendChild(table);
    385 
    386     buildPreview(content);
    387 
    388     //send button
    389     input = document.createElement('input');
    390     input.setAttribute("id", 'sendmail');
    391     input.setAttribute("class", "sendbutton");
    392     input.setAttribute("type", "button");
    393     input.setAttribute("value", gs.text.favourites.send);
    394     content.appendChild(input);
    395 }
    396 
    397 function showEmail(){
    398     var content = YAHOO.util.Dom.get('berryBasketContent');
    399     var email = YAHOO.util.Dom.get('email');
    400 
    401     berryCheckoutPageClear();
    402 
    403     if (docList.length == 0){
    404         content.appendChild(emptyBasketText());
    405         return;
    406     }
    407 
    408     var item;
    409     var tr;
    410     var td;
    411     var input;
    412 
    413     table = document.createElement('table');
    414     table.setAttribute("class","mailtable");
    415 
    416     for (item in mailinfo){
    417         tr = document.createElement('tr');
    418         td = document.createElement('td');
    419         td.setAttribute("class","mailitem");
    420         td.appendChild(document.createTextNode(mailinfo[item]));
    421         tr.appendChild(td);
    422 
    423         td = document.createElement('td');
    424         input = document.createElement('input');
    425         input.setAttribute("id", item);
    426         input.setAttribute("class", "mailinput");
    427         if(item === "address") {
    428             input.setAttribute("type", "email"); // https://html5-tutorial.net/form-validation/validating-email/
    429             input.required = true; // https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
    430         } else {
    431             input.setAttribute("type", "text");
    432         }
    433         td.appendChild(input);
    434         tr.appendChild(td);
    435         table.appendChild(tr);
    436 
    437     }
    438 
    439     // an empty line
    440     tr = document.createElement('tr');
    441     td = document.createElement('td');
    442     td.appendChild(document.createElement('br'));
    443     tr.appendChild(td);
    444     table.appendChild(tr);
    445 
    446     content.appendChild(table);
    447 
    448     buildPreview(content);
    449 
    450     //send button
    451     input = document.createElement('input');
    452     input.setAttribute("id", 'sendmail');
    453     input.setAttribute("class", "sendbutton");
    454     input.setAttribute("type", "button");
    455     input.setAttribute("value", gs.text.favourites.send);
    456     content.appendChild(input);
    457    
    458     YAHOO.util.Event.addListener(input, 'click', navigate);
    459 }
    460 
    461 function buildPreview(parent){
    462 
    463     var div = document.createElement('div');
    464     var cb = document.createElement('input');
    465     cb.setAttribute('class', 'sendbutton');
    466     cb.type = 'button';
    467     cb.id = 'urlcheck';
    468     if (urlonly)
    469     {
    470         cb.value=gs.text.favourites.url_and_metadata;
    471     }
    472     else
    473     {
    474         cb.value=gs.text.favourites.url_only;
    475     }
    476 
    477     YAHOO.util.Event.addListener(cb, 'click', navigate);
    478    
    479     var img = document.createElement('img');
    480     img.src = 'interfaces/default/images/check3.gif';
    481     img.id = 'urlcheck';
    482     div.appendChild(cb);
    483     //div.appendChild(img);
    484 
    485     var urls = document.createElement('span');
    486     urls.id = 'urls';
    487     urls.className = 'berrycheck';
    488     //urls.appendChild(document.createTextNode('URL only'));
    489     div.appendChild(urls);
    490 
    491     // var urlsmetadata = document.createElement('span');
    492     // urlsmetadata.id = 'urlsmetadata'
    493     // urlsmetadata.className = 'berryradio';
    494     // urlsmetadata.appendChild(document.createTextNode('URLs and Metadata'));
    495     // div.appendChild(urlsmetadata);
    496 
    497     parent.appendChild(div);
    498 
    499     var parea = document.createElement('textarea');
    500     parea.id = 'pretextarea';
    501     parea.required = true; // https://www.w3schools.com/tags/att_textarea_required.asp
    502             // and https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
    503    
    504     parent.appendChild(parea);
    505 
    506     if(urlonly)
    507     {
    508         populateUrls(parea);
    509     }
    510     else
    511     {
    512         populateUrlsAndMetadata(parea);
    513     }
    514 }
    515 
    516 function getDefaultLinkTypeUNUSED(collection) {
    517     var link_type;
    518     if (document_link_collections.includes(collection)) {
    519     link_type = "document";
    520     } else if (source_link_collections.includes(collection)) {
    521     link_type = "source";
    522     }
    523     else {
    524     link_type = default_link_type;
    525     if (link_type != "source" && link_type != "document") {
    526         link_type = "document"; //the default default
    527     }
    528     }
    529     return link_type;
    530 }
    531    
    532 function generateURLUNUSED(doc) {
    533 
    534     var url;
    535     var doc_url = document.URL;
    536     var root_url = doc_url.substring(0,doc_url.indexOf('?'));
    537 
    538     var link_type = getDefaultLinkType(doc["collection"]);
    539     if (link_type == "document") {
    540     url = root_url+"/collection/"+doc["collection"]+"/document/"+doc["nodeID"];
    541     } else if (link_type == "source") {
    542     url = root_url+"/sites/"+gs.xsltParams.site_name+"/collect/"+doc['collection']+"/index/assoc/"+doc["root_assocfilepath"]+"/"+doc["root_srclinkFile"];
    543     }
    544     return url;
    545 }
    546 
    547 
    548 function populateUrls(parea){
    549 
    550     var urls="";
    551     for (var i in docList){
    552         var doc = docList[i];
    553         urls += generateURL(doc)+"\n\n";
    554     }
    555 
    556     parea.value = urls;
    557 
    558 }
    559 
    560 function populateUrlsAndMetadataUNUSED(parea){
    561 
    562     var fulltext="";
    563     for (var i in docList){
    564         var doc = docList[i];
    565         var url = generateURL(doc)+"\n";
    566 
    567         var metadata = "";
    568         if (doc['Title']) {
    569         metadata += gs.text.favourites.doc_title+": "+doc['Title']+"\n";
    570         }
    571         if (doc['root_Title']) {
    572         metadata += gs.text.favourites.doc_root_title+": "+doc['root_Title']+"\n";
    573 
    574         }
    575         if (doc['name']) {
    576         metadata += gs.text.favourites.doc_name+": "+doc['name']+"\n";
    577         }
    578         if (doc['collection']) {
    579         metadata += gs.text.favourites.doc_collection+": "+doc['collection']+"\n";
    580         }
    581         if (doc['Date']) {
    582         metadata += gs.text.favourites.doc_date+": "+doc['Date']+"\n";
    583         }
    584         // allow for inclusion of custom metadata
    585         for (var m in doc) {
    586         if (!default_metas.includes(m)) {
    587             metadata += m +":" + doc[m]+"\n";
    588         }
    589         }
    590         fulltext +=url+metadata+"\n";
    591     }
    592 
    593     parea.value = fulltext;
    594 
    595 }
    596 
     171// send the email request to greenstone
    597172function sendMail(){
    598173    var url = gs.xsltParams.library_name + "?a=pr&rt=r&ro=1&s=SendFavouritesMail&c=";
     
    643218}   
    644219
    645 function berryCheckoutPageClear() {
    646     var bbc = document.getElementById('berryBasketContent');
    647     if ( bbc == null ) return;
    648     bbc.innerHTML = '';
    649 }
    650 
    651 function berryCheckoutHighlight( id ) {
    652 
    653     for ( var i=0; i<options.length; i++ ) {
    654         var option = document.getElementById( options[i] );
    655         if ( option != null ) {
    656             if ( id == options[i] ) {
    657                 //YAHOO.util.Dom.addClass( option, 'current' );
    658                 option.className='current';
    659             } else {
    660                 //YAHOO.util.Dom.removeClass( option, 'current' );
    661                 option.className='';
    662             }
    663         }
    664     }
    665 
    666     if ( option == null ) return;
    667     option.style.className = 'current';
    668 
    669     var del_options = document.getElementById("delOptions");
    670     if (id == "fullview") {
    671     del_options.style.display = "block";
    672     } else {
    673     del_options.style.display =  "none";
    674     }
    675 }
    676 
    677 YAHOO.util.Event.addListener(window,'load', pageLoad);
    678 
    679 
     220
Note: See TracChangeset for help on using the changeset viewer.