changeset e6572d4652be in sao:default
details: https://hg.tryton.org/sao?cmd=changeset;node=e6572d4652be
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
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 f081934d2365 -r e6572d4652be src/sao.js
--- a/src/sao.js        Thu Sep 24 23:00:52 2020 +0200
+++ b/src/sao.js        Sun Sep 27 12:29:38 2020 +0200
@@ -168,6 +168,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, utc) {
         var date;
         if (month === undefined) {
@@ -185,9 +206,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;
     };
 
@@ -234,13 +252,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 f081934d2365 -r e6572d4652be src/view/graph.js
--- a/src/view/graph.js Thu Sep 24 23:00:52 2020 +0200
+++ b/src/view/graph.js Sun Sep 27 12:29:38 2020 +0200
@@ -98,7 +98,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);
@@ -177,18 +177,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));
                     };
@@ -286,7 +289,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 f081934d2365 -r e6572d4652be tests/sao.js
--- a/tests/sao.js      Thu Sep 24 23:00:52 2020 +0200
+++ b/tests/sao.js      Sun Sep 27 12:29:38 2020 +0200
@@ -2918,6 +2918,27 @@
             ["Active: False", "Active: True"]));
     });
 
+    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