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

Last change on this file since 33107 was 33107, checked in by ak19, 5 years ago

Minor optimisation to the double for loop that depends on the ordering of the selections being in the order of the checkboxes, and the order of the checkboxes being in the order of the docList ids.

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