').attr('data-choice', measure.field)
.attr('href', '#')
.text(measure.string));
}));
},
// ----------------------------------------------------------------------
// Configuration methods
// ----------------------------------------------------------------------
set: function (domain, row_groupby, col_groupby) {
if (!this.pivot) {
this.pivot_options.domain = domain;
this.pivot_options.row_groupby = row_groupby;
this.pivot_options.col_groupby = col_groupby;
return;
}
var row_gbs = this.create_field_values(row_groupby),
col_gbs = this.create_field_values(col_groupby),
dom_changed = !_.isEqual(this.pivot.domain, domain),
row_gb_changed = !_.isEqual(row_gbs, this.pivot.rows.groupby),
col_gb_changed = !_.isEqual(col_gbs, this.pivot.cols.groupby),
row_reduced = is_strict_beginning_of(row_gbs, this.pivot.rows.groupby),
col_reduced = is_strict_beginning_of(col_gbs, this.pivot.cols.groupby);
if (!dom_changed && row_reduced && !col_gb_changed) {
this.pivot.fold_with_depth(this.pivot.rows, row_gbs.length);
this.display_data();
return;
}
if (!dom_changed && col_reduced && !row_gb_changed) {
this.pivot.fold_with_depth(this.pivot.cols, col_gbs.length);
this.display_data();
return;
}
if (dom_changed || row_gb_changed || col_gb_changed) {
this.pivot.set(domain, row_gbs, col_gbs).then(this.proxy('display_data'));
}
},
set_mode: function (mode) {
this.mode = mode;
if (mode === 'pivot') {
this.$('.graph_heatmap label').removeClass('disabled');
} else {
this.$('.graph_heatmap label').addClass('disabled');
}
this.display_data();
},
set_heatmap_mode: function (mode) { // none, row, col, all
this.heatmap_mode = mode;
if (mode === 'none') {
this.$('.graph_heatmap label').removeClass('disabled');
this.$('.graph_heatmap label').removeClass('active');
}
this.display_data();
},
create_field_value: function (f) {
var field = (_.contains(f, ':')) ? f.split(':')[0] : f,
groupby_field = _.findWhere(this.important_fields, {field:field}),
string = groupby_field ? groupby_field.string : this.fields[field].string,
result = {field: f, string: string, type: this.fields[field].type };
if (groupby_field) {
result.filter = groupby_field.filter;
}
return result;
},
create_field_values: function (field_ids) {
return _.map(field_ids, this.proxy('create_field_value'));
},
get_col_groupbys: function () {
return _.pluck(this.pivot.cols.groupby, 'field');
},
// ----------------------------------------------------------------------
// UI code
// ----------------------------------------------------------------------
events: {
'click .graph_mode_selection label' : 'mode_selection',
'click .graph_measure_selection li' : 'measure_selection',
'click .graph_options_selection label' : 'option_selection',
'click .graph_heatmap label' : 'heatmap_mode_selection',
'click .web_graph_click' : 'header_cell_clicked',
'click a.field-selection' : 'field_selection',
},
mode_selection: function (event) {
event.preventDefault();
var mode = event.currentTarget.getAttribute('data-mode');
this.set_mode(mode);
},
measure_selection: function (event) {
event.preventDefault();
event.stopPropagation();
var measure_field = event.target.getAttribute('data-choice');
var measure = {
field: measure_field,
type: this.fields[measure_field].type,
string: this.fields[measure_field].string
};
this.pivot.toggle_measure(measure).then(this.proxy('display_data'));
this.put_measure_checkmarks();
},
put_measure_checkmarks: function () {
var self = this,
measures_li = this.$('.graph_measure_selection a');
measures_li.removeClass('oe_selected');
_.each(this.measure_list, function (measure, index) {
if (_.findWhere(self.pivot.measures, measure)) {
measures_li.eq(index).addClass('oe_selected');
}
});
},
option_selection: function (event) {
event.preventDefault();
switch (event.currentTarget.getAttribute('data-choice')) {
case 'bar_grouped':
this.bar_ui = 'group';
if (this.mode === 'bar') {
this.display_data();
}
break;
case 'bar_stacked':
this.bar_ui = 'stack';
if (this.mode === 'bar') {
this.display_data();
}
break;
case 'swap_axis':
this.swap_axis();
break;
case 'expand_all':
this.pivot.expand_all().then(this.proxy('display_data'));
break;
case 'update_values':
this.pivot.update_data().then(this.proxy('display_data'));
break;
case 'export_data':
this.export_xls();
break;
}
},
heatmap_mode_selection: function (event) {
event.preventDefault();
var mode = event.currentTarget.getAttribute('data-mode');
if (this.heatmap_mode === mode) {
event.stopPropagation();
this.set_heatmap_mode('none');
} else {
this.set_heatmap_mode(mode);
}
},
header_cell_clicked: function (event) {
event.preventDefault();
event.stopPropagation();
var id = event.target.getAttribute('data-id'),
header = this.pivot.get_header(id),
self = this;
if (header.expanded) {
this.fold(header);
} else {
if (header.path.length < header.root.groupby.length) {
this.expand(id);
} else {
if (!this.important_fields.length) {
return;
}
var fields = _.map(this.important_fields, function (field) {
return {id: field.field, value: field.string, type:self.fields[field.field.split(':')[0]].type};
});
this.dropdown = $(QWeb.render('field_selection', {fields:fields, header_id:id}));
$(event.target).after(this.dropdown);
this.dropdown.css({position:'absolute',
left:event.pageX,
top:event.pageY});
this.$('.field-selection').next('.dropdown-menu').toggle();
}
}
},
field_selection: function (event) {
var id = event.target.getAttribute('data-id'),
field_id = event.target.getAttribute('data-field-id'),
interval,
groupby = this.create_field_value(field_id);
event.preventDefault();
if (this.fields[field_id].type === 'date' || this.fields[field_id].type === 'datetime') {
interval = event.target.getAttribute('data-interval');
groupby.field = groupby.field + ':' + interval;
}
this.expand(id, groupby);
},
// ----------------------------------------------------------------------
// Pivot Table integration
// ----------------------------------------------------------------------
expand: function (header_id, groupby) {
var self = this,
header = this.pivot.get_header(header_id),
update_groupby = !!groupby;
groupby = groupby || header.root.groupby[header.path.length];
this.pivot.expand(header_id, groupby).then(function () {
if (update_groupby && self.graph_view) {
self.graph_view.register_groupby(self.pivot.rows.groupby, self.pivot.cols.groupby);
}
self.display_data();
});
},
fold: function (header) {
var update_groupby = this.pivot.fold(header);
this.display_data();
if (update_groupby && this.graph_view) {
this.graph_view.register_groupby(this.pivot.rows.groupby, this.pivot.cols.groupby);
}
},
swap_axis: function () {
this.pivot.swap_axis();
this.display_data();
this.graph_view.register_groupby(this.pivot.rows.groupby, this.pivot.cols.groupby);
},
// ----------------------------------------------------------------------
// Main display method
// ----------------------------------------------------------------------
display_data: function () {
this.$('.graph_main_content svg').remove();
this.$('.graph_main_content div').remove();
this.table.empty();
this.table.toggleClass('heatmap', this.heatmap_mode !== 'none');
this.width = this.$el.width();
this.height = Math.min(Math.max(document.documentElement.clientHeight - 116 - 60, 250), Math.round(0.8*this.$el.width()));
this.$('.graph_header').toggle(this.visible_ui);
if (this.pivot.no_data) {
this.$('.graph_main_content').append($(QWeb.render('graph_no_data')));
} else {
if (this.mode === 'pivot') {
this.draw_table();
} else {
this.$('.graph_main_content').append($('