[ADD] a bunch of stuff to make it possible to enable and disable filters when clicking on them (yay)

bzr revid: xmo@openerp.com-20110324161842-6lri6u9pu0msa5cx
This commit is contained in:
Xavier Morel 2011-03-24 17:18:42 +01:00
parent 8a0faeb73f
commit 491e6017af
2 changed files with 138 additions and 24 deletions

View File

@ -278,12 +278,14 @@
<t t-name="SearchView">
<h3 class="title"><t t-esc="view.attrs['string']"/></h3>
<t t-call="SearchView.render_lines"/>
<form>
<t t-call="SearchView.render_lines"/>
<div style="text-align:right;">
<input id="search" type="button" value="Search"/>
<input type="button" value="Clear"/>
</div>
<div style="text-align:right;">
<input type="submit" value="Search"/>
<input type="reset" value="Clear"/>
</div>
</form>
</t>
<t t-name="SearchView.render_lines">
<table border="0" cellspacing="0" cellpadding="0"
@ -296,6 +298,7 @@
</table>
</t>
<button t-name="SearchView.filter" type="button"
t-att-id="element_id"
t-att-title="attrs.help"
t-att-class="attrs.string ? 'filter_label' : 'filter_icon'">
<img t-if="attrs.icon" t-att-src="'/base/static/openerp/img/icons/' + attrs.icon + '.png'" width="16" height="16"/>
@ -307,17 +310,19 @@
><t t-raw="filter.render(defaults)"/></t
></span>
<t t-name="SearchView.field">
<label style="display: block" t-att-title="attrs.help">
<label style="display: block" t-att-title="attrs.help"
t-att-for="element_id">
<t t-esc="attrs.string || attrs.name"/>
<span t-if="attrs.help">(?)</span>
</label>
<div style="white-space: nowrap;">
<input type="text" t-att-name="attrs.name"/>
<input type="text" t-att-name="attrs.name" t-att-id="element_id"/>
<t t-if="filters.length" t-raw="filters.render(defaults)"/>
</div>
</t>
<t t-name="SearchView.group">
<div t-att-class="'searchview_group ' + (attrs.expand == '0' ? 'folded' : 'expanded')">
<div t-att-class="'searchview_group ' + (attrs.expand == '0' ? 'folded' : 'expanded')"
t-att-id="element_id">
<a class="searchview_group_string" href="#" onclick="$(this).parent().toggleClass('folded expanded')"><t t-esc="attrs.string"/></a>
<div class="searchview_group_content">
<t t-call="SearchView.render_lines"/>

View File

