Bug 32172: Remove biblionumber param from Z3950 popup on results
[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_StaffHighlightedWords PREF_NotHighlightedWords __ */
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         $("#highlight_toggle_off" ).hide().click(function(e) {
206             e.preventDefault();
207             highlightOff();
208         });
209         $("#highlight_toggle_on").show().click(function(e) {
210             e.preventDefault();
211             highlightOn();
212         });
213         if( PREF_StaffHighlightedWords == 1 ){
214             highlightOn();
215         } else {
216             highlightOff();
217         }
218     }
219
220
221     if( SEARCH_RESULTS ){
222         var browser = KOHA.browser( search_result.searchid, parseInt( biblionumber, 10));
223         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 );
224     }
225
226     if( search_result.gotoPage && search_result.gotoNumber){
227         if( search_result.gotoNumber == 'first' ){
228             window.location = "/cgi-bin/koha/catalogue/" + search_result.gotoPage + "?biblionumber=" + search_result.first_biblionumber + "&searchid=" + search_result.searchid;
229         } else if( search_result.gotoNumber == "last" ){
230             window.location = "/cgi-bin/koha/catalogue/" + search_result.gotoPage + "?biblionumber=" + search_result.last_biblionumber + "&searchid=" + search_result.searchid;
231         }
232     }
233
234     if( PREF_LocalCoverImages ){
235         KOHA.LocalCover.LoadResultsCovers();
236     }
237
238     if( PREF_IntranetCoce && PREF_CoceProviders ){
239         KOHA.coce.getURL( CoceHost, CoceProviders );
240     }
241
242     $("#select_all").on("click",function(e){
243         e.preventDefault();
244         selectAll();
245     });
246
247     $("#clear_all").on("click",function(e){
248         e.preventDefault();
249         clearAll();
250     });
251
252     $("#searchresults").on("click",".addtocart",function(e){
253         e.preventDefault();
254         var selection_id = this.id;
255         var biblionumber = selection_id.replace("cart","");
256         addRecord(biblionumber);
257     });
258
259     $("#searchresults").on("click",".cartRemove",function(e){
260         e.preventDefault();
261         var selection_id = this.id;
262         var biblionumber = selection_id.replace("cartR","");
263         delSingleRecord(biblionumber);
264     });
265
266     if( !PREF_BrowseResultSelection ){
267         resetSearchContext();
268     }
269
270     $(".selection").change(function(){
271         if( $(".selection:checked").length > 0 ){
272             toggleBatchOp( true );
273         } else {
274             toggleBatchOp( false );
275         }
276         if ( $(this).is(':checked') == true ) {
277             addBibToContext( $(this).val() );
278         } else {
279             delBibToContext( $(this).val() );
280         }
281     });
282     $("#bookbag_form").ready(function(){
283         $("#bookbag_form input:checkbox").prop("checked", false);
284         var bibnums = getContextBiblioNumbers();
285         if (bibnums) {
286             for (var i=0; i < bibnums.length; i++) {
287                 var id = ('#bib' + bibnums[i]);
288                 if ($(id)) {
289                     $(id).attr('checked', true);
290                 }
291             }
292         }
293     });
294
295     if( $(".selection:checked") > 0 ){
296         toggleBatchOp( true );
297     }
298
299     $(".results_batch_op").on("click", function(e){
300         e.preventDefault();
301         var op = $(this).data("op");
302         resultsBatchProcess( op );
303     });
304 });
305
306
307 function highlightOff() {
308     if( toHighlight.length > 0 ){
309         toHighlight.removeHighlight();
310         $(".highlight_toggle").toggle();
311     }
312 }
313
314 function highlightOn() {
315     if( toHighlight.length > 0 ){
316         var x;
317         for (x in q_array) {
318             q_array[x] = q_array[x].toLowerCase();
319             var myStopwords = PREF_NotHighlightedWords.toLowerCase().split('|');
320             if ( (q_array[x].length > 0) && ($.inArray(q_array[x], myStopwords) == -1) ) {
321                 toHighlight.highlight(q_array[x]);
322             }
323         }
324         $(".highlight_toggle").toggle();
325     }
326 }
327
328
329 function selectAll () {
330     $("#bookbag_form").find("input[type='checkbox'][name='biblionumber']").each(function(){
331         $(this).prop("checked", true ).change();
332     } );
333 }
334 function clearAll () {
335     $("#bookbag_form").find("input[type='checkbox'][name='biblionumber']").each(function(){
336         $(this).prop("checked", false).change();
337     } );
338 }
339 function placeHold () {
340     var checkedItems = $(".selection:checked");
341     if ($(checkedItems).size() == 0) {
342         alert( __("Nothing is selected") );
343         return false;
344     }
345     var bibs = [];
346     $(checkedItems).each(function() {
347         var bib = $(this).val();
348         bibs.push(bib);
349     });
350     bibs.forEach(function (bib) {
351         var bib_param = $("<input>").attr("type", "hidden").attr("name", "biblionumber").val(bib);
352         $('#hold_form').append(bib_param);
353     });
354     $("#hold_form").submit();
355     return false;
356 }
357
358 function forgetPatronAndClub(){
359     Cookies.remove("holdfor", { path: '/' });
360     Cookies.remove("holdforclub", { path: '/' });
361     $(".holdforlink").remove();
362     $("#placeholdc").html("<a class=\"btn btn-default btn-xs placehold\" href=\"#\"><i class=\"fa fa-sticky-note-o\"></i> " + __("Place hold") + "</a>");
363 }
364
365 function browse_selection () {
366     var bibnums = getContextBiblioNumbers();
367     if ( bibnums && bibnums.length > 0 ) {
368         var browser = KOHA.browser('', parseInt( biblionumber, 10));
369         browser.create(1, search_result.query_cgi, search_result.limit_cgi, search_result.sort_by, bibnums, bibnums.length);
370         window.location = '/cgi-bin/koha/catalogue/detail.pl?biblionumber=' + bibnums[0] + '&searchid='+browser.searchid;
371     } else {
372         alert( __("Nothing is selected") );
373     }
374     return false;
375 }
376
377 function addToList () {
378     var checkedItems = $(".selection:checked");
379     if ($(checkedItems).size() == 0) {
380         alert( __("Nothing is selected") );
381         return false;
382     }
383     var bibs = "";
384     $(checkedItems).each(function() {
385         bibs += $(this).val() + "/";
386     });
387
388     var url = "/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?biblionumbers=" + bibs;
389     window.open(url, 'Add_to_virtualshelf', 'width=500, height=400, toolbar=false, scrollbars=yes');
390     return false;
391 }
392
393 /* this function open a popup to search on z3950 server.  */
394 function PopupZ3950() {
395     if( strQuery ){
396         window.open("/cgi-bin/koha/cataloguing/z3950_search.pl?" + strQuery,"z3950search",'width=740,height=450,location=yes,toolbar=no,scrollbars=yes,resize=yes');
397     }
398 }
399
400 function holdfor(){
401     $("#holdFor").val("");
402     $("#holdForClub").val("");
403     placeHold();
404 }
405
406 function holdForPatron() {
407     $("#holdFor").val( holdfor_cardnumber );
408     placeHold();
409 }
410
411 function holdForClub() {
412     $("#holdForClub").val( holdforclub );
413     placeHold();
414 }
415
416 function toggleBatchOp( b ){
417     var results_batch_ops = $("#results_batch_ops");
418     if( b ){
419         results_batch_ops.removeClass("disabled");
420     } else {
421         results_batch_ops.addClass("disabled");
422     }
423 }
424
425 function resultsBatchProcess( op ){
426     var selected = $(".selection:checked");
427     var params = [];
428     var url = "";
429     if( op == "edit" ){
430         /* batch edit selected records */
431         if ( selected.length < 1 ){
432             alert( __("You must select at least one record") );
433         } else {
434             selected.each(function() {
435                 params.push( $(this).val() );
436             });
437             url = "/cgi-bin/koha/tools/batch_record_modification.pl?op=list&amp;bib_list=" + params.join("/");
438             location.href = url;
439         }
440     } else if( op == "delete" ){
441         /* batch delete selected records */
442         if ( selected.length < 1) {
443             alert( __("You must select at least one record") );
444         } else {
445             selected.each(function() {
446                 params.push( $(this).val() );
447             });
448             url = "/cgi-bin/koha/tools/batch_delete_records.pl?op=list&type=biblio&bib_list=" + params.join("/");
449             location.href = url;
450         }
451     } else if( op == "merge" ){
452         /* merge selected records */
453         if ( selected.length < 2) {
454             alert( __("At least two records must be selected for merging") );
455         } else {
456             selected.each(function() {
457                 params.push('biblionumber=' + $(this).val());
458             });
459             url = "/cgi-bin/koha/cataloguing/merge.pl?" + params.join("&");
460             location.href = url;
461         }
462     } else {
463         return false;
464     }
465 }