Bug 21565: (follow-up) Make confirmation buttons more detailed
[koha.git] / koha-tmpl / opac-tmpl / bootstrap / js / global.js
1 (function( w ){
2     // if the class is already set, the font has already been loaded
3     if( w.document.documentElement.className.indexOf( "fonts-loaded" ) > -1 ){
4         return;
5     }
6     var PrimaryFont = new w.FontFaceObserver( "NotoSans", {
7         weight: 400
8     });
9
10     PrimaryFont.load(null, 5000).then(function(){
11         w.document.documentElement.className += " fonts-loaded";
12     }, function(){
13         console.log("Failed");
14     });
15 }( this ));
16
17 // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery/5341855#5341855
18 String.prototype.format = function() { return formatstr(this, arguments) }
19 function formatstr(str, col) {
20     col = typeof col === 'object' ? col : Array.prototype.slice.call(arguments, 1);
21     var idx = 0;
22     return str.replace(/%%|%s|%(\d+)\$s/g, function (m, n) {
23         if (m == "%%") { return "%"; }
24         if (m == "%s") { return col[idx++]; }
25         return col[n];
26     });
27 };
28
29 function confirmDelete(message) {
30     return (confirm(message) ? true : false);
31 }
32
33 function Dopop(link) {
34     newin=window.open(link,'popup','width=500,height=400,toolbar=false,scrollbars=yes,resizeable=yes');
35 }
36
37 jQuery.fn.preventDoubleFormSubmit = function() {
38     jQuery(this).submit(function() {
39         if (this.beenSubmitted)
40             return false;
41         else
42             this.beenSubmitted = true;
43     });
44 };
45
46 function prefixOf (s, tok) {
47     var index = s.indexOf(tok);
48     return s.substring(0, index);
49 }
50 function suffixOf (s, tok) {
51     var index = s.indexOf(tok);
52     return s.substring(index + 1);
53 }
54
55 // Adapted from https://gist.github.com/jnormore/7418776
56 function confirmModal(message, title, yes_label, no_label, callback) {
57     $("#bootstrap-confirm-box-modal").data('confirm-yes', false);
58     if($("#bootstrap-confirm-box-modal").length == 0) {
59         $("body").append('<div id="bootstrap-confirm-box-modal" class="modal">\
60             <div class="modal-dialog">\
61                 <div class="modal-content">\
62                     <div class="modal-header" style="min-height:40px;">\
63                         <button type="button" class="closebtn" data-dismiss="modal" aria-hidden="true">&times;</button>\
64                         <h4 class="modal-title"></h4>\
65                     </div>\
66                     <div class="modal-body"><p></p></div>\
67                     <div class="modal-footer">\
68                         <a href="#" id="bootstrap-confirm-box-modal-submit" class="btn btn-danger"><i class="fa fa-check"></i></a>\
69                         <a href="#" id="bootstrap-confirm-box-modal-cancel" data-dismiss="modal" class="btn btn-default"><i class="fa fa-remove"></i></a>\
70                     </div>\
71                 </div>\
72             </div>\
73         </div>');
74         $("#bootstrap-confirm-box-modal-submit").on('click', function () {
75             $("#bootstrap-confirm-box-modal").data('confirm-yes', true);
76             $("#bootstrap-confirm-box-modal").modal('hide');
77             return false;
78         });
79         $("#bootstrap-confirm-box-modal").on('hide.bs.modal', function () {
80             if(callback) callback($("#bootstrap-confirm-box-modal").data('confirm-yes'));
81         });
82     }
83
84     $("#bootstrap-confirm-box-modal .modal-header h4").text( title || "" );
85     if( message && message != "" ){
86         $("#bootstrap-confirm-box-modal .modal-body").html( message || "" );
87     } else {
88         $("#bootstrap-confirm-box-modal .modal-body").remove();
89     }
90     $("#bootstrap-confirm-box-modal-submit").text( yes_label || 'Confirm' );
91     $("#bootstrap-confirm-box-modal-cancel").text( no_label || 'Cancel' );
92     $("#bootstrap-confirm-box-modal").modal('show');
93 }