Bug 30514: Error in date format check following datepicker removal
[koha-ffzg.git] / koha-tmpl / intranet-tmpl / prog / js / calendar.js
index 2b335ec..81561cc 100644 (file)
@@ -1,5 +1,5 @@
-/* global debug sentmsg __ dateformat_pref dateformat_string bidi calendarFirstDayOfWeek */
-/* exported DateTime_from_syspref */
+/* global debug sentmsg __ dateformat_pref flatpickr_dateformat_string bidi calendarFirstDayOfWeek */
+/* exported DateTime_from_syspref flatpickr_weekdays flatpickr_months */
 var MSG_PLEASE_ENTER_A_VALID_DATE = ( __("Please enter a valid date (should match %s).") );
 if (debug > 1) {
     alert("dateformat: " + dateformat_pref + "\ndebug is on (level " + debug + ")");
@@ -8,23 +8,22 @@ if (debug > 1) {
 function is_valid_date(date) {
     // An empty string is considered as a valid date for convenient reasons.
     if (date === '') return 1;
-
-    var dateformat = dateformat_string;
+    var dateformat = flatpickr_dateformat_string;
     if (dateformat == 'us') {
         if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
-        dateformat = 'mm/dd/yy';
+        dateformat = 'm/d/Y';
     } else if (dateformat == 'metric') {
         if (date.search(/^\d{2}\/\d{2}\/\d{4}($|\s)/) == -1) return 0;
-        dateformat = 'dd/mm/yy';
+        dateformat = 'd/m/Y';
     } else if (dateformat == 'iso') {
         if (date.search(/^\d{4}-\d{2}-\d{2}($|\s)/) == -1) return 0;
-        dateformat = 'yy-mm-dd';
+        dateformat = 'Y-m-d';
     } else if (dateformat == 'dmydot') {
         if (date.search(/^\d{2}\.\d{2}\.\d{4}($|\s)/) == -1) return 0;
-        dateformat = 'dd.mm.yy';
+        dateformat = 'd.m.Y';
     }
     try {
-        $.datepicker.parseDate(dateformat, date);
+        flatpickr.parseDate(date, dateformat);
     } catch (e) {
         return 0;
     }
@@ -49,7 +48,7 @@ function validate_date(dateText, inst) {
     if (!is_valid_date(dateText)) {
         var dateformat_str = get_dateformat_str( dateformat_pref );
         alert(MSG_PLEASE_ENTER_A_VALID_DATE.format(dateformat_str));
-        $('#' + inst.id).val('');
+        inst.clear();
     }
 }
 
@@ -102,33 +101,6 @@ function DateTime_from_syspref(date_time) {
     return datetime;
 }
 
-/* Instead of including multiple localization files as you would normally see with
-   jQueryUI we expose the localization strings in the default configuration */
-jQuery(function ($) {
-    $.datepicker.regional[''] = {
-        closeText: __("Done"),
-        prevText: __("Prev"),
-        nextText: __("Next"),
-        currentText: __("Today"),
-        monthNames: [__("January"), __("February"), __("March"), __("April"), __("May"), __("June"),
-            __("July"), __("August"), __("September"), __("October"), __("November"), __("December")
-        ],
-        monthNamesShort: [__("Jan"), __("Feb"), __("Mar"), __("Apr"), __("May"), __("Jun"),
-            __("Jul"), __("Aug"), __("Sep"), __("Oct"), __("Nov"), __("Dec")
-        ],
-        dayNames: [__("Sunday"), __("Monday"), __("Tuesday"), __("Wednesday"), __("Thursday"), __("Friday"), __("Saturday")],
-        dayNamesShort: [__("Sun"), __("Mon"), __("Tue"), __("Wed"), __("Thu"), __("Fri"), __("Sat")],
-        dayNamesMin: [__("Su"), __("Mo"), __("Tu"), __("We"), __("Th"), __("Fr"), __("Sa")],
-        weekHeader: __("Wk"),
-        dateFormat: dateformat_string,
-        firstDay: calendarFirstDayOfWeek,
-        isRTL: bidi,
-        showMonthAfterYear: false,
-        yearSuffix: ''
-    };
-    $.datepicker.setDefaults($.datepicker.regional['']);
-});
-
 /*  jQuery Validator plugin custom method
     This allows you to check that a given date falls after another.
     It is required that a message be defined.
@@ -162,59 +134,12 @@ jQuery.validator.addMethod("date_on_or_after",
         return to >= from;
     });
 
-$(document).ready(function () {
-
-    $.datepicker.setDefaults({
-        showOn: "both",
-        buttonImage: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAT0lEQVQ4jWNgoAZYd/LVf3IwigGkAuwGLE4hDg9eA4il8RqADVdtLYVjZLVEuwDZAKJcgKxh+zkyXIBuI8lhgG4jOqZdLJACMAygKDNRAgBj9qOB+rWnhAAAAABJRU5ErkJggg==",
-        buttonImageOnly: true,
-        buttonText: __("Select date"),
-        changeMonth: true,
-        changeYear: true,
-        showButtonPanel: true,
-        showOtherMonths: true,
-        selectOtherMonths: true,
-        yearRange: "c-100:c+10"
-    });
-
-    $("#dateofbirth").datepicker({
-        yearRange: "c-100:c"
-    });
-
-    $(".futuredate").datepicker({
-        minDate: 1, // require that hold suspended until date is after today
-    });
+var flatpickr_weekdays = {
+    shorthand: [ __("Sun"), __("Mon"), __("Tue"), __("Wed"), __("Thu"), __("Fri"), __("Sat")],
+    longhand: [ __("Sunday"), __("Monday"), __("Tuesday"), __("Wednesday"), __("Thursday"), __("Friday"), __("Saturday") ]
+};
 
-    $(".datepicker").datepicker({
-        onClose: function (dateText, inst) {
-            validate_date(dateText, inst);
-        },
-    }).on("change", function () {
-        if (!is_valid_date($(this).val())) {
-            $(this).val("");
-        } else {
-            $(this).datepicker("setDate",$(this).val());
-        }
-    });
-    // http://jqueryui.com/demos/datepicker/#date-range
-    var dates = $(".datepickerfrom, .datepickerto").datepicker({
-        changeMonth: true,
-        numberOfMonths: 1,
-        onSelect: function (selectedDate) {
-            var option = this.id == "from" ? "minDate" : "maxDate",
-                instance = $(this).data("datepicker");
-            var date = $.datepicker.parseDate(
-                instance.settings.dateFormat ||
-                $.datepicker._defaults.dateFormat,
-                selectedDate, instance.settings);
-            dates.not(this).datepicker("option", option, date);
-        },
-        onClose: function (dateText, inst) {
-            validate_date(dateText, inst);
-        },
-    }).on("change", function () {
-        if (!is_valid_date($(this).val())) {
-            $(this).val("");
-        }
-    });
-});
+var flatpickr_months = {
+    shorthand: [ __("Jan"), __("Feb"), __("Mar"), __("Apr"), __("May"), __("Jun"), __("Jul"), __("Aug"), __("Sep"), __("Oct"), __("Nov"), __("Dec")],
+    longhand: [ __("January"), __("February"), __("March"), __("April"), __("May"), __("June"), __("July"), __("August"), __("September"), __("October"), __("November"), __("December")]
+};