1 /* global __ getContextBiblioNumbers */
2 /* exported readCookie readCookieValue SelectAll addMultiple selRecord delSingleRecord delBasket quit sendBasket downloadBasket printBasket showMore showLess openBiblio addSelToShelf vShelfAdd */
4 //////////////////////////////////////////////////////////////////////////////
5 // BASIC FUNCTIONS FOR COOKIE MANAGEMENT //
6 //////////////////////////////////////////////////////////////////////////////
8 var CGIBIN = "/cgi-bin/koha/";
10 var nameCookie = "intranet_bib_list";
11 var nameParam = "bib_list";
12 var valCookie = readCookie(nameCookie);
16 var arrayRecords = valCookie.split("/");
17 if(arrayRecords.length > 0){
18 basketcount = arrayRecords.length-1;
26 function writeCookie(name, val, wd) {
28 parent.opener.document.cookie = name + "=" + val + "; SameSite=Lax; path=/";
31 parent.document.cookie = name + "=" + val + "; SameSite=Lax; path=/";
35 function readCookieValue (str, val_beg) {
36 var val_end = str.indexOf(";", val_end);
39 return str.substring(val_beg, val_end);
42 function readCookie(name, wd) {
43 var str_name = name + "=";
46 str_cookie = parent.opener.document.cookie;
49 str_cookie = parent.document.cookie;
51 // fixed - getting the part of the basket that is bib_list
52 var cookie_parts = str_cookie.split(";");
53 for(var i=0;i < cookie_parts.length;i++) {
54 var c = cookie_parts[i];
55 while (c.charAt(0)==' ') c = c.substring(1,c.length);
56 if(c.indexOf(str_name) === 0) return c.substring(str_name.length,c.length);
61 function delCookie(name) {
63 exp.setTime(exp.getTime()-1);
65 parent.opener.document.cookie = name + "=null; SameSite=Lax; path=/; expires=" + exp.toGMTString();
67 document.cookie = name + "=null; SameSite=Lax; path=/; expires=" + exp.toGMTString();
71 ///////////////////////////////////////////////////////////////////
72 // SPECIFIC FUNCTIONS USING COOKIES //
73 ///////////////////////////////////////////////////////////////////
75 function openBasket() {
77 var valCookie = readCookie(nameCookie);
79 strCookie = nameParam + "=" + valCookie;
85 var optWin = "status=yes,scrollbars=yes,resizable=yes,toolbar=no,location=yes,height="+iH+",width="+iW;
86 var loc = CGIBIN + "basket/basket.pl?" + strCookie;
87 var basket = open(loc, "basket", optWin);
88 if (window.focus) { basket.focus(); }
91 showCartUpdate( __("Your cart is currently empty") );
95 function addRecord(val, selection,NoMsgAlert) {
96 var valCookie = readCookie(nameCookie);
99 if ( ! valCookie ) { // empty basket
100 valCookie = val + '/';
105 // is this record already in the basket ?
107 var arrayRecords = valCookie.split("/");
108 for (var i = 0; i < valCookie.length - 1; i++) {
109 if (val == arrayRecords[i]) {
119 showCartUpdate( __("This item is already in your cart") );
123 valCookie += val + '/';
125 updateBasket(arrayRecords.length);
130 writeCookie(nameCookie, valCookie);
131 if (selection) { // when adding a selection of records
132 updateLink(val,"add");
136 showCartUpdate( __("This item has been added to your cart") );
137 updateLink(val,"add");
142 function AllAreChecked(s){
143 if (! s.length){ return false;}
145 for (var i=0; i < l; i++) {
146 if(! s[i].checked) { return false; }
151 function SelectAll(){
152 if(document.bookbag_form.biblionumber.length > 0) {
153 var checky = AllAreChecked(document.bookbag_form.biblionumber);
154 var l = document.bookbag_form.biblionumber.length;
155 for (var i=0; i < l; i++) {
156 document.bookbag_form.biblionumber[i].checked = (checky) ? false : true;
161 function addMultiple(biblist){
164 if( biblist && biblist.length > 0 ) {
165 for ( i=0; i < biblist.length; i++ ) {
166 if (biblist[i].checked) {
167 c_value = c_value + biblist[i].value + "/";
171 var bibnums = getContextBiblioNumbers();
172 if ( bibnums.length > 0 ) {
173 for ( i = 0 ; i < bibnums.length ; i++ ) {
174 c_value = c_value + bibnums[i] + "/";
177 if(document.bookbag_form.biblionumber.length > 0) {
178 for ( i=0; i < document.bookbag_form.biblionumber.length; i++ ) {
179 if (document.bookbag_form.biblionumber[i].checked) {
180 c_value = c_value + document.bookbag_form.biblionumber[i].value + "/";
184 c_value = c_value + document.bookbag_form.biblionumber.value + "/";
188 addSelRecords(c_value);
191 /* function for adding a selection of biblios to the basket
192 from the results list */
193 function addSelRecords(valSel) {
194 var arrayRecords = valSel.split("/");
197 for (i=0;i<arrayRecords.length;i++) {
198 if (arrayRecords[i]) {
199 nbAdd += addRecord(arrayRecords[i], 1);
208 msg = __("%s item(s) added to your cart").format(nbAdd);
210 msg += __("%s already in your cart").format((i-nbAdd));
213 msg = __("%s item(s) added to your cart").format(nbAdd);
218 msg = __("No item was selected");
221 msg = __("No item was added to your cart (already in your cart)!");
227 function showCartUpdate(msg){
228 // set body of popup window
229 $("#cartDetails").html(msg);
231 setTimeout(hideCart,2000);
234 function showListsUpdate(msg){
235 // set body of popup window
239 function selRecord(num, status) {
240 var str = document.myform.records.value;
245 str = delRecord(num, str);
248 document.myform.records.value = str;
251 function delSingleRecord(biblionumber){
252 var valCookie = readCookie(nameCookie);
253 var arrayRecords = valCookie.split("/");
254 var pos = jQuery.inArray(biblionumber,arrayRecords);
255 arrayRecords.splice(pos,1);
256 valCookie = arrayRecords.join("/");
257 writeCookie( nameCookie, valCookie );
258 updateBasket( arrayRecords.length-1 );
259 updateLink(biblionumber,"del");
260 showCartUpdate(__("The item has been removed from your cart"));
263 function delSelRecords() {
266 var valCookie = readCookie(nameCookie, 1);
269 var str = document.myform.records.value;
272 var str2 = valCookie;
274 s = str.indexOf("/");
276 var num = str.substring(0, s);
277 str = delRecord(num,str);
278 str2 = delRecord(num,str2);
279 updateLink(num,"del",top.opener);
285 if (str2.length === 0) { // equivalent to emptying the basket
287 rep = confirm(__("Are you sure you want to empty your cart?"));
289 delCookie(nameCookie);
290 document.location = "about:blank";
291 updateBasket(0,top.opener);
297 writeCookie(nameCookie, str2, 1);
304 valCookie = readCookie(nameCookie, 1);
305 strCookie = nameParam + "=" + valCookie;
306 var arrayRecords = valCookie.split("/");
307 updateBasket(arrayRecords.length-1,top.opener);
308 document.location = CGIBIN + "basket/basket.pl?" + strCookie;
311 alert(__("No item was selected"));
315 function delRecord (n, s) {
322 pos = aux.indexOf(n, pos+1);
323 var charAfter = aux.charAt(pos+n.length); // character right after the researched string
324 if (charAfter.match(re)) { // record number inside another one
327 else { // good record number
328 aux = s.substring(0, pos)+ s.substring(pos+n.length+1, s.length);
337 function delBasket(context,rep) {
338 if (rep === undefined){
339 rep = confirm(__("Are you sure you want to empty your cart?"));
342 if(context == "popup"){
343 delCookie(nameCookie);
344 updateAllLinks(top.opener);
345 document.location = "about:blank";
346 updateBasket(0,top.opener);
349 delCookie(nameCookie);
350 updateBasket(0,top.opener);
356 if (document.myform.records.value) {
358 rep = confirm(__("Are you sure you want to remove the selected items?"));
363 updateBasket(arrayRecords.length-1,top.opener);
367 function sendBasket() {
368 var valCookie = readCookie(nameCookie);
369 var strCookie = nameParam + "=" + valCookie;
371 var loc = CGIBIN + "basket/sendbasket.pl?" + strCookie;
373 var optWin="scrollbars=no,resizable=no,height=400,width=650,top=50,left=100";
374 open(loc,"win_form",optWin);
377 function downloadBasket() {
378 var valCookie = readCookie(nameCookie);
379 var strCookie = nameParam + "=" + valCookie;
381 var loc = CGIBIN + "basket/downloadcart.pl?" + strCookie;
383 open(loc,"win_form",'scrollbars=no,resizable=no,height=300,width=450,top=50,left=100');
386 function printBasket() {
390 function showMore() {
393 var valCookie = readCookie(nameCookie);
395 strCookie = nameParam + "=" + valCookie;
397 var loc = CGIBIN + "basket/basket.pl?" + strCookie + "&verbose=1";
398 document.location = loc;
401 function showLess() {
404 var valCookie = readCookie(nameCookie);
406 strCookie = nameParam + "=" + valCookie;
408 var loc = CGIBIN + "basket/basket.pl?" + strCookie + "&verbose=0";
409 document.location = loc;
412 function updateBasket(updated_value,target) {
414 target.$('#basketcount').html(" <span>("+updated_value+")</span>");
415 target.$('#cartDetails').html(__("Items in your cart: %s").format(updated_value));
417 $('#basketcount').html(" <span>("+updated_value+")</span>");
418 $('#cartDetails').html(__("Items in your cart: %s").format(updated_value));
422 function openBiblio(openerURL) {
423 opener.document.location = openerURL;
427 function addSelToShelf() {
428 var items = document.getElementById('records').value;
430 document.location = "/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?biblionumber="+items;
432 alert(__("No item was selected"));
436 /// vShelfAdd() builds url string for multiple-biblio adds.
438 function vShelfAdd(biblist) {
439 var bibs = new Array;
441 if( biblist && biblist.length > 0 ) {
442 for ( i=0; i < biblist.length; i++ ) {
443 if (biblist[i].checked) {
444 bibs.push("biblionumber=" + biblist[i].value);
447 if (bibs.length === 0) { showListsUpdate(__("No item was selected")); }
448 return bibs.join("&");
450 var bibnums = getContextBiblioNumbers();
451 if ( bibnums.length > 0 ) {
452 for ( i = 0 ; i < bibnums.length ; i++ ) {
453 bibs.push("biblionumber=" + bibnums[i]);
455 return bibs.join("&");
457 showListsUpdate(__("No item was selected"));
463 var position = $("#cartmenulink").offset();
464 var toolbarh = $(".floating").outerHeight();
465 var scrolld = $(window).scrollTop();
466 var top = position.top + $("#cartmenulink").outerHeight();
468 top = scrolld + toolbarh + 15;
470 var left = position.left;
472 .css("position","absolute")
475 .css("z-index",1000);
476 $("#cartDetails").fadeIn("fast");
480 $("#cartDetails").fadeOut("fast");
483 function updateLink(val, op, target){
484 var cart = target ? target.$("#cart" + val) : $("#cart" + val);
485 var cartR = target ? target.$("#cartR" + val) : $("#cartR" + val);
488 if( cart.hasClass("btn") ){
489 /* Cart link is a button with an icon */
490 cart.html("<i class=\"fa fa-shopping-cart\"></i> " + __("Add to cart")).addClass("incart");
493 cart.html( __("In your cart") ).addClass("incart");
497 if (cart.hasClass("btn")) {
498 cart.html("<i class=\"fa fa-shopping-cart\"></i> " + __("Add to cart")).addClass("addtocart");
501 cart.html(__("Add to cart")).addClass("addtocart");
507 function updateAllLinks(target){
508 var cart = target ? target.$("a.incart") : $("a.incart");
509 var cartR = target ? target.$(".cartRemove") : $(".cartRemove");
510 if( cart.hasClass("btn") ){
511 /* Cart link is a button with an icon */
512 cart.html("<i class=\"fa fa-shopping-cart\"></i> " + __("Add to cart")).addClass("incart");
515 cart.html( __("In your cart") ).addClass("incart");
520 $(document).ready(function(){
521 $("#cartmenulink").click(function( e ){
525 if(basketcount){ updateBasket(basketcount); }