[IMP] fix a pair of warnings, remove some pointless code in custom filters, use registry

bzr revid: xmo@openerp.com-20110331133117-epdpu0sw18zslstb
This commit is contained in:
Xavier Morel
2011-03-31 15:31:17 +02:00
3 changed files with 96 additions and 77 deletions
+8 -2
View File
@@ -71,7 +71,10 @@ openerp.base.KeyNotFound = openerp.base.NotFound.extend(
* @param {String} key the key which could not be found
*/
init: function (key) {
self.key = key;
this.key = key;
},
toString: function () {
return "The key " + this.key + " was not found";
}
});
openerp.base.ObjectNotFound = openerp.base.NotFound.extend(
@@ -85,7 +88,10 @@ openerp.base.ObjectNotFound = openerp.base.NotFound.extend(
* @param {String} path the invalid object path
*/
init: function (path) {
self.path = path;
this.path = path;
},
toString: function () {
return "Could not find any object of path " + this.path;
}
});
openerp.base.Registry = Class.extend(
+75 -64
View File
@@ -371,7 +371,7 @@ openerp.base.search.ExtendedSearch = openerp.base.BaseWidget.extend({
this._super(parent);
this.fields = fields;
},
add_group: function(group) {
add_group: function() {
var group = new openerp.base.search.ExtendedSearchGroup(this, this.fields);
var render = group.render({});
this.$element.find('.searchview_extended_groups_list').append(render);
@@ -382,10 +382,8 @@ openerp.base.search.ExtendedSearch = openerp.base.BaseWidget.extend({
var _this = this;
openerp.base.search.add_expand_listener(this.$element);
this.add_group();
this.$element.find('.searchview_extended_add_group').click(function (e) {
this.$element.find('.searchview_extended_add_group').click(function () {
_this.add_group();
e.stopPropagation();
e.preventDefault();
});
},
get_context: function() {
@@ -395,9 +393,8 @@ openerp.base.search.ExtendedSearch = openerp.base.BaseWidget.extend({
if(this.$element.hasClass("folded")) {
return null;
}
var domain = _.reduce(this.children,
function(mem, x) { return mem.concat(x.get_domain());}, []);
return domain;
return _.reduce(this.children,
function(mem, x) { return mem.concat(x.get_domain());}, []);
}
});
@@ -418,16 +415,12 @@ openerp.base.search.ExtendedSearchGroup = openerp.base.BaseWidget.extend({
this._super();
var _this = this;
this.add_prop();
this.$element.find('.searchview_extended_add_proposition').click(function (e) {
this.$element.find('.searchview_extended_add_proposition').click(function () {
_this.add_prop();
e.stopPropagation();
e.preventDefault();
});
var delete_btn = this.$element.find('.searchview_extended_delete_group');
delete_btn.click(function (e) {
_this.stop();
e.stopPropagation();
e.preventDefault();
});
},
get_domain: function() {
@@ -436,30 +429,16 @@ openerp.base.search.ExtendedSearchGroup = openerp.base.BaseWidget.extend({
}).compact().value();
var choice = this.$element.find(".searchview_extended_group_choice").val();
var op = choice == "all" ? "&" : "|";
var domain = [].concat(choice == "none" ? ['!'] : [],
_.map(_.range(_.max([0,props.length - 1])), function(x) { return op; }),
props);
return domain;
return [].concat(choice == "none" ? ['!'] : [],
_.map(_.range(_.max([0,props.length - 1])), function() { return op; }),
props);
}
});
openerp.base.search.extended_filters_types = {
char: {
operators: [
{value: "ilike", text: "contains"},
{value: "not like", text: "doesn't contain"},
{value: "=", text: "is equal to"},
{value: "!=", text: "is not equal to"},
{value: ">", text: "greater than"},
{value: "<", text: "less than"},
{value: ">=", text: "greater or equal than"},
{value: "<=", text: "less or equal than"},
],
build_component: function(parent) {
return new openerp.base.search.ExtendedSearchProposition.Char(parent);
}
}
};
openerp.base.search.custom_filters = new openerp.base.Registry({
'char': 'openerp.base.search.ExtendedSearchProposition.Char',
'datetime': 'openerp.base.search.ExtendedSearchProposition.DateTime'
});
openerp.base.search.ExtendedSearchProposition = openerp.base.BaseWidget.extend({
template: 'SearchView.extended_search.proposition',
@@ -467,64 +446,72 @@ openerp.base.search.ExtendedSearchProposition = openerp.base.BaseWidget.extend({
init: function (parent, fields) {
this._super(parent);
this.fields = _(fields).chain()
.map(function(val,key) {return {name:key, obj:val};})
.sortBy(function(x) {return x.obj.string;}).value();
.map(function(val, key) { return _.extend({}, val, {'name': key}); })
.sortBy(function(field) {return field.string;})
.value();
this.attrs = {_: _, fields: this.fields, selected: null};
this.value_component = null;
this.value = null;
},
start: function () {
this._super();
this.set_selected(this.fields.length > 0 ? this.fields[0] : null);
this.select_field(this.fields.length > 0 ? this.fields[0] : null);
var _this = this;
this.$element.find(".searchview_extended_prop_field").change(function(e) {
this.$element.find(".searchview_extended_prop_field").change(function() {
_this.changed();
e.stopPropagation();
e.preventDefault();
});
var delete_btn = this.$element.find('.searchview_extended_delete_prop');
delete_btn.click(function (e) {
delete_btn.click(function () {
_this.stop();
e.stopPropagation();
e.preventDefault();
});
},
changed: function() {
var nval = this.$element.find(".searchview_extended_prop_field").val();
if(this.attrs.selected == null || nval != this.attrs.selected.name) {
this.set_selected(_.detect(this.fields, function(x) {return x.name == nval;}));
this.select_field(_.detect(this.fields, function(x) {return x.name == nval;}));
}
},
set_selected: function(selected) {
/**
* Selects the provided field object
*
* @param field a field descriptor object (as returned by fields_get, augmented by the field name)
*/
select_field: function(field) {
var _this = this;
if(this.attrs.selected != null) {
this.value_component.stop();
this.value_component = null;
this.value.stop();
this.value = null;
this.$element.find('.searchview_extended_prop_op').html('');
}
this.attrs.selected = selected;
if(selected == null) {
this.attrs.selected = field;
if(field == null) {
return;
}
var type = selected.obj.type;
var extended_filters_types = openerp.base.search.extended_filters_types;
type = type in extended_filters_types ? type : "char";
_.each(extended_filters_types[type].operators, function(operator) {
var option = jQuery('<option/>')
.attr('value', operator.value)
.text(operator.text)
.appendTo(_this.$element.find('.searchview_extended_prop_op'));
});
this.value_component = extended_filters_types[type].build_component(this);
var render = this.value_component.render({});
this.$element.find('.searchview_extended_prop_value').html(render);
this.value_component.start();
try {
this.value = new (openerp.base.search.custom_filters.get_object(field.type))
(this);
_.each(this.value.operators, function(operator) {
var option = jQuery('<option>', {value: operator.value})
.text(operator.text)
.appendTo(_this.$element.find('.searchview_extended_prop_op'));
});
this.$element.find('.searchview_extended_prop_value').html(
this.value.render({}));
this.value.start();
} catch (e) {
if (! e instanceof openerp.base.KeyNotFound) {
throw e;
}
this.attrs.selected = null;
this.log('Unknow field type ' + e.key);
}
},
get_proposition: function() {
if ( this.attrs.selected == null)
return null;
var field = this.attrs.selected.name;
var op = this.$element.find('.searchview_extended_prop_op').val();
var value = this.value_component.get_value();
var value = this.value.get_value();
return [field, op, value];
}
});
@@ -532,7 +519,31 @@ openerp.base.search.ExtendedSearchProposition = openerp.base.BaseWidget.extend({
openerp.base.search.ExtendedSearchProposition.Char = openerp.base.BaseWidget.extend({
template: 'SearchView.extended_search.proposition.char',
identifier_prefix: 'extended-search-proposition-char',
operators: [
{value: "ilike", text: "contains"},
{value: "not ilike", text: "doesn't contain"},
{value: "=", text: "is equal to"},
{value: "!=", text: "is not equal to"},
{value: ">", text: "greater than"},
{value: "<", text: "less than"},
{value: ">=", text: "greater or equal than"},
{value: "<=", text: "less or equal than"}
],
get_value: function() {
return this.$element.val();
}
});
openerp.base.search.ExtendedSearchProposition.DateTime = openerp.base.BaseWidget.extend({
template: 'SearchView.extended_search.proposition.char',
identifier_prefix: 'extended-search-proposition-datetime',
operators: [
{value: "=", text: "is equal to"},
{value: "!=", text: "is not equal to"},
{value: ">", text: "greater than"},
{value: "<", text: "less than"},
{value: ">=", text: "greater or equal than"},
{value: "<=", text: "less or equal than"}
],
get_value: function() {
return this.$element.val();
}
+13 -11
View File
@@ -368,8 +368,9 @@
<t t-set="expand" t-value="false"/>
<t t-set="label" t-value="'Extended Filters'"/>
<t t-set="content">
<div class="searchview_extended_groups_list"></div>
<button class="searchview_extended_add_group">Add group of conditions</button>
<div class="searchview_extended_groups_list"/>
<button class="searchview_extended_add_group"
type="button">Add group of conditions</button>
</t>
</t>
</t>
@@ -382,8 +383,10 @@
</select>
<div class="searchview_extended_propositions_list">
</div>
<button class="searchview_extended_add_proposition">Add condition</button>
<button class="searchview_extended_delete_group">Delete this group</button>
<button class="searchview_extended_add_proposition"
type="button">Add condition</button>
<button class="searchview_extended_delete_group"
type="button">Delete this group</button>
</div>
</t>
<t t-name="SearchView.extended_search.proposition">
@@ -392,18 +395,17 @@
<t t-foreach="attrs.fields" t-as="field">
<option t-att="{'selected': field === attrs.selected ? 'selected' : null}"
t-att-value="field.name">
<t t-esc="field.obj.string"/>
<t t-esc="field.string"/>
</option>
</t>
</select>
<select class="searchview_extended_prop_op">
</select>
<span class="searchview_extended_prop_value">
</span>
<button class="searchview_extended_delete_prop">Delete this condition</button>
<select class="searchview_extended_prop_op"/>
<span class="searchview_extended_prop_value"/>
<button class="searchview_extended_delete_prop"
type="button">Delete this condition</button>
</div>
</t>
<t t-name="SearchView.extended_search.proposition.char">
<input t-att-id="element_id"></input>
<input t-att-id="element_id"/>
</t>
</templates>