Browse Source

web_tree_dynamic_colored_field v9

demo view
pull/1223/head
Holger Brunn 8 years ago
committed by mreficent
parent
commit
e8706b1e27
  1. 17
      web_tree_dynamic_colored_field/README.rst
  2. 21
      web_tree_dynamic_colored_field/__init__.py
  3. 32
      web_tree_dynamic_colored_field/__openerp__.py
  4. 22
      web_tree_dynamic_colored_field/demo/res_users.xml
  5. 52
      web_tree_dynamic_colored_field/static/src/js/web_tree_dynamic_colored_field.js

17
web_tree_dynamic_colored_field/README.rst

@ -65,18 +65,17 @@ Usage
With this example, the content of the field named `color` will be used to With this example, the content of the field named `color` will be used to
populate the `color` CSS value. Use a function field to return whichever populate the `color` CSS value. Use a function field to return whichever
color you want depending on the other record values. Note this this
color you want depending on the other record values. Note that this
overrides the `colors` attribute, and that you need the tree to load your overrides the `colors` attribute, and that you need the tree to load your
field in the first place by adding it as invisible field. field in the first place by adding it as invisible field.
Bug Tracker Bug Tracker
=========== ===========
Bugs are tracked on `GitHub Issues <https://github.com/OCA/web/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed feedback
`here <https://github.com/OCA/web/issues/new?body=module:%20web_widget_color_tree_field%0Aversion:%208.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
Bugs are tracked on `GitHub Issues
<https://github.com/OCA/web/issues>`_. In case of trouble, please
check there if your issue has already been reported. If you spotted it first,
help us smashing it by providing a detailed and welcomed feedback.
Credits Credits
======= =======
@ -90,9 +89,9 @@ Contributors
Maintainer Maintainer
---------- ----------
.. image:: http://odoo-community.org/logo.png
.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association :alt: Odoo Community Association
:target: http://odoo-community.org
:target: https://odoo-community.org
This module is maintained by the OCA. This module is maintained by the OCA.
@ -100,4 +99,4 @@ OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and mission is to support the collaborative development of Odoo features and
promote its widespread use. promote its widespread use.
To contribute to this module, please visit http://odoo-community.org.
To contribute to this module, please visit https://odoo-community.org.

21
web_tree_dynamic_colored_field/__init__.py

@ -1,20 +1,3 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
##############################################################################
#
# Author: Damien Crier
# Copyright 2015 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 2015 Camptocamp SA, Damien Crier
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

32
web_tree_dynamic_colored_field/__openerp__.py

@ -1,32 +1,18 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
##############################################################################
#
# Author: Damien Crier
# Copyright 2015 Camptocamp SA
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
# © 2015 Camptocamp SA, Damien Crier
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
{ {
'name': 'Colorize field in tree views', 'name': 'Colorize field in tree views',
'summary': 'Allows you to dynamically color fields on tree views', 'summary': 'Allows you to dynamically color fields on tree views',
'category': 'Hidden',
'version': '8.0.2.0.0',
'category': 'Hidden/Dependency',
'version': '9.0.2.0.0',
'depends': ['web'], 'depends': ['web'],
'author': "Camptocamp,Odoo Community Association (OCA)",
'author': "Camptocamp,Therp BV,Odoo Community Association (OCA)",
'license': 'AGPL-3', 'license': 'AGPL-3',
'website': 'http://www.camptocamp.com',
'website': 'https://github.com/OCA/web',
'demo': [
"demo/res_users.xml",
],
'data': [ 'data': [
'views/web_tree_dynamic_colored_field.xml', 'views/web_tree_dynamic_colored_field.xml',
], ],

22
web_tree_dynamic_colored_field/demo/res_users.xml

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<openerp>
<data>
<record id="view_users_tree" model="ir.ui.view">
<field name="model">res.users</field>
<field name="inherit_id" ref="base.view_users_tree" />
<field name="arch" type="xml">
<xpath expr="." position="attributes">
<attribute name="color_field">lang</attribute>
</xpath>
<field name="login_date" position="attributes">
<attribute name="bg_color">red: login_date == False</attribute>
<attribute name="fg_color">white: login_date == False</attribute>
</field>
<field name="name" position="attributes">
<attribute name="bg_color">red: login == 'admin'</attribute>
<attribute name="fg_color">white: login == 'admin'</attribute>
</field>
</field>
</record>
</data>
</openerp>

52
web_tree_dynamic_colored_field/static/src/js/web_tree_dynamic_colored_field.js

@ -1,10 +1,16 @@
openerp.web_tree_dynamic_colored_field = function(instance){
odoo.define('web_tree_dynamic_colored_field', function(require)
{
'use strict';
var ListView = require('web.ListView'),
pyeval = require('web.pyeval'),
py = window.py;
var pair_colors = function(pair_color){ var pair_colors = function(pair_color){
if (pair_color != ""){
if (pair_color !== ""){
var pair_list = pair_color.split(':'), var pair_list = pair_color.split(':'),
color = pair_list[0], color = pair_list[0],
expression = pair_list[1]; expression = pair_list[1];
return [color, py.parse(py.tokenize(expression)), expression]
return [color, py.parse(py.tokenize(expression)), expression];
} }
}; };
@ -12,7 +18,7 @@ openerp.web_tree_dynamic_colored_field = function(instance){
return _.extend( return _.extend(
{}, {},
record.attributes, record.attributes,
instance.web.pyeval.context()
pyeval.context()
); );
}; };
@ -22,21 +28,21 @@ openerp.web_tree_dynamic_colored_field = function(instance){
var colors = _(column[field_attribute].split(';')) var colors = _(column[field_attribute].split(';'))
.chain() .chain()
.map(pair_colors) .map(pair_colors)
.value();
var colors = colors.filter(function CheckUndefined(value, index, ar) {
return value != undefined;
})
.value()
.filter(function CheckUndefined(value, index, ar) {
return value !== undefined;
});
var ctx = get_eval_context(record); var ctx = get_eval_context(record);
for(i=0, len=colors.length; i<len; ++i) {
pair = colors[i];
var color = pair[0];
var expression = pair[1];
for(var i=0, len=colors.length; i<len; ++i) {
var pair = colors[i],
color = pair[0],
expression = pair[1];
if (py.evaluate(expression, ctx).toJSON()) { if (py.evaluate(expression, ctx).toJSON()) {
result = css_attribute + ': ' + color + ';'; result = css_attribute + ': ' + color + ';';
} }
} }
} }
return result
return result;
}; };
var colorize = function(record, column){ var colorize = function(record, column){
@ -46,14 +52,28 @@ openerp.web_tree_dynamic_colored_field = function(instance){
return res; return res;
}; };
instance.web.ListView.List.include({
ListView.List.include({
init: function(group, opts){ init: function(group, opts){
this._super(group, opts); this._super(group, opts);
this.columns.fct_colorize = colorize; this.columns.fct_colorize = colorize;
}, },
}); });
instance.web.ListView.include({
ListView.include({
load_view: function()
{
var self = this;
return this._super.apply(this, arguments)
.then(function()
{
// the style_for helper is only called if one of colors or
// fonts is not null
if(self.fields_view.arch.attrs.color_field)
{
self.colors = [];
}
});
},
style_for: function (record) style_for: function (record)
{ {
var result = this._super.apply(this, arguments); var result = this._super.apply(this, arguments);
@ -72,4 +92,4 @@ openerp.web_tree_dynamic_colored_field = function(instance){
return result; return result;
}, },
}); });
}
});
Loading…
Cancel
Save