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

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

Refactoring to remove repeated code blocks. Tested Delete Selected and Delete All berries once again.

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