Bug 23349: Add batch operations to staff interface catalog search 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_AmazonCoverImages PREF_LocalCoverImages PREF_IntranetCoce PREF_CoceProviders CoceHost CoceProviders addRecord delSingleRecord PREF_BrowseResultSelection resetSearchContext addBibToContext delBibToContext getContextBiblioNumbers MSG_NO_ITEM_SELECTED MSG_NO_ITEM_SELECTED holdfor_cardnumber holdforclub strQuery MSG_NON_RESERVES_SELECTED PREF_NotHighlightedWords PLACE_HOLD _ */
2
3 if( PREF_AmazonCoverImages ){
4     $(window).load(function() {
5         verify_images();
6     });
7 }
8
9 var Sticky;
10 var toHighlight = {};
11 var q_array;
12
13 $(document).ready(function() {
14
15     $(".moretoggle").click(function(e) {
16         e.preventDefault();
17         $(this).siblings(".collapsible-facet").toggle();
18         $(this).siblings(".moretoggle").toggle();
19         $(this).toggle();
20     });
21
22     Sticky = $("#searchheader");
23     Sticky.hcSticky({
24         stickTo: "main",
25         stickyClass: "floating"
26     });
27
28     $("#cartsubmit").click(function(e){
29         e.preventDefault();
30         addMultiple();
31     });
32
33     $(".addtolist").on("click",function(e){
34         e.preventDefault();
35         var shelfnumber = $(this).data("shelfnumber");
36         var vshelf = vShelfAdd();
37         if( vshelf ){
38             if( $(this).hasClass("morelists") ){
39                 openWindow('/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?' + vshelf);
40             } else if( $(this).hasClass("newlist") ){
41                 openWindow('/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?newshelf=1&' + vshelf);
42             } else {
43                 openWindow('/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?shelfnumber='+shelfnumber+'&confirm=1&' + vshelf);
44             }
45         }
46     });
47
48     $("#z3950submit").click(function(){
49         PopupZ3950();
50         return false;
51     });
52
53     $("#searchheader").on("click", ".browse_selection", function(){
54         browse_selection();
55         return false;
56     });
57
58     $("#searchheader").on("click",".placehold", function(){
59         $("#holdFor").val("");
60         $("#holdForClub").val("");
61         placeHold();
62         $(".btn-group").removeClass("open");
63         return false;
64     });
65
66     $(".placeholdfor").click(function(){
67         holdForPatron();
68         $(".btn-group").removeClass("open");
69         return false;
70     });
71
72     $(".placeholdforclub").click(function(){
73         holdForClub();
74         $(".btn-group").removeClass("open");
75         return false;
76     });
77
78     $("#forgetholdfor, #forgetholdforclub").click(function(){
79         forgetPatronAndClub();
80         $(".btn-group").removeClass("open");
81         return false;
82     });
83
84     $(".selection").show();
85
86     if( search_result.query_desc ){
87         toHighlight = $("p,span.results_summary,a.title");
88         q_array = search_result.query_desc.split(" ");
89         // ensure that we don't have "" at the end of the array, which can
90         // break the highlighter
91         while ( q_array.length > 0 && q_array[q_array.length-1] == "") {
92             q_array = q_array.splice(0,-1);
93         }
94         highlightOn();
95         $("#highlight_toggle_on" ).hide().click(function(e) {
96             e.preventDefault();
97             highlightOn();
98         });
99         $("#highlight_toggle_off").show().click(function(e) {
100             e.preventDefault();
101             highlightOff();
102         });
103     }
104
105     if( SEARCH_RESULTS ){
106         var browser = KOHA.browser( search_result.searchid, parseInt( biblionumber, 10));
107         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 );
108     }
109
110     if( search_result.gotoPage && search_result.gotoNumber){
111         if( search_result.gotoNumber == 'first' ){
112             window.location = "/cgi-bin/koha/catalogue/" + search_result.gotoPage + "?biblionumber=" + search_result.first_biblionumber + "&searchid=" + search_result.searchid;
113         } else if( search_result.gotoNumber == "last" ){
114             window.location = "/cgi-bin/koha/catalogue/" + search_result.gotoPage + "?biblionumber=" + search_result.last_biblionumber + "&searchid=" + search_result.searchid;
115         }
116     }
117
118     if( PREF_LocalCoverImages ){
119         KOHA.LocalCover.LoadResultsCovers();
120     }
121
122     if( PREF_IntranetCoce && PREF_CoceProviders ){
123         KOHA.coce.getURL( CoceHost, CoceProviders );
124     }
125
126     $("#select_all").on("click",function(e){
127         e.preventDefault();
128         selectAll();
129     });
130
131     $("#clear_all").on("click",function(e){
132         e.preventDefault();
133         clearAll();
134     });
135
136     $("#searchresults").on("click",".addtocart",function(e){
137         e.preventDefault();
138         var selection_id = this.id;
139         var biblionumber = selection_id.replace("cart","");
140         addRecord(biblionumber);
141     });
142
143     $("#searchresults").on("click",".cartRemove",function(e){
144         e.preventDefault();
145         var selection_id = this.id;
146         var biblionumber = selection_id.replace("cartR","");
147         delSingleRecord(biblionumber);
148     });
149
150     if( !PREF_BrowseResultSelection ){
151         resetSearchContext();
152     }
153
154     $(".selection").change(function(){
155         if( $(".selection:checked").length > 0 ){
156             toggleBatchOp( true );
157         } else {
158             toggleBatchOp( false );
159         }
160         if ( $(this).is(':checked') == true ) {
161             addBibToContext( $(this).val() );
162         } else {
163             delBibToContext( $(this).val() );
164         }
165     });
166     $("#bookbag_form").ready(function(){
167         $("#bookbag_form").unCheckCheckboxes();
168         var bibnums = getContextBiblioNumbers();
169         if (bibnums) {
170             for (var i=0; i < bibnums.length; i++) {
171                 var id = ('#bib' + bibnums[i]);
172                 if ($(id)) {
173                     $(id).attr('checked', true);
174                 }
175             }
176         }
177     });
178
179     if( $(".selection:checked") > 0 ){
180         toggleBatchOp( true );
181     }
182
183     $(".results_batch_op").on("click", function(e){
184         e.preventDefault();
185         var op = $(this).data("op");
186         resultsBatchProcess( op );
187     });
188 });
189
190
191 function highlightOff() {
192     if( toHighlight.length > 0 ){
193         toHighlight.removeHighlight();
194         $(".highlight_toggle").toggle();
195     }
196 }
197
198 function highlightOn() {
199     if( toHighlight.length > 0 ){
200         var x;
201         for (x in q_array) {
202             q_array[x] = q_array[x].toLowerCase();
203             var myStopwords = PREF_NotHighlightedWords.toLowerCase().split('|');
204             if ( (q_array[x].length > 0) && ($.inArray(q_array[x], myStopwords) == -1) ) {
205                 toHighlight.highlight(q_array[x]);
206             }
207         }
208         $(".highlight_toggle").toggle();
209     }
210 }
211
212
213 function selectAll () {
214     $("#bookbag_form").checkCheckboxes();
215     $("#bookbag_form").find("input[type='checkbox'][name='biblionumber']").each(function(){
216         $(this).change();
217     } );
218     return false;
219 }
220 function clearAll () {
221     $("#bookbag_form").unCheckCheckboxes();
222     $("#bookbag_form").find("input[type='checkbox'][name='biblionumber']").each(function(){
223         $(this).change();
224     } );
225     return false;
226 }
227 function placeHold () {
228     var checkedItems = $(".selection:checked");
229     if ($(checkedItems).size() == 0) {
230         alert(MSG_NO_ITEM_SELECTED);
231         return false;
232     }
233     var bibs = "";
234     var badBibs = false;
235     $(checkedItems).each(function() {
236         var bib = $(this).val();
237         if ($("#reserve_" + bib).size() == 0) {
238             alert(MSG_NON_RESERVES_SELECTED);
239             badBibs = true;
240             return false;
241         }
242         bibs += bib + "/";
243     });
244     if (badBibs) {
245         return false;
246     }
247     $("#hold_form_biblios").val(bibs);
248     $("#hold_form").submit();
249     return false;
250 }
251
252 function forgetPatronAndClub(){
253     $.removeCookie("holdfor", { path: '/' });
254     $.removeCookie("holdforclub", { path: '/' });
255     $(".holdforlink").remove();
256     $("#placeholdc").html("<a class=\"btn btn-default btn-xs placehold\" href=\"#\"><i class=\"fa fa-sticky-note-o\"></i> " + PLACE_HOLD + "</a>");
257 }
258
259 function browse_selection () {
260     var bibnums = getContextBiblioNumbers();
261     if ( bibnums && bibnums.length > 0 ) {
262         var browser = KOHA.browser('', parseInt( biblionumber, 10));
263         browser.create(1, search_result.query_cgi, search_result.limit_cgi, search_result.sort_by, bibnums, bibnums.length);
264         window.location = '/cgi-bin/koha/catalogue/detail.pl?biblionumber=' + bibnums[0] + '&searchid='+browser.searchid;
265     } else {
266         alert(MSG_NO_ITEM_SELECTED);
267     }
268     return false;
269 }
270
271 function addToList () {
272     var checkedItems = $(".selection:checked");
273     if ($(checkedItems).size() == 0) {
274         alert(MSG_NO_ITEM_SELECTED);
275         return false;
276     }
277     var bibs = "";
278     $(checkedItems).each(function() {
279         bibs += $(this).val() + "/";
280     });
281
282     var url = "/cgi-bin/koha/virtualshelves/addbybiblionumber.pl?biblionumbers=" + bibs;
283     window.open(url, 'Add_to_virtualshelf', 'width=500, height=400, toolbar=false, scrollbars=yes');
284     return false;
285 }
286
287 /* this function open a popup to search on z3950 server.  */
288 function PopupZ3950() {
289     if( strQuery ){
290         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');
291     }
292 }
293
294 function holdfor(){
295     $("#holdFor").val("");
296     $("#holdForClub").val("");
297     placeHold();
298 }
299
300 function holdForPatron() {
301     $("#holdFor").val( holdfor_cardnumber );
302     placeHold();
303 }
304
305 function holdForClub() {
306     $("#holdForClub").val( holdforclub );
307     placeHold();
308 }
309
310 // http://www.oreillynet.com/pub/a/javascript/2003/10/21/amazonhacks.html
311 function verify_images() {
312     $("img").each(function(){
313         if ((this.src.indexOf('images-amazon.com') >= 0) || (this.src.indexOf('images.amazon.com') >=0)) {
314             var w = this.width;
315             var h = this.height;
316             if ((w == 1) || (h == 1)) {
317                 $(this).parent().html('<span class="no-image">No cover image available</span>');
318             } else if ((this.complete != null) && (!this.complete)) {
319                 $(this).parent().html('<span class="no-image">No cover image available</span>');
320             }
321         }
322     });
323 }
324
325 function toggleBatchOp( b ){
326     var results_batch_ops = $("#results_batch_ops");
327     if( b ){
328         results_batch_ops.removeClass("disabled");
329     } else {
330         results_batch_ops.addClass("disabled");
331     }
332 }
333
334 function resultsBatchProcess( op ){
335     var selected = $(".selection:checked");
336     var params = [];
337     var url = "";
338     if( op == "edit" ){
339         // batch edit selected records
340         if ( selected.length < 1 ){
341             alert( _("You must select at least one record") );
342         } else {
343             selected.each(function() {
344                 params.push( $(this).val() );
345             });
346             url = "/cgi-bin/koha/tools/batch_record_modification.pl?op=list&amp;bib_list=" + params.join("/");
347             location.href = url;
348         }
349     } else if( op == "delete" ){
350         // batch delete selected records
351         if ( selected.length < 1) {
352             alert( _("You must select at least one record") );
353         } else {
354             selected.each(function() {
355                 params.push( $(this).val() );
356             });
357             url = "/cgi-bin/koha/tools/batch_delete_records.pl?op=list&type=biblio&bib_list=" + params.join("/");
358             location.href = url;
359         }
360     } else if( op == "merge" ){
361         // merge selected records
362         if ( selected.length < 2) {
363             alert( _("At least two records must be selected for merging") );
364         } else {
365             selected.each(function() {
366                 params.push('biblionumber=' + $(this).val());
367             });
368             url = "/cgi-bin/koha/cataloguing/merge.pl?" + params.join("&");
369             location.href = url;
370         }
371     } else {
372         return false;
373     }
374 }