[MERGE] ViewManager now triggers do_search() in on_mode_switch()

The views doesn't have to worry about the search in do_show().
They just have to listen to do_search() if they are searchable.

bzr revid: fme@openerp.com-20111004094738-at91ab6a2kkxf8bq
This commit is contained in:
Fabien Meghazi 2011-10-04 11:47:38 +02:00
commit f76b2f55ae
9 changed files with 108 additions and 169 deletions

View File

@ -13,24 +13,35 @@ openerp.web.SearchView = openerp.web.Widget.extend(/** @lends openerp.web.Search
* @param view_id
* @param defaults
*/
init: function(parent, dataset, view_id, defaults) {
init: function(parent, dataset, view_id, defaults, hidden) {
this._super(parent);
this.dataset = dataset;
this.model = dataset.model;
this.view_id = view_id;
this.defaults = defaults || {};
this.has_defaults = !_.isEmpty(this.defaults);
this.inputs = [];
this.enabled_filters = [];
this.has_focus = false;
this.hidden = !!hidden;
this.headless = this.hidden && !this.has_defaults;
this.ready = $.Deferred();
},
start: function() {
this._super();
this.rpc("/web/searchview/load", {"model": this.model, "view_id":this.view_id}, this.on_loaded);
if (this.hidden) {
this.$element.hide();
}
if (this.headless) {
this.ready.resolve();
} else {
this.rpc("/web/searchview/load", {"model": this.model, "view_id":this.view_id}, this.on_loaded);
}
return this.ready.promise();
},
show: function () {
@ -137,10 +148,7 @@ openerp.web.SearchView = openerp.web.Widget.extend(/** @lends openerp.web.Search
'defaults': this.defaults
});
// We don't understand why the following commented line does not work in Chrome but
// the non-commented line does. As far as we investigated, only God knows.
//this.$element.html(render);
jQuery(render).appendTo(this.$element);
this.$element.html(render);
this.$element.find(".oe_search-view-custom-filter-btn").click(ext.on_activate);
var f = this.$element.find('form');
@ -246,10 +254,13 @@ openerp.web.SearchView = openerp.web.Widget.extend(/** @lends openerp.web.Search
* @param e jQuery event object coming from the "Search" button
*/
do_search: function (e) {
if (this.headless && !this.has_defaults) {
return this.on_search([], [], []);
}
// reset filters management
var select = this.$element.find(".oe_search-view-filters-management");
select.val("_filters");
if (e && e.preventDefault) { e.preventDefault(); }
var data = this.build_search_data();

View File

@ -463,9 +463,6 @@ openerp.web.FormView = openerp.web.View.extend( /** @lends openerp.web.FormView#
return $.Deferred().then(success).resolve(_.extend(r, {created: true}));
}
},
do_search: function (domains, contexts, groupbys) {
console.debug("Searching form");
},
on_action: function (action) {
console.debug('Executing action', action);
},

View File

@ -391,9 +391,6 @@ openerp.web.ListView = openerp.web.View.extend( /** @lends openerp.web.ListView#
if (this.sidebar) {
this.sidebar.$element.show();
}
if (!_(this.dataset.ids).isEmpty()) {
this.reload_content();
}
},
do_hide: function () {
this.$element.hide();
@ -442,41 +439,22 @@ openerp.web.ListView = openerp.web.View.extend( /** @lends openerp.web.ListView#
self.compute_aggregates();
}));
},
/**
* Event handler for a search, asks for the computation/folding of domains
* and contexts (and group-by), then reloads the view's content.
*
* @param {Array} domains a sequence of literal and non-literal domains
* @param {Array} contexts a sequence of literal and non-literal contexts
* @param {Array} groupbys a sequence of literal and non-literal group-by contexts
* @returns {$.Deferred} fold request evaluation promise
*/
do_search: function (domains, contexts, groupbys) {
return this.rpc('/web/session/eval_domain_and_context', {
domains: _([this.dataset.get_domain()].concat(domains)).compact(),
contexts: _([this.dataset.get_context()].concat(contexts)).compact(),
group_by_seq: groupbys
}, $.proxy(this, 'do_actual_search'));
},
/**
* Handler for the result of eval_domain_and_context, actually perform the
* searching
*
* @param {Object} results results of evaluating domain and process for a search
*/
do_actual_search: function (results) {
do_search: function (domain, context, group_by) {
this.groups.datagroup = new openerp.web.DataGroup(
this, this.model,
results.domain,
results.context,
results.group_by);
this, this.model, domain, context, group_by);
this.groups.datagroup.sort = this.dataset._sort;
if (_.isEmpty(results.group_by) && !results.context['group_by_no_leaf']) {
results.group_by = null;
if (_.isEmpty(group_by) && !context['group_by_no_leaf']) {
group_by = null;
}
this.reload_view(!!results.group_by, results.context).then(
this.reload_view(!!group_by, context).then(
$.proxy(this, 'reload_content'));
},
/**

View File

@ -56,11 +56,11 @@ openerp.web.list_editable = function (openerp) {
|| this.defaults.editable);
},
/**
* Replace do_actual_search to handle editability process
* Replace do_search to handle editability process
*/
do_actual_search: function (results) {
this.set_editable(results.context['set_editable']);
this._super(results);
do_search: function(domain, context, group_by) {
this.set_editable(context['set_editable']);
this._super.apply(this, arguments);
},
/**
* Replace do_add_record to handle editability (and adding new record

View File

@ -166,6 +166,7 @@ db.web.ViewManager = db.web.Widget.extend(/** @lends db.web.ViewManager# */{
this.model = dataset ? dataset.model : undefined;
this.dataset = dataset;
this.searchview = null;
this.last_search = false;
this.active_view = null;
this.views_src = _.map(views, function(x) {return x instanceof Array? {view_id: x[0], view_type: x[1]} : x;});
this.views = {};
@ -224,35 +225,21 @@ db.web.ViewManager = db.web.Widget.extend(/** @lends db.web.ViewManager# */{
controller.set_embedded_view(view.embedded_view);
}
controller.do_switch_view.add_last(this.on_mode_switch);
if (view_type === 'list' && this.flags.search_view === false && this.action && this.action['auto_search']) {
// In case the search view is not instantiated: manually call ListView#search
var domains = !_(self.action.domain).isEmpty()
? [self.action.domain] : [],
contexts = !_(self.action.context).isEmpty()
? [self.action.context] : [];
controller.on_loaded.add({
callback: function () {
controller.do_search(domains, contexts, []);
},
position: 'last',
unique: true
});
}
var container = $("#" + this.element_id + '_view_' + view_type);
view_promise = controller.appendTo(container);
this.views[view_type].controller = controller;
$.when(view_promise).then(function() {
self.on_controller_inited(view_type, controller);
if (self.searchview && view.controller.searchable !== false) {
self.do_searchview_search();
}
});
this.views[view_type].controller = controller;
} else if (this.searchview && view.controller.searchable !== false) {
self.do_searchview_search();
}
if (this.searchview) {
if (view.controller.searchable === false) {
this.searchview.hide();
} else {
this.searchview.show();
}
this.searchview[(view.controller.searchable === false || this.searchview.hidden) ? 'hide' : 'show']();
}
this.$element
@ -272,14 +259,6 @@ db.web.ViewManager = db.web.Widget.extend(/** @lends db.web.ViewManager# */{
}
return view_promise;
},
/**
* Event launched when a controller has been inited.
*
* @param {String} view_type type of view
* @param {String} view the inited controller
*/
on_controller_inited: function(view_type, view) {
},
/**
* Sets up the current viewmanager's search view.
*
@ -293,14 +272,37 @@ db.web.ViewManager = db.web.Widget.extend(/** @lends db.web.ViewManager# */{
}
this.searchview = new db.web.SearchView(
this, this.dataset,
view_id, search_defaults);
view_id, search_defaults, this.flags.search_view === false);
this.searchview.on_search.add(function(domains, contexts, groupbys) {
var controller = self.views[self.active_view].controller;
controller.do_search.call(controller, domains, contexts, groupbys);
});
this.searchview.on_search.add(this.do_searchview_search);
return this.searchview.appendTo($("#" + this.element_id + "_search"));
},
do_searchview_search: function(domains, contexts, groupbys) {
var self = this,
controller = this.views[this.active_view].controller;
if (domains || contexts) {
this.rpc('/web/session/eval_domain_and_context', {
domains: [this.action.domain || []].concat(domains || []),
contexts: [this.action.context || {}].concat(contexts || []),
group_by_seq: groupbys || []
}, function (results) {
self.dataset.context = results.context;
self.dataset.domain = results.domain;
self.last_search = [results.domain, results.context, results.group_by];
controller.do_search(results.domain, results.context, results.group_by);
});
} else if (this.last_search) {
controller.do_search.apply(controller, this.last_search);
}
},
/**
* Event launched when a controller has been inited.
*
* @param {String} view_type type of view
* @param {String} view the inited controller
*/
on_controller_inited: function(view_type, view) {
},
/**
* Called when one of the view want to execute an action
*/
@ -360,23 +362,19 @@ db.web.ViewManagerAction = db.web.ViewManager.extend(/** @lends oepnerp.web.View
* launches an initial search after both views are done rendering.
*/
start: function() {
var self = this;
var self = this,
searchview_loaded,
search_defaults = {};
_.each(this.action.context, function (value, key) {
var match = /^search_default_(.*)$/.exec(key);
if (match) {
search_defaults[match[1]] = value;
}
});
// init search view
var searchview_id = this.action['search_view_id'] && this.action['search_view_id'][0];
var searchview_loaded;
if (this.flags.search_view !== false) {
var search_defaults = {};
_.each(this.action.context, function (value, key) {
var match = /^search_default_(.*)$/.exec(key);
if (match) {
search_defaults[match[1]] = value;
}
});
// init search view
var searchview_id = this.action['search_view_id'] && this.action['search_view_id'][0];
searchview_loaded = this.setup_search_view(
searchview_id || false, search_defaults);
}
searchview_loaded = this.setup_search_view(searchview_id || false, search_defaults);
var main_view_loaded = this._super();
@ -816,6 +814,8 @@ db.web.View = db.web.Widget.extend(/** @lends db.web.View# */{
},
do_switch_view: function(view) {
},
do_search: function(view) {
},
set_common_sidebar_sections: function(sidebar) {
sidebar.add_section('customize', "Customize", [
{

View File

@ -13,9 +13,8 @@ openerp.web_calendar.CalendarView = openerp.web.View.extend({
this.set_default_options(options);
this.dataset = dataset;
this.model = dataset.model;
this.fields_view = {};
this.view_id = view_id;
this.domain = this.dataset.domain || [];
this.context = this.dataset.context || {};
this.has_been_loaded = $.Deferred();
this.creating_event_id = null;
this.dataset_events = [];
@ -92,9 +91,6 @@ openerp.web_calendar.CalendarView = openerp.web.View.extend({
this.init_scheduler();
this.has_been_loaded.resolve();
if (this.dataset.ids.length) {
this.dataset.read_ids(this.dataset.ids, _.keys(this.fields), this.on_events_loaded);
}
},
init_scheduler: function() {
var self = this;
@ -293,26 +289,17 @@ openerp.web_calendar.CalendarView = openerp.web.View.extend({
}
return data;
},
do_search: function(domains, contexts, groupbys) {
do_search: function(domain, context, group_by) {
var self = this;
scheduler.clearAll();
$.when(this.has_been_loaded).then(function() {
self.rpc('/web/session/eval_domain_and_context', {
domains: domains,
contexts: contexts,
group_by_seq: groupbys
}, function (results) {
// TODO: handle non-empty results.group_by with read_group
self.dataset.context = self.context = results.context;
self.dataset.domain = self.domain = results.domain;
self.dataset.read_slice(_.keys(self.fields), {
offset:0,
limit: self.limit
}, function(events) {
self.dataset_events = events;
self.on_events_loaded(events);
}
);
// TODO: handle non-empty results.group_by with read_group
self.dataset.read_slice(_.keys(self.fields), {
offset: 0,
limit: self.limit
}, function(events) {
self.dataset_events = events;
self.on_events_loaded(events);
});
});
},

View File

@ -509,15 +509,7 @@ init: function(parent, dataset, view_id) {
do_search: function (domains, contexts, groupbys) {
var self = this;
this.grp = groupbys;
return this.rpc('/web/session/eval_domain_and_context', {
domains: domains,
contexts: contexts,
group_by_seq: groupbys
}, function (results) {
self.dataset.context = results.context;
self.dataset.domain = results.domain;
self.reload_gantt();
});
self.reload_gantt();
}
});

View File

@ -29,7 +29,6 @@ openerp.web_graph.GraphView = openerp.web.View.extend({
this.group_field = null;
},
do_show: function () {
// TODO: re-trigger search
this.$element.show();
},
do_hide: function () {
@ -79,9 +78,6 @@ openerp.web_graph.GraphView = openerp.web.View.extend({
}
}, this);
this.ordinate = this.columns[0].name;
this.dataset.read_slice(
this.list_fields(), {}, $.proxy(this, 'schedule_chart'));
},
schedule_chart: function(results) {
var self = this;
@ -366,24 +362,14 @@ openerp.web_graph.GraphView = openerp.web.View.extend({
});
},
do_search: function(domains, contexts, groupbys) {
var self = this;
this.rpc('/web/session/eval_domain_and_context', {
domains: domains,
contexts: contexts,
group_by_seq: groupbys
}, function (results) {
// TODO: handle non-empty results.group_by with read_group?
if(!_(results.group_by).isEmpty()){
self.abscissa = results.group_by[0];
} else {
self.abscissa = self.first_field;
}
self.dataset.read_slice(self.list_fields(), {
context: results.context,
domain: results.domain
}, $.proxy(self, 'schedule_chart'));
});
do_search: function(domain, context, group_by) {
// TODO: handle non-empty group_by with read_group?
if (!_(group_by).isEmpty()) {
this.abscissa = group_by[0];
} else {
this.abscissa = this.first_field;
}
this.dataset.read_slice(this.list_fields(), {}, $.proxy(this, 'schedule_chart'));
}
});
};

View File

@ -9,8 +9,6 @@ openerp.web_kanban.KanbanView = openerp.web.View.extend({
this.set_default_options(options);
this.dataset = dataset;
this.model = dataset.model;
this.domain = dataset.domain;
this.context = dataset.context;
this.view_id = view_id;
this.fields_view = {};
this.group_by = [];
@ -33,9 +31,6 @@ openerp.web_kanban.KanbanView = openerp.web.View.extend({
var self = this;
this.fields_view = data;
this.add_qweb_template();
if (this.qweb.has_template('kanban-box')) {
this.do_actual_search();
}
},
add_qweb_template: function() {
var group_operator = ["avg", "max", "min", "sum", "count"]
@ -294,7 +289,7 @@ openerp.web_kanban.KanbanView = openerp.web.View.extend({
this.do_execute_action(
button_attrs, dataset,
record_id, function () {
self.do_actual_search();
self.on_reload_record(record_id);
}
);
},
@ -427,28 +422,18 @@ openerp.web_kanban.KanbanView = openerp.web.View.extend({
});
return new_record;
},
do_search: function (domains, contexts, group_by) {
do_search: function (domain, context, group_by) {
var self = this;
this.rpc('/web/session/eval_domain_and_context', {
domains: [this.dataset.get_domain()].concat(domains),
contexts: [this.dataset.get_context()].concat(contexts),
group_by_seq: group_by
}, function (results) {
self.domain = results.domain;
self.context = results.context;
self.group_by = results.group_by;
self.do_actual_search();
});
},
do_actual_search : function () {
self.group_by = group_by;
var self = this,
group_by = self.group_by;
if (!group_by.length && this.fields_view.arch.attrs.default_group_by) {
group_by = [this.fields_view.arch.attrs.default_group_by];
self.group_by = group_by;
}
self.datagroup = new openerp.web.DataGroup(self, self.model, self.domain, self.context, group_by);
self.datagroup.list(_.keys(self.fields_view.fields),
self.datagroup = new openerp.web.DataGroup(self, self.model, domain, context, group_by);
self.datagroup.list(
_.keys(self.fields_view.fields),
function (groups) {
self.groups = groups;
if (groups.length) {
@ -461,9 +446,12 @@ openerp.web_kanban.KanbanView = openerp.web.View.extend({
},
function (dataset) {
self.groups = [];
self.dataset.read_slice([], {'domain': self.domain, 'context': self.context}, function(records) {
if (records.length) self.all_display_data = [{'records': records, 'value':false, 'header' : false, 'ids': self.dataset.ids}];
else self.all_display_data = [];
self.dataset.read_slice([], {}, function(records) {
if (records.length) {
self.all_display_data = [{'records': records, 'value':false, 'header' : false, 'ids': self.dataset.ids}];
} else {
self.all_display_data = [];
}
self.$element.find(".oe_kanban_view").remove();
self.on_show_data();
});
@ -487,7 +475,7 @@ openerp.web_kanban.KanbanView = openerp.web.View.extend({
_.each(self.aggregates, function(value, key) {
group_aggregates[value] = group.aggregates[key];
});
self.dataset.read_slice([], {'domain': group.domain, 'context': group.context}, function(records) {
self.dataset.read_slice([], {'domain': group.domain, 'conext': group.context}, function(records) {
self.all_display_data.push({"value" : group_value, "records": records, 'header':group_name, 'ids': self.dataset.ids, 'aggregates': group_aggregates});
if (datagroups.length == self.all_display_data.length) {
self.$element.find(".oe_kanban_view").remove();