Skip to content

Commit

Permalink
Feature 22921 mig colorize field in tree views jpr (#4)
Browse files Browse the repository at this point in the history
[MIG] web_tree_dynamic_colored_field: Migration to 17.0

- Implementation now is based on updating `style` attribute of `cell/td` instead of directly manipulating element's CSS value.

- Cleanup docs about no more implemented `colors` parameter for `tree`
  • Loading branch information
dz0 authored Mar 14, 2024
1 parent 8f02ce6 commit 384ba04
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 246 deletions.
33 changes: 6 additions & 27 deletions web_tree_dynamic_colored_field/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Usage
</field>
...

With this example, column which renders 'name' field will have its background colored in red.
With this example, column which renders 'name' field will have its **background** colored in red on customer records.

- In the tree view declaration, put
``options='{"fg_color": "white:customer == True"}'`` attribute in the
Expand All @@ -81,26 +81,7 @@ Usage
</field>
...

With this example, column which renders 'name' field will have its text colored in white on a customer records.

- In the tree view declaration, use
``options='"color_field": "my_color"'`` attribute in the ``tree``
tag:

::

...
<field name="arch" type="xml">
<tree string="View name" colors="color_field: my_color" >
...
<field name="my_color" invisible="1"/>
...
</tree>
</field>
...

- You can also use ``colors="bg_color_field: my_color"`` to defined the
field name that will be used for the background color of the line.
With this example, column which renders 'name' field will have its **text** colored in white on customer records.

- If you want to use more than one color, you can split the attributes
using ';':
Expand All @@ -123,12 +104,6 @@ Example:
</field>
...
With this example, the content of the field named `my_color` will be used to
populate the `my_color` CSS value. Use a function field to return whichever
color you want depending on the other record values. Note that this
overrides the rest of `colors` attributes, and that you need the tree
to load your field in the first place by adding it as invisible field.
- Can use strings too... In the tree view declaration, put
``options="{'fg_color': 'green:customer_state == \'success\''}"``
attribute in the ``field`` tag:
Expand Down Expand Up @@ -158,6 +133,8 @@ Known issues / Roadmap
``colors`` attribute is no longer in the RelaxNG schema of the tree
view, so we can't use it anymore. This feature has then been dropped,
but could be reimplement in another way.
- Since version 17.0 coloring is written into ``style`` attribute of
(td) element

Bug Tracker
===========
Expand Down Expand Up @@ -187,13 +164,15 @@ Contributors
- Guewen Baconnier <guewen.baconnier@camptocamp.com>
- Phuc Tran Thanh <phuc@trobz.com>
- Sylvain LE GAL <https://twitter.com/legalsylvain>
- Jurgis Pralgauskis <jurgis@versada.eu>

Other credits
-------------

The development of this module has been financially supported by:

- Camptocamp
- Versada

Maintainers
-----------
Expand Down
5 changes: 3 additions & 2 deletions web_tree_dynamic_colored_field/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Colorize field in tree views",
"summary": "Allows you to dynamically color fields on tree views",
"category": "Hidden/Dependency",
"version": "15.0.1.0.1",
"version": "17.0.1.0.0",
"depends": ["web"],
"author": "Camptocamp, Therp BV, Odoo Community Association (OCA)",
"license": "AGPL-3",
Expand All @@ -13,7 +13,8 @@
"installable": True,
"assets": {
"web.assets_backend": [
"/web_tree_dynamic_colored_field/static/src/js/web_tree_dynamic_colored_field.js",
"web_tree_dynamic_colored_field/static/src/xml/list.xml",
"web_tree_dynamic_colored_field/static/src/js/list_renderer.esm.js",
],
},
}
1 change: 1 addition & 0 deletions web_tree_dynamic_colored_field/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
- Guewen Baconnier \<<guewen.baconnier@camptocamp.com>\>
- Phuc Tran Thanh \<<phuc@trobz.com>\>
- Sylvain LE GAL \<<https://twitter.com/legalsylvain>\>
- Jurgis Pralgauskis \<<jurgis@versada.eu>\>
1 change: 1 addition & 0 deletions web_tree_dynamic_colored_field/readme/CREDITS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
The development of this module has been financially supported by:

- Camptocamp
- Versada
1 change: 1 addition & 0 deletions web_tree_dynamic_colored_field/readme/ROADMAP.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
`colors` attribute is no longer in the RelaxNG schema of the tree
view, so we can't use it anymore. This feature has then been dropped,
but could be reimplement in another way.
- Since version 17.0 coloring is written into ``style`` attribute of (td) element
28 changes: 3 additions & 25 deletions web_tree_dynamic_colored_field/readme/USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</field>
...

