changeset 4ab5bf57a2d1 in sao:5.0
details: https://hg.tryton.org/sao?cmd=changeset;node=4ab5bf57a2d1
description:
        Use ISO string format for timeseries graph

        c3 does not support moment but only string with a specific format.
        We implement .toString on the moment prototype because form widgets 
does not
        use Sao.Date nor Sao.DateTime. This ensures that calling .toString will 
always
        return the ISO format.
        And finally the x field type is retrieved from the model instead of the
        attribute which is no more filled since changeset 2fc36d9e359b

        issue9539
        review296221002
        (grafted from e6572d4652bed2e83dd9704da57dd983f03a9549)
diffstat:

 src/sao.js        |  31 +++++++++++++++++++++----------
 src/view/graph.js |  10 +++++++---
 tests/sao.js      |  21 +++++++++++++++++++++
 3 files changed, 49 insertions(+), 13 deletions(-)

diffs (131 lines):

diff -r 9b2af81410f7 -r 4ab5bf57a2d1 src/sao.js
--- a/src/sao.js        Thu Sep 24 22:43:50 2020 +0200
+++ b/src/sao.js        Sun Sep 27 12:29:38 2020 +0200
@@ -160,6 +160,27 @@
 
     Sao.Decimal = Number;
 
+    var _moment_to_string = moment.prototype.toString;
+    moment.prototype.toString = function() {
+        if (this.isDate) {
+            return this.format('YYYY-MM-DD');
+        } else if (this.isDateTime) {
+            if (this.milliseconds()) {
+                return this.format('YYYY-MM-DD HH:mm:ss.SSSSSS');
+            } else {
+                return this.format('YYYY-MM-DD HH:mm:ss');
+            }
+        } else if (this.isTime) {
+            if (this.milliseconds()) {
+                return this.format('HH:mm:ss.SSSSSS');
+            } else {
+                return this.format('HH:mm:ss');
+            }
+        } else {
+            return _moment_to_string.call(this);
+        }
+    };
+
     Sao.Date = function(year, month, day) {
         var date;
         if (month === undefined) {
@@ -174,9 +195,6 @@
         date.date(day);
         date.set({hour: 0, minute: 0, second: 0, millisecond: 0});
         date.isDate = true;
-        date.toString = function() {
-            return this.format('YYYY-MM-DD');
-        };
         return date;
     };
 
@@ -223,13 +241,6 @@
             datetime.milliseconds(millisecond);
         }
         datetime.isDateTime = true;
-        datetime.toString = function() {
-            if (this.milliseconds()) {
-                return this.format('YYYY-MM-DD HH:mm:ss.SSSSSS');
-            } else {
-                return this.format('YYYY-MM-DD HH:mm:ss');
-            }
-        };
         datetime.local();
         return datetime;
     };
diff -r 9b2af81410f7 -r 4ab5bf57a2d1 src/view/graph.js
--- a/src/view/graph.js Thu Sep 24 22:43:50 2020 +0200
+++ b/src/view/graph.js Sun Sep 27 12:29:38 2020 +0200
@@ -112,7 +112,7 @@
                     var x = record.field_get_client(this.xfield.name);
                     // c3 does not support moment
                     if (x && (x.isDate || x.isDateTime)) {
-                        x = x.toDate();
+                        x = x.toString();
                     }
                     data.columns[0][index + 1] = x;
                     this._add_id(x, record.id);
@@ -191,18 +191,21 @@
             c3_config.data.x = 'labels';
             c3_config.data.onclick = this.action.bind(this);
 
-            var type = this.xfield.type;
+            var type = this.view.screen.model.fields[this.xfield.name]
+                .description.type;
             if ((type == 'date') || (type == 'datetime')) {
                 var format_func, date_format, time_format;
                 date_format = Sao.common.date_format(
                     this.view.screen.context().date_format);
                 time_format = '%X';
                 if (type == 'datetime') {
+                    c3_config.data.xFormat = '%Y-%m-%d %H:%M:%S';
                     format_func = function(dt) {
                         return Sao.common.format_datetime(date_format,
                                 time_format, moment(dt));
                     };
                 } else {
+                    c3_config.data.xFormat = '%Y-%m-%d';
                     format_func = function(dt) {
                         return Sao.common.format_date(date_format, moment(dt));
                     };
@@ -289,7 +292,8 @@
             delete config.axis;
             delete config.data.x;
             var format_func;
-            var type = this.xfield.type;
+            var type = this.view.screen.model.fields[this.xfield.name]
+                .description.type;
             if ((type == 'date') || (type == 'datetime')) {
                 var date_format = Sao.common.date_format(
                     this.view.screen.context().date_format);
diff -r 9b2af81410f7 -r 4ab5bf57a2d1 tests/sao.js
--- a/tests/sao.js      Thu Sep 24 22:43:50 2020 +0200
+++ b/tests/sao.js      Sun Sep 27 12:29:38 2020 +0200
@@ -2494,6 +2494,27 @@
         });
     });
 
+    QUnit.test('Date.toString', function() {
+        QUnit.strictEqual(Sao.Date(2020, 8, 11).toString(), '2020-09-11');
+    });
+
+    QUnit.test('DateTime.toString', function() {
+        QUnit.strictEqual(
+            Sao.DateTime(2020, 8, 11, 10, 30, 42).toString(),
+            '2020-09-11 10:30:42');
+
+        QUnit.strictEqual(
+            Sao.DateTime(2020, 8, 11, 10, 30, 42, 123).toString(),
+            '2020-09-11 10:30:42.123000');
+    });
+
+    QUnit.test('Time.toString', function() {
+        QUnit.strictEqual(Sao.Time(10, 30, 42).toString(), '10:30:42');
+
+        QUnit.strictEqual(
+            Sao.Time(10, 30, 42, 123).toString(), '10:30:42.123000');
+    });
+
     QUnit.test('HTML Sanitization', function() {
         var examples = [
             ["Test", "Test"],

Reply via email to