changeset ac910af440ef in sao:default
details: https://hg.tryton.org/sao?cmd=changeset;node=ac910af440ef
description:
        Improve report and CSV for tree structure

        issue10116
        review341431002
diffstat:

 CHANGELOG        |   3 +++
 src/screen.js    |  19 +++++++++++++++++++
 src/tab.js       |  47 +++++++++++++++++++++++++++++++++++++----------
 src/view/tree.js |  56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/window.js    |  47 ++++++++++++++++++++++++++++++++++++++---------
 5 files changed, 153 insertions(+), 19 deletions(-)

diffs (309 lines):

diff -r c44c8f6bc280 -r ac910af440ef CHANGELOG
--- a/CHANGELOG Thu Mar 04 23:32:38 2021 +0100
+++ b/CHANGELOG Sun Mar 07 19:07:53 2021 +0100
@@ -1,3 +1,6 @@
+* Add paths and model context to action data
+* Add indentation in CSV export of tree
+* Manage which records to use for actions
 * Add drag and drop on CSV export and import
 * Remove tempusdominus dependency
 * Handle device cookie
diff -r c44c8f6bc280 -r ac910af440ef src/screen.js
--- a/src/screen.js     Thu Mar 04 23:32:38 2021 +0100
+++ b/src/screen.js     Sun Mar 07 19:07:53 2021 +0100
@@ -1403,6 +1403,25 @@
             }
             return [];
         },
+        get selected_paths() {
+            if (this.current_view && this.current_view.view_type == 'tree') {
+                return this.current_view.get_selected_paths();
+            }
+        },
+        get listed_records() {
+            if (this.current_view && this.current_view.view_type == 'tree') {
+                return this.current_view.listed_records;
+            } else if (this.current_record) {
+                return [this.current_record];
+            } else {
+                return [];
+            }
+        },
+        get listed_paths() {
+            if (this.current_view && this.current_view.view_type == 'tree') {
+                return this.current_view.get_listed_paths();
+            }
+        },
         clear: function() {
             this.current_record = null;
             this.group.clear();
diff -r c44c8f6bc280 -r ac910af440ef src/tab.js
--- a/src/tab.js        Thu Mar 04 23:32:38 2021 +0100
+++ b/src/tab.js        Sun Mar 07 19:07:53 2021 +0100
@@ -621,10 +621,14 @@
                                     });
                                 var id = screen.current_record ?
                                     screen.current_record.id : null;
+                                var model_context = screen.context_screen ?
+                                    screen.context_screen.model_name : null;
                                 func({
                                     'model': screen.model.name,
+                                    'model_context': model_context,
+                                    'id': id,
                                     'ids': ids,
-                                    'id': id,
+                                    'paths': screen.selected_paths(),
                                 });
                             })
                             .appendTo(menu);
@@ -648,14 +652,25 @@
                                 if (screen.current_record) {
                                     record_id = screen.current_record.id;
                                 }