@ -363,9 +363,8 @@ openerp.base.SearchView = openerp.base.Controller.extend({
this.dataset = dataset;
this.model = dataset.model;
this.view_id = view_id;
this.input_index = 0;
this.input_ids = {};
this.domain = [];
this.inputs = [];
},
start: function() {
//this.log('Starting SearchView '+this.model+this.view_id)
@ -376,6 +375,7 @@ openerp.base.SearchView = openerp.base.Controller.extend({
*
* @param {Array} items a list of nodes to convert to widgets
* @param {Object} fields a mapping of field names to (ORM) field attributes
* @returns Array
*/
make_widgets: function (items, fields) {
var rows = [],
@ -416,7 +416,12 @@ openerp.base.SearchView = openerp.base.Controller.extend({
return rows;
},
/**
* Creates a field for the provided field descriptor item (which comes
* from fields_view_get)
*
* @param {Object} item fields_view_get node for the field
* @param {Object} field fields_get result for the field
* @returns openerp.base.search.Field
*/
make_field: function (item, field) {
// TODO: should fetch from an actual registry
@ -443,6 +448,7 @@ openerp.base.SearchView = openerp.base.Controller.extend({
console.info('View field', field);
console.info('In view', this);
console.groupEnd();
return null;
}
},
on_loaded: function(data) {
@ -459,9 +465,36 @@ openerp.base.SearchView = openerp.base.Controller.extend({
'defaults': default_values
});
this.$element.html(render);
// TODO: setup events
this.$element.find('form')
.submit(this.on_search)
.bind('reset', this.on_reset);
setTimeout(function () {
// start() all the widgets
_(lines).chain().flatten().each(function (widget) {
widget.start();
});
}, 0);
},
on_search: function () {}
on_search: function (e) {
e.preventDefault();
var inputs_ = _(this.inputs);
var domains = inputs_.chain()
.map(function (input) { return input.get_domain(); })
.compact()
.value();
var contexts = inputs_.chain()
.map(function (input) { return input.get_context(); })
.compact()
.value();
console.log('domains', domains);
console.log('contexts', contexts);
},
on_reset: function (e) {
e.preventDefault();
console.log('on_reset');
}
});
openerp.base.search = {};
@ -470,28 +503,62 @@ openerp.base.search.Widget = openerp.base.Controller.extend({
init: function (view) {
this.view = view;
},
/**
* Sets and returns a globally unique identifier for the widget.
*
* If a prefix is appended, the identifier will be appended to it.
*
* @params sections prefix sections, empty/falsy sections will be removed
*/
make_id: function () {
this.element_id = _.uniqueId(
['search'].concat(
_.compact(_.toArray(arguments)),
['']).join('_'));
return this.element_id;
},
/**
* "Starts" the widgets. Called at the end of the rendering, this allows
* widgets to hook themselves to their view sections.
*
* On widgets, if they kept a reference to a view and have an element_id,
* will fetch and set their root element on $element.
*/
start: function () {
this._super();
if (this.view && this.element_id) {
// id is unique, and no getElementById on elements
this.$element = $(document.getElementById(
this.element_id));
}
},
/**
* "Stops" the widgets. Called when the view destroys itself, this
* lets the widgets clean up after themselves.
*/
stop: function () {
delete this.view;
this._super();
},
render: function (defaults) {
return QWeb.render(
this.template, _.extend(this, {
defaults: defaults
}));
}
// TODO: rendering
// TODO: validation
});
openerp.base.search.Filter = openerp.base.search.Widget.extend({
template: 'SearchView.filter',
// TODO: force rendering
init: function (node, view) {
this._super(view);
this.attrs = node.attrs;
}
});
openerp.base.search.FilterGroup = openerp.base.search.Widget.extend({
template: 'SearchView.filters',
init: function (filters, view) {
this._super(view);
this.filters = filters;
this.view.inputs.push(this);
},
start: function () {
this._super();
_.each(this.filters, function (filter) {
filter.start();
});
}
});
openerp.base.search.Group = openerp.base.search.Widget.extend({
@ -503,9 +570,46 @@ openerp.base.search.Group = openerp.base.search.Widget.extend({
this.attrs = view_section.attrs;
this.lines = view.make_widgets(
view_section.children, fields);
this.make_id('group');
},
start: function () {
this._super();
_(this.lines)
.chain()
.flatten()
.each(function (widget) { widget.start(); });
}
});
openerp.base.search.Field = openerp.base.search.Widget.extend({
openerp.base.search.Input = openerp.base.search.Widget.extend({
init: function (view) {
this._super(view);
this.view.inputs.push(this);
},
get_context: function () {
throw new Error(
"get_context not implemented for widget " + this.attrs.type);
},
get_domain: function () {
throw new Error(
"get_domain not implemented for widget " + this.attrs.type);
}
});
openerp.base.search.Filter = openerp.base.search.Input.extend({
template: 'SearchView.filter',
// TODO: force rendering
init: function (node, view) {
this._super(view);
this.attrs = node.attrs;
this.make_id('filter', this.attrs.name);
},
start: function () {
this._super();
this.$element.click(function () {
$(this).toggleClass('enabled');
});
}
});
openerp.base.search.Field = openerp.base.search.Input.extend({
template: 'SearchView.field',
// TODO: set default values
// TODO: get context, domain
@ -518,6 +622,11 @@ openerp.base.search.Field = openerp.base.search.Widget.extend({
return new openerp.base.search.Filter(
filter_node, view);
}), view);
this.make_id('input', field.type, this.attrs.name);
},
start: function () {
this._super();
this.filters.start();
}
});
openerp.base.search.CharField = openerp.base.search.Field.extend({