Bug 29960: Remove Modernizr dependency in the OPAC
[koha.git] / koha-tmpl / opac-tmpl / bootstrap / js / global.js
1 /* global enquire readCookie updateBasket delCookie */
2 (function( w ){
3     // if the class is already set, the font has already been loaded
4     if( w.document.documentElement.className.indexOf( "fonts-loaded" ) > -1 ){
5         return;
6     }
7     var PrimaryFont = new w.FontFaceObserver( "NotoSans", {
8         weight: 400
9     });
10
11     PrimaryFont.load(null, 5000).then(function(){
12         w.document.documentElement.className += " fonts-loaded";
13     }, function(){
14         console.log("Failed");
15     });
16 }( this ));
17
18 // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery/5341855#5341855
19 String.prototype.format = function() { return formatstr(this, arguments) }
20 function formatstr(str, col) {
21     col = typeof col === 'object' ? col : Array.prototype.slice.call(arguments, 1);
22     var idx = 0;
23     return str.replace(/%%|%s|%(\d+)\$s/g, function (m, n) {
24         if (m == "%%") { return "%"; }
25         if (m == "%s") { return col[idx++]; }
26         return col[n];
27     });
28 };
29
30 function confirmDelete(message) {
31     return (confirm(message) ? true : false);
32 }
33
34 function Dopop(link) {
35     newin=window.open(link,'popup','width=500,height=400,toolbar=false,scrollbars=yes,resizable=yes');
36 }
37
38 jQuery.fn.preventDoubleFormSubmit = function() {
39     jQuery(this).submit(function() {
40         if (this.beenSubmitted)
41             return false;
42         else
43             this.beenSubmitted = true;
44     });
45 };
46
47 function prefixOf (s, tok) {
48     var index = s.indexOf(tok);
49     return s.substring(0, index);
50 }
51 function suffixOf (s, tok) {
52     var index = s.indexOf(tok);
53     return s.substring(index + 1);
54 }
55
56 $("body").on("keypress", ".noEnterSubmit", function(e){
57     return checkEnter(e);
58 });
59
60 // http://jennifermadden.com/javascript/stringEnterKeyDetector.html
61 function checkEnter(e){ //e is event object passed from function invocation
62     var characterCode; // literal character code will be stored in this variable
63     if(e && e.which){ //if which property of event object is supported (NN4)
64         characterCode = e.which; //character code is contained in NN4's which property
65     } else {
66         characterCode = e.keyCode; //character code is contained in IE's keyCode property
67     }
68     if( characterCode == 13 //if generated character code is equal to ascii 13 (if enter key)
69         && e.target.nodeName == "INPUT"
70         && e.target.type != "submit" // Allow enter to submit using the submit button
71     ){
72         return false;
73     } else {
74         return true;
75     }
76 }
77
78 // Adapted from https://gist.github.com/jnormore/7418776
79 function confirmModal(message, title, yes_label, no_label, callback) {
80     $("#bootstrap-confirm-box-modal").data('confirm-yes', false);
81     if($("#bootstrap-confirm-box-modal").length == 0) {
82         $("body").append('<div id="bootstrap-confirm-box-modal" tabindex="-1" role="dialog" aria-hidden="true" class="modal">\
83             <div class="modal-dialog">\
84                 <div class="modal-content">\
85                     <div class="modal-header" style="min-height:40px;">\
86                         <h4 class="modal-title"></h4>\
87                         <button type="button" class="closebtn" data-dismiss="modal" aria-label="Close">\
88                         <span aria-hidden="true">×</span>\
89                     </button>\
90                     </div>\
91                     <div class="modal-body"><p></p></div>\
92                     <div class="modal-footer">\
93                         <a href="#" id="bootstrap-confirm-box-modal-submit" class="btn btn-danger"><i class="fa fa-check" aria-hidden="true"></i></a>\
94                         <a href="#" id="bootstrap-confirm-box-modal-cancel" data-dismiss="modal" class="btn btn-secondary"><i class="fa fa-remove" aria-hidden="true"></i></a>\
95                     </div>\
96                 </div>\
97             </div>\
98         </div>');
99         $("#bootstrap-confirm-box-modal-submit").on('click', function () {
100             $("#bootstrap-confirm-box-modal").data('confirm-yes', true);
101             $("#bootstrap-confirm-box-modal").modal('hide');
102             return false;
103         });
104         $("#bootstrap-confirm-box-modal").on('hide.bs.modal', function () {
105             if(callback) callback($("#bootstrap-confirm-box-modal").data('confirm-yes'));
106         });
107     }
108
109     $("#bootstrap-confirm-box-modal .modal-header h4").text( title || "" );
110     if( message && message != "" ){
111         $("#bootstrap-confirm-box-modal .modal-body").html( message || "" );
112     } else {
113         $("#bootstrap-confirm-box-modal .modal-body").remove();
114     }
115     $("#bootstrap-confirm-box-modal-submit").text( yes_label || 'Confirm' );
116     $("#bootstrap-confirm-box-modal-cancel").text( no_label || 'Cancel' );
117     $("#bootstrap-confirm-box-modal").modal('show');
118 }
119
120 //Add jQuery :focusable selector
121 (function($) {
122     function visible(element) {
123         return $.expr.filters.visible(element) && !$(element).parents().addBack().filter(function() {
124             return $.css(this, 'visibility') === 'hidden';
125         }).length;
126     }
127
128     function focusable(element, isTabIndexNotNaN) {
129         var map, mapName, img, nodeName = element.nodeName.toLowerCase();
130         if ('area' === nodeName) {
131             map = element.parentNode;
132             mapName = map.name;
133             if (!element.href || !mapName || map.nodeName.toLowerCase() !== 'map') {
134                 return false;
135             }
136             img = $('img[usemap=#' + mapName + ']')[0];
137             return !!img && visible(img);
138         }
139         return (/input|select|textarea|button|object/.test(nodeName) ?
140                 !element.disabled :
141                 'a' === nodeName ?
142                 element.href || isTabIndexNotNaN :
143                 isTabIndexNotNaN) &&
144             // the element and all of its ancestors must be visible
145             visible(element);
146     }
147
148     $.extend($.expr[':'], {
149         focusable: function(element) {
150             return focusable(element, !isNaN($.attr(element, 'tabindex')));
151         }
152     });
153 })(jQuery);
154
155 enquire.register("screen and (max-width:608px)", {
156     match : function() {
157         if($("body.scrollto").length > 0){
158             window.scrollTo( 0, $(".maincontent").offset().top );
159         }
160     }
161 });
162
163 enquire.register("screen and (min-width:768px)", {
164     match : function() {
165         facetMenu( "show" );
166     },
167     unmatch : function() {
168         facetMenu( "hide" );
169     }
170 });
171
172 function facetMenu( action ){
173     if( action == "show" ){
174         $(".menu-collapse-toggle").off("click", facetHandler );
175         $(".menu-collapse").show();
176     } else {
177         $(".menu-collapse-toggle").on("click", facetHandler ).removeClass("menu-open");
178         $(".menu-collapse").hide();
179     }
180 }
181
182 var facetHandler = function(e){
183     e.preventDefault();
184     $(this).toggleClass("menu-open");
185     $(".menu-collapse").toggle();
186 };
187
188 $(document).ready(function(){
189     $("html").removeClass("no-js").addClass("js");
190     $(".close").click(function(){
191         window.close();
192     });
193     $(".focus").focus();
194     $(".js-show").show();
195     $(".js-hide").hide();
196
197     if( $(window).width() < 768 ){
198         facetMenu("hide");
199     }
200
201     // clear the basket when user logs out
202     $("#logout").click(function(){
203         var nameCookie = "bib_list";
204         var valCookie = readCookie(nameCookie);
205         if (valCookie) { // basket has contents
206             updateBasket(0,null);
207             delCookie(nameCookie);
208             return true;
209         } else {
210             return true;
211         }
212     });
213
214     $(".loginModal-trigger").on("click",function(e){
215         e.preventDefault();
216         $("#loginModal").modal("show");
217     });
218     $("#loginModal").on("shown.bs.modal", function(){
219         $("#muserid").focus();
220     });
221
222     $("#scrolltocontent").click(function() {
223         var content = $(".maincontent");
224         if (content.length > 0) {
225             $('html,body').animate({
226                 scrollTop: content.first().offset().top
227             },
228             'slow');
229             content.first().find(':focusable').eq(0).focus();
230         }
231     });
232 });
233
234 $.fn.modal.Constructor.prototype.enforceFocus = function () {};