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