|
Lines 1-74
Link Here
|
| 1 |
/* global debug sentmsg __ dateformat_pref flatpickr_dateformat_string bidi calendarFirstDayOfWeek */ |
1 |
/* global debug sentmsg __ dateformat_pref flatpickr_dateformat_string */ |
| 2 |
/* exported DateTime_from_syspref flatpickr_weekdays flatpickr_months */ |
2 |
/* exported DateTime_from_syspref flatpickr_weekdays flatpickr_months validate_date */ |
| 3 |
var MSG_PLEASE_ENTER_A_VALID_DATE = ( __("Please enter a valid date (should match %s).") ); |
3 |
var MSG_PLEASE_ENTER_A_VALID_DATE = ( __("Please enter a valid date (should match %s).") ); |
| 4 |
if (debug > 1) { |
4 |
if (debug > 1) { |
| 5 |
alert("dateformat: " + dateformat_pref + "\ndebug is on (level " + debug + ")"); |
5 |
alert("dateformat: " + dateformat_pref + "\ndebug is on (level " + debug + ")"); |
| 6 |
} |
6 |
} |
| 7 |
|
7 |
|
| 8 |
function is_valid_date(date) { |
8 |
function is_valid_date( date, dateformat_string ) { |
| 9 |
// An empty string is considered as a valid date for convenient reasons. |
9 |
// An empty string is considered as a valid date for convenient reasons. |
| 10 |
if (date === '') return 1; |
10 |
if (date === '') return 1; |
| 11 |
var dateformat = flatpickr_dateformat_string; |
11 |
let dateformat; |
| 12 |
if (dateformat == 'us') { |
12 |
const ourformat = dateformat_string === undefined ? flatpickr_dateformat_string : dateformat_string; |
|
|
13 |
/* Check that the date string matches the general date format rules */ |
| 14 |
if (ourformat == 'm/d/Y') { |
| 15 |
dateformat = "us"; |
| 13 |
if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0; |
16 |
if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0; |
| 14 |
dateformat = 'm/d/Y'; |
17 |
} else if (ourformat == 'd/m/Y') { |
| 15 |
} else if (dateformat == 'metric') { |
18 |
dateformat = "metric"; |
| 16 |
if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0; |
19 |
if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0; |
| 17 |
dateformat = 'd/m/Y'; |
20 |
} else if (ourformat == 'd.m.Y') { |
| 18 |
} else if (dateformat == 'iso') { |
21 |
dateformat = "dmydot"; |
| 19 |
if (date.search(/^\d{4}-\d{2}-\d{2}($|\s)/) == -1) return 0; |
|
|
| 20 |
dateformat = 'Y-m-d'; |
| 21 |
} else if (dateformat == 'dmydot') { |
| 22 |
if (date.search(/^\d{2}\.\d{2}\.\d{4}($|\s)/) == -1) return 0; |
22 |
if (date.search(/^\d{2}\.\d{2}\.\d{4}($|\s)/) == -1) return 0; |
| 23 |
dateformat = 'd.m.Y'; |
23 |
} else if (ourformat == 'Y-m-d') { |
| 24 |
} |
24 |
dateformat = "iso"; |
| 25 |
try { |
25 |
if (date.search(/^\d{4}-\d{2}-\d{2}($|\s)/) == -1) return 0; |
| 26 |
flatpickr.parseDate(date, dateformat); |
|
|
| 27 |
} catch (e) { |
| 28 |
return 0; |
| 29 |
} |
26 |
} |
| 30 |
return 1; |
27 |
/* Check that the date is a valid date */ |
|
|
28 |
const dateobj = Date_from_syspref(date, dateformat); |
| 29 |
return ( dateobj instanceof Date && !isNaN( dateobj ) ) ? 1 : 0; |
| 31 |
} |
30 |
} |
| 32 |
|
31 |
|
| 33 |
function get_dateformat_str(dateformat) { |
32 |
function get_dateformat_str(dateformat) { |
| 34 |
var dateformat_str; |
33 |
var dateformat_str; |
| 35 |
if (dateformat == 'us') { |
34 |
if (dateformat == 'us') { |
| 36 |
dateformat_str = 'mm/dd/yyyy'; |
35 |
dateformat_str = __("mm/dd/yyyy"); |
| 37 |
} else if (dateformat == 'metric') { |
36 |
} else if (dateformat == 'metric') { |
| 38 |
dateformat_str = 'dd/mm/yyyy'; |
37 |
dateformat_str = __("dd/mm/yyyy"); |
| 39 |
} else if (dateformat == 'iso') { |
38 |
} else if (dateformat == 'iso') { |
| 40 |
dateformat_str = 'yyyy-mm-dd'; |
39 |
dateformat_str = __("yyyy-mm-dd"); |
| 41 |
} else if (dateformat == 'dmydot') { |
40 |
} else if (dateformat == 'dmydot') { |
| 42 |
dateformat_str = 'dd.mm.yyyy'; |
41 |
dateformat_str = __("dd.mm.yyyy"); |
| 43 |
} |
42 |
} |
| 44 |
return dateformat_str; |
43 |
return dateformat_str; |
| 45 |
} |
44 |
} |
| 46 |
|
45 |
|
| 47 |
function validate_date(dateText, inst) { |
46 |
function validate_date(dateText, inst) { |
| 48 |
if (!is_valid_date(dateText)) { |
47 |
if (!is_valid_date( dateText, inst.config.dateFormat )) { |
| 49 |
var dateformat_str = get_dateformat_str( dateformat_pref ); |
48 |
var dateformat_str = get_dateformat_str( dateformat_pref ); |
| 50 |
alert(MSG_PLEASE_ENTER_A_VALID_DATE.format(dateformat_str)); |
49 |
alert(MSG_PLEASE_ENTER_A_VALID_DATE.format(dateformat_str)); |
| 51 |
inst.clear(); |
50 |
inst.clear(); |
| 52 |
} |
51 |
} |
| 53 |
} |
52 |
} |
| 54 |
|
53 |
|
| 55 |
function Date_from_syspref(dstring) { |
54 |
function Date_from_syspref( dstring, dateformat = dateformat_pref ) { |
| 56 |
var dateX = dstring.split(/[-/.]/); |
55 |
var dateX = dstring.split(/[-/.]/); |
| 57 |
if (debug > 1 && sentmsg < 1) { |
56 |
if (debug > 1 && sentmsg < 1) { |
| 58 |
sentmsg++; |
57 |
sentmsg++; |
| 59 |
alert("Date_from_syspref(" + dstring + ") splits to:\n" + dateX.join("\n")); |
58 |
alert("Date_from_syspref(" + dstring + ") splits to:\n" + dateX.join("\n")); |
| 60 |
} |
59 |
} |
| 61 |
if (dateformat_pref === "iso") { |
60 |
if (dateformat === "iso") { |
| 62 |
return new Date(dateX[0], (dateX[1] - 1), dateX[2]); // YYYY-MM-DD to (YYYY,m(0-11),d) |
61 |
return new Date(dateX[0], (dateX[1] - 1), dateX[2]); // YYYY-MM-DD to (YYYY,m(0-11),d) |
| 63 |
} else if (dateformat_pref === "us") { |
62 |
} else if (dateformat === "us") { |
| 64 |
return new Date(dateX[2], (dateX[0] - 1), dateX[1]); // MM/DD/YYYY to (YYYY,m(0-11),d) |
63 |
return new Date(dateX[2], (dateX[0] - 1), dateX[1]); // MM/DD/YYYY to (YYYY,m(0-11),d) |
| 65 |
} else if (dateformat_pref === "metric") { |
64 |
} else if (dateformat === "metric") { |
| 66 |
return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD/MM/YYYY to (YYYY,m(0-11),d) |
65 |
return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD/MM/YYYY to (YYYY,m(0-11),d) |
| 67 |
} else if (dateformat_pref === "dmydot") { |
66 |
} else if (dateformat === "dmydot") { |
| 68 |
return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD.MM.YYYY to (YYYY,m(0-11),d) |
67 |
return new Date(dateX[2], (dateX[1] - 1), dateX[0]); // DD.MM.YYYY to (YYYY,m(0-11),d) |
| 69 |
} else { |
68 |
} else { |
| 70 |
if (debug > 0) { |
69 |
if (debug > 0) { |
| 71 |
alert("KOHA ERROR - Unrecognized date format: " + dateformat_pref); |
70 |
alert("KOHA ERROR - Unrecognized date format: " + dateformat); |
| 72 |
} |
71 |
} |
| 73 |
return 0; |
72 |
return 0; |
| 74 |
} |
73 |
} |