From 2fcb49dc9317ec92522e85943c77ac8751c29355 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A0n=20Todorovich?= Date: Tue, 13 Oct 2020 10:40:43 -0300 Subject: [PATCH] [ADD] web_calendar_color_field --- .../odoo/addons/web_calendar_color_field | 1 + setup/web_calendar_color_field/setup.py | 6 ++ web_calendar_color_field/__init__.py | 0 web_calendar_color_field/__manifest__.py | 16 +++++ .../readme/CONTRIBUTORS.rst | 1 + .../readme/DESCRIPTION.rst | 9 +++ web_calendar_color_field/readme/USAGE.rst | 13 ++++ .../static/src/js/calendar.js | 67 +++++++++++++++++++ web_calendar_color_field/views/assets.xml | 11 +++ 9 files changed, 124 insertions(+) create mode 120000 setup/web_calendar_color_field/odoo/addons/web_calendar_color_field create mode 100644 setup/web_calendar_color_field/setup.py create mode 100644 web_calendar_color_field/__init__.py create mode 100644 web_calendar_color_field/__manifest__.py create mode 100644 web_calendar_color_field/readme/CONTRIBUTORS.rst create mode 100644 web_calendar_color_field/readme/DESCRIPTION.rst create mode 100644 web_calendar_color_field/readme/USAGE.rst create mode 100644 web_calendar_color_field/static/src/js/calendar.js create mode 100644 web_calendar_color_field/views/assets.xml diff --git a/setup/web_calendar_color_field/odoo/addons/web_calendar_color_field b/setup/web_calendar_color_field/odoo/addons/web_calendar_color_field new file mode 120000 index 000000000..bd453a8a0 --- /dev/null +++ b/setup/web_calendar_color_field/odoo/addons/web_calendar_color_field @@ -0,0 +1 @@ +../../../../web_calendar_color_field \ No newline at end of file diff --git a/setup/web_calendar_color_field/setup.py b/setup/web_calendar_color_field/setup.py new file mode 100644 index 000000000..28c57bb64 --- /dev/null +++ b/setup/web_calendar_color_field/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +) diff --git a/web_calendar_color_field/__init__.py b/web_calendar_color_field/__init__.py new file mode 100644 index 000000000..e69de29bb diff --git a/web_calendar_color_field/__manifest__.py b/web_calendar_color_field/__manifest__.py new file mode 100644 index 000000000..a69006d85 --- /dev/null +++ b/web_calendar_color_field/__manifest__.py @@ -0,0 +1,16 @@ +# Copyright 2019 Iván Todorovich +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +{ + "name": "Calendar Color Field", + "version": "14.0.1.0.0", + "category": "Web", + "website": "https://github.com/OCA/web", + "author": "Iván Todorovich, Odoo Community Association (OCA)", + "license": "AGPL-3", + "depends": [ + "web", + ], + "data": [ + "views/assets.xml", + ], +} diff --git a/web_calendar_color_field/readme/CONTRIBUTORS.rst b/web_calendar_color_field/readme/CONTRIBUTORS.rst new file mode 100644 index 000000000..25a7ad47e --- /dev/null +++ b/web_calendar_color_field/readme/CONTRIBUTORS.rst @@ -0,0 +1 @@ +* Iván Todorovich diff --git a/web_calendar_color_field/readme/DESCRIPTION.rst b/web_calendar_color_field/readme/DESCRIPTION.rst new file mode 100644 index 000000000..3aed1975f --- /dev/null +++ b/web_calendar_color_field/readme/DESCRIPTION.rst @@ -0,0 +1,9 @@ +This is a technical module. It doesn't do anything on its own. + +Odoo introduced a mechanism to set the desired color for a calendar filter, +through the field's `color` attribute. + +Unfortunately, this only works for filters, but not if we want to use the +calendar's `color` attribute. + +This module makes the appropiate changes so that the filter's color is used, if available. diff --git a/web_calendar_color_field/readme/USAGE.rst b/web_calendar_color_field/readme/USAGE.rst new file mode 100644 index 000000000..101973eca --- /dev/null +++ b/web_calendar_color_field/readme/USAGE.rst @@ -0,0 +1,13 @@ +Set a color attribute value on any many2one calendar field set as filter and +set the calendar's color attribute to use the same many2one field. + +Example: + +.. code-block:: xml + + + + + +- `category_id` is a `Many2one` field in your record. +- `kanban_color` is an integer field in your category record. diff --git a/web_calendar_color_field/static/src/js/calendar.js b/web_calendar_color_field/static/src/js/calendar.js new file mode 100644 index 000000000..b96d01031 --- /dev/null +++ b/web_calendar_color_field/static/src/js/calendar.js @@ -0,0 +1,67 @@ +odoo.define("web_calendar_color_field.CalendarModel", function (require) { + "use strict"; + + const CalendarModel = require("web.CalendarModel"); + + CalendarModel.include({ + /** + * Overload to handle custom colorIndex + * + * Default beaviour when using a many2one field as color attribute + * is to generate random colors based on the record id. + * + * We are changing that behaviour to play nicely with the newly added + * color attribute. + * + * If the field is also added to filters, and a color field is defined, + * we use that color field as colorIndex. + */ + _loadColors: function (element, events) { + var self = this; + if (this.fieldColor) { + var fieldName = this.fieldColor; + var filter = this.data.filters[fieldName]; + if (filter && filter.color_model && filter.field_color) { + // If we haven't loaded the color fields, we do it, and we + // store it in self._field_color_map. We only do it once, + // because subsequent calls will simply read it from the map. + // Note: _loadRecordsToFilters will also read these values + // from db, but on first render it's called after this method + var defs = []; + if (!this._field_color_map) { + var ids = _.map(events, function (event) { + return event.record[fieldName][0]; + }); + defs.push( + this._rpc({ + model: filter.color_model, + method: "read", + args: [_.uniq(ids), [filter.field_color]], + }).then(function (res) { + self._field_color_map = self._field_color_map || {}; + _.each(res, function (item) { + self._field_color_map[item.id] = + item[filter.field_color]; + }); + }) + ); + } + Promise.all(defs).then(function () { + _.each(events, function (event) { + var value = event.record[fieldName][0]; + event.color_index = self._field_color_map[value]; + }); + }); + // Don't set model color, so that filters generate their own + // https://github.com/odoo/odoo/blob/14.0/addons/web/static/src/js/views/calendar/calendar_model.js#L629 + // this.model_color = this.fields[fieldName].relation || element.model; + } else { + return this._super.apply(this, arguments); + } + } + return Promise.resolve(); + }, + }); + + return CalendarModel; +}); diff --git a/web_calendar_color_field/views/assets.xml b/web_calendar_color_field/views/assets.xml new file mode 100644 index 000000000..2a4b97938 --- /dev/null +++ b/web_calendar_color_field/views/assets.xml @@ -0,0 +1,11 @@ + + +