source: main/trunk/greenstone3/web/interfaces/default/js/berrybasket/berrycheckout.js@ 33185

Last change on this file since 33185 was 33182, checked in by kjdon, 5 years ago

source links now do have library/sites/localsite etc, so we don't need to remove the library part

File size: 20.4 KB
Line 
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"
3var default_link_type = "source";
4// use the appropriate one of these to override the default for particular collections.
5var source_link_collections = new Array(); // or add list of collections like ["pdfberry", "mgppdemo"];
6var document_link_collections = new Array(); // or add list of collections as above.
7//these are the default metadata items used by berry baskets.
8var default_metas = ["Title", "root_Title", "root_assocfilepath", "root_srclinkFile", "name", "collection", "Date"];
9
10var docList = new Array();
11var urlonly = false;
12var mailinfo = new Array();
13mailinfo['address'] = gs.text.berry.to;
14mailinfo['cc'] = gs.text.berry.cc;
15mailinfo['bcc'] = gs.text.berry.bcc;
16mailinfo['subject'] = gs.text.berry.subject;
17var textwin;
18var mailwin;
19
20var options = ['fullview', 'textview', 'email'];
21
22function toggleSelectAll(selAllCheckbox) {
23 // https://stackoverflow.com/questions/386281/how-to-implement-select-all-check-box-in-html
24 var allBerriesCheckboxList = document.getElementsByName('select-berry-checkbox');
25 for (var i = 0; i < allBerriesCheckboxList.length; i++) {
26 // if the selectAllCheckbox is checked, then all the berries' checkboxes will get checked
27 // And vice-versa.
28 allBerriesCheckboxList[i].checked = selAllCheckbox.checked;
29 }
30}
31
32function deleteSelected() {
33
34 if(docList.length == 0) return; // no berries on page, nothing to delete
35
36 // https://stackoverflow.com/questions/590018/getting-all-selected-checkboxes-in-an-array
37 // https://www.w3schools.com/jsref/met_document_queryselectorall.asp
38 // https://www.w3schools.com/cssref/css_selectors.asp
39 var selectedList = document.querySelectorAll('input[name=select-berry-checkbox]:checked');
40 if(selectedList.length == 0) return; // nothing selected, so nothing to delete
41
42 // if all berries selected for deletion, can optimise
43 if(selectedList.length === docList.length) {
44 deleteAll();
45 return; // done!
46 }
47
48 // otherwise selected list of berries is a proper subset of total berries (berries in docList)
49 var idsToDelete = [];
50
51 // construct the deletion url by concatenating the ids with | which is %7C in URL-encoded form
52 var delurl = delurlPath; // var delurlPath is declared in ygDDPlayer.js.
53
54 for(var i = 0; i < selectedList.length; i++) {
55 selected_id = selectedList[i].id;
56 // Format of checkbox id: "<docid>-checkbox"
57 var end = selected_id.indexOf("-checkbox");
58 var doc_id = selected_id.substring(0, end);
59
60 idsToDelete[i] = doc_id;
61
62 // Now just need to append each doc_id to the deletion URL separated by |,
63 // but this character needs to be URL encoded, else the delete doesn't work.
64 if((i+1) == selectedList.length) { // if it's the last id to process, don't append separator
65 delurl += doc_id;
66 } else { // there's more ids to process, so append separator
67 delurl += doc_id + "%7C"; // url-encoded version of |
68 }
69
70 }
71
72 var delAll = false;
73 doDelete(delAll, delurl, selectedList, idsToDelete);
74}
75
76function deleteAll() {
77
78 if(docList.length == 0) return; // nothing to delete
79
80 var delurl = delurlPath; // var delurlPath is declared in ygDDPlayer.js.
81 // Just need to append each doc id separated by |, but this character needs to be URL encoded,
82 // else the delete doesn't work.
83
84 for(var i = 0; i < docList.length; i++) {
85 var doc = docList[i];
86 var doc_id = doc['collection']+":"+ doc['name'];
87
88 if((i+1) == docList.length) { // if it's the last id to process, don't append separator
89 delurl += doc_id;
90 } else { // there's more ids to process, so append separator (in URL encoded form!)
91 delurl += doc_id + "%7C"; // url-encoded version of |
92 }
93 }
94
95 var delAll = true;
96 doDelete(delAll, delurl, null, null);
97}
98
99
100function doDelete(deleteAll, delurl, selectedList, idsToDelete) { // given list of selected checkboxes
101
102 // The following is a modified version of methods internal to
103 // ygDDPlayer.js's ygDDPlayer.prototype.onDragDrop
104 var delSuccess = function(o) {
105 var result = o.responseXML;
106
107 if(!deleteAll) { // then we're given a selection to delete: Not deleting all berries, just a subset
108 // Remove id of selected doc to be deleted from docList.
109 // Minor optimisation to double for loop, dependent on ordering of selected berries being
110 // in order of checkboxes (i.e. order of docList ids), and order of docList ids having
111 // the same order as the checkboxes
112 var searchForNextSelectedIdFromIndex = idsToDelete.length-1;
113 for (var i = docList.length - 1; i >= 0; i--) {
114 var berry = docList[i];
115 var berry_id = berry['collection'] + ":" + berry['name'];
116
117 for(var j = searchForNextSelectedIdFromIndex; j >= 0; j--) {
118 if(idsToDelete[j] == berry_id) {
119 docList.splice(i, 1); // i indexes into docList, delete element i from docList
120 searchForNextSelectedIdFromIndex = j-1;
121 break;
122 }
123 }
124 }
125
126 // remove the selected documents' HTML display elements
127 var berryDocsList = YAHOO.util.Dom.get('berryDocsList'); // ordered list item containing the berries
128 for(var i = 0; i < selectedList.length; i++) {
129 var li = selectedList[i].parentNode; // list item parent of checkbox
130 // remove the list item from its containing orderedList
131 berryDocsList.removeChild(li);
132 }
133 }
134
135
136 // if all docs are deleted by this stage, then display "berry basket is empty" message
137 if (deleteAll || !berryDocsList.hasChildNodes()) { // 2nd clause no longer needed?, then this just becomes an else against the first if(!deleteAll) test
138
139 // if deleting all docs, just use the easy way to empty the docList array
140 docList.length = 0; // https://www.jstips.co/en/javascript/two-ways-to-empty-an-array/
141
142 // Removing all child nodes (done one at a time) is more optimal
143 // than setting innerHTML to empty string, see
144 // https://stackoverflow.com/questions/3955229/remove-all-child-elements-of-a-dom-node-in-javascript
145 var content = YAHOO.util.Dom.get('berryBasketContent');
146 while (content.hasChildNodes()) {
147 content.removeChild(content.firstChild);
148 }
149 content.appendChild(document.createTextNode('Your berry basket is empty.'));
150
151 var trashbin = YAHOO.util.Dom.get('trashbin');
152 if ( trashbin !=null){
153 trashbin.style.background = 'url("interfaces/default/images/trash-full.png") 0 0 no-repeat';
154 }
155 }
156
157 // Ensure the select-all, delete-all and delete-selected checkboxes are deselected
158 YAHOO.util.Dom.get('select-all-checkbox').checked = false;
159 YAHOO.util.Dom.get('delete-selected-checkbox').checked = false;
160 YAHOO.util.Dom.get('delete-all-checkbox').checked = false;
161 }
162
163 var delFailure = function(o){ alert("Deletion failed" + o);}
164
165 var delcallback = {
166 success:delSuccess,
167 failure:delFailure,
168 argument:null // supposed to be the ygDDPlayer object, but don't have a ref to it here, so trying null
169 }
170
171 // Finally send the actual delete request
172 // request_type defaults to GET, which is what's used for add and del, see ygDDPlayer.js.
173 YAHOO.util.Connect.asyncRequest(request_type, delurl , delcallback);
174}
175
176function navigate(e){
177
178 var target = this;
179
180 if ( target.id.toLowerCase() == '' ) {
181 target = target.parentNode;
182 }
183
184 if (target.id.toLowerCase() == 'fullview'){
185 berryCheckoutHighlight( 'fullview' );
186 showFullView();
187 }
188
189 if (target.id.toLowerCase() == 'textview'){
190 berryCheckoutHighlight( 'textview' );
191 showTextView();
192 }
193
194 if (target.id.toLowerCase() == 'email'){
195 berryCheckoutHighlight( 'email' );
196 showEmail();
197 }
198
199 if (target.id.toLowerCase() == 'sendmail'){
200 sendMail();
201 }
202
203 if (target.id.toLowerCase() == 'urlcheck' && urlonly){
204 var urlcheck = YAHOO.util.Dom.get('urlcheck');
205 urlcheck.src = 'interfaces/default/images/check3.gif';
206 var parea =YAHOO.util.Dom.get('pretextarea');
207 urlonly = false;
208
209 this.value=gs.text.berry.url_only;
210
211 populateUrlsAndMetadata(parea);
212 return;
213 }
214
215 if (target.id.toLowerCase() == 'urlcheck' && !urlonly ){
216 var urlcheck = YAHOO.util.Dom.get('urlcheck');
217 urlcheck.src = 'interfaces/default/images/check4.gif';
218 var parea =YAHOO.util.Dom.get('pretextarea');
219 populateUrls(parea);
220 urlonly = true;
221
222 this.value=gs.text.berry.url_and_metadata;
223
224 return;
225 }
226
227 if (target.id.toLowerCase() == 'extextview' ){
228 if (textwin != null){
229 textwin.close();
230 }
231
232 textwin = window.open("","Berry basket plain text view","status=1,width=450,height=300");
233 textwin.moveTo(0,0);
234 var content = document.createElement('div');
235 buildPreview(content);
236 var body = textwin.document.getElementsByTagName('body')[0];
237 body.appendChild(content);
238 var prearea = textwin.document.getElementsByTagName('textarea')[0];
239 prearea.cols = '55';
240 prearea.rows = '15';
241 }
242
243 if (target.id.toLowerCase() == 'exemail' ){
244 if (mailwin != null){
245 mailwin.close();
246 }
247 mailwin = window.open("","Berry basket mail to a friend","status=1,width=450,height=350");
248 mailwin.moveTo(0,0);
249 var content = document.createElement('div');
250 getEmailContent(content);
251 var body = mailwin.document.getElementsByTagName('body')[0];
252 body.appendChild(content);
253 var prearea = mailwin.document.getElementsByTagName('textarea')[0];
254 prearea.cols = '50';
255 prearea.rows = '11';
256 }
257}
258
259function pageLoad(){
260 for(var j = 0; j < options.length; j++)
261 {
262 var ele = document.getElementById(options[j]);
263 YAHOO.util.Event.addListener(ele, 'click', navigate);
264 }
265
266 showFullView();
267}
268
269function showFullView(){
270
271 var content = YAHOO.util.Dom.get('berryBasketContent');
272 var fullview = YAHOO.util.Dom.get('fullview');
273 berryCheckoutPageClear();
274
275 if (docList.length == 0){
276 content.appendChild(document.createTextNode(gs.text.berry.empty_basket));
277 return;
278 }
279
280 var trashbin = document.createElement('div');
281 trashbin.id ='trashbin';
282
283 var binhandle = document.createElement('div');
284 binhandle.id = 'binhandle';
285 binhandle.appendChild(document.createElement('span'));
286 trashbin.appendChild(binhandle);
287 content.appendChild(trashbin);
288
289 var dd = new ygDDOnTop('trashbin');
290 dd.setHandleElId('binhandle');
291 new YAHOO.util.DDTarget('trashbin','trash');
292
293 var dlist = document.createElement('div');
294 content.appendChild(dlist);
295 var ol = document.createElement('ol');
296 dlist.appendChild(ol);
297
298 ol.setAttribute("id", "berryDocsList");
299
300 for (var i in docList){
301 var doc = docList[i];
302 var li = document.createElement('li');
303 var img = document.createElement('img');
304 var text ="";
305
306 var doc_id = doc['collection']+":"+ doc['name'];
307
308 img.setAttribute("src", "interfaces/default/images/berry.png");
309 img.setAttribute("id", doc_id);
310 img.setAttribute("height", "15px");
311 img.setAttribute("width", "15px");
312 li.appendChild(img);
313
314 generateDocDisplay(li, doc, doc_id)
315 li.className = 'berrydoc';
316 ol.appendChild(li);
317 new ygDDPlayer(img.id,'trash',docList);
318 }
319
320}
321
322function generateDocDisplay(li, doc, doc_id) {
323 var checkbox = document.createElement('input');
324 checkbox.setAttribute("type", "checkbox");
325 checkbox.setAttribute("id", doc_id+"-checkbox");
326 checkbox.setAttribute("name", "select-berry-checkbox");
327 checkbox.setAttribute("value", "select-"+doc_id);
328
329 var a = document.createElement('a');
330 var text="";
331 a.href=generateURL(doc);
332 a.appendChild(document.createTextNode(doc['Title']));
333
334 if (doc['root_Title']){
335 li.appendChild(document.createTextNode(doc['root_Title']+": "));
336 }
337 li.appendChild(checkbox);
338 li.appendChild(a);
339 li.appendChild(document.createTextNode(" ("+doc['collection']+")"));
340 var metadata = "";
341 for (var metaItem in doc) {
342 if ( !default_metas.includes(metaItem)){
343 metadata += " "+metaItem+": "+ doc[metaItem]+" ";
344 }
345 }
346 text +=metadata;
347 li.appendChild(document.createTextNode(text));
348
349}
350
351function showTextView(){
352
353 var content = YAHOO.util.Dom.get('berryBasketContent');
354 var textview = YAHOO.util.Dom.get('textview');
355
356 berryCheckoutPageClear();
357 if (docList.length == 0){
358 content.appendChild(document.createTextNode(gs.text.berry.empty_basket));
359 return;
360 }
361 buildPreview(content);
362
363}
364
365function getEmailContent(content){
366 var item ;
367 var tr;
368 var td;
369 var input;
370
371 table = document.createElement('table');
372 table.setAttribute("class","mailtable");
373
374 for (item in mailinfo){
375 tr = document.createElement('tr');
376 td = document.createElement('td');
377 td.setAttribute("class","mailitem");
378 td.appendChild(document.createTextNode(mailinfo[item]));
379 tr.appendChild(td);
380 td = document.createElement('td');
381 input = document.createElement('input');
382 input.setAttribute("id", item);
383 input.setAttribute("class", "mailinput");
384 if(item === "address") {
385 input.setAttribute("type", "email"); // https://html5-tutorial.net/form-validation/validating-email/
386 input.required = true; // https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
387 } else {
388 input.setAttribute("type", "text");
389 }
390 td.appendChild(input);
391 tr.appendChild(td);
392 table.appendChild(tr);
393 }
394
395 // an empty line
396 tr = document.createElement('tr');
397 td = document.createElement('td');
398 td.appendChild(document.createElement('br'));
399 tr.appendChild(td);
400 table.appendChild(tr);
401
402 content.appendChild(table);
403
404 buildPreview(content);
405
406 //send button
407 input = document.createElement('input');
408 input.setAttribute("id", 'sendmail');
409 input.setAttribute("class", "sendbutton");
410 input.setAttribute("type", "button");
411 input.setAttribute("value", gs.text.berry.send);
412 content.appendChild(input);
413}
414
415function showEmail(){
416 var content = YAHOO.util.Dom.get('berryBasketContent');
417 var email = YAHOO.util.Dom.get('email');
418
419 berryCheckoutPageClear();
420
421 if (docList.length == 0){
422 content.appendChild(document.createTextNode(gs.text.berry.empty_basket));
423 return;
424 }
425
426 var item;
427 var tr;
428 var td;
429 var input;
430
431 table = document.createElement('table');
432 table.setAttribute("class","mailtable");
433
434 for (item in mailinfo){
435 tr = document.createElement('tr');
436 td = document.createElement('td');
437 td.setAttribute("class","mailitem");
438 td.appendChild(document.createTextNode(mailinfo[item]));
439 tr.appendChild(td);
440
441 td = document.createElement('td');
442 input = document.createElement('input');
443 input.setAttribute("id", item);
444 input.setAttribute("class", "mailinput");
445 if(item === "address") {
446 input.setAttribute("type", "email"); // https://html5-tutorial.net/form-validation/validating-email/
447 input.required = true; // https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
448 } else {
449 input.setAttribute("type", "text");
450 }
451 td.appendChild(input);
452 tr.appendChild(td);
453 table.appendChild(tr);
454
455 }
456
457 // an empty line
458 tr = document.createElement('tr');
459 td = document.createElement('td');
460 td.appendChild(document.createElement('br'));
461 tr.appendChild(td);
462 table.appendChild(tr);
463
464 content.appendChild(table);
465
466 buildPreview(content);
467
468 //send button
469 input = document.createElement('input');
470 input.setAttribute("id", 'sendmail');
471 input.setAttribute("class", "sendbutton");
472 input.setAttribute("type", "button");
473 input.setAttribute("value", gs.text.berry.send);
474 content.appendChild(input);
475
476 YAHOO.util.Event.addListener(input, 'click', navigate);
477}
478
479function buildPreview(parent){
480
481 var div = document.createElement('div');
482 var cb = document.createElement('input');
483 cb.setAttribute('class', 'sendbutton');
484 cb.type = 'button';
485 cb.id = 'urlcheck';
486 if (urlonly)
487 {
488 cb.value=gs.text.berry.url_and_metadata;
489 }
490 else
491 {
492 cb.value=gs.text.berry.url_only;
493 }
494
495 YAHOO.util.Event.addListener(cb, 'click', navigate);
496
497 var img = document.createElement('img');
498 img.src = 'interfaces/default/images/check3.gif';
499 img.id = 'urlcheck';
500 div.appendChild(cb);
501 //div.appendChild(img);
502
503 var urls = document.createElement('span');
504 urls.id = 'urls';
505 urls.className = 'berrycheck';
506 //urls.appendChild(document.createTextNode('URL only'));
507 div.appendChild(urls);
508
509 // var urlsmetadata = document.createElement('span');
510 // urlsmetadata.id = 'urlsmetadata'
511 // urlsmetadata.className = 'berryradio';
512 // urlsmetadata.appendChild(document.createTextNode('URLs and Metadata'));
513 // div.appendChild(urlsmetadata);
514
515 parent.appendChild(div);
516
517 var parea = document.createElement('textarea');
518 parea.id = 'pretextarea';
519 parea.required = true; // https://www.w3schools.com/tags/att_textarea_required.asp
520 // and https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
521
522 parent.appendChild(parea);
523
524 if(urlonly)
525 {
526 populateUrls(parea);
527 }
528 else
529 {
530 populateUrlsAndMetadata(parea);
531 }
532}
533
534function getDefaultLinkType(collection) {
535 var link_type;
536 if (document_link_collections.includes(collection)) {
537 link_type = "document";
538 } else if (source_link_collections.includes(collection)) {
539 link_type = "source";
540 }
541 else {
542 link_type = default_link_type;
543 if (link_type != "source" && link_type != "document") {
544 link_type = "document"; //the default default
545 }
546 }
547 return link_type;
548}
549
550function generateURL(doc) {
551
552 var url;
553 var doc_url = document.URL;
554 var root_url = doc_url.substring(0,doc_url.indexOf('?'));
555
556 var link_type = getDefaultLinkType(doc["collection"]);
557 if (link_type == "document") {
558 url = root_url+"/collection/"+doc["collection"]+"/document/"+doc["name"];
559 } else if (link_type == "source") {
560 url = root_url+"/sites/"+gs.xsltParams.site_name+"/collect/"+doc['collection']+"/index/assoc/"+doc["root_assocfilepath"]+"/"+doc["root_srclinkFile"];
561 }
562 return url;
563}
564
565
566function populateUrls(parea){
567
568 var urls="";
569 for (var i in docList){
570 var doc = docList[i];
571 urls += generateURL(doc)+"\n\n";
572 }
573
574 parea.value = urls;
575
576}
577
578function populateUrlsAndMetadata(parea){
579
580 var fulltext="";
581 for (var i in docList){
582 var doc = docList[i];
583 var url = generateURL(doc)+"\n";
584
585 var metadata = "";
586 if (doc['Title']) {
587 metadata += gs.text.berry.doc_title+": "+doc['Title']+"\n";
588 }
589 if (doc['root_Title']) {
590 metadata += gs.text.berry.doc_root_title+": "+doc['root_Title']+"\n";
591
592 }
593 if (doc['name']) {
594 metadata += gs.text.berry.doc_name+": "+doc['name']+"\n";
595 }
596 if (doc['collection']) {
597 metadata += gs.text.berry.doc_collection+": "+doc['collection']+"\n";
598 }
599 if (doc['Date']) {
600 metadata += gs.text.berry.doc_date+": "+doc['Date']+"\n";
601 }
602 // allow for inclusion of custom metadata
603 for (var m in doc) {
604 if (!default_metas.includes(m)) {
605 metadata += m +":" + doc[m]+"\n";
606 }
607 }
608 fulltext +=url+metadata+"\n";
609 }
610
611 parea.value = fulltext;
612
613}
614
615function sendMail(){
616 var url = gs.xsltParams.library_name + "?a=pr&rt=r&ro=1&s=SendMail&c=";
617 var request_type = "POST";
618 var postdata = "";
619 var i;
620
621 var content = YAHOO.util.Dom.get('pretextarea').value;
622
623 // To send an email, the To address and message Body must contain data.
624 // HTML5 input checking (required attribute) would make empty fields red outlined,
625 // but did not prevent Send button submitting form. So some basic sanity checking in JS:
626 // Checking non-empty and to address field must further be a URL: checking it contains @
627 var to_address = YAHOO.util.Dom.get('address').value;
628
629 if(to_address.trim() === "") {
630 alert(gs.text.berry.invalid_to_address_empty);
631 return;
632 } else if(to_address.indexOf('@') === -1) {
633 alert(gs.text.berry.invalid_to_address);
634 return;
635 } else if(content.trim() === "") {
636 alert(gs.text.berry.invalid_msg_body_empty);
637 return;
638 }
639
640 //get checked items
641 for (i in mailinfo) {
642 var input = YAHOO.util.Dom.get(i);
643 var value = input.value;
644 postdata +="&s1."+i+"="+value;
645 }
646
647
648 content = content.replace(/&/g,'-------');
649 postdata +="&s1.content="+content;
650
651 var callback = {
652 success: function(o) {
653 var result = o.responseText;
654 alert(gs.text.berry.send_success);
655 } ,
656 failure: function(o) {
657 alert(gs.text.berry.send_fail);
658 }
659 }
660 YAHOO.util.Connect.asyncRequest(request_type , url , callback, postdata);
661}
662
663function berryCheckoutPageClear() {
664 var bbc = document.getElementById('berryBasketContent');
665 if ( bbc == null ) return;
666 bbc.innerHTML = '';
667}
668
669function berryCheckoutHighlight( id ) {
670
671 for ( var i=0; i<options.length; i++ ) {
672 var option = document.getElementById( options[i] );
673 if ( option != null ) {
674 if ( id == options[i] ) {
675 //YAHOO.util.Dom.addClass( option, 'current' );
676 option.className='current';
677 } else {
678 //YAHOO.util.Dom.removeClass( option, 'current' );
679 option.className='';
680 }
681 }
682 }
683
684 if ( option == null ) return;
685 option.style.className = 'current';
686
687}
688
689YAHOO.util.Event.addListener(window,'load', pageLoad);
690
691
Note: See TracBrowser for help on using the repository browser.