From: Sven Schöling Date: Fri, 23 Feb 2018 12:38:51 +0000 (+0100) Subject: Validator: time X-Git-Tag: release-3.5.4~454 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=eb4745651abf3b484577fd051624826bc16d2467;p=kivitendo-erp.git Validator: time --- diff --git a/js/kivi.Validator.js b/js/kivi.Validator.js index e48692749..bc19ee537 100644 --- a/js/kivi.Validator.js +++ b/js/kivi.Validator.js @@ -104,6 +104,27 @@ namespace("kivi.Validator", function(ns) { ns.annotate($e); return true; } + }, + time: function($e) { + var time_string = $e.val(); + + var parsed_time = kivi.parse_time(time_string); + if (parsed_time === null) { + $e.val(''); + ns.annotate($e); + return true; + } else + if (parsed_time === undefined) { + ns.annotate($e, kivi.t8('Wrong time format (#1)', [ kivi.myconfig.timeformat ])); + return false; + } else + { + var formatted_time = kivi.format_time(parsed_time); + if (formatted_time != time_string) + $e.val(formatted_time); + ns.annotate($e); + return true; + } } }; diff --git a/js/kivi.js b/js/kivi.js index f194b8107..7c1f00a43 100644 --- a/js/kivi.js +++ b/js/kivi.js @@ -8,6 +8,11 @@ namespace("kivi", function(ns) { m: 1, d: 0 }; + ns._time_format = { + sep: ':', + h: 0, + m: 1, + }; ns._number_format = { decimalSep: ',', thousandSep: '.' @@ -22,6 +27,13 @@ namespace("kivi", function(ns) { ns._date_format[res[4].substr(0, 1)] = 2; } + res = (params.times || "").match(/^([hm]+)([^a-z])([hm]+)$/); + if (res) { + ns._time_format = { sep: res[2] }; + ns._time_format[res[1].substr(0, 1)] = 0; + ns._time_format[res[3].substr(0, 1)] = 1; + } + res = (params.numbers || "").match(/^\d*([^\d]?)\d+([^\d])\d+$/); if (res) ns._number_format = { @@ -101,6 +113,47 @@ namespace("kivi", function(ns) { return parts.join(ns._date_format.sep); }; + ns.parse_time = function(time) { + var now = new Date(); + + if (time === undefined) + return undefined; + + if (time === '') + return null; + + if (time === '0') + return now; + + // special case 1: military time in fixed "hhmm" format + if (time.length == 4) { + var res = time.match(/(\d\d)(\d\d)/); + if (res) { + now.setHours(res[1], res[2]); + return now; + } else { + return undefined; + } + } + + var parts = time.replace(/\s+/g, "").split(ns._time_format.sep); + if (parts.length == 2) { + now.setHours(parts[ns._time_format.h], parts[ns._time_format.m]); + return now; + } else + return undefined; + } + + ns.format_time = function(date) { + if (isNaN(date.getTime())) + return undefined; + + var parts = [ "", "" ] + parts[ ns._time_format.h ] = date.getHours().toString().padStart(2, '0'); + parts[ ns._time_format.m ] = date.getMinutes().toString().padStart(2, '0'); + return parts.join(ns._time_format.sep); + }; + ns.parse_amount = function(amount) { if (amount === undefined) return undefined; diff --git a/js/locale/de.js b/js/locale/de.js index 9e254d70d..a72965920 100644 --- a/js/locale/de.js +++ b/js/locale/de.js @@ -134,6 +134,7 @@ namespace("kivi").setupLocale({ "Version actions":"Aktionen für Versionen", "Wrong date format (#1)":"Falsches Datumsformat (#1)", "Wrong number format (#1)":"Falsches Zahlenformat (#1)", +"Wrong time format (#1)":"Falsches Zeitformat (#1)", "Yes":"Ja", "filename has not uploadable characters ":"Bitte Dateinamen ändern. Er hat für den Upload nicht verwendbare Sonderzeichen ", "filesize too big: ":"Datei zu groß: ", diff --git a/js/t/kivi/parse_format_time.js b/js/t/kivi/parse_format_time.js new file mode 100644 index 000000000..901de6415 --- /dev/null +++ b/js/t/kivi/parse_format_time.js @@ -0,0 +1,29 @@ +function custom_time(h,m) { + var time = new Date(); + time.setHours(h,m); + return time; +} + +QUnit.test("kivi.parse_time function for German time style with colon", function( assert ) { + kivi.setup_formats({ times: "hh:mm" }); + + assert.deepEqual(kivi.parse_time("12:34"), custom_time(12,34)); + assert.deepEqual(kivi.parse_time("10:00"), custom_time(10,0)); + assert.deepEqual(kivi.parse_time(" 12 : 23 "), custom_time(12,23)); + + assert.deepEqual(kivi.parse_time("00:20"), custom_time(0,20)); + + assert.deepEqual(kivi.parse_time("23:60"), custom_time(23,60)); + + assert.deepEqual(kivi.parse_time("1142"), custom_time(11,42)); + + assert.deepEqual(kivi.parse_time("Totally Invalid!"), undefined); + assert.deepEqual(kivi.parse_time("."), undefined); + assert.deepEqual(kivi.parse_time(".."), undefined); + assert.deepEqual(kivi.parse_time(":"), custom_time(0,0)); + assert.deepEqual(kivi.parse_time("::"), undefined); + assert.deepEqual(kivi.parse_time(""), null); + assert.deepEqual(kivi.parse_time("0"), new Date()); + assert.deepEqual(kivi.parse_time("29:20008"), custom_time(29,20008)); +}); + diff --git a/locale/de/all b/locale/de/all index 52418cd24..b40f6199f 100755 --- a/locale/de/all +++ b/locale/de/all @@ -3819,6 +3819,7 @@ $self->{texts} = { 'Wrong number format (#1)' => 'Falsches Zahlenformat (#1)', 'Wrong tax keys recorded' => 'Gespeicherte Steuerschlüssel sind falsch', 'Wrong taxes recorded' => 'Gespeicherte Steuern passen nicht zum Steuerschlüssel', + 'Wrong time format (#1)' => 'Falsches Zeitformat (#1)', 'X' => 'X', 'YYYY' => 'JJJJ', 'Year' => 'Jahr', diff --git a/templates/webpages/layout/javascript_setup.js b/templates/webpages/layout/javascript_setup.js index 3481502d5..2b03f4a2d 100644 --- a/templates/webpages/layout/javascript_setup.js +++ b/templates/webpages/layout/javascript_setup.js @@ -16,7 +16,8 @@ $(function() { kivi.setup_formats({ numbers: kivi.myconfig.numberformat, - dates: kivi.myconfig.dateformat + dates: kivi.myconfig.dateformat, + times: kivi.myconfig.timeformat }); kivi.reinit_widgets();