From 014542a91dc8ef32427e57488f1b4085670326e8 Mon Sep 17 00:00:00 2001 From: David Coninckx Date: Mon, 30 Mar 2015 15:55:16 +0200 Subject: [PATCH] Add options to colors many2x field depending on specified field --- web_m2x_options/static/src/js/form.js | 39 +++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/web_m2x_options/static/src/js/form.js b/web_m2x_options/static/src/js/form.js index 0975a069..30b09b30 100644 --- a/web_m2x_options/static/src/js/form.js +++ b/web_m2x_options/static/src/js/form.js @@ -12,7 +12,7 @@ openerp.web_m2x_options = function (instance) { 'web_m2x_options.create_edit', 'web_m2x_options.limit',]; - instance.web.form.FieldMany2One.include({ + instance.web.form.FieldMany2One = instance.web.form.FieldMany2One.extend({ start: function() { this._super.apply(this, arguments); @@ -45,7 +45,7 @@ openerp.web_m2x_options = function (instance) { }, get_search_result: function (search_val) { - + var Objects = new instance.web.Model(this.field.relation); var def = $.Deferred(); var self = this; // add options limit used to change number of selections record @@ -65,7 +65,11 @@ openerp.web_m2x_options = function (instance) { if(typeof this.options.search_more === 'boolean') { this.search_more = this.options.search_more } - + + // add options field_color and colors to color item(s) depending on field_color value + this.field_color = this.options.field_color + this.colors = this.options.colors + var dataset = new instance.web.DataSet(this, this.field.relation, self.build_context()); var blacklist = this.get_search_blacklist(); @@ -102,6 +106,28 @@ openerp.web_m2x_options = function (instance) { id: x[0], }; }); + + // Search result value colors + + if (self.colors && self.field_color) { + var value_ids = []; + for (var index in values) { + value_ids.push(values[index].id); + } + + // RPC request to get field_color from Objects + Objects.query([self.field_color]) + .filter([['id', 'in', value_ids]]) + .all().done(function (objects) { + console.log(objects); + for (var index in objects) { + var value = values[index]; + var color = self.colors[objects[index].state] || 'black'; + value.label = ''+value.label+''; + } + def.resolve(values); + }); + } // search more... if more results than max @@ -162,8 +188,11 @@ openerp.web_m2x_options = function (instance) { classname: 'oe_m2o_dropdown_option' }); } - - def.resolve(values); + + // Check if colors specified to wait for RPC + if (!(self.field_color && self.colors)){ + def.resolve(values); + } }); return def;