Bug 32214: Prevent empty search option block in catalog search header
[koha-ffzg.git] / koha-tmpl / intranet-tmpl / prog / js / staff-global.js
1 /* global shortcut delBasket Sticky AUDIO_ALERT_PATH Cookies */
2 /* exported addBibToContext delBibToContext escape_str escape_price openWindow _ removeFocus toUC confirmDelete confirmClone playSound */
3 if ( KOHA === undefined ) var KOHA = {};
4
5 function _(s) { return s; } // dummy function for gettext
6
7 // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery/5341855#5341855
8 String.prototype.format = function() { return formatstr(this, arguments); };
9 function formatstr(str, col) {
10     col = typeof col === 'object' ? col : Array.prototype.slice.call(arguments, 1);
11     var idx = 0;
12     return str.replace(/%%|%s|%(\d+)\$s/g, function (m, n) {
13         if (m == "%%") { return "%"; }
14         if (m == "%s") { return col[idx++]; }
15         return col[n];
16     });
17 }
18
19 var HtmlCharsToEscape = {
20     '&': '&',
21     '<': '&lt;',
22     '>': '&gt;'
23 };
24 String.prototype.escapeHtml = function() {
25     return this.replace(/[&<>]/g, function(c) {
26         return HtmlCharsToEscape[c] || c;
27     });
28 };
29 function escape_str(s){
30     return s != null ? s.escapeHtml() : "";
31 }
32
33 /*
34  * Void method for numbers, for consistency
35  */
36 Number.prototype.escapeHtml = function() {
37     return this;
38 };
39 function escape_price(p){
40     return p != null ? p.escapeHtml().format_price() : "";
41 }
42
43 // http://stackoverflow.com/questions/14859281/select-tab-by-name-in-jquery-ui-1-10-0/16550804#16550804
44 $.fn.tabIndex = function () {
45     return $(this).parent().children('div').index(this);
46 };
47 $.fn.selectTabByID = function (tabID) {
48     $(this).tabs("option", "active", $( tabID ).tabIndex());
49 };
50
51 $(document).ready(function() {
52     $('#header_search').tabs({
53         create: function( e, ui ){
54             ui.panel.find("input:text:first").focus();
55         },
56         activate: function ( e, ui ) {
57             ui.newPanel.find("input:text:first").focus();
58         }
59     });
60
61     $(".close").click(function(){ window.close(); });
62
63     $("#checkin_search form").preventDoubleFormSubmit();
64
65     if($("#header_search #checkin_search").length > 0){ shortcut.add('Alt+r',function (){ $("#header_search").selectTabByID("#checkin_search"); $("#ret_barcode").focus(); }); } else { shortcut.add('Alt+r',function (){ location.href="/cgi-bin/koha/circ/returns.pl"; }); }
66     if($("#header_search #circ_search").length > 0){ shortcut.add('Alt+u',function (){ $("#header_search").selectTabByID("#circ_search"); $("#findborrower").focus(); }); } else { shortcut.add('Alt+u',function(){ location.href="/cgi-bin/koha/circ/circulation.pl"; }); }
67     if($("#header_search #catalog_search").length > 0){ shortcut.add('Alt+q',function (){ $("#header_search").selectTabByID("#catalog_search"); $("#search-form").focus(); }); } else { shortcut.add('Alt+q',function(){ location.href="/cgi-bin/koha/catalogue/search.pl"; }); }
68     if($("#header_search #renew_search").length > 0){ shortcut.add('Alt+w',function (){ $("#header_search").selectTabByID("#renew_search"); $("#ren_barcode").focus(); }); } else { shortcut.add('Alt+w',function(){ location.href="/cgi-bin/koha/circ/renew.pl"; }); }
69
70     $("#header_search > ul > li").show();
71
72     $('#header_search .form-extra-content-toggle').on('click', function () {
73         const extraContent = $(this).closest('form').find('.form-extra-content');
74         if (extraContent.is(':visible')) {
75             extraContent.hide();
76         } else {
77             extraContent.show();
78         }
79     });
80
81     $(".focus").focus();
82     $(".validated").each(function() {
83         $(this).validate();
84     });
85
86     $("#logout").on("click",function(){
87         logOut();
88     });
89     $("#helper").on("click",function(){
90         openHelp();
91         return false;
92     });
93
94     $("body").on("keypress", ".noEnterSubmit", function(e){
95         return checkEnter(e);
96     });
97
98     $(".keep_text").on("click",function(){
99         var field_index = $(this).parent().index();
100         keep_text( field_index );
101     });
102
103     $(".toggle_element").on("click",function(e){
104         e.preventDefault();
105         $( $(this).data("element") ).toggle();
106         if (typeof Sticky !== "undefined" && typeof hcSticky === "function") {
107             Sticky.hcSticky('update');
108         }
109     });
110
111     var navmenulist = $("#navmenulist");
112     if( navmenulist.length > 0 ){
113         var path = location.pathname.substring(1);
114         var url = window.location.toString();
115         var params = '';
116         if ( url.match(/\?(.+)$/) ) {
117             params = "?" + RegExp.$1;
118         }
119         $("a[href$=\"/" + path + params + "\"]", navmenulist).addClass("current");
120     }
121
122     $("#catalog-search-link a").on("mouseenter mouseleave", function(){
123         $("#catalog-search-dropdown a").toggleClass("catalog-search-dropdown-hover");
124     });
125
126     if ( localStorage.getItem("lastborrowernumber") ){
127         if( $("#hiddenborrowernumber").val() != localStorage.getItem("lastborrowernumber") ) {
128             $("#lastborrowerlink").show();
129             $("#lastborrowerlink").prop("title", localStorage.getItem("lastborrowername") + " (" + localStorage.getItem("lastborrowercard") + ")");
130             $("#lastborrowerlink").prop("href", "/cgi-bin/koha/circ/circulation.pl?borrowernumber=" + localStorage.getItem("lastborrowernumber"));
131             $("#lastborrower-window").css("display", "inline-flex");
132         }
133     }
134
135     if( !localStorage.getItem("lastborrowernumber") || ( $("#hiddenborrowernumber").val() != localStorage.getItem("lastborrowernumber") && localStorage.getItem("currentborrowernumber") != $("#hiddenborrowernumber").val())) {
136         if( $("#hiddenborrowernumber").val() ){
137             localStorage.setItem("lastborrowernumber", $("#hiddenborrowernumber").val() );
138             localStorage.setItem("lastborrowername", $("#hiddenborrowername").val() );
139             localStorage.setItem("lastborrowercard", $("#hiddenborrowercard").val() );
140         }
141     }
142
143     if( $("#hiddenborrowernumber").val() ){
144         localStorage.setItem("currentborrowernumber", $("#hiddenborrowernumber").val() );
145     }
146
147     $("#lastborrower-remove").click(function() {
148         removeLastBorrower();
149         $("#lastborrower-window").hide();
150     });
151
152     /* Search results browsing */
153     /* forms with action leading to search */
154     $("form[action*='search.pl']").submit(function(){
155         $('[name^="limit"]').each(function(){
156             if( $(this).val() == '' ){
157                 $(this).prop("disabled","disabled");
158             }
159         });
160         var disabledPrior = false;
161         $(".search-term-row").each(function(){
162             if( disabledPrior ){
163                 $(this).find('select[name="op"]').prop("disabled","disabled");
164                 disabledPrior = false;
165             }
166             if( $(this).find('input[name="q"]').val() == "" ){
167                 $(this).find('input').prop("disabled","disabled");
168                 $(this).find('select').prop("disabled","disabled");
169                 disabledPrior = true;
170             }
171         });
172         resetSearchContext();
173         saveOrClearSimpleSearchParams();
174     });
175     /* any link to launch a search except navigation links */
176     $("[href*='search.pl?']").not(".nav").not('.searchwithcontext').click(function(){
177         resetSearchContext();
178     });
179     /* any link to a detail page from the results page. */
180     $("#bookbag_form a[href*='detail.pl?']").click(function(){
181         resetSearchContext();
182     });
183
184 });
185
186 function removeLastBorrower(){
187     localStorage.removeItem("lastborrowernumber");
188     localStorage.removeItem("lastborrowername");
189     localStorage.removeItem("lastborrowercard");
190     localStorage.removeItem("currentborrowernumber");
191 }
192
193 // http://jennifermadden.com/javascript/stringEnterKeyDetector.html
194 function checkEnter(e){ //e is event object passed from function invocation
195     var characterCode; // literal character code will be stored in this variable
196     if(e && e.which){ //if which property of event object is supported (NN4)
197         characterCode = e.which; //character code is contained in NN4's which property
198     } else {
199         characterCode = e.keyCode; //character code is contained in IE's keyCode property
200     }
201     if( characterCode == 13 //if generated character code is equal to ascii 13 (if enter key)
202         && e.target.nodeName == "INPUT"
203         && e.target.type != "submit" // Allow enter to submit using the submit button
204     ){
205         return false;
206     } else {
207         return true;
208     }
209 }
210
211 function clearHoldFor(){
212     Cookies.remove("holdfor", { path: '/' });
213 }
214
215 function logOut(){
216     if( typeof delBasket == 'function' ){
217         delBasket('main', true);
218     }
219     clearHoldFor();
220     removeLastBorrower();
221     localStorage.removeItem("sql_reports_activetab");
222     localStorage.removeItem("searches");
223     localStorage.removeItem("bibs_selected");
224     localStorage.removeItem("patron_search_selections");
225 }
226
227 function openHelp(){
228     window.open( "/cgi-bin/koha/help.pl", "_blank");
229 }
230
231 jQuery.fn.preventDoubleFormSubmit = function() {
232     jQuery(this).submit(function() {
233         $("body, form input[type='submit'], form button[type='submit'], form a").addClass('waiting');
234         if (this.beenSubmitted)
235             return false;
236         else
237             this.beenSubmitted = true;
238     });
239 };
240
241 function openWindow(link,name,width,height) {
242     name = (typeof name == "undefined")?'popup':name;
243     width = (typeof width == "undefined")?'600':width;
244     height = (typeof height == "undefined")?'400':height;
245     //IE <= 9 can't handle a "name" with whitespace
246     try {
247         window.open(link,name,'width='+width+',height='+height+',resizable=yes,toolbar=false,scrollbars=yes,top');
248     } catch(e) {
249         window.open(link,null,'width='+width+',height='+height+',resizable=yes,toolbar=false,scrollbars=yes,top');
250     }
251 }
252
253 // Use this function to remove the focus from any element for
254 // repeated scanning actions on errors so the librarian doesn't
255 // continue scanning and miss the error.
256 function removeFocus() {
257     $(':focus').blur();
258 }
259
260 function toUC(f) {
261     var x=f.value.toUpperCase();
262     f.value=x;
263     return true;
264 }
265
266 function confirmDelete(message) {
267     return (confirm(message) ? true : false);
268 }
269
270 function confirmClone(message) {
271     return (confirm(message) ? true : false);
272 }
273
274 function playSound( sound ) {
275     if ( ! ( sound.indexOf('http://') === 0 || sound.indexOf('https://') === 0  ) ) {
276         sound = AUDIO_ALERT_PATH + sound;
277     }
278     document.getElementById("audio-alert").innerHTML = '<audio src="' + sound + '" autoplay="autoplay" autobuffer="autobuffer"></audio>';
279 }
280
281 // For keeping the text when navigating the search tabs
282 function keep_text(clicked_index) {
283     var searchboxes = document.getElementsByClassName("head-searchbox");
284     var persist = searchboxes[0].value;
285
286     for (var i = 0; i < searchboxes.length - 1; i++) {
287         if (searchboxes[i].value != searchboxes[i+1].value) {
288             if (i === searchboxes.length-2) {
289                 if (searchboxes[i].value != searchboxes[0].value) {
290                     persist = searchboxes[i].value;
291                 } else if (searchboxes.length === 2) {
292                     if (clicked_index === 0) {
293                         persist = searchboxes[1].value;
294                     }
295                 } else {
296                     persist = searchboxes[i+1].value;
297                 }
298             } else if (searchboxes[i+1].value != searchboxes[i+2].value) {
299                 persist = searchboxes[i+1].value;
300             }
301         }
302     }
303
304     for (i = 0; i < searchboxes.length; i++) {
305         searchboxes[i].value = persist;
306     }
307 }
308
309 // Extends jQuery API
310 jQuery.extend({uniqueArray:function(array){
311     return $.grep(array, function(el, index) {
312         return index === $.inArray(el, array);
313     });
314 }});
315
316 function removeByValue(arr, val) {
317     for(var i=0; i<arr.length; i++) {
318         if(arr[i] == val) {
319             arr.splice(i, 1);
320             break;
321         }
322     }
323 }
324
325 function addBibToContext( bibnum ) {
326     bibnum = parseInt(bibnum, 10);
327     var bibnums = getContextBiblioNumbers();
328     bibnums.push(bibnum);
329     setContextBiblioNumbers( bibnums );
330     setContextBiblioNumbers( $.uniqueArray( bibnums ) );
331 }
332
333 function delBibToContext( bibnum ) {
334     var bibnums = getContextBiblioNumbers();
335     removeByValue( bibnums, bibnum );
336     setContextBiblioNumbers( $.uniqueArray( bibnums ) );
337 }
338
339 function setContextBiblioNumbers( bibnums ) {
340     localStorage.setItem('bibs_selected', JSON.stringify( bibnums ) );
341 }
342
343 function getContextBiblioNumbers() {
344     var r = localStorage.getItem('bibs_selected');
345     if ( r ) {
346         return JSON.parse(r);
347     }
348     r = new Array();
349     return r;
350 }
351
352 function resetSearchContext() {
353     setContextBiblioNumbers( new Array() );
354 }
355
356 function saveOrClearSimpleSearchParams() {
357     // Simple masthead search - pass value for display on details page
358     var pulldown_selection;
359     var searchbox_value;
360     if( $("#cat-search-block select.advsearch").length ){
361         pulldown_selection = $("#cat-search-block select.advsearch").val();
362     } else {
363         pulldown_selection ="";
364     }
365     if( $("#cat-search-block #search-form").length ){
366         searchbox_value = $("#cat-search-block #search-form").val();
367     } else {
368         searchbox_value ="";
369     }
370     localStorage.setItem('cat_search_pulldown_selection', pulldown_selection );
371     localStorage.setItem('searchbox_value', searchbox_value );
372 }