With this example, column which renders 'name' field will have its background colored in red.
With this example, column which renders 'name' field will have its **background** colored in red on customer records.

- In the tree view declaration, put
`options='{"fg_color": "white:customer == True"}'` attribute in the
Expand All @@ -28,28 +28,12 @@
</field>
...

With this example, column which renders 'name' field will have its text colored in white on a customer records.

- In the tree view declaration, use
`options='"color_field": "my_color"'` attribute in the `tree` tag:

...
<field name="arch" type="xml">
<tree string="View name" colors="color_field: my_color" >
...
<field name="my_color" invisible="1"/>
...
</tree>
</field>
...

- You can also use `colors="bg_color_field: my_color"` to defined the
field name that will be used for the background color of the line.
With this example, column which renders 'name' field will have its **text** colored in white on customer records.

- If you want to use more than one color, you can split the attributes
using ';':

```
```
options='{"fg_color": "red:red_color == True; green:green_color == True"}'
```

Expand All @@ -65,12 +49,6 @@ Example:
</tree>
</field>
...

With this example, the content of the field named `my_color` will be used to
populate the `my_color` CSS value. Use a function field to return whichever
color you want depending on the other record values. Note that this
overrides the rest of `colors` attributes, and that you need the tree
to load your field in the first place by adding it as invisible field.
```

- Can use strings too... In the tree view declaration, put
Expand Down
34 changes: 7 additions & 27 deletions web_tree_dynamic_colored_field/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ <h2><a class="toc-backref" href="#toc-entry-1">Usage</a></h2>
&lt;/field&gt;
...

With this example, column which renders 'name' field will have its background colored in red.
With this example, column which renders 'name' field will have its **background** colored in red on customer records.
</pre>
</li>
<li><p class="first">In the tree view declaration, put
Expand All @@ -423,27 +423,9 @@ <h2><a class="toc-backref" href="#toc-entry-1">Usage</a></h2>
&lt;/field&gt;
...

With this example, column which renders 'name' field will have its text colored in white on a customer records.
With this example, column which renders 'name' field will have its **text** colored in white on customer records.
</pre>
</li>
<li><p class="first">In the tree view declaration, use
<tt class="docutils literal"><span class="pre">options='&quot;color_field&quot;:</span> &quot;my_color&quot;'</tt> attribute in the <tt class="docutils literal">tree</tt>
tag:</p>
<pre class="literal-block">
...
&lt;field name=&quot;arch&quot; type=&quot;xml&quot;&gt;
&lt;tree string=&quot;View name&quot; colors=&quot;color_field: my_color&quot; &gt;
...
&lt;field name=&quot;my_color&quot; invisible=&quot;1&quot;/&gt;
...
&lt;/tree&gt;
&lt;/field&gt;
...
</pre>
</li>
<li><p class="first">You can also use <tt class="docutils literal"><span class="pre">colors=&quot;bg_color_field:</span> my_color&quot;</tt> to defined the
field name that will be used for the background color of the line.</p>
</li>
<li><p class="first">If you want to use more than one color, you can split the attributes
using ‘;’:</p>
</li>
Expand All @@ -461,13 +443,7 @@ <h2><a class="toc-backref" href="#toc-entry-1">Usage</a></h2>
</span>...<span class="w">
</span><span class="nt">&lt;/tree&gt;</span><span class="w">
</span><span class="nt">&lt;/field&gt;</span><span class="w">
</span>...<span class="w">

