[IMP] beginning of searchbar integration in graph view (addon web_graph)

bzr revid: ged@openerp.com-20131205151945-8yo2vkykztvlru3g
This commit is contained in:
Gery Debongnie 2013-12-05 16:19:45 +01:00
parent 15547d9267
commit d3fffbff50
2 changed files with 191 additions and 150 deletions

View File

@ -33,12 +33,14 @@ instance.web_graph.GraphView = instance.web.View.extend({
init: function(parent, dataset, view_id, options) {
this._super(parent);
this.model = new instance.web.Model(dataset.model, {group_by_no_leaf: true});
this.dataset = dataset;
this.pivot_table = new openerp.web_graph.PivotTable(this.model, dataset.domain);
this.set_default_options(options);
this.dropdown = null;
this.pivot_table = null;
this.heat_map_mode = false;
this.mode = 'pivot';
this.mode = 'pivot'; // pivot, bar_chart, line_chart, pie_chart, heatmap, row_heatmap, col_heatmap
this.measure = null;
this.measure_list = [];
this.important_fields = [];
@ -48,6 +50,8 @@ instance.web_graph.GraphView = instance.web.View.extend({
get_domain: function () { },
get_groupby: function () { },
};
this.groupby_mode = 'default'; // 'default' or 'manual'
this.default_row_groupby = [];
},
start: function () {
@ -64,8 +68,7 @@ instance.web_graph.GraphView = instance.web.View.extend({
view_loading: function (fields_view_get) {
var self = this,
model = new instance.web.Model(fields_view_get.model, {group_by_no_leaf: true}),
row_groupby = [];
measure = null;
if (fields_view_get.arch.attrs.type === 'bar') {
this.mode = 'bar_chart';
@ -77,17 +80,18 @@ instance.web_graph.GraphView = instance.web.View.extend({
if ('operator' in field.attrs) {
self.measure_list.push(field.attrs.name);
} else {
row_groupby.push(field.attrs.name);
self.default_row_groupby.push(field.attrs.name);
}
}
});
if (this.measure_list.length > 0) {
this.measure = this.measure_list[0];
measure = this.measure_list[0];
this.pivot_table.set_measure(measure);
}
// get the most important fields (of the model) by looking at the
// groupby filters defined in the search view
var options = {model:model, view_type: 'search'},
var options = {model:this.model, view_type: 'search'},
deferred1 = instance.web.fields_view_get(options).then(function (search_view) {
var groups = _.select(search_view.arch.children, function (c) {
return (c.tag == 'group') && (c.attrs.string != 'Display');
@ -103,8 +107,8 @@ instance.web_graph.GraphView = instance.web.View.extend({
});
// get the fields descriptions from the model
var deferred2 = model.call('fields_get', []).then(function (fs) {
self.fields = fs;
var deferred2 = this.model.call('fields_get', []).then(function (fs) {
self.fields = fs;
var measure_selection = self.$('.graph_measure_selection');
_.each(self.measure_list, function (measure) {
var choice = $('<a></a>').attr('data-choice', measure)
@ -115,51 +119,41 @@ instance.web_graph.GraphView = instance.web.View.extend({
});
});
return $.when(deferred1, deferred2).then(function () {
var data = {
model: model,
domain: [],
measure: self.measure,
col_groupby: [],
row_groupby: row_groupby,
};
self.pivot_table = new openerp.web_graph.PivotTable(data);
});
return $.when(deferred1, deferred2);
},
do_search: function (domain, context, group_by) {
console.log("START DO SEARCH",group_by);
var self = this;
var self = this,
col_groupby = get_col_groupby('ColGroupBy');
this.pivot_table.domain = domain;
if (group_by.length > 0) {
this.pivot_table.set_groupby(this.pivot_table.rows, group_by)
.done(this.proxy('display_data'));
if (group_by.length || col_groupby.length) {
this.groupby_mode = 'manual';
if (!group_by.length) {
group_by = this.default_row_groupby;
}
}
this.pivot_table.set_domain(domain);
if (this.groupby_mode === 'manual') {
this.pivot_table.set_row_groupby(group_by);
this.pivot_table.set_col_groupby(col_groupby);
} else {
this.pivot_table.update_values().done(this.proxy('display_data'));
this.pivot_table.set_row_groupby(this.default_row_groupby);
this.pivot_table.set_col_groupby([]);
}
this.display_data();
var colgroupby = get_search_col_groupby();
console.log ("colgroupby", colgroupby);
console.log("pivot cbh",this.pivot_table.cols.groupby);
if (!_.isEqual(colgroupby, this.pivot_table.cols.groupby)) {
// console.log ('yop');
// this.pivot_table.set_groupby(this.pivot_table.cols, colgroupby)
// .done(this.proxy('display_data'));
}
// console.log("query",this.search_view.query.models);
function get_search_col_groupby() {
var colgroupby = [],
function get_col_groupby() {
var groupby = [],
search = _.find(self.search_view.query.models, function (model) {
return model.attributes.category == 'ColGroupBy';
});
if (search) {
colgroupby = _.map(search.values.models, function (val) {
groupby = _.map(search.values.models, function (val) {
return val.attributes.value;
});
}
return colgroupby;
return groupby;
}
},
@ -169,21 +163,25 @@ instance.web_graph.GraphView = instance.web.View.extend({
},
display_data: function () {
console.log('displaying data');
this.$('.graph_main_content svg').remove();
this.table.empty();
if (this.pivot_table.no_data) {
var msg = 'No data available. Try to remove any filter or add some data.';
this.table.append($('<tr><td>' + msg + '</td></tr>'));
var pivot = this.pivot_table;
if (pivot.stale_data) {
pivot.update_data().done(this.proxy('display_data'));
} else {
var table_modes = ['pivot', 'heatmap', 'row_heatmap', 'col_heatmap'];
if (_.contains(table_modes, this.mode)) {
this.draw_table();
this.$('.graph_main_content svg').remove();
this.table.empty();
if (pivot.no_data) {
var msg = 'No data available. Try to remove any filter or add some data.';
this.table.append($('<tr><td>' + msg + '</td></tr>'));
} else {
this.$('.graph_main_content').append($('<div><svg></svg></div>'));
var svg = this.$('.graph_main_content svg')[0];
openerp.web_graph.draw_chart(this.mode, this.pivot_table, svg);
var table_modes = ['pivot', 'heatmap', 'row_heatmap', 'col_heatmap'];
if (_.contains(table_modes, this.mode)) {
this.draw_table();
} else {
this.$('.graph_main_content').append($('<div><svg></svg></div>'));
var svg = this.$('.graph_main_content svg')[0];
openerp.web_graph.draw_chart(this.mode, this.pivot_table, svg);
}
}
}
},
@ -221,8 +219,6 @@ instance.web_graph.GraphView = instance.web.View.extend({
this.mode = mode;
this.display_data();
// console.log('yop');
// var self = this;
// var attr = [{label: self.fields['user_id'].string, value: 'user_id'}];
// var attr2 = [{label: self.fields['user_id'].string, value: {
// attrs: {domain: [], context: {'group_by':'user_id'}}
// }}];
@ -234,24 +230,48 @@ instance.web_graph.GraphView = instance.web.View.extend({
// };
// // console.log("rnst",self.search_view);
// var self = this;
// var attr = [{label: self.fields['user_id'].string, value: 'user_id'}];
// var test = {
// values: attr,
// icon: 'u',
// field: self.search_field,
// category: _t("ColGroupBy"),
// };
// self.search_view.query.add(test);
// self.search_view.query.add(test);
// self.search_view.query.add(groupbytest, {});
},
register_groupby: function (category, field) {
var groupby;
if (category === 'ColGroupBy') {
groupby = {
category: 'ColGroupBy',
values: [{label: this.fields[field].string, value: field}],
icon: 'u',
field: this.search_field,
};
this.search_view.query.add(groupby);
}
if (category === 'GroupBy') {
var value = {attrs: {domain: [], context: {'group_by':field}}};
groupby = {
category: 'GroupBy',
values: [{label: this.fields[field].string, value: value}],
icon: 'u',
field: this.search_view._s_groupby,
};
this.search_view.query.add(groupby);
}
},
measure_selection: function (event) {
event.preventDefault();
var measure = event.target.attributes['data-choice'].nodeValue;
this.measure = (measure === '__count') ? null : measure;
this.pivot_table.set_measure(this.measure)
.then(this.proxy('display_data'));
this.pivot_table.set_measure(this.measure);
this.display_data();
},
expand_selection: function (event) {
@ -270,7 +290,8 @@ instance.web_graph.GraphView = instance.web.View.extend({
this.display_data();
break;
case 'expand_all':
this.pivot_table.expand_all().then(this.proxy('display_data'));
this.pivot_table.invalidate_data();
this.display_data();
break;
}
},
@ -283,7 +304,8 @@ instance.web_graph.GraphView = instance.web.View.extend({
this.display_data();
break;
case 'update_values':
this.pivot_table.update_values().then(this.proxy('display_data'));
this.pivot_table.stale_data = true;
this.display_data();
break;
case 'export_data':
// Export code... To do...
@ -306,32 +328,38 @@ instance.web_graph.GraphView = instance.web.View.extend({
event.preventDefault();
this.pivot_table.expand(id, field_id).then(function () {
var attr = [{label: self.fields[field_id].string, value: field_id}];
var attr2 = [{label: self.fields[field_id].string, value: {
attrs: {domain: [], context: {'group_by':'stage_id'}}
}}];
if (self.pivot_table.is_col(id)) {
self.register_groupby('ColGroupBy', field_id);
} else {
self.register_groupby('GroupBy', field_id);
// self.display_data();
}
// var attr = [{label: self.fields[field_id].string, value: field_id}];
// var attr2 = [{label: self.fields[field_id].string, value: {
// attrs: {domain: [], context: {'group_by':'stage_id'}}
// }}];
var test = {
values: attr,
icon: 'u',
field: self.search_field,
category: _t("ColGroupBy"),
};
// var test = {
// values: attr,
// icon: 'u',
// field: self.search_field,
// category: _t("ColGroupBy"),
// };
var groupbytest = {
category: 'GroupBy',
values: attr2,
icon: 'u',
field: self.search_view._s_groupby,
};
// var groupbytest = {
// category: 'GroupBy',
// values: attr2,
// icon: 'u',
// field: self.search_view._s_groupby,
// };
// console.log("rnst",self.search_view);
console.log('BEF ADD CGB');
// self.search_view.query.add(test);
console.log('BEF ADD RGB');
// self.search_view.query.add(groupbytest, {});
console.log('AFTER ADD GB');
self.display_data();
// // console.log("rnst",self.search_view);
// console.log('BEF ADD CGB');
// // self.search_view.query.add(test);
// console.log('BEF ADD RGB');
// // self.search_view.query.add(groupbytest, {});
// console.log('AFTER ADD GB');
// self.display_data();
});
},

View File

@ -8,19 +8,17 @@
'use strict';
openerp.web_graph.PivotTable = openerp.web.Class.extend({
init: function (options) {
this.rows = { groupby: options.row_groupby };
this.cols = { groupby: [] };
init: function (model, domain) {
this.rows = { groupby: [], main: null, headers: null };
this.cols = { groupby: [], main: null, headers: null };
this.cells = [];
this.model = options.model;
this.domain = options.domain;
this.measure = options.measure;
this.model = model;
this.domain = domain;
this.measure = null;
this.id_seed = 0;
this.no_data = true;
},
start: function () {
return this.expand_all();
this.stale_data = true;
},
generate_id: function () {
@ -36,6 +34,34 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
return result;
},
set_domain: function (domain) {
if (!_.isEqual(domain, this.domain)) {
this.domain = domain;
this.stale_data = true;
}
},
set_measure: function (measure) {
if (measure !== this.measure) {
this.measure = measure;
this.stale_data = true;
}
},
set_row_groupby: function (groupby) {
if (!_.isEqual(groupby, this.rows.groupby)) {
this.rows.groupby = groupby;
this.stale_data = true;
}
},
set_col_groupby: function (groupby) {
if (!_.isEqual(groupby, this.cols.groupby)) {
this.cols.groupby = groupby;
this.stale_data = true;
}
},
set_value: function (id1, id2, value) {
var x = Math.min(id1, id2),
y = Math.max(id1, id2),
@ -58,11 +84,6 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
return (cell === undefined) ? undefined : cell.value;
},
set_measure: function (measure) {
this.measure = measure;
return this.update_values();
},
is_row: function (id) {
return !!_.find(this.rows.headers, function (header) {
return header.id == id;
@ -118,26 +139,26 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
var other = _.find(otherDim.headers, function (h) {
if (header.root === self.cols) {
return _.isEqual(data.path.slice(1), h.path);
} else {
return _.isEqual(_.rest(data.path), h.path);
}
});
if (other) {
if (self.measure) {
self.set_value(new_header_id, other.id, data.attributes.aggregates[self.measure]);
} else {
self.set_value(new_header_id, other.id, data.attributes.length);
}
}
});
});
header.is_expanded = true;
});
} else {
return _.isEqual(_.rest(data.path), h.path);
}
});
if (other) {
if (self.measure) {
self.set_value(new_header_id, other.id, data.attributes.aggregates[self.measure]);
} else {
self.set_value(new_header_id, other.id, data.attributes.length);
}
}
});
});
header.is_expanded = true;
});
},
make_header: function (groups, parent) {
var name = groups[0].attributes.value[1],
new_header = {
new_header = {
id: this.generate_id(),
path: parent.path.concat(name),
title: name,
@ -171,21 +192,6 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
this.fold_cols();
},
expand_all: function () {
var self = this;
return this.query_all_values().then(function (result) {
if (result) {
self.no_data = false;
self.expand_headers(self.rows, result.row_headers);
self.expand_headers(self.cols, result.col_headers);
self.cells = result.cells;
} else {
self.no_data = true;
}
});
},
expand_headers: function (root, new_headers) {
root.headers = new_headers;
root.main = new_headers[0];
@ -229,27 +235,36 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
}
},
invalidate_data: function () {
this.cells = null;
this.rows.main = null;
this.cols.main = null;
this.rows.headers = null;
this.cols.headers = null;
this.stale_data = true;
},
update_values: function () {
if (!this.rows.headers) {
return this.expand_all();
}
update_data: function () {
var self = this;
return this.query_all_values().then(function (result) {
if (!result) {
return undefined;
self.stale_data = false;
if (result) {
self.no_data = false;
if (self.cols.headers) {
self.update_headers(self.cols, result.col_headers);
self.update_headers(self.rows, result.row_headers);
} else {
self.expand_headers(self.cols, result.col_headers);
self.expand_headers(self.rows, result.row_headers);
}
self.cells = result.cells;
} else {
self.no_data = true;
}
self.no_data = false;
self.update_headers(self.cols, result.col_headers);
self.update_headers(self.rows, result.row_headers);
self.cells = result.cells;
});
},
update_headers: function (root, new_headers) {
if (!root.main) {
return this.expand_headers(root, new_headers);
}
_.each(root.headers, function (header) {
var corresponding_header = _.find(new_headers, function (h) {
return _.isEqual(h.path, header.path);
@ -279,7 +294,7 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
// this method is a little tricky. In order to obtain all the values
// required to draw the full table, we have to do at least
// 2 + min(row.groupby.length, col.groupby.length)
// 2 + min(row.groupby.length, col.groupby.length)
// calls to readgroup. For example, if row.groupby = [r1, r2, r3] and
// col.groupby = [c1, c2, c3, c4], then a minimal set of calls is done
// with the following groupby:
@ -317,10 +332,8 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
row_data = _.last(_.initial(args)),
cell_data = args;
if (total === undefined) { self.no_data = true;}
return (total === undefined)
? undefined
: self.format_data(total, col_data, row_data, cell_data);
return (total === undefined) ? undefined
: self.format_data(total, col_data, row_data, cell_data);
});
},
@ -343,8 +356,8 @@ openerp.web_graph.PivotTable = openerp.web.Class.extend({
}
return {col_headers: col_headers,
row_headers: row_headers,
cells: cells};
row_headers: row_headers,
cells: cells};
function make_headers (data, depth) {
var main = {