Bug 26988: Add API route to fetch hold pickup locations and use it in the holds table
[koha.git] / koha-tmpl / intranet-tmpl / prog / js / calendar.js
1 /* global debug sentmsg __ dateformat_pref dateformat_string bidi calendarFirstDayOfWeek */
2 /* exported DateTime_from_syspref */
3 var MSG_PLEASE_ENTER_A_VALID_DATE = ( __("Please enter a valid date (should match %s).") );
4 if (debug > 1) {
5     alert("dateformat: " + dateformat_pref + "\ndebug is on (level " + debug + ")");
6 }
7
8 function is_valid_date(date) {
9     // An empty string is considered as a valid date for convenient reasons.
10     if (date === '') return 1;
11
12     var dateformat = dateformat_string;
13     if (dateformat == 'us') {
14         if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
15         dateformat = 'mm/dd/yy';
16     } else if (dateformat == 'metric') {
17         if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
18         dateformat = 'dd/mm/yy';
19     } else if (dateformat == 'iso') {
20         if (date.search(/^\d{4}-\d{2}-\d{2}($|\s)/) == -1) return 0;
21         dateformat = 'yy-mm-dd';
22     } else if (dateformat == 'dmydot') {
23         if (date.search(/^\d{2}\.\d{2}\.\d{4}($|\s)/) == -1) return 0;
24         dateformat = 'dd.mm.yy';
25     }
26     try {
27         $.datepicker.parseDate(dateformat, date);
28     } catch (e) {
29         return 0;
30     }
31     return 1;
32 }
33
34 function get_dateformat_str(dateformat) {
35     var dateformat_str;
36     if (dateformat == 'us') {
37         dateformat_str = 'mm/dd/yyyy';
38     } else if (dateformat == 'metric') {
39         dateformat_str = 'dd/mm/yyyy';
40     } else if (dateformat == 'iso') {
41         dateformat_str = 'yyyy-mm-dd';
42     } else if (dateformat == 'dmydot') {
43         dateformat_str = 'dd.mm.yyyy';
44     }
45     return dateformat_str;
46 }
47
48 function validate_date(dateText, inst) {
49     if (!is_valid_date(dateText)) {
50         var dateformat_str = get_dateformat_str( dateformat_pref );
51         alert(MSG_PLEASE_ENTER_A_VALID_DATE.format(dateformat_str));
52         $('#' + inst.id).val('');
53     }
54 }
55
56 function Date_from_syspref(dstring) {
57     var dateX = dstring.split(/[-/.]/);
58     if (debug > 1 && sentmsg < 1) {
59         sentmsg++;
60         alert("Date_from_syspref(" + dstring + ") splits to:\n" + dateX.join("\n"));
61     }
62     if (dateformat_pref === "iso") {
63         return new Date(dateX[0], (dateX[1] - 1), dateX[2]); // YYYY-MM-DD to (YYYY,m(0-11),d)
64     } else if (dateformat_pref === "us") {
65         return new Date(dateX[2], (dateX[0] - 1), dateX[1]); // MM/DD/YYYY to (YYYY,m(0-11),d)
66     } else if (dateformat_pref === "metric") {
67         return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD/MM/YYYY to (YYYY,m(0-11),d)
68     } else if (dateformat_pref === "dmydot") {
69         return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD.MM.YYYY to (YYYY,m(0-11),d)
70     } else {
71         if (debug > 0) {
72             alert("KOHA ERROR - Unrecognized date format: " + dateformat_pref);
73         }
74         return 0;
75     }
76 }
77
78 function DateTime_from_syspref(date_time) {
79     var parts = date_time.split(" ");
80     var date = parts[0];
81     var time = parts[1];
82     parts = time.split(":");
83     var hour = parts[0];
84     var minute = parts[1];
85
86     if (hour < 0 || hour > 23) {
87         return 0;
88     }
89     if (minute < 0 || minute > 59) {
90         return 0;
91     }
92
93     var datetime = Date_from_syspref(date);
94
95     if (isNaN(datetime.getTime())) {
96         return 0;
97     }
98
99     datetime.setHours(hour);
100     datetime.setMinutes(minute);
101
102     return datetime;
103 }
104
105 /* Instead of including multiple localization files as you would normally see with
106    jQueryUI we expose the localization strings in the default configuration */
107 jQuery(function ($) {
108     $.datepicker.regional[''] = {
109         closeText: __("Done"),
110         prevText: __("Prev"),
111         nextText: __("Next"),
112         currentText: __("Today"),
113         monthNames: [__("January"), __("February"), __("March"), __("April"), __("May"), __("June"),
114             __("July"), __("August"), __("September"), __("October"), __("November"), __("December")
115         ],
116         monthNamesShort: [__("Jan"), __("Feb"), __("Mar"), __("Apr"), __("May"), __("Jun"),
117             __("Jul"), __("Aug"), __("Sep"), __("Oct"), __("Nov"), __("Dec")
118         ],
119         dayNames: [__("Sunday"), __("Monday"), __("Tuesday"), __("Wednesday"), __("Thursday"), __("Friday"), __("Saturday")],
120         dayNamesShort: [__("Sun"), __("Mon"), __("Tue"), __("Wed"), __("Thu"), __("Fri"), __("Sat")],
121         dayNamesMin: [__("Su"), __("Mo"), __("Tu"), __("We"), __("Th"), __("Fr"), __("Sa")],
122         weekHeader: __("Wk"),
123         dateFormat: dateformat_string,
124         firstDay: calendarFirstDayOfWeek,
125         isRTL: bidi,
126         showMonthAfterYear: false,
127         yearSuffix: ''
128     };
129     $.datepicker.setDefaults($.datepicker.regional['']);
130 });
131
132 /*  jQuery Validator plugin custom method
133     This allows you to check that a given date falls after another.
134     It is required that a message be defined.
135
136    Syntax:
137        $("#form_id").validate({
138         rules: {
139             input_name_of_later_date_field: {
140                 is_date_after: "#input_id_of_earlier_date_field"
141             },
142         },
143         messages: {
144             input_name_of_later_date_field: {
145                 is_date_after: _("Validation error to be shown, i.e. End date must come after start date")
146             }
147         }
148     });
149 */
150
151 jQuery.validator.addMethod("is_date_after",
152     function (value, element, params) {
153         var from = Date_from_syspref($(params).val());
154         var to = Date_from_syspref(value);
155         return to > from;
156     });
157
158 jQuery.validator.addMethod("date_on_or_after",
159     function (value, element, params) {
160         var from = Date_from_syspref($(params).val());
161         var to = Date_from_syspref(value);
162         return to >= from;
163     });
164
165 $(document).ready(function () {
166
167     $.datepicker.setDefaults({
168         showOn: "both",
169         buttonImage: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAT0lEQVQ4jWNgoAZYd/LVf3IwigGkAuwGLE4hDg9eA4il8RqADVdtLYVjZLVEuwDZAKJcgKxh+zkyXIBuI8lhgG4jOqZdLJACMAygKDNRAgBj9qOB+rWnhAAAAABJRU5ErkJggg==",
170         buttonImageOnly: true,
171         buttonText: __("Select date"),
172         changeMonth: true,
173         changeYear: true,
174         showButtonPanel: true,
175         showOtherMonths: true,
176         selectOtherMonths: true,
177         yearRange: "c-100:c+10"
178     });
179
180     $("#dateofbirth").datepicker({
181         yearRange: "c-100:c"
182     });
183
184     $(".futuredate").datepicker({
185         minDate: 1, // require that hold suspended until date is after today
186     });
187
188     $(".datepicker").datepicker({
189         onClose: function (dateText, inst) {
190             validate_date(dateText, inst);
191         },
192     }).on("change", function () {
193         if (!is_valid_date($(this).val())) {
194             $(this).val("");
195         }
196     });
197     // http://jqueryui.com/demos/datepicker/#date-range
198     var dates = $(".datepickerfrom, .datepickerto").datepicker({
199         changeMonth: true,
200         numberOfMonths: 1,
201         onSelect: function (selectedDate) {
202             var option = this.id == "from" ? "minDate" : "maxDate",
203                 instance = $(this).data("datepicker");
204             var date = $.datepicker.parseDate(
205                 instance.settings.dateFormat ||
206                 $.datepicker._defaults.dateFormat,
207                 selectedDate, instance.settings);
208             dates.not(this).datepicker("option", option, date);
209         },
210         onClose: function (dateText, inst) {
211             validate_date(dateText, inst);
212         },
213     }).on("change", function () {
214         if (!is_valid_date($(this).val())) {
215             $(this).val("");
216         }
217     });
218 });