From 1f84b7a37dd0bee6ad599e3fdc8afd5f2520a1b7 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 26 Jun 2012 11:13:07 +0200 Subject: [PATCH 1/2] [ADD] set 'default_' key in context when searching on a single existing record in an m2o, by default bzr revid: xmo@openerp.com-20120626091307-45vygymu8y1198ar --- addons/web/static/src/js/search.js | 9 +++++++++ addons/web/static/test/search.js | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/addons/web/static/src/js/search.js b/addons/web/static/src/js/search.js index 85521a14651..4faf9dabdd5 100644 --- a/addons/web/static/src/js/search.js +++ b/addons/web/static/src/js/search.js @@ -1530,6 +1530,15 @@ instance.web.search.ManyToOneField = instance.web.search.CharField.extend({ return [[name, '=', facetValue.get('value')]]; } return this._super(name, operator, facetValue); + }, + get_context: function (facet) { + var values = facet.values; + if (!this.attrs.context && values.length === 1) { + var c = {}; + c['default_' + this.attrs.name] = values.at(0).get('value'); + return c; + } + return this._super(facet); } }); diff --git a/addons/web/static/test/search.js b/addons/web/static/test/search.js index e5beab22498..dc94fad6d7a 100644 --- a/addons/web/static/test/search.js +++ b/addons/web/static/test/search.js @@ -843,6 +843,25 @@ $(document).ready(function () { deepEqual(f.get_domain(facet), [['foo', '=', 42]], "m2o should use identity if default domain"); + deepEqual(f.get_context(facet), {default_foo: 42}, + "m2o should use value as context default"); + }); + test("M2O default multiple values", function () { + var f = new instance.web.search.ManyToOneField( + {}, {name: 'foo'}, {inputs: []}); + var facet = new instance.web.search.Facet({ + field: f, + values: [ + {label: "Foo", value: 42}, + {label: "Bar", value: 36} + ] + }); + + deepEqual(f.get_domain(facet).__domains, + [['|'], [['foo', '=', 42]], [['foo', '=', 36]]], + "m2o should or multiple values"); + equal(f.get_context(facet), null, + "m2o should not have default context in case of multiple values"); }); test("M2O custom operator", function () { var f = new instance.web.search.ManyToOneField( @@ -854,6 +873,8 @@ $(document).ready(function () { deepEqual(f.get_domain(facet), [['foo', 'boos', 'Foo']], "m2o should use label with custom operators"); + deepEqual(f.get_context(facet), {default_foo: 42}, + "m2o should use value as context default"); }); test("M2O custom domain & context", function () { var f = new instance.web.search.ManyToOneField({attrs: { From 7b0bef5bccf900100b7c463a0c3ae62f4db27064 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Wed, 27 Jun 2012 11:57:44 +0200 Subject: [PATCH 2/2] [FIX] incorrect handling of present-but-empty contexts, which seem to be rather common bzr revid: xmo@openerp.com-20120627095744-ijhf7p769pw4fqdg --- addons/web/static/src/js/search.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addons/web/static/src/js/search.js b/addons/web/static/src/js/search.js index 4faf9dabdd5..087c4c22294 100644 --- a/addons/web/static/src/js/search.js +++ b/addons/web/static/src/js/search.js @@ -1234,7 +1234,7 @@ instance.web.search.Field = instance.web.search.Input.extend( /** @lends instanc var self = this; // A field needs a context to send when active var context = this.attrs.context; - if (!context || !facet.values.length) { + if (_.isEmpty(context) || !facet.values.length) { return; } var contexts = facet.values.map(function (facetValue) { @@ -1533,7 +1533,7 @@ instance.web.search.ManyToOneField = instance.web.search.CharField.extend({ }, get_context: function (facet) { var values = facet.values; - if (!this.attrs.context && values.length === 1) { + if (_.isEmpty(this.attrs.context) && values.length === 1) { var c = {}; c['default_' + this.attrs.name] = values.at(0).get('value'); return c;