From 300d5be0e6af4d24d09419e0be6807b265da3cd8 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Thu, 28 Mar 2013 13:03:37 +0100 Subject: [PATCH 1/2] [FIX] web: int/float fields were not offering auto-completion in search views, making them un-searchable except via advanced search Added the missing complete() function and removed the incorrect value_from() override that seemed to be a leftover remnant of the 6.1 search field implementation of get_value(), wrongly renamed for 7.0. bzr revid: odo@openerp.com-20130328120337-lao4o9i0owsbpysi --- addons/web/static/src/js/search.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/addons/web/static/src/js/search.js b/addons/web/static/src/js/search.js index c1ace6b57f3..50576687808 100644 --- a/addons/web/static/src/js/search.js +++ b/addons/web/static/src/js/search.js @@ -1346,20 +1346,22 @@ instance.web.search.CharField = instance.web.search.Field.extend( /** @lends ins } }); instance.web.search.NumberField = instance.web.search.Field.extend(/** @lends instance.web.search.NumberField# */{ - value_from: function () { - if (!this.$el.val()) { - return null; - } - var val = this.parse(this.$el.val()), - check = Number(this.$el.val()); - if (isNaN(val) || val !== check) { - this.$el.addClass('error'); - throw new instance.web.search.Invalid( - this.attrs.name, this.$el.val(), this.error_message); - } - this.$el.removeClass('error'); - return val; - } + complete: function (value) { + var val = this.parse(value); + if (!val || isNaN(val)) { return $.when(); } + var label = _.str.sprintf(_.str.escapeHTML( + _t("Search %(field)s for: %(value)s")), { + field: '' + this.attrs.string + '', + value: '' + _.str.escapeHTML(value) + ''}); + return $.when([{ + label: label, + facet: { + category: this.attrs.string, + field: this, + values: [{label: value, value: val}] + } + }]); + }, }); /** * @class From 35d61bbd8bbfd034b3d9e01347f0f13ee0ef17ac Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Thu, 28 Mar 2013 16:35:02 +0100 Subject: [PATCH 2/2] [FIX] web search: correct previous fix to properly handle searching for 0 on number fields Also removed useless HTML escaping and added basic tests. bzr revid: odo@openerp.com-20130328153502-v71q2m60wh37ganq --- addons/web/static/src/js/search.js | 6 ++-- addons/web/static/test/search.js | 53 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/addons/web/static/src/js/search.js b/addons/web/static/src/js/search.js index 50576687808..37f8bd8b3f2 100644 --- a/addons/web/static/src/js/search.js +++ b/addons/web/static/src/js/search.js @@ -1348,9 +1348,9 @@ instance.web.search.CharField = instance.web.search.Field.extend( /** @lends ins instance.web.search.NumberField = instance.web.search.Field.extend(/** @lends instance.web.search.NumberField# */{ complete: function (value) { var val = this.parse(value); - if (!val || isNaN(val)) { return $.when(); } - var label = _.str.sprintf(_.str.escapeHTML( - _t("Search %(field)s for: %(value)s")), { + if (isNaN(val)) { return $.when(); } + var label = _.str.sprintf( + _t("Search %(field)s for: %(value)s"), { field: '' + this.attrs.string + '', value: '' + _.str.escapeHTML(value) + ''}); return $.when([{ diff --git a/addons/web/static/test/search.js b/addons/web/static/test/search.js index 6c4b6d61d3c..73d984506b2 100644 --- a/addons/web/static/test/search.js +++ b/addons/web/static/test/search.js @@ -614,6 +614,59 @@ openerp.testing.section('search.completions', { {relation: 'dummy.model'}, view); return f.complete("bob"); }); + test('Integer: invalid', {asserts: 1}, function (instance) { + var view = {inputs: []}; + var f = new instance.web.search.IntegerField( + {attrs: {string: "Dummy"}}, {}, view); + return f.complete("qux") + .done(function (completions) { + ok(!completions, "non-number => no completion"); + }); + }); + test('Integer: non-zero', {asserts: 5}, function (instance) { + var view = {inputs: []}; + var f = new instance.web.search.IntegerField( + {attrs: {string: "Dummy"}}, {}, view); + return f.complete("-2") + .done(function (completions) { + equal(completions.length, 1, "number fields provide 1 completion only"); + var facet = new instance.web.search.Facet(completions[0].facet); + equal(facet.get('category'), f.attrs.string); + equal(facet.get('field'), f); + var value = facet.values.at(0); + equal(value.get('label'), "-2"); + equal(value.get('value'), -2); + }); + }); + test('Integer: zero', {asserts: 3}, function (instance) { + var view = {inputs: []}; + var f = new instance.web.search.IntegerField( + {attrs: {string: "Dummy"}}, {}, view); + return f.complete("0") + .done(function (completions) { + equal(completions.length, 1, "number fields provide 1 completion only"); + var facet = new instance.web.search.Facet(completions[0].facet); + var value = facet.values.at(0); + equal(value.get('label'), "0"); + equal(value.get('value'), 0); + }); + }); + test('Float: non-zero', {asserts: 5}, function (instance) { + var view = {inputs: []}; + var f = new instance.web.search.FloatField( + {attrs: {string: "Dummy"}}, {}, view); + return f.complete("42.37") + .done(function (completions) { + equal(completions.length, 1, "float fields provide 1 completion only"); + var facet = new instance.web.search.Facet(completions[0].facet); + equal(facet.get('category'), f.attrs.string); + equal(facet.get('field'), f); + var value = facet.values.at(0); + equal(value.get('label'), "42.37"); + equal(value.get('value'), 42.37); + }); + }); + }); openerp.testing.section('search.serialization', { dependencies: ['web.search'],