Bug 32302: Hide "ISBN" label when no ISBN data when sending list
[koha.git] / koha-tmpl / opac-tmpl / bootstrap / js / basket.js
1 /* global __ __p */
2 /* exported addMultiple delSingleRecord vShelfAdd openBiblio addSelToShelf delBasket sendBasket showMore showLess delSelRecords holdSel selRecord */
3 //////////////////////////////////////////////////////////////////////////////
4 // BASIC FUNCTIONS FOR COOKIE MANAGEMENT //
5 //////////////////////////////////////////////////////////////////////////////
6
7 function basketCount(){
8     var valCookie = readCookie("bib_list");
9     var basketcount = 0;
10     if(valCookie){
11         var arrayRecords = valCookie.split("/");
12         if(arrayRecords.length > 0){
13             basketcount = arrayRecords.length-1;
14         } else {
15             basketcount = 0;
16         }
17     } else {
18         basketcount = 0;
19     }
20     return basketcount;
21 }
22
23 function writeCookie(name, val, wd) {
24     if (wd) {
25         parent.opener.document.cookie = name + "=" + val + ";path=/";
26     }
27     else {
28         parent.document.cookie = name + "=" + val + ";path=/";
29     }
30 }
31
32 function readCookie(name, wd) {
33     var str_name = name + "=";
34     var str_cookie = "";
35     if (wd) {
36         str_cookie = parent.opener.document.cookie;
37     }
38     else {
39         str_cookie = parent.document.cookie;
40     }
41     // fixed - getting the part of the basket that is bib_list
42     var cookie_parts = str_cookie.split(";");
43     for(var i=0;i < cookie_parts.length;i++) {
44         var c = cookie_parts[i];
45         while (c.charAt(0)==' ') c = c.substring(1,c.length);
46         if(c.indexOf(str_name) == 0) return c.substring(str_name.length,c.length);
47     }
48     return null;
49 }
50
51 function delCookie(name) {
52     var exp = new Date();
53     exp.setTime(exp.getTime()-1);
54     if(parent.opener){
55         parent.opener.document.cookie = name + "=null; path=/; expires=" + exp.toGMTString();
56     } else {
57         document.cookie = name + "=null; path=/; expires=" + exp.toGMTString();
58     }
59 }
60
61 ///////////////////////////////////////////////////////////////////
62 // SPECIFIC FUNCTIONS USING COOKIES //
63 ///////////////////////////////////////////////////////////////////
64
65 function openBasket() {
66     var strCookie = "";
67     var nameCookie = "bib_list";
68     var valCookie = readCookie(nameCookie);
69     if ( valCookie ) {
70         strCookie = nameCookie + "=" + valCookie;
71     }
72
73     if ( strCookie ) {
74         var iW = 820;
75         var iH = 450;
76         var optWin = "status=yes,scrollbars=yes,resizable=yes,toolbar=no,location=yes,height="+iH+",width="+iW;
77         var loc = "/cgi-bin/koha/opac-basket.pl?" + strCookie;
78         var basket = open(loc, "basket", optWin);
79         if (window.focus) { basket.focus(); }
80     }
81     else {
82         showCartUpdate( __("Your cart is currently empty") );
83     }
84 }
85
86 function addRecord(val, selection,NoMsgAlert) {
87     var nameCookie = "bib_list";
88     var valCookie = readCookie(nameCookie);
89     var write = 0;
90
91     if ( ! valCookie ) { // empty basket
92         valCookie = val + '/';
93         write = 1;
94         updateBasket(1);
95     }
96     else {
97         // is this record already in the basket ?
98         var found = false;
99         var arrayRecords = valCookie.split("/");
100         for (var i = 0; i < valCookie.length - 1; i++) {
101             if (val == arrayRecords[i]) {
102                 found = true;
103                 break;
104             }
105         }
106         if ( found ) {
107             if (selection) {
108                 return 0;
109             }
110             if (! NoMsgAlert ) {
111                 showCartUpdate( __p("Bibliographic record", "The item is already in your cart") );
112             }
113         }
114         else {
115             valCookie += val + '/';
116             write = 1;
117             updateBasket(arrayRecords.length);
118         }
119     }
120
121     if (write) {
122         writeCookie(nameCookie, valCookie);
123         if (selection) { // when adding a selection of records
124             updateLink(val,"add");
125             return 1;
126         }
127         if (! NoMsgAlert ) {
128             showCartUpdate( __p("Bibliographic record", "The item has been added to your cart") );
129             updateLink(val,"add");
130         }
131
132     }
133 }
134
135 function addMultiple(){
136     var c_value = "";
137     if(document.bookbag_form.biblionumber.length > 0) {
138         for (var i=0; i < document.bookbag_form.biblionumber.length; i++) {
139             if (document.bookbag_form.biblionumber[i].checked) {
140                 c_value = c_value + document.bookbag_form.biblionumber[i].value + "/";
141             }
142         }
143         addSelRecords(c_value);
144     } else {
145         c_value = c_value + document.bookbag_form.biblionumber.value + "/";
146         addSelRecords(c_value);
147     }
148 }
149
150 /* function for adding a selection of biblios to the basket from the results list */
151 function addSelRecords(valSel) {
152     var arrayRecords = valSel.split("/");
153     var i = 0;
154     var nbAdd = 0;
155     for (i=0;i<arrayRecords.length;i++) {
156         if (arrayRecords[i]) {
157             nbAdd += addRecord(arrayRecords[i], 1);
158         }
159         else {
160             break;
161         }
162     }
163     var msg = "";
164     if (nbAdd) {
165         if (i > nbAdd) {
166             msg = nbAdd+" "+ __p("Bibliographic record", " item(s) added to your cart") +", "+(i-nbAdd)+" " + __("already in your cart");
167         }
168         else {
169             msg = nbAdd+" "+ __p("Bibliographic record", " item(s) added to your cart");
170         }
171     }
172     else {
173         if (i < 1) {
174             msg = __p("Bibliographic record", "No item was selected");
175         }
176         else {
177             msg = __p("Bibliographic record", "No item was added to your cart") + " (" + __("already in your cart") + ") !";
178         }
179     }
180     showCartUpdate(msg);
181 }
182
183 function showCartUpdate(msg){
184     // set body of popup window
185     $("#cartDetails").html(msg);
186     showCart();
187     setTimeout("hideCart()",2000);
188 }
189
190 function showListsUpdate(msg){
191     // set body of popup window
192     alert(msg);
193 }
194
195 function selRecord(num, status) {
196     var str = document.myform.records.value;
197     if (status){
198         str += num+"/";
199     }
200     else {
201         str = delRecord(num, str);
202     }
203
204     document.myform.records.value = str;
205 }
206
207 function delSingleRecord(biblionumber){
208     biblionumber = String( biblionumber );
209     var nameCookie = "bib_list";
210     var valCookie = readCookie(nameCookie);
211     var arrayRecords = valCookie.split("/");
212     var pos = jQuery.inArray(biblionumber,arrayRecords);
213     arrayRecords.splice( pos, 1 );
214     valCookie = arrayRecords.join("/");
215     writeCookie( nameCookie, valCookie );
216     updateBasket( arrayRecords.length-1 );
217     updateLink(biblionumber,"del");
218     showCartUpdate( __p("Bibliographic record", "The item has been removed from your cart") );
219 }
220
221 function delSelRecords() {
222     var recordsSel = 0;
223     var end = 0;
224     var nameCookie = "bib_list";
225     var valCookie = readCookie(nameCookie, 1);
226     if (valCookie) {
227         var str = document.myform.records.value;
228         if (str.length > 0){
229             recordsSel = 1;
230             var str2 = valCookie;
231             while (!end){
232                 var s = str.indexOf("/");
233                 if (s>0){
234                     var num = str.substring(0, s);
235                     str = delRecord(num,str);
236                     str2 = delRecord(num,str2);
237                     updateLink(num,"del",top.opener);
238                 } else {
239                     end = 1;
240                 }
241             }
242
243             if (str2.length == 0) { // equivalent to emptying the basket
244                 var rep = false;
245                 rep = confirm( __("Are you sure you want to empty your cart?") );
246                 if (rep) {
247                     delCookie(nameCookie);
248                     document.location = "about:blank";
249                     updateBasket(0,top.opener);
250                     window.close();
251                 } else {
252                     return;
253                 }
254             } else {
255                 writeCookie(nameCookie, str2, 1);
256             }
257         }
258     }
259
260     if (recordsSel) {
261         var strCookie = "";
262         valCookie = readCookie(nameCookie, 1);
263         strCookie = nameCookie + "=" + valCookie;
264         var arrayRecords = valCookie.split("/");
265         updateBasket(arrayRecords.length-1,top.opener);
266         document.location = "/cgi-bin/koha/opac-basket.pl?" + strCookie;
267     }
268     else {
269         alert( __p("Bibliographic record", "No item was selected") );
270     }
271 }
272
273 function delRecord (n, s) {
274     var re = /\d/;
275     var aux = s;
276     var found = 0;
277     var pos = -1;
278
279     while (!found) {
280         pos = aux.indexOf(n, pos+1);
281         var charAfter = aux.charAt(pos+n.length); // character right after the researched string
282         if (charAfter.match(re)) { // record number inside another one
283             continue;
284         }
285         else { // good record number
286             aux = s.substring(0, pos)+ s.substring(pos+n.length+1, s.length);
287             s = aux;
288             found = 1;
289         }
290     }
291
292     return s;
293 }
294
295
296 function delBasket() {
297     var nameCookie = "bib_list";
298
299     var rep = false;
300     rep = confirm( __("Are you sure you want to empty your cart?") );
301     if (rep) {
302         delCookie(nameCookie);
303         updateAllLinks(top.opener);
304         document.location = "about:blank";
305         updateBasket(0,top.opener);
306         window.close();
307     }
308 }
309
310 function sendBasket() {
311     var nameCookie = "bib_list";
312     var valCookie = readCookie(nameCookie);
313     var strCookie = nameCookie + "=" + valCookie;
314
315     var loc = "/cgi-bin/koha/opac-sendbasket.pl?" + strCookie;
316
317     var optWin="scrollbars=yes,resizable=yes,height=600,width=900,top=50,left=100";
318     open(loc,"win_form",optWin);
319 }
320
321 function showMore() {
322     var strCookie = "";
323
324     var nameCookie = "bib_list";
325     var valCookie = readCookie(nameCookie);
326     if (valCookie) {
327         strCookie = nameCookie + "=" + valCookie;
328     }
329     var loc = "/cgi-bin/koha/opac-basket.pl?" + strCookie + "&verbose=1";
330     document.location = loc;
331 }
332
333 function showLess() {
334     var strCookie = "";
335
336     var nameCookie = "bib_list";
337     var valCookie = readCookie(nameCookie);
338     if (valCookie) {
339         strCookie = nameCookie + "=" + valCookie;
340     }
341     var loc = "/cgi-bin/koha/opac-basket.pl?" + strCookie + "&verbose=0";
342     document.location = loc;
343 }
344
345 function holdSel() {
346     var items = document.getElementById('records').value;
347     if (items) {
348         parent.opener.document.location = "/cgi-bin/koha/opac-reserve.pl?biblionumbers=" + items;
349         window.close();
350     } else {
351         alert( __p("Bibliographic record", "No item was selected") );
352     }
353 }
354
355 function updateBasket(updated_value,target) {
356     var bcount = "";
357     if(updated_value > 0){
358         bcount = "<span>"+updated_value+"</span>";
359     }
360     if(target){
361         target.$('#basketcount').html(bcount);
362     } else {
363         $('#basketcount').html(bcount);
364     }
365 }
366
367 function openBiblio(dest,biblionumber) {
368     var openerURL = dest + "?biblionumber=" + biblionumber;
369     opener.document.location = openerURL;
370     opener.focus();
371 }
372
373 function addSelToShelf() {
374     var items = document.getElementById('records').value;
375     if(items){
376         var iW = 820;
377         var iH = 450;
378         var optWin = "status=yes,scrollbars=yes,resizable=yes,toolbar=no,location=yes,height="+iH+",width="+iW;
379         var loc = "/cgi-bin/koha/opac-addbybiblionumber.pl?biblionumber="+items;
380         open(loc, "shelf", optWin);
381     } else {
382         alert( __p("Bibliographic record", "No item was selected") );
383     }
384 }
385
386 ///  vShelfAdd()  builds url string for multiple-biblio adds.
387
388 function vShelfAdd() {
389     var bibs = new Array;
390     if(document.bookbag_form.biblionumber.length > 0) {
391         for (var i=0; i < document.bookbag_form.biblionumber.length; i++) {
392             if (document.bookbag_form.biblionumber[i].checked) {
393                 bibs.push("biblionumber=" +  document.bookbag_form.biblionumber[i].value);
394             }
395         }
396         if (bibs.length == 0) {
397             showListsUpdate( __p("Bibliographic record", "No item was selected") );
398         }
399         return bibs.join("&");
400     } else {
401         if (document.bookbag_form.biblionumber.checked) {
402             return "biblionumber=" + document.bookbag_form.biblionumber.value;
403         }
404     }
405 }
406
407 function showCart(){
408     $("#cartDetails").fadeIn("fast");
409 }
410
411 function hideCart(){
412     $("#cartDetails").fadeOut("fast");
413 }
414
415 function updateLink(val,op,target){
416     if(target){
417         if(op == "add"){
418             target.$("a.cart"+val).html( "<i class=\"fa fa-fw fa-shopping-cart\"></i> " + __("In your cart") ).addClass("incart");
419             target.$("a.cartR"+val).show();
420         } else {
421             target.$("a.cart"+val).html( "<i class=\"fa fa-fw fa-shopping-cart\"></i> " + __("Add to cart") ).removeClass("incart").addClass("addtocart cart"+val);
422             target.$("a.cartR"+val).hide();
423         }
424     } else {
425         if(op == "add"){
426             $("a.cart"+val).html( "<i class=\"fa fa-fw fa-shopping-cart\"></i> " + __("In your cart") ).addClass("incart");
427             $("a.cartR"+val).show();
428         } else {
429             $("a.cart"+val).html( "<i class=\"fa fa-fw fa-shopping-cart\"></i> " + __("Add to cart") ).removeClass("incart").addClass("addtocart cart"+val);
430             $("a.cartR"+val).hide();
431         }
432     }
433 }
434
435 function updateAllLinks(target){
436     if(target){
437         target.$("a.incart").html( "<i class=\"fa fa-fw fa-shopping-cart\"></i> " + __("Add to cart") ).removeClass("incart").addClass("addtocart");
438         target.$("a.cartRemove").hide();
439     } else {
440         $("a.incart").html( "<i class=\"fa fa-fw fa-shopping-cart\"></i> " + __("Add to cart") ).removeClass("incart").addClass("addtocart");
441         $("a.cartRemove").hide();
442     }
443 }
444
445 $("#cartDetails").ready(function(){
446     $("#cartDetails,#cartmenulink").on("click",function(){ hideCart(); });
447     $("#cartmenulink").click(function(e){
448         e.preventDefault();
449         openBasket();
450         $("li").closest().removeClass("open");
451     });
452     updateBasket(basketCount());
453 });