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