Bug 29933: Fix stray usage of jquery.cookie.js plugin
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / pages / results.js
1 /* global KOHA biblionumber new_results_browser addMultiple vShelfAdd openWindow search_result SEARCH_RESULTS PREF_LocalCoverImages PREF_IntranetCoce PREF_CoceProviders CoceHost CoceProviders addRecord delSingleRecord PREF_BrowseResultSelection resetSearchContext addBibToContext delBibToContext getContextBiblioNumbers holdfor_cardnumber holdforclub strQuery PREF_NotHighlightedWords __ Cookies */
2
3 function verify_cover_images() {
4     /* Loop over each container in the template which contains covers */
5     var coverSlides = $(".cover-slides"); /* One coverSlides for each search result */
6     coverSlides.each( function( index ){
7         var slide = $(this);
8         var biblionumber = $(this).data("biblionumber");
9         var coverImages = $(".cover-image", slide ); /* Multiple coverImages for each coverSlides */
10         var blanks = [];
11         coverImages.each( function( index ){
12             var div = $(this);
13             var coverId = div.attr("id");
14             /* Find the image in the container */
15             var img = div.find("img")[0];
16             if( $(img).length > 0 ){
17                 if( (img.complete != null) && (!img.complete) || img.naturalHeight == 0 ){
18                     /* No image loaded in the container. Remove the slide */
19                     blanks.push( coverId );
20                     div.remove();
21                 } else {
22                     /* Check if Amazon image is present */
23                     if ( div.hasClass("amazon-bookcoverimg") ) {
24                         w = img.width;
25                         h = img.height;
26                         if ((w == 1) || (h == 1)) {
27                             /* Amazon returned single-pixel placeholder */
28                             /* Remove the container */
29                             blanks.push( coverId );
30                             div.remove();
31                         }
32                     }
33                     /* Check if Local image is present */
34                     if ( div.hasClass("local-coverimg" ) ) {
35                         w = img.width;
36                         h = img.height;
37                         if ((w == 1) || (h == 1)) {
38                             /* Local cover image returned single-pixel placeholder */
39                             /* Remove the container */
40                             blanks.push( coverId );
41                             div.remove();
42                         }
43                     }
44                     if( div.hasClass("custom-img") ){
45                         /* Check for image from CustomCoverImages system preference */
46                         if ( (img.complete != null) && (!img.complete) || img.naturalHeight == 0 ) {
47                             /* No image was loaded via the CustomCoverImages system preference */
48                             /* Remove the container */
49                             blanks.push( coverId );
50                             div.remove();
51                         }
52                     }
53
54                     if( div.hasClass("coce-coverimg") ){
55                         /* Identify which service's image is being loaded by IntranetCoce system pref */
56                         if( $(img).attr("src").indexOf('amazon.com') >= 0 ){
57                             div.find(".hint").html(_("Coce image from Amazon.com"));
58                         } else if( $(img).attr("src").indexOf('google.com') >= 0 ){
59                             div.find(".hint").html(_("Coce image from Google Books"));
60                         } else if( $(img).attr("src").indexOf('openlibrary.org') >= 0 ){
61                             div.find(".hint").html(_("Coce image from Open Library"));
62                         } else {
63                             blanks.push( coverId );
64                             div.remove();
65                         }
66                     }
67                     if( coverImages.length > 1 ){
68                         if( blanks.includes( coverId ) ){
69                             /* Don't add covernav link */
70                         } else {
71                             var covernav = $("<a href=\"#\" data-coverid=\"" + coverId + "\" data-biblionumber=\"" + biblionumber + "\" class=\"cover-nav\"></a>");
72                             $(covernav).html("<i class=\"fa fa-circle\"></i>");
73                             slide.addClass("cover-slides").append( covernav );
74                         }
75                     }
76                 } /* /IF image loaded */
77             } else {
78                 blanks.push( coverId );
79                 div.remove();
80             } /* /IF there is an image tag */
81             /* console.log( coverImages ); */
82         });
83
84         /* Show the first cover image slide after empty ones have been removed */
85         $(".cover-image", slide).eq(0).show();
86         /* Remove "loading" background gif */
87         $(".bookcoverimg").css("background","unset");
88
89         if( $(".cover-image", slide).length < 2 ){
90             /* Don't show controls for switching between covers if there is only 1 */
91             $(".cover-nav", slide).remove();
92         }
93         /* Set the first navigation link as active */
94         $(".cover-nav", slide).eq(0).addClass("nav-active");
95
96         /* If no slides contain any cover images, remove the container */
97         if( $(".cover-image:visible", slide).length < 1 ){
98             slide.html('<div class="no-image">' + __("No cover image available") + '</div>');
99         }
100     });
101 }
102
103 $(window).load(function() {
104     verify_cover_images();
105 });
106
107 var Sticky;
108 var toHighlight = {};
109 var q_array;
110
111 $(document).ready(function() {
112
113     $("#searchresults").on("click",".cover-nav", function(e){
114         e.preventDefault();
115         /* Adding click handler for cover image navigation links */
116         var coverid = $(this).data("coverid");
117         var biblionumber = $(this).data("biblionumber");
118         var slides = $("#cover-slides-" + biblionumber );
119
120         $(".cover-nav", slides).removeClass("nav-active");
121         $(this).addClass("nav-active");
122         $(".cover-image", slides).hide();
123         $( "#" + coverid ).show();
124     });
125
126     $(".moretoggle").click(function(e) {
127         e.preventDefault();
128         $(this).siblings(".collapsible-facet").toggle();
129         $(this).siblings(".moretoggle").toggle();
130         $(this).toggle();
131     });
132
133     Sticky = $("#searchheader");
134     Sticky.hcSticky({
135         stickTo: "main",
136         stickyClass: "floating"
137     });
138
139     $("#cartsubmit").click(function(e){
140         e.preventDefault();
141         addMultiple();
142     });
143
144     $(".addtolist").on("click",function(e){
145         e.preventDefault();
146         var shelfnumber = $(this).data("shelfnumber");
147         var vshelf = vShelfAdd();
148         if( vshelf ){
149             if( $(this).hasClass("morelists") ){
150                 openWindow('/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?' + vshelf);
151             } else if( $(this).hasClass("newlist") ){
152                 openWindow('/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?newshelf=1&' + vshelf);
153             } else {
154                 openWindow('/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?shelfnumber='+shelfnumber+'&confirm=1&' + vshelf);
155             }
156         }
157     });
158
159     $("#z3950submit").click(function(){
160         PopupZ3950();
161         return false;
162     });
163
164     $("#searchheader").on("click", ".browse_selection", function(){
165         browse_selection();
166         return false;
167     });
168
169     $("#searchheader").on("click",".placehold", function(){
170         $("#holdFor").val("");
171         $("#holdForClub").val("");
172         placeHold();
173         $(".btn-group").removeClass("open");
174         return false;
175     });
176
177     $(".placeholdfor").click(function(){
178         holdForPatron();
179         $(".btn-group").removeClass("open");
180         return false;
181     });
182
183     $(".placeholdforclub").click(function(){
184         holdForClub();
185         $(".btn-group").removeClass("open");
186         return false;
187     });
188
189     $("#forgetholdfor, #forgetholdforclub").click(function(){
190         forgetPatronAndClub();
191         $(".btn-group").removeClass("open");
192         return false;
193     });
194
195     $(".selection").show();
196
197     if( search_result.query_desc ){
198         toHighlight = $("p,span.results_summary,a.title");
199         q_array = search_result.query_desc.split(" ");
200         /* ensure that we don't have "" at the end of the array, which can */
201         /* break the highlighter */
202         while ( q_array.length > 0 && q_array[q_array.length-1] == "") {
203             q_array = q_array.splice(0,-1);
204         }
205         highlightOn();
206         $("#highlight_toggle_on" ).hide().click(function(e) {
207             e.preventDefault();
208             highlightOn();
209         });
210         $("#highlight_toggle_off").show().click(function(e) {
211             e.preventDefault();
212             highlightOff();
213         });
214     }
215
216     if( SEARCH_RESULTS ){
217         var browser = KOHA.browser( search_result.searchid, parseInt( biblionumber, 10));
218         browser.create( search_result.first_result_number, search_result.query_cgi, search_result.limit_cgi, search_result.sort_by, new_results_browser, search_result.total );
219     }
220
221     if( search_result.gotoPage && search_result.gotoNumber){
222         if( search_result.gotoNumber == 'first' ){
223             window.location = "/cgi-bin/koha/catalogue/" + search_result.gotoPage + "?biblionumber=" + search_result.first_biblionumber + "&searchid=" + search_result.searchid;
224         } else if( search_result.gotoNumber == "last" ){
225             window.location = "/cgi-bin/koha/catalogue/" + search_result.gotoPage + "?biblionumber=" + search_result.last_biblionumber + "&searchid=" + search_result.searchid;
226         }
227     }
228
229     if( PREF_LocalCoverImages ){
230         KOHA.LocalCover.LoadResultsCovers();
231     }
232
233     if( PREF_IntranetCoce && PREF_CoceProviders ){
234         KOHA.coce.getURL( CoceHost, CoceProviders );
235     }
236
237     $("#select_all").on("click",function(e){
238         e.preventDefault();
239         selectAll();
240     });
241
242     $("#clear_all").on("click",function(e){
243         e.preventDefault();
244         clearAll();
245     });
246
247     $("#searchresults").on("click",".addtocart",function(e){
248         e.preventDefault();
249         var selection_id = this.id;
250         var biblionumber = selection_id.replace("cart","");
251         addRecord(biblionumber);
252     });
253
254     $("#searchresults").on("click",".cartRemove",function(e){
255         e.preventDefault();
256         var selection_id = this.id;
257         var biblionumber = selection_id.replace("cartR","");
258         delSingleRecord(biblionumber);
259     });
260
261     if( !PREF_BrowseResultSelection ){
262         resetSearchContext();
263     }
264
265     $(".selection").change(function(){
266         if( $(".selection:checked").length > 0 ){
267             toggleBatchOp( true );
268         } else {
269             toggleBatchOp( false );
270         }
271         if ( $(this).is(':checked') == true ) {
272             addBibToContext( $(this).val() );
273         } else {
274             delBibToContext( $(this).val() );
275         }
276     });
277     $("#bookbag_form").ready(function(){
278         $("#bookbag_form input:checkbox").prop("checked", false);
279         var bibnums = getContextBiblioNumbers();
280         if (bibnums) {
281             for (var i=0; i < bibnums.length; i++) {
282                 var id = ('#bib' + bibnums[i]);
283                 if ($(id)) {
284                     $(id).attr('checked', true);
285                 }
286             }
287         }
288     });
289
290     if( $(".selection:checked") > 0 ){
291         toggleBatchOp( true );
292     }
293
294     $(".results_batch_op").on("click", function(e){
295         e.preventDefault();
296         var op = $(this).data("op");
297         resultsBatchProcess( op );
298     });
299 });
300
301
302 function highlightOff() {
303     if( toHighlight.length > 0 ){
304         toHighlight.removeHighlight();
305         $(".highlight_toggle").toggle();
306     }
307 }
308
309 function highlightOn() {
310     if( toHighlight.length > 0 ){
311         var x;
312         for (x in q_array) {
313             q_array[x] = q_array[x].toLowerCase();
314             var myStopwords = PREF_NotHighlightedWords.toLowerCase().split('|');
315             if ( (q_array[x].length > 0) && ($.inArray(q_array[x], myStopwords) == -1) ) {
316                 toHighlight.highlight(q_array[x]);
317             }
318         }
319         $(".highlight_toggle").toggle();
320     }
321 }
322
323
324 function selectAll () {
325     $("#bookbag_form").find("input[type='checkbox'][name='biblionumber']").each(function(){
326         $(this).prop("checked", true ).change();
327     } );
328 }
329 function clearAll () {
330     $("#bookbag_form").find("input[type='checkbox'][name='biblionumber']").each(function(){
331         $(this).prop("checked", false).change();
332     } );
333 }
334 function placeHold () {
335     var checkedItems = $(".selection:checked");
336     if ($(checkedItems).size() == 0) {
337         alert( __("Nothing is selected") );
338         return false;
339     }
340     var bibs = "";
341     $(checkedItems).each(function() {
342         var bib = $(this).val();
343         bibs += bib + "/";
344     });
345     $("#hold_form_biblios").val(bibs);
346     $("#hold_form").submit();
347     return false;
348 }
349
350 function forgetPatronAndClub(){
351     Cookies.remove("holdfor", { path: '/' });
352     Cookies.remove("holdforclub", { path: '/' });
353     $(".holdforlink").remove();
354     $("#placeholdc").html("<a class=\"btn btn-default btn-xs placehold\" href=\"#\"><i class=\"fa fa-sticky-note-o\"></i> " + __("Place hold") + "</a>");
355 }
356
357 function browse_selection () {
358     var bibnums = getContextBiblioNumbers();
359     if ( bibnums && bibnums.length > 0 ) {
360         var browser = KOHA.browser('', parseInt( biblionumber, 10));
361         browser.create(1, search_result.query_cgi, search_result.limit_cgi, search_result.sort_by, bibnums, bibnums.length);
362         window.location = '/cgi-bin/koha/catalogue/detail.pl?biblionumber=' + bibnums[0] + '&searchid='+browser.searchid;
363     } else {
364         alert( __("Nothing is selected") );
365     }
366     return false;
367 }
368
369 function addToList () {
370     var checkedItems = $(".selection:checked");
371     if ($(checkedItems).size() == 0) {
372         alert( __("Nothing is selected") );
373         return false;
374     }
375     var bibs = "";
376     $(checkedItems).each(function() {
377         bibs += $(this).val() + "/";
378     });
379
380     var url = "/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?biblionumbers=" + bibs;
381     window.open(url, 'Add_to_virtualshelf', 'width=500, height=400, toolbar=false, scrollbars=yes');
382     return false;
383 }
384
385 /* this function open a popup to search on z3950 server.  */
386 function PopupZ3950() {
387     if( strQuery ){
388         window.open("/cgi-bin/koha/cataloguing/z3950_search.pl?biblionumber=" + biblionumber + strQuery,"z3950search",'width=740,height=450,location=yes,toolbar=no,scrollbars=yes,resize=yes');
389     }
390 }
391
392 function holdfor(){
393     $("#holdFor").val("");
394     $("#holdForClub").val("");
395     placeHold();
396 }
397
398 function holdForPatron() {
399     $("#holdFor").val( holdfor_cardnumber );
400     placeHold();
401 }
402
403 function holdForClub() {
404     $("#holdForClub").val( holdforclub );
405     placeHold();
406 }
407
408 function toggleBatchOp( b ){
409     var results_batch_ops = $("#results_batch_ops");
410     if( b ){
411         results_batch_ops.removeClass("disabled");
412     } else {
413         results_batch_ops.addClass("disabled");
414     }
415 }
416
417 function resultsBatchProcess( op ){
418     var selected = $(".selection:checked");
419     var params = [];
420     var url = "";
421     if( op == "edit" ){
422         /* batch edit selected records */
423         if ( selected.length < 1 ){
424             alert( __("You must select at least one record") );
425         } else {
426             selected.each(function() {
427                 params.push( $(this).val() );
428             });
429             url = "/cgi-bin/koha/tools/batch_record_modification.pl?op=list&amp;bib_list=" + params.join("/");
430             location.href = url;
431         }
432     } else if( op == "delete" ){
433         /* batch delete selected records */
434         if ( selected.length < 1) {
435             alert( __("You must select at least one record") );
436         } else {
437             selected.each(function() {
438                 params.push( $(this).val() );
439             });
440             url = "/cgi-bin/koha/tools/batch_delete_records.pl?op=list&type=biblio&bib_list=" + params.join("/");
441             location.href = url;
442         }
443     } else if( op == "merge" ){
444         /* merge selected records */
445         if ( selected.length < 2) {
446             alert( __("At least two records must be selected for merging") );
447         } else {
448             selected.each(function() {
449                 params.push('biblionumber=' + $(this).val());
450             });
451             url = "/cgi-bin/koha/cataloguing/merge.pl?" + params.join("&");
452             location.href = url;
453         }
454     } else {
455         return false;
456     }
457 }