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

Last change on this file since 37523 was 37523, checked in by kjdon, 14 months ago

changes reflecting changes to favourites service names, and the xml that gets returned. also started work on mailing which is not finished.

File size: 19.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 favourite baskets.
7var default_metas = ["Title", "root_Title", "root_assocfilepath", "root_srclinkFile", "nodeID", "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=DeleteFavourites&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('berryBasketContentXX');
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 share_anchor = YAHOO.util.Dom.get('shareBasket');
267 var plain_text = "";
268 var dlist = document.createElement('div');
269 content.appendChild(dlist);
270 var ul = document.createElement('ul');
271 dlist.appendChild(ul);
272
273 ul.setAttribute("id", "berryDocsList");
274 ul.setAttribute("style","list-style: none;");
275
276 for (var i in docList){
277 var doc = docList[i];
278 var li = document.createElement('li');
279 var img = document.createElement('img');
280 var text ="";
281
282 var doc_id = doc['collection']+":"+ doc['name'];
283
284 img.setAttribute("src", gs.variables.selected_favourite_icon_url);
285 img.setAttribute("id", doc_id);
286 img.setAttribute("height", "20px");
287 img.setAttribute("width", "20px");
288 img.setAttribute("style","padding-right: 5px;"); // **** better to do this with CSS
289 img.setAttribute("onClick", "deleteFavouriteFromCheckout(this)");
290 li.appendChild(img);
291
292 generateDocDisplay(li, doc, doc_id);
293 plain_text += generateMailEntry(doc, doc_id)+"%0D%0A%0D%0A";
294 li.className = 'berrydoc';
295 ul.appendChild(li);
296 }
297
298 var mailto = "mailto:?Subject=Favourites&body="+plain_text;
299 share_anchor.setAttribute("href", mailto);
300}
301
302function generateMailEntry(doc) {
303
304 var url=generateURL(doc);
305 var title = doc['Title'];
306 return "Title: "+title+"%0D%0AURL:"+url;
307
308}
309function generateDocDisplay(li, doc, doc_id) {
310 var a = document.createElement('a');
311 var text="";
312 a.href=generateURL(doc);
313 a.appendChild(document.createTextNode(doc['Title']));
314
315 if (doc['root_Title']){
316 li.appendChild(document.createTextNode(doc['root_Title']+": "));
317 }
318
319 li.appendChild(a);
320 li.appendChild(document.createTextNode(" ("+doc['collection']+")"));
321 var metadata = "";
322 for (var metaItem in doc) {
323 if ( !default_metas.includes(metaItem)){
324 metadata += " "+metaItem+": "+ doc[metaItem]+" ";
325 }
326 }
327 text +=metadata;
328 li.appendChild(document.createTextNode(text));
329
330}
331
332function showTextView(){
333
334 var content = YAHOO.util.Dom.get('berryBasketContent');
335 var textview = YAHOO.util.Dom.get('textview');
336
337 berryCheckoutPageClear();
338 if (docList.length == 0){
339 content.appendChild(emptyBasketText());
340 return;
341 }
342 buildPreview(content);
343
344}
345
346function getEmailContent(content){
347 var item ;
348 var tr;
349 var td;
350 var input;
351
352 table = document.createElement('table');
353 table.setAttribute("class","mailtable");
354
355 for (item in mailinfo){
356 tr = document.createElement('tr');
357 td = document.createElement('td');
358 td.setAttribute("class","mailitem");
359 td.appendChild(document.createTextNode(mailinfo[item]));
360 tr.appendChild(td);
361 td = document.createElement('td');
362 input = document.createElement('input');
363 input.setAttribute("id", item);
364 input.setAttribute("class", "mailinput");
365 if(item === "address") {
366 input.setAttribute("type", "email"); // https://html5-tutorial.net/form-validation/validating-email/
367 input.required = true; // https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
368 } else {
369 input.setAttribute("type", "text");
370 }
371 td.appendChild(input);
372 tr.appendChild(td);
373 table.appendChild(tr);
374 }
375
376 // an empty line
377 tr = document.createElement('tr');
378 td = document.createElement('td');
379 td.appendChild(document.createElement('br'));
380 tr.appendChild(td);
381 table.appendChild(tr);
382
383 content.appendChild(table);
384
385 buildPreview(content);
386
387 //send button
388 input = document.createElement('input');
389 input.setAttribute("id", 'sendmail');
390 input.setAttribute("class", "sendbutton");
391 input.setAttribute("type", "button");
392 input.setAttribute("value", gs.text.favourites.send);
393 content.appendChild(input);
394}
395
396function showEmail(){
397 var content = YAHOO.util.Dom.get('berryBasketContent');
398 var email = YAHOO.util.Dom.get('email');
399
400 berryCheckoutPageClear();
401
402 if (docList.length == 0){
403 content.appendChild(emptyBasketText());
404 return;
405 }
406
407 var item;
408 var tr;
409 var td;
410 var input;
411
412 table = document.createElement('table');
413 table.setAttribute("class","mailtable");
414
415 for (item in mailinfo){
416 tr = document.createElement('tr');
417 td = document.createElement('td');
418 td.setAttribute("class","mailitem");
419 td.appendChild(document.createTextNode(mailinfo[item]));
420 tr.appendChild(td);
421
422 td = document.createElement('td');
423 input = document.createElement('input');
424 input.setAttribute("id", item);
425 input.setAttribute("class", "mailinput");
426 if(item === "address") {
427 input.setAttribute("type", "email"); // https://html5-tutorial.net/form-validation/validating-email/
428 input.required = true; // https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
429 } else {
430 input.setAttribute("type", "text");
431 }
432 td.appendChild(input);
433 tr.appendChild(td);
434 table.appendChild(tr);
435
436 }
437
438 // an empty line
439 tr = document.createElement('tr');
440 td = document.createElement('td');
441 td.appendChild(document.createElement('br'));
442 tr.appendChild(td);
443 table.appendChild(tr);
444
445 content.appendChild(table);
446
447 buildPreview(content);
448
449 //send button
450 input = document.createElement('input');
451 input.setAttribute("id", 'sendmail');
452 input.setAttribute("class", "sendbutton");
453 input.setAttribute("type", "button");
454 input.setAttribute("value", gs.text.favourites.send);
455 content.appendChild(input);
456
457 YAHOO.util.Event.addListener(input, 'click', navigate);
458}
459
460function buildPreview(parent){
461
462 var div = document.createElement('div');
463 var cb = document.createElement('input');
464 cb.setAttribute('class', 'sendbutton');
465 cb.type = 'button';
466 cb.id = 'urlcheck';
467 if (urlonly)
468 {
469 cb.value=gs.text.favourites.url_and_metadata;
470 }
471 else
472 {
473 cb.value=gs.text.favourites.url_only;
474 }
475
476 YAHOO.util.Event.addListener(cb, 'click', navigate);
477
478 var img = document.createElement('img');
479 img.src = 'interfaces/default/images/check3.gif';
480 img.id = 'urlcheck';
481 div.appendChild(cb);
482 //div.appendChild(img);
483
484 var urls = document.createElement('span');
485 urls.id = 'urls';
486 urls.className = 'berrycheck';
487 //urls.appendChild(document.createTextNode('URL only'));
488 div.appendChild(urls);
489
490 // var urlsmetadata = document.createElement('span');
491 // urlsmetadata.id = 'urlsmetadata'
492 // urlsmetadata.className = 'berryradio';
493 // urlsmetadata.appendChild(document.createTextNode('URLs and Metadata'));
494 // div.appendChild(urlsmetadata);
495
496 parent.appendChild(div);
497
498 var parea = document.createElement('textarea');
499 parea.id = 'pretextarea';
500 parea.required = true; // https://www.w3schools.com/tags/att_textarea_required.asp
501 // and https://stackoverflow.com/questions/18770369/how-to-set-html5-required-attribute-in-javascript
502
503 parent.appendChild(parea);
504
505 if(urlonly)
506 {
507 populateUrls(parea);
508 }
509 else
510 {
511 populateUrlsAndMetadata(parea);
512 }
513}
514
515function getDefaultLinkType(collection) {
516 var link_type;
517 if (document_link_collections.includes(collection)) {
518 link_type = "document";
519 } else if (source_link_collections.includes(collection)) {
520 link_type = "source";
521 }
522 else {
523 link_type = default_link_type;
524 if (link_type != "source" && link_type != "document") {
525 link_type = "document"; //the default default
526 }
527 }
528 return link_type;
529}
530
531function generateURL(doc) {
532
533 var url;
534 var doc_url = document.URL;
535 var root_url = doc_url.substring(0,doc_url.indexOf('?'));
536
537 var link_type = getDefaultLinkType(doc["collection"]);
538 if (link_type == "document") {
539 url = root_url+"/collection/"+doc["collection"]+"/document/"+doc["nodeID"];
540 } else if (link_type == "source") {
541 url = root_url+"/sites/"+gs.xsltParams.site_name+"/collect/"+doc['collection']+"/index/assoc/"+doc["root_assocfilepath"]+"/"+doc["root_srclinkFile"];
542 }
543 return url;
544}
545
546
547function populateUrls(parea){
548
549 var urls="";
550 for (var i in docList){
551 var doc = docList[i];
552 urls += generateURL(doc)+"\n\n";
553 }
554
555 parea.value = urls;
556
557}
558
559function populateUrlsAndMetadata(parea){
560
561 var fulltext="";
562 for (var i in docList){
563 var doc = docList[i];
564 var url = generateURL(doc)+"\n";
565
566 var metadata = "";
567 if (doc['Title']) {
568 metadata += gs.text.favourites.doc_title+": "+doc['Title']+"\n";
569 }
570 if (doc['root_Title']) {
571 metadata += gs.text.favourites.doc_root_title+": "+doc['root_Title']+"\n";
572
573 }
574 if (doc['name']) {
575 metadata += gs.text.favourites.doc_name+": "+doc['name']+"\n";
576 }
577 if (doc['collection']) {
578 metadata += gs.text.favourites.doc_collection+": "+doc['collection']+"\n";
579 }
580 if (doc['Date']) {
581 metadata += gs.text.favourites.doc_date+": "+doc['Date']+"\n";
582 }
583 // allow for inclusion of custom metadata
584 for (var m in doc) {
585 if (!default_metas.includes(m)) {
586 metadata += m +":" + doc[m]+"\n";
587 }
588 }
589 fulltext +=url+metadata+"\n";
590 }
591
592 parea.value = fulltext;
593
594}
595
596function sendMail(){
597 var url = gs.xsltParams.library_name + "?a=pr&rt=r&ro=1&s=SendFavouritesMail&c=";
598 var request_type = "POST";
599 var postdata = "";
600 var i;
601
602 var content = YAHOO.util.Dom.get('pretextarea').value;
603
604 // To send an email, the To address and message Body must contain data.
605 // HTML5 input checking (required attribute) would make empty fields red outlined,
606 // but did not prevent Send button submitting form. So some basic sanity checking in JS:
607 // Checking non-empty and to address field must further be a URL: checking it contains @
608 var to_address = YAHOO.util.Dom.get('address').value;
609
610 if(to_address.trim() === "") {
611 alert(gs.text.favourites.invalid_to_address_empty);
612 return;
613 } else if(to_address.indexOf('@') === -1) {
614 alert(gs.text.favourites.invalid_to_address);
615 return;
616 } else if(content.trim() === "") {
617 alert(gs.text.favourites.invalid_msg_body_empty);
618 return;
619 }
620
621 //get checked items
622 for (i in mailinfo) {
623 var input = YAHOO.util.Dom.get(i);
624 var value = input.value;
625 postdata +="&s1."+i+"="+value;
626 }
627
628
629 content = content.replace(/&/g,'-------');
630 postdata +="&s1.content="+content;
631
632 var callback = {
633 success: function(o) {
634 var result = o.responseText;
635 alert(gs.text.favourites.send_success);
636 } ,
637 failure: function(o) {
638 alert(gs.text.favourites.send_fail);
639 }
640 }
641 YAHOO.util.Connect.asyncRequest(request_type , url , callback, postdata);
642}
643
644function berryCheckoutPageClear() {
645 var bbc = document.getElementById('berryBasketContent');
646 if ( bbc == null ) return;
647 bbc.innerHTML = '';
648}
649
650function berryCheckoutHighlight( id ) {
651
652 for ( var i=0; i<options.length; i++ ) {
653 var option = document.getElementById( options[i] );
654 if ( option != null ) {
655 if ( id == options[i] ) {
656 //YAHOO.util.Dom.addClass( option, 'current' );
657 option.className='current';
658 } else {
659 //YAHOO.util.Dom.removeClass( option, 'current' );
660 option.className='';
661 }
662 }
663 }
664
665 if ( option == null ) return;
666 option.style.className = 'current';
667
668 var del_options = document.getElementById("delOptions");
669 if (id == "fullview") {
670 del_options.style.display = "block";
671 } else {
672 del_options.style.display = "none";
673 }
674}
675
676YAHOO.util.Event.addListener(window,'load', pageLoad);
677
678
Note: See TracBrowser for help on using the repository browser.