Bug 29408: Add JSDoc documentation for kohaTable function
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / onboarding.js
1 function _(s) { return s; } // dummy function for gettext
2
3 // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery/5341855#5341855
4 String.prototype.format = function() { return formatstr(this, arguments); };
5 function formatstr(str, col) {
6     col = typeof col === 'object' ? col : Array.prototype.slice.call(arguments, 1);
7     var idx = 0;
8     return str.replace(/%%|%s|%(\d+)\$s/g, function (m, n) {
9         if (m == "%%") { return "%"; }
10         if (m == "%s") { return col[idx++]; }
11         return col[n];
12     });
13 }
14
15 jQuery.validator.addMethod( "category_code_check", function(value,element){
16     var patt = /^[A-Za-z0-9]{0,10}$/g;
17     if (patt.test(element.value)) {
18         return true;
19     } else {
20         return false;
21     }
22     }, MSG_LETTERS_NUMBERS_ONLY
23 );
24
25 jQuery.validator.addMethod( "enrollment_period", function(){
26       enrolmentperiod = $("#enrolmentperiod").val();
27       enrolmentperioddate = $("#enrolmentperioddate").val();
28       if (( $("#enrolmentperiod").val() === "" && $("#enrolmentperioddate").val() === "") || ($("#enrolmentperiod").val() !== "" && $("#enrolmentperioddate").val() !== "")) {
29              return false;
30       } else {
31              return true;
32       }
33     }, MSG_ONLY_ONE_ENROLLMENTPERIOD
34 );
35
36 jQuery.validator.addMethod( "password_match", function(value,element){
37         var MSG_PASSWORD_MISMATCH = ( MSG_PASSWORD_MISMATCH );
38         var password = document.getElementById('password').value;
39         var confirmpassword = document.getElementById('password2').value;
40
41         if ( password != confirmpassword ){
42                return false;
43           }
44           else{
45                return true;
46           }
47     },  MSG_PASSWORD_MISMATCH
48 );
49
50 function toUC(f) {
51     var x=f.value.toUpperCase();
52     f.value=x;
53     return true;
54 }
55
56 $(document).ready(function() {
57     if ($("#branches option:selected").length < 1) {
58         $("#branches option:first").attr("selected", "selected");
59     }
60     $("#categorycode").on("blur",function(){
61          toUC(this);
62     });
63
64     $("#enrolmentperioddate").flatpickr({
65         /* Default Flatpickr configuration uses Font Awesome icons for arrows. Onboarding doesn't include Font Awesome, so we redefine these arrows with some SVG icons */
66         nextArrow: '<svg width="17" height="17" viewBox="0 0 4.498 4.498" xmlns="http://www.w3.org/2000/svg"><path d="M3.761 2.491c.158.17.47.12.566-.09.085-.158.02-.356-.116-.461L2.562.292C2.445.17 2.3.282 2.217.382c-.087.102-.255.193-.195.35.107.149.254.265.378.399l1.361 1.36zm.496.017c.17-.157.12-.47-.091-.566-.158-.085-.355-.02-.46.117L2.057 3.707c-.12.118-.01.263.091.345.101.087.193.255.35.195.148-.106.264-.254.398-.378l1.36-1.36zm-.746.095c.206.006.457-.124.462-.353-.005-.23-.256-.36-.462-.354-1.054 0-2.109-.002-3.163 0-.174-.003-.234.166-.212.308.01.128-.043.3.104.369.157.056.33.02.493.03h2.778z"/></svg>',
67         prevArrow: '<svg width="17" height="17" viewBox="0 0 4.498 4.498" xmlns="http://www.w3.org/2000/svg"><path d="M.737 2.008c-.158-.17-.47-.12-.566.09-.085.158-.02.356.116.461l1.649 1.648c.117.121.263.01.345-.09.087-.102.255-.193.195-.35-.107-.149-.254-.265-.378-.399L.737 2.008zM.241 1.99c-.17.157-.12.47.091.566.158.085.355.02.46-.117L2.441.792C2.56.674 2.45.53 2.35.447 2.249.36 2.157.192 2 .252c-.148.106-.264.254-.398.378L.242 1.99zm.746-.095C.781 1.89.53 2.02.525 2.249c.005.23.256.36.462.354 1.054 0 2.109.002 3.163 0 .174.003.234-.166.212-.308-.01-.128.043-.3-.104-.369-.157-.056-.33-.02-.493-.03H.987z"/></svg>',
68         minDate: new Date().fp_incr(1),
69         onReady: function( selectedDates, dateStr, instance ){
70             $(instance.input)
71                 /* Add a wrapper element so that we can prevent the clear button from wrapping */
72                 .wrap("<span class='flatpickr_wrapper'></span>")
73                 .after( $("<a/>")
74                     .attr("href","#")
75                     .addClass("clear_date")
76                     .on("click", function(e){
77                         e.preventDefault();
78                         instance.clear();
79                     })
80                     .attr("aria-hidden", true)
81                 );
82         }
83     }); // Require that "until date" be in the future
84
85     $("#category_form").validate({
86         rules: {
87             categorycode: {
88                 required: true,
89                 category_code_check: true
90             },
91             description: {
92                     required:true
93             },
94             enrolmentperiod: {
95                required: function(element){
96                      return $("#enrolmentperioddate").val() === "";
97                },
98                digits: true,
99                enrollment_period: true,
100             },
101             enrolmentperioddate: {
102                 required: function(element){
103                     return $("#enrolmentperiod").val() === "";
104                 },
105                 enrollment_period: true,
106                 // is_valid_date ($(#"enrolementperioddate").val());
107             },
108             dateofbirthrequired: {
109                 digits: true
110             },
111             upperagelimit: {
112                 digits: true
113             },
114             enrolmentfee: {
115                 number: true
116             },
117             reservefee: {
118                 number: true
119             },
120             category_type: {
121                 required: true
122             }
123         },
124         messages: {
125             enrolmentperiod: {
126                 required: MSG_ONE_ENROLLMENTPERIOD
127             },
128             enrolmentperioddate: {
129                 required: MSG_ONE_ENROLLMENTPERIOD
130             }
131         }
132     });
133
134     $("#createpatron").validate({
135         rules: {
136             surname: {
137                 required: true
138             },
139             firstname: {
140                 required: true
141             },
142             cardnumber: {
143                 required: true
144             },
145             password: {
146                 required: true,
147                 password_strong: true,
148                 password_no_spaces: true
149             },
150             password2: {
151                 required: true,
152                 password_match: true
153             }
154         },
155         messages: {
156             password: {
157                 required: MSG_PASSWORD_MISMATCH
158             },
159         }
160
161     });
162
163     $("#createitemform").validate();
164     $("#createcirculationrule").validate();
165 });