</span>With<span class="w"> </span>this<span class="w"> </span>example,<span class="w"> </span>the<span class="w"> </span>content<span class="w"> </span>of<span class="w"> </span>the<span class="w"> </span>field<span class="w"> </span>named<span class="w"> </span>`my_color`<span class="w"> </span>will<span class="w"> </span>be<span class="w"> </span>used<span class="w"> </span>to<span class="w">
</span>populate<span class="w"> </span>the<span class="w"> </span>`my_color`<span class="w"> </span>CSS<span class="w"> </span>value.<span class="w"> </span>Use<span class="w"> </span>a<span class="w"> </span>function<span class="w"> </span>field<span class="w"> </span>to<span class="w"> </span>return<span class="w"> </span>whichever<span class="w">
</span>color<span class="w"> </span>you<span class="w"> </span>want<span class="w"> </span>depending<span class="w"> </span>on<span class="w"> </span>the<span class="w"> </span>other<span class="w"> </span>record<span class="w"> </span>values.<span class="w"> </span>Note<span class="w"> </span>that<span class="w"> </span>this<span class="w">
</span>overrides<span class="w"> </span>the<span class="w"> </span>rest<span class="w"> </span>of<span class="w"> </span>`colors`<span class="w"> </span>attributes,<span class="w"> </span>and<span class="w"> </span>that<span class="w"> </span>you<span class="w"> </span>need<span class="w"> </span>the<span class="w"> </span>tree<span class="w">
</span>to<span class="w"> </span>load<span class="w"> </span>your<span class="w"> </span>field<span class="w"> </span>in<span class="w"> </span>the<span class="w"> </span>first<span class="w"> </span>place<span class="w"> </span>by<span class="w"> </span>adding<span class="w"> </span>it<span class="w"> </span>as<span class="w"> </span>invisible<span class="w"> </span>field.
</span>...
</pre>
<ul>
<li><p class="first">Can use strings too… In the tree view declaration, put
Expand Down Expand Up @@ -499,6 +475,8 @@ <h2><a class="toc-backref" href="#toc-entry-2">Known issues / Roadmap</a></h2>
<tt class="docutils literal">colors</tt> attribute is no longer in the RelaxNG schema of the tree
view, so we can’t use it anymore. This feature has then been dropped,
but could be reimplement in another way.</li>
<li>Since version 17.0 coloring is written into <tt class="docutils literal">style</tt> attribute of
(td) element</li>
</ul>
</div>
<div class="section" id="bug-tracker">
Expand Down Expand Up @@ -529,13 +507,15 @@ <h1>Contributors</h1>
<li>Guewen Baconnier &lt;<a class="reference external" href="mailto:guewen.baconnier&#64;camptocamp.com">guewen.baconnier&#64;camptocamp.com</a>&gt;</li>
<li>Phuc Tran Thanh &lt;<a class="reference external" href="mailto:phuc&#64;trobz.com">phuc&#64;trobz.com</a>&gt;</li>
<li>Sylvain LE GAL &lt;<a class="reference external" href="https://twitter.com/legalsylvain">https://twitter.com/legalsylvain</a>&gt;</li>
<li>Jurgis Pralgauskis &lt;<a class="reference external" href="mailto:jurgis&#64;versada.eu">jurgis&#64;versada.eu</a>&gt;</li>
</ul>
</div>
<div class="section" id="other-credits">
<h1>Other credits</h1>
<p>The development of this module has been financially supported by:</p>
<ul class="simple">
<li>Camptocamp</li>
<li>Versada</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
78 changes: 78 additions & 0 deletions web_tree_dynamic_colored_field/static/src/js/list_renderer.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/** @odoo-module **/

import {patch} from "@web/core/utils/patch";
import {ListRenderer} from "@web/views/list/list_renderer";
import {evaluateBooleanExpr} from "@web/core/py_js/py";

patch(ListRenderer.prototype, {
/**
* @param {Object} column represents field
* @param {Record} record
* @returns {String} style code for the html element
*/
getDynamicColoredStyle(column, record) {
let style = "";

let color = this.getDynamicColor(column, record, "bg_color");
if (color !== undefined) {
style += `background-color: ${color};`;
}

color = this.getDynamicColor(column, record, "fg_color");
if (color !== undefined) {
// $td.css('color', color);
style += `color: ${color};`;
}

return style;
},

/**
* Return the `color` that has truthfull expresssion
*
* @param column {Object} represents field
* @param record {Record}
* @param color_target {String} 'bg_color' or 'fg_color'
* @returns {String | undefined} color
*/
getDynamicColor(column, record, color_target) {
if (color_target in column.options) {
const definition = column.options[color_target];
let result = "";
for (const color_def of definition.split(";")) {
const color_to_expression = this.pairColorParse(color_def);
if (color_to_expression !== undefined) {
const [color, expression] = color_to_expression;
if (
evaluateBooleanExpr(
expression,
record.evalContextWithVirtualIds
)
) {
// We don't return first match,
// as it can be default color (with "True" expression),
// and later more precise condition may be found.
result = color;
}
}
}
return result | undefined;
}
},

/**
* @param {String} pairColor `color: expression` pair
* @returns {Array} undefined or array of color, expression
*/
pairColorParse: function (pairColor) {
if (pairColor !== "") {
var pairList = pairColor.split(":"),
color = pairList[0],
// If one passes a bare color instead of an expression,
// then we consider that color is to be shown in any case
expression = pairList[1] ? pairList[1] : "True";
return [color, expression];
}
return undefined;
},
});
Loading

0 comments on commit 384ba04

Please sign in to comment.