source: main/trunk/greenstone3/web/interfaces/default/js/favouritebasket/favouritecheckout.js@ 36071

Last change on this file since 36071 was 36071, checked in by kjdon, 2 years ago

deleted all the old berry basket stuff that we don't need any more

File size: 19.2 KB
Line 
1// The default link type in the basket - "document" = greenstone version of the document, "source" = link to source file eg pdf.
2var default_link_type = "document"; // "source" or "document"
3// use the appropriate one of these to override the default for particular collections.
4var source_link_collections = new Array(); // or add list of collections like ["pdfberry", "mgppdemo"];
5var document_link_collections = new Array(); // or add list of collections as above.
6//these are the default metadata items used by favourite baskets.
7var default_metas = ["Title", "root_Title", "root_assocfilepath", "root_srclinkFile", "name", "collection", "Date"];
8
9var docList = new Array();
10var urlonly = false;
11var mailinfo = new Array();
12mailinfo['address'] = gs.text.favourites.to;
13mailinfo['cc'] = gs.text.favourites.cc;
14mailinfo['bcc'] = gs.text.favourites.bcc;
15mailinfo['subject'] = gs.text.favourites.subject;
16var textwin;
17var mailwin;
18
19var options = ['fullview', 'textview', 'email'];
20var delurlPath = gs.xsltParams.library_name + "?a=pr&rt=r&ro=1&s=DeleteItems&c=&o=XML&hhf="+makeURLComponentSafe("[{\"name\":\"Cache-Control\", \"value\":\"no-cache\"}]")+"&s1.items=";
21var request_type = "GET"; // the default
22
23
24function deleteAll() {
25
26 if(docList.length == 0) return; // nothing to delete
27
28 var delurl = delurlPath;
29 // Just need to append each doc id separated by |, but this character needs to be URL encoded,
30 // else the delete doesn't work.
31
32 for(var i = 0; i < docList.length; i++) {
33 var doc = docList[i];
34 var doc_id = doc['collection']+":"+ doc['name'];
35
36 if((i+1) == docList.length) { // if it's the last id to process, don't append separator
37 delurl += doc_id;
38 } else { // there's more ids to process, so append separator (in URL encoded form!)
39 delurl += doc_id + "%7C"; // url-encoded version of |
40 }
41 }
42
43 var delAll = true;
44 doDelete(delAll, delurl, null, null);
45}
46
47function deleteFavouriteFromCheckout(el)
48{
49 var delurl = delurlPath;
50
51 var doc_id = el.getAttribute('id');
52 delurl += doc_id;
53
54 var deleteAll = false;
55 var selectedList = [ el ];
56 var idsToDelete = [ doc_id ];
57 doDelete(deleteAll, delurl, selectedList, idsToDelete);
58}
59
60function doDelete(deleteAll, delurl, selectedList, idsToDelete) { // given list of selected checkboxes
61
62 // The following is a modified version of methods internal to
63 // ygDDPlayer.js's ygDDPlayer.prototype.onDragDrop
64 var delSuccess = function(o) {
65 var result = o.responseXML;
66
67 if(!deleteAll) { // then we're given a selection to delete: Not deleting all berries, just a subset
68 // Remove id of selected doc to be deleted from docList.
69 // Minor optimisation to double for loop, dependent on ordering of selected berries being
70 // in order of checkboxes (i.e. order of docList ids), and order of docList ids having
71 // the same order as the checkboxes
72 var searchForNextSelectedIdFromIndex = idsToDelete.length-1;
73 for (var i = docList.length - 1; i >= 0; i--) {
74 var berry = docList[i];
75 var berry_id = berry['collection'] + ":" + berry['name'];
76
77 for(var j = searchForNextSelectedIdFromIndex; j >= 0; j--) {
78 if(idsToDelete[j] == berry_id) {
79 docList.splice(i, 1); // i indexes into docList, delete element i from docList
80 searchForNextSelectedIdFromIndex = j-1;
81 break;
82 }
83 }
84 }
85
86 // remove the selected documents' HTML display elements
87 var berryDocsList = YAHOO.util.Dom.get('berryDocsList'); // ordered list item containing the berries
88 for(var i = 0; i < selectedList.length; i++) {
89 var li = selectedList[i].parentNode; // list item parent of checkbox
90 // remove the list item from its containing orderedList
91 berryDocsList.removeChild(li);
92 }
93 }
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
98
99 // if deleting all docs, just use the easy way to empty the docList array
100 docList.length = 0; // https://www.jstips.co/en/javascript/two-ways-to-empty-an-array/
101
102 // Removing all child nodes (done one at a time) is more optimal
103 // than setting innerHTML to empty string, see
104 // https://stackoverflow.com/questions/3955229/remove-all-child-elements-of-a-dom-node-in-javascript
105 var content = YAHOO.util.Dom.get('berryBasketContent');
106 while (content.hasChildNodes()) {
107 content.removeChild(content.firstChild);
108 }
109 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 }
128 }
129
130 var delFailure = function(o){ alert("Deletion failed" + o);}
131
132 var delcallback = {
133 success:delSuccess,
134 failure:delFailure,
135 argument:null // supposed to be the ygDDPlayer object, but don't have a ref to it here, so trying null
136 }
137
138 // Finally send the actual delete request
139 // request_type defaults to GET, which is what's used for add and del, see ygDDPlayer.js.
140 YAHOO.util.Connect.asyncRequest(request_type, delurl , delcallback);
141}
142
143
144// switch between Full, Text and Email views
145function 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
228function 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
238function showFullView() {
239 showFullViewFavourites();
240}
241
242function emptyBasketText() {
243 return document.createTextNode(gs.text.favourites.empty_basket);
244}
245
246
247function showFullViewFavourites(){
248
249 var content = YAHOO.util.Dom.get('berryBasketContent');
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 dlist = document.createElement('div');
267 content.appendChild(dlist);
268 var ul = document.createElement('ul');
269 dlist.appendChild(ul);
270
271 ul.setAttribute("id", "berryDocsList");
272 ul.setAttribute("style","list-style: none;");
273
274 for (var i in docList){
275 var doc = docList[i];
276 var li = document.createElement('li');
277 var img = document.createElement('img');
278 var text ="";
279
280 var doc_id = doc['collection']+":"+ doc['name'];
281
282 img.setAttribute("src", gs.variables.selected_favourite_icon_url);
283 img.setAttribute("id", doc_id);
284 img.setAttribute("height", "20px");
285 img.setAttribute("width", "20px");
286 img.setAttribute("style","padding-right: 5px;"); // **** better to do this with CSS
287 img.setAttribute("onClick", "deleteFavouriteFromCheckout(this)");
288 li.appendChild(img);
289
290 generateDocDisplay(li, doc, doc_id)
291 li.className = 'berrydoc';
292 ul.appendChild(li);
293 }
294
295}
296
297function generateDocDisplay(li, doc, doc_id) {
298 var a = document.createElement('a');
299 var text="";
300 a.href=generateURL(doc);
301 a.appendChild(document.createTextNode(doc['Title']));
302
303 if (doc['root_Title']){
304 li.appendChild(document.createTextNode(doc['root_Title']+": "));
305 }
306
307 li.appendChild(a);
308 li.appendChild(document.createTextNode(" ("+doc['collection']+")"));
309 var metadata = "";
310 for (var metaItem in doc) {
311 if ( !default_metas.includes(metaItem)){
312 metadata += " "+metaItem+": "+ doc[metaItem]+" ";
313 }
314 }
315 text +=metadata;
316 li.appendChild(document.createTextNode(text));
317
318}
319
320function showTextView(){
321
322 var content = YAHOO.util.Dom.get('berryBasketContent');
323 var textview = YAHOO.util.Dom.get('textview');
324
325 berryCheckoutPageClear();
326 if (docList.length == 0){
327 content.appendChild(emptyBasketText());
328 return;
329 }
330 buildPreview(content);
331
332}
333
334function getEmailContent(content){
335 var item ;
336 var tr;
337 var td;
338 var input;
339
340 table = document.createElement('table');
341 table.setAttribute("class","mailtable");
342
343 for (item in mailinfo){
344 tr = document.createElement('tr');
345 td = document.createElement('td');
346 td.setAttribute("class","mailitem");
347 td.appendChild(document.createTextNode(mailinfo[item]));
348 tr.appendChild(td);
349 td = document.createElement('td');
350 input = document.createElement('input');
351 input.setAttribute("id", item);
352 input.setAttribute("class", "mailinput");
353 if(item === "address") {
354 input.setAttribute("type", "email"); // https://html5-tutorial.net/form-validation/validating-email/
355 input.required = true; // https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
356 } else {
357 input.setAttribute("type", "text");
358 }
359 td.appendChild(input);
360 tr.appendChild(td);
361 table.appendChild(tr);
362 }
363
364 // an empty line
365 tr = document.createElement('tr');
366 td = document.createElement('td');
367 td.appendChild(document.createElement('br'));
368 tr.appendChild(td);
369 table.appendChild(tr);
370
371 content.appendChild(table);
372
373 buildPreview(content);
374
375 //send button
376 input = document.createElement('input');
377 input.setAttribute("id", 'sendmail');
378 input.setAttribute("class", "sendbutton");
379 input.setAttribute("type", "button");
380 input.setAttribute("value", gs.text.favourites.send);
381 content.appendChild(input);
382}
383
384function showEmail(){
385 var content = YAHOO.util.Dom.get('berryBasketContent');
386 var email = YAHOO.util.Dom.get('email');
387
388 berryCheckoutPageClear();
389
390 if (docList.length == 0){
391 content.appendChild(emptyBasketText());
392 return;
393 }
394
395 var item;
396 var tr;
397 var td;
398 var input;
399
400 table = document.createElement('table');
401 table.setAttribute("class","mailtable");
402
403 for (item in mailinfo){
404 tr = document.createElement('tr');
405 td = document.createElement('td');
406 td.setAttribute("class","mailitem");
407 td.appendChild(document.createTextNode(mailinfo[item]));
408 tr.appendChild(td);
409
410 td = document.createElement('td');
411 input = document.createElement('input');
412 input.setAttribute("id", item);
413 input.setAttribute("class", "mailinput");
414 if(item === "address") {
415 input.setAttribute("type", "email"); // https://html5-tutorial.net/form-validation/validating-email/
416 input.required = true; // https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
417 } else {
418 input.setAttribute("type", "text");
419 }
420 td.appendChild(input);
421 tr.appendChild(td);
422 table.appendChild(tr);
423
424 }
425
426 // an empty line
427 tr = document.createElement('tr');
428 td = document.createElement('td');
429 td.appendChild(document.createElement('br'));
430 tr.appendChild(td);
431 table.appendChild(tr);
432
433 content.appendChild(table);
434
435 buildPreview(content);
436
437 //send button
438 input = document.createElement('input');
439 input.setAttribute("id", 'sendmail');
440 input.setAttribute("class", "sendbutton");
441 input.setAttribute("type", "button");
442 input.setAttribute("value", gs.text.favourites.send);
443 content.appendChild(input);
444
445 YAHOO.util.Event.addListener(input, 'click', navigate);
446}
447
448function buildPreview(parent){
449
450 var div = document.createElement('div');
451 var cb = document.createElement('input');
452 cb.setAttribute('class', 'sendbutton');
453 cb.type = 'button';
454 cb.id = 'urlcheck';
455 if (urlonly)
456 {
457 cb.value=gs.text.favourites.url_and_metadata;
458 }
459 else
460 {
461 cb.value=gs.text.favourites.url_only;
462 }
463
464 YAHOO.util.Event.addListener(cb, 'click', navigate);
465
466 var img = document.createElement('img');
467 img.src = 'interfaces/default/images/check3.gif';
468 img.id = 'urlcheck';
469 div.appendChild(cb);
470 //div.appendChild(img);
471
472 var urls = document.createElement('span');
473 urls.id = 'urls';
474 urls.className = 'berrycheck';
475 //urls.appendChild(document.createTextNode('URL only'));
476 div.appendChild(urls);
477
478 // var urlsmetadata = document.createElement('span');
479 // urlsmetadata.id = 'urlsmetadata'
480 // urlsmetadata.className = 'berryradio';
481 // urlsmetadata.appendChild(document.createTextNode('URLs and Metadata'));
482 // div.appendChild(urlsmetadata);
483
484 parent.appendChild(div);
485
486 var parea = document.createElement('textarea');
487 parea.id = 'pretextarea';
488 parea.required = true; // https://www.w3schools.com/tags/att_textarea_required.asp
489 // and https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
490
491 parent.appendChild(parea);
492
493 if(urlonly)
494 {
495 populateUrls(parea);
496 }
497 else
498 {
499 populateUrlsAndMetadata(parea);
500 }
501}
502
503function getDefaultLinkType(collection) {
504 var link_type;
505 if (document_link_collections.includes(collection)) {
506 link_type = "document";
507 } else if (source_link_collections.includes(collection)) {
508 link_type = "source";
509 }
510 else {
511 link_type = default_link_type;
512 if (link_type != "source" && link_type != "document") {
513 link_type = "document"; //the default default
514 }
515 }
516 return link_type;
517}
518
519function generateURL(doc) {
520
521 var url;
522 var doc_url = document.URL;
523 var root_url = doc_url.substring(0,doc_url.indexOf('?'));
524
525 var link_type = getDefaultLinkType(doc["collection"]);
526 if (link_type == "document") {
527 url = root_url+"/collection/"+doc["collection"]+"/document/"+doc["name"];
528 } else if (link_type == "source") {
529 url = root_url+"/sites/"+gs.xsltParams.site_name+"/collect/"+doc['collection']+"/index/assoc/"+doc["root_assocfilepath"]+"/"+doc["root_srclinkFile"];
530 }
531 return url;
532}
533
534
535function populateUrls(parea){
536
537 var urls="";
538 for (var i in docList){
539 var doc = docList[i];
540 urls += generateURL(doc)+"\n\n";
541 }
542
543 parea.value = urls;
544
545}
546
547function populateUrlsAndMetadata(parea){
548
549 var fulltext="";
550 for (var i in docList){
551 var doc = docList[i];
552 var url = generateURL(doc)+"\n";
553
554 var metadata = "";
555 if (doc['Title']) {
556 metadata += gs.text.favourites.doc_title+": "+doc['Title']+"\n";
557 }
558 if (doc['root_Title']) {
559 metadata += gs.text.favourites.doc_root_title+": "+doc['root_Title']+"\n";
560
561 }
562 if (doc['name']) {
563 metadata += gs.text.favourites.doc_name+": "+doc['name']+"\n";
564 }
565 if (doc['collection']) {
566 metadata += gs.text.favourites.doc_collection+": "+doc['collection']+"\n";
567 }
568 if (doc['Date']) {
569 metadata += gs.text.favourites.doc_date+": "+doc['Date']+"\n";
570 }
571 // allow for inclusion of custom metadata
572 for (var m in doc) {
573 if (!default_metas.includes(m)) {
574 metadata += m +":" + doc[m]+"\n";
575 }
576 }
577 fulltext +=url+metadata+"\n";
578 }
579
580 parea.value = fulltext;
581
582}
583
584function sendMail(){
585 var url = gs.xsltParams.library_name + "?a=pr&rt=r&ro=1&s=SendMail&c=";
586 var request_type = "POST";
587 var postdata = "";
588 var i;
589
590 var content = YAHOO.util.Dom.get('pretextarea').value;
591
592 // To send an email, the To address and message Body must contain data.
593 // HTML5 input checking (required attribute) would make empty fields red outlined,
594 // but did not prevent Send button submitting form. So some basic sanity checking in JS:
595 // Checking non-empty and to address field must further be a URL: checking it contains @
596 var to_address = YAHOO.util.Dom.get('address').value;
597
598 if(to_address.trim() === "") {
599 alert(gs.text.favourites.invalid_to_address_empty);
600 return;
601 } else if(to_address.indexOf('@') === -1) {
602 alert(gs.text.favourites.invalid_to_address);
603 return;
604 } else if(content.trim() === "") {
605 alert(gs.text.favourites.invalid_msg_body_empty);
606 return;
607 }
608
609 //get checked items
610 for (i in mailinfo) {
611 var input = YAHOO.util.Dom.get(i);
612 var value = input.value;
613 postdata +="&s1."+i+"="+value;
614 }
615
616
617 content = content.replace(/&/g,'-------');
618 postdata +="&s1.content="+content;
619
620 var callback = {
621 success: function(o) {
622 var result = o.responseText;
623 alert(gs.text.favourites.send_success);
624 } ,
625 failure: function(o) {
626 alert(gs.text.favourites.send_fail);
627 }
628 }
629 YAHOO.util.Connect.asyncRequest(request_type , url , callback, postdata);
630}
631
632function berryCheckoutPageClear() {
633 var bbc = document.getElementById('berryBasketContent');
634 if ( bbc == null ) return;
635 bbc.innerHTML = '';
636}
637
638function berryCheckoutHighlight( id ) {
639
640 for ( var i=0; i<options.length; i++ ) {
641 var option = document.getElementById( options[i] );
642 if ( option != null ) {
643 if ( id == options[i] ) {
644 //YAHOO.util.Dom.addClass( option, 'current' );
645 option.className='current';
646 } else {
647 //YAHOO.util.Dom.removeClass( option, 'current' );
648 option.className='';
649 }
650 }
651 }
652
653 if ( option == null ) return;
654 option.style.className = 'current';
655
656 var del_options = document.getElementById("delOptions");
657 if (id == "fullview") {
658 del_options.style.display = "block";
659 } else {
660 del_options.style.display = "none";
661 }
662}
663
664YAHOO.util.Event.addListener(window,'load', pageLoad);
665
666
Note: See TracBrowser for help on using the repository browser.