source: main/trunk/model-interfaces-dev/heritage-nz/js/berrybasket/berrycheckout.js@ 33228

Last change on this file since 33228 was 33228, checked in by davidb, 5 years ago

Hack to make emptying the berrybasket a single click operation

File size: 20.7 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 // ****
152 var deleteAllDiv = YAHOO.util.Dom.get('berryBasketDeleteAll');
153 if (deleteAllDiv != null) {
154 deleteAllDiv.style.display = "none";
155 }
156
157 var trashbin = YAHOO.util.Dom.get('trashbin');
158 if ( trashbin !=null){
159 trashbin.style.background = 'url("interfaces/default/images/trash-full.png") 0 0 no-repeat';
160 }
161 }
162
163 // Ensure the select-all, delete-all and delete-selected checkboxes are deselected
164 YAHOO.util.Dom.get('select-all-checkbox').checked = false;
165 YAHOO.util.Dom.get('delete-selected-checkbox').checked = false;
166 YAHOO.util.Dom.get('delete-all-checkbox').checked = false;
167 }
168
169 var delFailure = function(o){ alert("Deletion failed" + o);}
170
171 var delcallback = {
172 success:delSuccess,
173 failure:delFailure,
174 argument:null // supposed to be the ygDDPlayer object, but don't have a ref to it here, so trying null
175 }
176
177 // Finally send the actual delete request
178 // request_type defaults to GET, which is what's used for add and del, see ygDDPlayer.js.
179 YAHOO.util.Connect.asyncRequest(request_type, delurl , delcallback);
180}
181
182function navigate(e){
183
184 var target = this;
185
186 if ( target.id.toLowerCase() == '' ) {
187 target = target.parentNode;
188 }
189
190 if (target.id.toLowerCase() == 'fullview'){
191 berryCheckoutHighlight( 'fullview' );
192 showFullView();
193 }
194
195 if (target.id.toLowerCase() == 'textview'){
196 berryCheckoutHighlight( 'textview' );
197 showTextView();
198 }
199
200 if (target.id.toLowerCase() == 'email'){
201 berryCheckoutHighlight( 'email' );
202 showEmail();
203 }
204
205 if (target.id.toLowerCase() == 'sendmail'){
206 sendMail();
207 }
208
209 if (target.id.toLowerCase() == 'urlcheck' && urlonly){
210 var urlcheck = YAHOO.util.Dom.get('urlcheck');
211 urlcheck.src = 'interfaces/default/images/check3.gif';
212 var parea =YAHOO.util.Dom.get('pretextarea');
213 urlonly = false;
214
215 this.value=gs.text.berry.url_only;
216
217 populateUrlsAndMetadata(parea);
218 return;
219 }
220
221 if (target.id.toLowerCase() == 'urlcheck' && !urlonly ){
222 var urlcheck = YAHOO.util.Dom.get('urlcheck');
223 urlcheck.src = 'interfaces/default/images/check4.gif';
224 var parea =YAHOO.util.Dom.get('pretextarea');
225 populateUrls(parea);
226 urlonly = true;
227
228 this.value=gs.text.berry.url_and_metadata;
229
230 return;
231 }
232
233 if (target.id.toLowerCase() == 'extextview' ){
234 if (textwin != null){
235 textwin.close();
236 }
237
238 textwin = window.open("","Berry basket plain text view","status=1,width=450,height=300");
239 textwin.moveTo(0,0);
240 var content = document.createElement('div');
241 buildPreview(content);
242 var body = textwin.document.getElementsByTagName('body')[0];
243 body.appendChild(content);
244 var prearea = textwin.document.getElementsByTagName('textarea')[0];
245 prearea.cols = '55';
246 prearea.rows = '15';
247 }
248
249 if (target.id.toLowerCase() == 'exemail' ){
250 if (mailwin != null){
251 mailwin.close();
252 }
253 mailwin = window.open("","Berry basket mail to a friend","status=1,width=450,height=350");
254 mailwin.moveTo(0,0);
255 var content = document.createElement('div');
256 getEmailContent(content);
257 var body = mailwin.document.getElementsByTagName('body')[0];
258 body.appendChild(content);
259 var prearea = mailwin.document.getElementsByTagName('textarea')[0];
260 prearea.cols = '50';
261 prearea.rows = '11';
262 }
263}
264
265function pageLoad(){
266 for(var j = 0; j < options.length; j++)
267 {
268 var ele = document.getElementById(options[j]);
269 YAHOO.util.Event.addListener(ele, 'click', navigate);
270 }
271
272 showFullView();
273}
274
275function showFullView(){
276
277 var content = YAHOO.util.Dom.get('berryBasketContent');
278 var fullview = YAHOO.util.Dom.get('fullview');
279 berryCheckoutPageClear();
280
281 if (docList.length == 0){
282 content.appendChild(document.createTextNode(gs.text.berry.empty_basket));
283
284 // ****
285 var deleteAllDiv = YAHOO.util.Dom.get('berryBasketDeleteAll');
286 if (deleteAllDiv != null) {
287 deleteAllDiv.style.display = "none";
288 }
289
290 return;
291 }
292
293 var trashbin = document.createElement('div');
294 trashbin.id ='trashbin';
295
296 var binhandle = document.createElement('div');
297 binhandle.id = 'binhandle';
298 binhandle.appendChild(document.createElement('span'));
299 trashbin.appendChild(binhandle);
300 content.appendChild(trashbin);
301
302 var dd = new ygDDOnTop('trashbin');
303 dd.setHandleElId('binhandle');
304 new YAHOO.util.DDTarget('trashbin','trash');
305
306 var dlist = document.createElement('div');
307 content.appendChild(dlist);
308 var ol = document.createElement('ol');
309 dlist.appendChild(ol);
310
311 ol.setAttribute("id", "berryDocsList");
312
313 for (var i in docList){
314 var doc = docList[i];
315 var li = document.createElement('li');
316 var img = document.createElement('img');
317 var text ="";
318
319 var doc_id = doc['collection']+":"+ doc['name'];
320
321 img.setAttribute("src", "interfaces/default/images/berry.png");
322 img.setAttribute("id", doc_id);
323 img.setAttribute("height", "15px");
324 img.setAttribute("width", "15px");
325 li.appendChild(img);
326
327 generateDocDisplay(li, doc, doc_id)
328 li.className = 'berrydoc';
329 ol.appendChild(li);
330 new ygDDPlayer(img.id,'trash',docList);
331 }
332
333}
334
335function generateDocDisplay(li, doc, doc_id) {
336 var checkbox = document.createElement('input');
337 checkbox.setAttribute("type", "checkbox");
338 checkbox.setAttribute("id", doc_id+"-checkbox");
339 checkbox.setAttribute("name", "select-berry-checkbox");
340 checkbox.setAttribute("value", "select-"+doc_id);
341
342 var a = document.createElement('a');
343 var text="";
344 a.href=generateURL(doc);
345 a.appendChild(document.createTextNode(doc['Title']));
346
347 if (doc['root_Title']){
348 li.appendChild(document.createTextNode(doc['root_Title']+": "));
349 }
350 li.appendChild(checkbox);
351 li.appendChild(a);
352 li.appendChild(document.createTextNode(" ("+doc['collection']+")"));
353 var metadata = "";
354 for (var metaItem in doc) {
355 if ( !default_metas.includes(metaItem)){
356 metadata += " "+metaItem+": "+ doc[metaItem]+" ";
357 }
358 }
359 text +=metadata;
360 li.appendChild(document.createTextNode(text));
361
362}
363
364function showTextView(){
365
366 var content = YAHOO.util.Dom.get('berryBasketContent');
367 var textview = YAHOO.util.Dom.get('textview');
368
369 berryCheckoutPageClear();
370 if (docList.length == 0){
371 content.appendChild(document.createTextNode(gs.text.berry.empty_basket));
372 return;
373 }
374 buildPreview(content);
375
376}
377
378function getEmailContent(content){
379 var item ;
380 var tr;
381 var td;
382 var input;
383
384 table = document.createElement('table');
385 table.setAttribute("class","mailtable");
386
387 for (item in mailinfo){
388 tr = document.createElement('tr');
389 td = document.createElement('td');
390 td.setAttribute("class","mailitem");
391 td.appendChild(document.createTextNode(mailinfo[item]));
392 tr.appendChild(td);
393 td = document.createElement('td');
394 input = document.createElement('input');
395 input.setAttribute("id", item);
396 input.setAttribute("class", "mailinput");
397 if(item === "address") {
398 input.setAttribute("type", "email"); // https://html5-tutorial.net/form-validation/validating-email/
399 input.required = true; // https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
400 } else {
401 input.setAttribute("type", "text");
402 }
403 td.appendChild(input);
404 tr.appendChild(td);
405 table.appendChild(tr);
406 }
407
408 // an empty line
409 tr = document.createElement('tr');
410 td = document.createElement('td');
411 td.appendChild(document.createElement('br'));
412 tr.appendChild(td);
413 table.appendChild(tr);
414
415 content.appendChild(table);
416
417 buildPreview(content);
418
419 //send button
420 input = document.createElement('input');
421 input.setAttribute("id", 'sendmail');
422 input.setAttribute("class", "sendbutton");
423 input.setAttribute("type", "button");
424 input.setAttribute("value", gs.text.berry.send);
425 content.appendChild(input);
426}
427
428function showEmail(){
429 var content = YAHOO.util.Dom.get('berryBasketContent');
430 var email = YAHOO.util.Dom.get('email');
431
432 berryCheckoutPageClear();
433
434 if (docList.length == 0){
435 content.appendChild(document.createTextNode(gs.text.berry.empty_basket));
436 return;
437 }
438
439 var item;
440 var tr;
441 var td;
442 var input;
443
444 table = document.createElement('table');
445 table.setAttribute("class","mailtable");
446
447 for (item in mailinfo){
448 tr = document.createElement('tr');
449 td = document.createElement('td');
450 td.setAttribute("class","mailitem");
451 td.appendChild(document.createTextNode(mailinfo[item]));
452 tr.appendChild(td);
453
454 td = document.createElement('td');
455 input = document.createElement('input');
456 input.setAttribute("id", item);
457 input.setAttribute("class", "mailinput");
458 if(item === "address") {
459 input.setAttribute("type", "email"); // https://html5-tutorial.net/form-validation/validating-email/
460 input.required = true; // https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
461 } else {
462 input.setAttribute("type", "text");
463 }
464 td.appendChild(input);
465 tr.appendChild(td);
466 table.appendChild(tr);
467
468 }
469
470 // an empty line
471 tr = document.createElement('tr');
472 td = document.createElement('td');
473 td.appendChild(document.createElement('br'));
474 tr.appendChild(td);
475 table.appendChild(tr);
476
477 content.appendChild(table);
478
479 buildPreview(content);
480
481 //send button
482 input = document.createElement('input');
483 input.setAttribute("id", 'sendmail');
484 input.setAttribute("class", "sendbutton");
485 input.setAttribute("type", "button");
486 input.setAttribute("value", gs.text.berry.send);
487 content.appendChild(input);
488
489 YAHOO.util.Event.addListener(input, 'click', navigate);
490}
491
492function buildPreview(parent){
493
494 var div = document.createElement('div');
495 var cb = document.createElement('input');
496 cb.setAttribute('class', 'sendbutton');
497 cb.type = 'button';
498 cb.id = 'urlcheck';
499 if (urlonly)
500 {
501 cb.value=gs.text.berry.url_and_metadata;
502 }
503 else
504 {
505 cb.value=gs.text.berry.url_only;
506 }
507
508 YAHOO.util.Event.addListener(cb, 'click', navigate);
509
510 var img = document.createElement('img');
511 img.src = 'interfaces/default/images/check3.gif';
512 img.id = 'urlcheck';
513 div.appendChild(cb);
514 //div.appendChild(img);
515
516 var urls = document.createElement('span');
517 urls.id = 'urls';
518 urls.className = 'berrycheck';
519 //urls.appendChild(document.createTextNode('URL only'));
520 div.appendChild(urls);
521
522 // var urlsmetadata = document.createElement('span');
523 // urlsmetadata.id = 'urlsmetadata'
524 // urlsmetadata.className = 'berryradio';
525 // urlsmetadata.appendChild(document.createTextNode('URLs and Metadata'));
526 // div.appendChild(urlsmetadata);
527
528 parent.appendChild(div);
529
530 var parea = document.createElement('textarea');
531 parea.id = 'pretextarea';
532 parea.required = true; // https://www.w3schools.com/tags/att_textarea_required.asp
533 // and https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
534
535 parent.appendChild(parea);
536
537 if(urlonly)
538 {
539 populateUrls(parea);
540 }
541 else
542 {
543 populateUrlsAndMetadata(parea);
544 }
545}
546
547function getDefaultLinkType(collection) {
548 var link_type;
549 if (document_link_collections.includes(collection)) {
550 link_type = "document";
551 } else if (source_link_collections.includes(collection)) {
552 link_type = "source";
553 }
554 else {
555 link_type = default_link_type;
556 if (link_type != "source" && link_type != "document") {
557 link_type = "document"; //the default default
558 }
559 }
560 return link_type;
561}
562
563function generateURL(doc) {
564
565 var url;
566 var doc_url = document.URL;
567 var root_url = doc_url.substring(0,doc_url.indexOf('?'));
568
569 var link_type = getDefaultLinkType(doc["collection"]);
570 if (link_type == "document") {
571 url = root_url+"/collection/"+doc["collection"]+"/document/"+doc["name"];
572 } else if (link_type == "source") {
573 url = root_url+"/sites/"+gs.xsltParams.site_name+"/collect/"+doc['collection']+"/index/assoc/"+doc["root_assocfilepath"]+"/"+doc["root_srclinkFile"];
574 }
575 return url;
576}
577
578
579function populateUrls(parea){
580
581 var urls="";
582 for (var i in docList){
583 var doc = docList[i];
584 urls += generateURL(doc)+"\n\n";
585 }
586
587 parea.value = urls;
588
589}
590
591function populateUrlsAndMetadata(parea){
592
593 var fulltext="";
594 for (var i in docList){
595 var doc = docList[i];
596 var url = generateURL(doc)+"\n";
597
598 var metadata = "";
599 if (doc['Title']) {
600 metadata += gs.text.berry.doc_title+": "+doc['Title']+"\n";
601 }
602 if (doc['root_Title']) {
603 metadata += gs.text.berry.doc_root_title+": "+doc['root_Title']+"\n";
604
605 }
606 if (doc['name']) {
607 metadata += gs.text.berry.doc_name+": "+doc['name']+"\n";
608 }
609 if (doc['collection']) {
610 metadata += gs.text.berry.doc_collection+": "+doc['collection']+"\n";
611 }
612 if (doc['Date']) {
613 metadata += gs.text.berry.doc_date+": "+doc['Date']+"\n";
614 }
615 // allow for inclusion of custom metadata
616 for (var m in doc) {
617 if (!default_metas.includes(m)) {
618 metadata += m +":" + doc[m]+"\n";
619 }
620 }
621 fulltext +=url+metadata+"\n";
622 }
623
624 parea.value = fulltext;
625
626}
627
628function sendMail(){
629 var url = gs.xsltParams.library_name + "?a=pr&rt=r&ro=1&s=SendMail&c=";
630 var request_type = "POST";
631 var postdata = "";
632 var i;
633
634 var content = YAHOO.util.Dom.get('pretextarea').value;
635
636 // To send an email, the To address and message Body must contain data.
637 // HTML5 input checking (required attribute) would make empty fields red outlined,
638 // but did not prevent Send button submitting form. So some basic sanity checking in JS:
639 // Checking non-empty and to address field must further be a URL: checking it contains @
640 var to_address = YAHOO.util.Dom.get('address').value;
641
642 if(to_address.trim() === "") {
643 alert(gs.text.berry.invalid_to_address_empty);
644 return;
645 } else if(to_address.indexOf('@') === -1) {
646 alert(gs.text.berry.invalid_to_address);
647 return;
648 } else if(content.trim() === "") {
649 alert(gs.text.berry.invalid_msg_body_empty);
650 return;
651 }
652
653 //get checked items
654 for (i in mailinfo) {
655 var input = YAHOO.util.Dom.get(i);
656 var value = input.value;
657 postdata +="&s1."+i+"="+value;
658 }
659
660
661 content = content.replace(/&/g,'-------');
662 postdata +="&s1.content="+content;
663
664 var callback = {
665 success: function(o) {
666 var result = o.responseText;
667 alert(gs.text.berry.send_success);
668 } ,
669 failure: function(o) {
670 alert(gs.text.berry.send_fail);
671 }
672 }
673 YAHOO.util.Connect.asyncRequest(request_type , url , callback, postdata);
674}
675
676function berryCheckoutPageClear() {
677 var bbc = document.getElementById('berryBasketContent');
678 if ( bbc == null ) return;
679 bbc.innerHTML = '';
680}
681
682function berryCheckoutHighlight( id ) {
683
684 for ( var i=0; i<options.length; i++ ) {
685 var option = document.getElementById( options[i] );
686 if ( option != null ) {
687 if ( id == options[i] ) {
688 //YAHOO.util.Dom.addClass( option, 'current' );
689 option.className='current';
690 } else {
691 //YAHOO.util.Dom.removeClass( option, 'current' );
692 option.className='';
693 }
694 }
695 }
696
697 if ( option == null ) return;
698 option.style.className = 'current';
699
700}
701
702YAHOO.util.Event.addListener(window,'load', pageLoad);
703
704
Note: See TracBrowser for help on using the repository browser.