Add context in ir.action.act_window.domain to use it with tree_invisible

Hi,

I would like to have domains with different fields. For this tree_invisible is the perfect option, but I can’t find out which tab I’m in.

The different solutions are:

  1. add in the context the identifier of the tab: it does not require any action in the module other than the definition of the pyson, however if a field must be hidden in several tabs, then the pyson will be complex
  2. Add in the definition of the tab a context: the context must be defined in the xml of the Domain and in the pyson. it opens more freedom and less complexity to write the pyson of the tree_invisible.

I did a quick POC with only SAO. It requires more work and unit testing.

SAO --------------

diff --git a/src/action.js b/src/action.js
index 1b388ef1..4b642e8b 100644
--- a/src/action.js
+++ b/src/action.js
@@ -96,7 +96,7 @@
                 params.tab_domain = [];
                 for (const element of action.domains) {
                     params.tab_domain.push(
-                        [element[0], decoder.decode(element[1]), element[2]]);
+                        [element[0], decoder.decode(element[1]), element[2], decoder.decode(element[3])]);
                 }
                 name_prm = jQuery.when(action.name);
                 params.model = action.res_model || data.res_model;
diff --git a/src/screen.js b/src/screen.js
index 9babcb26..4a3e1c54 100644
--- a/src/screen.js
+++ b/src/screen.js
@@ -8,6 +8,7 @@
             this.alternate_viewport = jQuery('<div/>', {
                 'class': 'screen-container'
             });
+            console.log('ScreenContainer', tab_domain);
             this.alternate_view = false;
             this.search_modal = null;
             this.search_form = null;
@@ -359,6 +360,16 @@
             }
             return this.tab_domain[idx][1];
         },
+        get_tab_context: function() {
+            if (!this.tab) {
+                return {};
+            }
+            var idx = this.get_tab_index();
+            if (idx < 0) {
+                return {};
+            }
+            return this.tab_domain[idx][3];
+        },
         set_tab_counter: function(count, idx=null) {
             if (jQuery.isEmptyObject(this.tab_counter) || !this.tab) {
                 return;
diff --git a/src/view/tree.js b/src/view/tree.js
index 56d55e98..06cfe6e9 100644
--- a/src/view/tree.js
+++ b/src/view/tree.js
@@ -798,12 +798,15 @@
                 domain.push(this.screen.domain);
             }
             var tab_domain = this.screen.screen_container.get_tab_domain();
+            var tab_context = this.screen.screen_container.get_tab_context();
             if (!jQuery.isEmptyObject(tab_domain)) {
                 domain.push(tab_domain);
             }
             var inversion = new Sao.common.DomainInversion();
             domain = inversion.simplify(domain);
-            var decoder = new Sao.PYSON.Decoder(this.screen.context);
+            var context = jQuery.extend({}, this.screen.context, tab_context);
+            var decoder = new Sao.PYSON.Decoder(context);
+            console.log('View / Tree', decoder, context);
             var min_width = [];
             var tree_column_optional = (
                 Sao.Screen.tree_column_optional[this.view_id] || {});
@@ -822,6 +825,7 @@
                 }
                 var invisible = decoder.decode(
                     column.attributes.tree_invisible || '0');
+                console.log('View / Tree', column.attributes.name, invisible);
                 if (invisible || optional) {
                     visible_columns -= 1;
                     column.set_visible(false);

trytond ----

diff -r 086f841d105c trytond/ir/action.py
--- a/trytond/ir/action.py	Fri Aug 26 18:12:02 2022 +0200
+++ b/trytond/ir/action.py	Wed Feb 15 15:12:56 2023 +0100
@@ -915,7 +915,13 @@
             for view in self.act_window_views]
 
     def get_domains(self, name):
-        return [(domain.name, domain.domain or '[]', domain.count)
+        return [
+            (
+                domain.name,
+                domain.domain or '[]',
+                domain.count,
+                domain.context or '{}',
+            )
             for domain in self.act_window_domains]
 
     @classmethod
@@ -1000,6 +1006,7 @@
     count = fields.Boolean('Count')
     act_window = fields.Many2One('ir.action.act_window', 'Action',
         select=True, required=True, ondelete='CASCADE')
+    context = fields.Char('Context Value')
 
     @classmethod
     def __register__(cls, module_name):

I do not think it is wise to add another context layer. There are already so much.
But I think Allow hidding columns depending on the domain (#12019) · Issues · Tryton / Tryton · GitLab would be simpler and enough for most of the cases.

1 Like

I understand your point of view, the context and its use is localized and is only used to decode the tree view pyson.

Besides, where could decode the optional attribute in the same way, which would make it sensitive to the tab?

Optional is static because it is just a default value that the user can change.