[REF] extract building of a facet descriptor from m2o/selection pairs into a function

bzr revid: xmo@openerp.com-20120427083024-zqq9hs203uaywlbr
This commit is contained in:
Xavier Morel 2012-04-27 10:30:24 +02:00
parent da37acedf6
commit 661eddb330
1 changed files with 22 additions and 22 deletions

View File

@ -1129,6 +1129,22 @@ instance.web.search.FloatField = instance.web.search.NumberField.extend(/** @len
} }
} }
}); });
/**
* Utility function for m2o & selection fields taking a selection/name_get pair
* (value, name) and converting it to a Facet descriptor
*
* @param {instance.web.search.Field} field holder field
* @param {Array} pair pair value to convert
*/
function facet_from(field, pair) {
return {
field: field,
category: field['attrs'].string,
values: [{label: pair[1], value: pair[0]}]
};
}
/** /**
* @class * @class
* @extends instance.web.search.Field * @extends instance.web.search.Field
@ -1163,11 +1179,7 @@ instance.web.search.SelectionField = instance.web.search.Field.extend(/** @lends
.map(function (sel) { .map(function (sel) {
return { return {
label: sel[1], label: sel[1],
facet: { facet: facet_from(self, sel)
category: self.attrs.string,
field: self,
values: [{value: sel[0], label: sel[1]}]
}
}; };
}).value(); }).value();
if (_.isEmpty(results)) { return $.when(null); } if (_.isEmpty(results)) { return $.when(null); }
@ -1180,11 +1192,7 @@ instance.web.search.SelectionField = instance.web.search.Field.extend(/** @lends
return sel[0] === value; return sel[0] === value;
}); });
if (!match) { return $.when(null); } if (!match) { return $.when(null); }
return $.when({ return $.when(facet_from(this, match));
category: this.attrs.string,
field: this,
values: [{label: match[1], value: match[0]}]
});
}, },
get_value: function (facet) { get_value: function (facet) {
return facet.get('values'); return facet.get('values');
@ -1271,27 +1279,19 @@ instance.web.search.ManyToOneField = instance.web.search.CharField.extend({
_(results).map(function (result) { _(results).map(function (result) {
return { return {
label: result[1], label: result[1],
facet: { facet: facet_from(self, result)
category: self.attrs.string,
field: self,
values: [{label: result[1], value: result[0]}]
}
}; };
})); }));
}); });
}, },
facet_for: function (value) { facet_for: function (value) {
var self = this, fromPair = function (a) { return { var self = this;
category: self.attrs.string,
field: self,
values: [{label: a[1], value: a[0]}]
} };
if (value instanceof Array) { if (value instanceof Array) {
return $.when(fromPair(value)); return $.when(facet_from(this, value));
} }
return this.model.call('name_get', [value], {}).pipe(function (names) { return this.model.call('name_get', [value], {}).pipe(function (names) {
if (_(names).isEmpty()) { return null; } if (_(names).isEmpty()) { return null; }
return fromPair(names[0]); return facet_from(self, names[0]);
}) })
}, },
make_domain: function (name, operator, facet) { make_domain: function (name, operator, facet) {