-                                var record_ids = screen.current_view
-                                .selected_records.map(function(record) {
+                                var records, paths;
+                                if (action.records == 'listed') {
+                                    records = screen.listed_records;
+                                    paths = screen.listed_paths;
+                                } else {
+                                    records = screen.selected_records;
+                                    paths = screen.selected_paths;
+                                }
+                                var record_ids = records.map(function(record) {
                                     return record.id;
                                 });
+                                var model_context = screen.context_screen ?
+                                    screen.context_screen.model_name : null;
                                 var data = {
-                                    model: screen.model_name,
-                                    id: record_id,
-                                    ids: record_ids
+                                    'model': screen.model_name,
+                                    'model_context': model_context,
+                                    'id': record_id,
+                                    'ids': record_ids,
+                                    'paths': paths,
                                 };
                                 Sao.Action.execute(exec_action, data,
                                     jQuery.extend({}, screen.local_context));
@@ -1343,10 +1358,20 @@
         },
         do_export: function(export_) {
             this.modified_save().then(function() {
-                var ids = this.screen.current_view.selected_records
-                    .map(function(r) {
+                var ids, paths;
+                if (this.screen.current_view &&
+                    (this.screen.current_view.view_type == 'tree') &&
+                    this.screen.current_view.children_field) {
+                    ids = this.screen.listed_records.map(function(r) {
                         return r.id;
                     });
+                    paths = this.screen.listed_paths;
+                } else {
+                    ids = this.screen.selected_records.map(function(r) {
+                        return r.id;
+                    });
+                    paths = this.screen.selected_paths;
+                }
                 var fields = export_['export_fields.'].map(function(field) {
                     return field.name;
                 });
@@ -1357,8 +1382,10 @@
                             'fields': fields,
                             'data': data,
                         };
-                        unparse_obj.data = data.map(function(row) {
-                            return Sao.Window.Export.format_row(row);
+                        unparse_obj.data = data.map(function(row, i) {
+                            var indent = (
+                                paths && paths[i] ? paths[i].length -1 : 0);
+                            return Sao.Window.Export.format_row(row, indent);
                         });
                         var delimiter = ',';
                         if (navigator.platform &&
diff -r c44c8f6bc280 -r ac910af440ef src/view/tree.js
--- a/src/view/tree.js  Thu Mar 04 23:32:38 2021 +0100
+++ b/src/view/tree.js  Sun Mar 07 19:07:53 2021 +0100
@@ -914,6 +914,62 @@
             }
             return records;
         },
+        get listed_records() {
+            if (!this.children_field) {
+                return this.group.slice();
+            }
+
+            var get_listed_records = function(start) {
+                var records = [];
+                var row = this.find_row(start);
+                var children_rows = row ? row.rows : this.rows;
+                for (var idx = 0, len = this.n_children(row);
+                    idx < len; idx++) {
+                    var path = start.concat([idx]);
+                    row = children_rows[idx];
+                    if (row) {
+                        var record = row.record;
+                        records.push(record);
+                        if (row.is_expanded()) {
+                            records = records.concat(get_listed_records(path));
+                        }
+                    }
+                }
+                return records;
+            }.bind(this);
+            return 
get_listed_records([]).concat(this.group.slice(this.rows.length));
+        },
+        get_listed_paths: function() {
+            if (!this.children_field) {
+                return this.group.map(function(record) {
+                    return [record.id];
+                });
+            }
+
+            var get_listed_paths = function(start, start_path) {
+                var paths = [];
+                var row = this.find_row(start);
+                var children_rows = row ? row.rows : this.rows;
+                for (var idx = 0, len = this.n_children(row);
+                    idx < len; idx++) {
+                    var path = start.concat([idx]);
+                    row = children_rows[idx];
+                    if (row) {
+                        var record = row.record;
+                        var id_path = start_path.concat([record.id]);
+                        paths.push(id_path);
+                        if (row.is_expanded()) {
+                            paths = paths.concat(get_listed_paths(path, 
id_path));
+                        }
+                    }
+                }
+                return paths;
+            }.bind(this);
+            return get_listed_paths([], []).concat(
+                this.group.slice(this.rows.length).map(function(record) {
+                    return [record.id];
+                }));
+        },
         select_records: function(from, to) {
             if (!from && to) {
                 from = this.rows[0].record;
diff -r c44c8f6bc280 -r ac910af440ef src/window.js
--- a/src/window.js     Thu Mar 04 23:32:38 2021 +0100
+++ b/src/window.js     Sun Mar 07 19:07:53 2021 +0100
@@ -1536,9 +1536,11 @@
                 'id': 'input-records',
             }).append(jQuery('<option/>', {
                 'val': true,
+                'selected': this.screen_is_tree ? false : true,
             }).text(Sao.i18n.gettext("Selected Records")))
                 .append(jQuery('<option/>', {
                     'val': false,
+                    'selected': this.screen_is_tree ? true : false,
                 }).text(Sao.i18n.gettext("Listed Records")));
 
             this.ignore_search_limit = jQuery('<input/>', {
@@ -1547,7 +1549,8 @@
 
             this.selected_records.change(function() {
                 this.ignore_search_limit.parents('.form-group').first().toggle(
-                    !JSON.parse(this.selected_records.val()));
+                    !JSON.parse(this.selected_records.val()) &&
+                    !this.screen_is_tree);
             }.bind(this));
 
             jQuery('<div/>', {
@@ -1602,6 +1605,12 @@
         get context() {
             return this.screen.context;
         },
+        get screen_is_tree() {
+            return Boolean(
+                this.screen.current_view &&
+                (this.screen.current_view.view_type == 'tree') &&
+                this.screen.current_view.children_field);
+        },
         view_populate: function(parent_node, parent_view) {
             var names = Object.keys(parent_node).sort(function(a, b) {
                 if (parent_node[b].string < parent_node[a].string) {
@@ -1884,11 +1893,23 @@
                     fields2.push(field.innerText);
                 });
 
-                var prm;
+                var prm, ids, paths;
                 if (JSON.parse(this.selected_records.val())) {
-                    var ids = 
this.screen.current_view.selected_records.map(function(r) {
+                    ids = this.screen.selected_records.map(function(r) {
                         return r.id;
                     });
+                    paths = this.screen.selected_paths;
+                    prm = Sao.rpc({
+                        'method': (
+                            'model.' + this.screen.model_name +
+                            '.export_data'),
+                        'params': [ids, fields, this.context]
+                    }, this.session);
+                } else if (this.screen_is_tree) {
+                    ids = this.screen.listed_records.map(function(r) {
+                        return r.id;
+                    });
+                    paths = this.screen.listed_paths;
                     prm = Sao.rpc({
                         'method': (
                             'model.' + this.screen.model_name +
@@ -1916,7 +1937,7 @@
                     }, this.session);
                 }
                 prm.then(function(data) {
-                    this.export_csv(fields2, data).then(function() {
+                    this.export_csv(fields2, data, paths).then(function() {
                         this.destroy();
                     }.bind(this));
                 }.bind(this));
@@ -1924,11 +1945,13 @@
                 this.destroy();
             }
         },
-        export_csv: function(fields, data) {
+        export_csv: function(fields, data, paths) {
             var locale_format = this.el_csv_locale.prop('checked');
             var unparse_obj = {};
-            unparse_obj.data = data.map(function(row) {
-                return Sao.Window.Export.format_row(row, locale_format);
+            unparse_obj.data = data.map(function(row, i) {
+                var indent = paths && paths[i] ? paths[i].length -1 : 0;
+                return Sao.Window.Export.format_row(
+                    row, indent, locale_format);
             });
             if (this.el_add_field_names.is(':checked')) {
                 unparse_obj.fields = fields;
@@ -2001,12 +2024,15 @@
         },
     });
 
-    Sao.Window.Export.format_row = function(line, locale_format) {
+    Sao.Window.Export.format_row = function(line, indent, locale_format) {
+        if (indent === undefined) {
+            indent = 0;
+        }
         if (locale_format === undefined) {
             locale_format = true;
         }
         var row = [];
-        line.forEach(function(val) {
+        line.forEach(function(val, i) {
             if (locale_format) {
                 if (val.isDateTime) {
                     val = val.format(
@@ -2026,6 +2052,9 @@
             } else if (typeof(val) == 'boolean') {
                 val += 0;
             }
+            if ((i === 0) && indent && (typeof(val) == 'string')) {
+                val = '  '.repeat(indent) + val;
+            }
             row.push(val);
         });
         return row;

Reply via email to