Normalize free-form date and time fields before submitting, and warn user if there...
authorJan Schneider <jan@horde.org>
Wed, 20 Jan 2010 19:30:23 +0000 (20:30 +0100)
committerJan Schneider <jan@horde.org>
Wed, 20 Jan 2010 22:32:07 +0000 (23:32 +0100)
kronolith/js/date.js
kronolith/js/kronolith.js
kronolith/lib/Kronolith.php

index 72bca4c..9e88197 100644 (file)
     var _ = $D.Parsing.Operators, g = $D.Grammar, t = $D.Translator, _fn;
 
     g.datePartDelimiter = _.rtoken(/^([\s\-\.\,\/\x27]+)/);
-    g.timePartDelimiter = _.stoken(":");
+    g.timePartDelimiter = _.rtoken(/^([\.:])/);
     g.whiteSpace = _.rtoken(/^\s*/);
     g.generalDelimiter = _.rtoken(/^(([\s\,]|at|@|on)+)/);
 
index 49b90be..ba82f8b 100644 (file)
@@ -30,6 +30,7 @@ KronolithCore = {
     tasktype: 'incomplete',
     growls: 0,
     alarms: [],
+    wrongFormat: $H(),
     mapMarker: null,
     map: null,
     mapInitialized: false,
@@ -1945,6 +1946,11 @@ KronolithCore = {
      */
     saveTask: function()
     {
+        if (this.wrongFormat.size()) {
+            this.showNotifications([{ type: 'horde.warning', message: Kronolith.text.fix_form_values }]);
+            return;
+        }
+
         var tasklist = $F('kronolithTaskList'),
             taskid = $F('kronolithTaskId');
         this.startLoading('tasks:' + ($F('kronolithTaskCompleted') ? 'complete' : 'incomplete') + tasklist, this.tasktype);
@@ -3229,6 +3235,11 @@ KronolithCore = {
      */
     saveEvent: function()
     {
+        if (this.wrongFormat.size()) {
+            this.showNotifications([{ type: 'horde.warning', message: Kronolith.text.fix_form_values }]);
+            return;
+        }
+
         var cal = $F('kronolithEventTarget'),
             eventid = $F('kronolithEventId'),
             viewDates = this.viewDates(this.date, this.view),
@@ -3529,6 +3540,34 @@ KronolithCore = {
         }
     },
 
+    checkDate: function(e) {
+        var elm = e.element();
+        if ($F(elm)) {
+            var date = Date.parseExact($F(elm), Kronolith.conf.date_format) || Date.parse($F(elm));
+            if (date) {
+                elm.setValue(date.toString(Kronolith.conf.date_format));
+                this.wrongFormat.unset(elm.id);
+            } else {
+                this.showNotifications([{ type: 'horde.warning', message: Kronolith.text.wrong_date_format.interpolate({ wrong: $F(elm), right: new Date().toString(Kronolith.conf.date_format) }) }]);
+                this.wrongFormat.set(elm.id, true);
+            }
+        }
+    },
+
+    checkTime: function(e) {
+        var elm = e.element();
+        if ($F(elm)) {
+            var time = Date.parseExact(new Date().toString(Kronolith.conf.date_format) + ' ' + $F(elm), Kronolith.conf.date_format + ' ' + Kronolith.conf.time_format) || Date.parse(new Date().toString('yyyy-MM-dd ') + $F(elm));
+            if (time) {
+                elm.setValue(time.toString(Kronolith.conf.time_format));
+                this.wrongFormat.unset(elm.id);
+            } else {
+                this.showNotifications([{ type: 'horde.warning', message: Kronolith.text.wrong_time_format.interpolate({ wrong: $F(elm), right: new Date().toString(Kronolith.conf.time_format) }) }]);
+                this.wrongFormat.set(elm.id, true);
+            }
+        }
+    },
+
     _closeRedBox: function()
     {
         var content = RedBox.getWindowContents();
@@ -3724,6 +3763,13 @@ KronolithCore = {
             }
         });
 
+        $('kronolithEventStartDate').observe('blur', this.checkDate.bind(this));
+        $('kronolithEventEndDate').observe('blur', this.checkDate.bind(this));
+        $('kronolithTaskDueDate').observe('blur', this.checkDate.bind(this));
+        $('kronolithEventStartTime').observe('blur', this.checkTime.bind(this));
+        $('kronolithEventEndTime').observe('blur', this.checkTime.bind(this));
+        $('kronolithTaskDueTime').observe('blur', this.checkTime.bind(this));
+
         // Mouse wheel handler.
         [ 'kronolithEventStartDate', 'kronolithEventEndDate' ].each(function(field) {
             $(field).observe(Prototype.Browser.Gecko ? 'DOMMouseScroll' : 'mousewheel', function(e) {
index 7c69420..d7393b2 100644 (file)
@@ -313,6 +313,9 @@ class Kronolith
             'no_url' => _("You must specify a URL."),
             'wrong_auth' => _("The authentication information you specified wasn't accepted."),
             'geocode_error' => _("Unable to locate requested address"),
+            'wrong_date_format' => sprintf(_("You used an unknown date format \"%s\". Please try something like \"%s\"."), '#{wrong}', '#{right}'),
+            'wrong_time_format' => sprintf(_("You used an unknown time format \"%s\". Please try something like \"%s\"."), '#{wrong}', '#{right}'),
+            'fix_form_values' => _("Please enter correct values in the form first."),
         );
         for ($i = 1; $i <= 12; ++$i) {
             $code['text']['month'][$i - 1] = Horde_Nls::getLangInfo(constant('MON_' . $i));