').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 && col_reduced && row_reduced) {
this.pivot.fold_with_depth(this.pivot.rows, row_gbs.length);
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');
this.$('.graph_main_content').removeClass('graph_chart_mode').addClass('graph_pivot_mode');
} else {
this.$('.graph_heatmap label').addClass('disabled');
this.$('.graph_main_content').removeClass('graph_pivot_mode').addClass('graph_chart_mode');
}
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 '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) {
if (header.root === this.pivot.rows) {
this.fold_row(header, event);
} else {
this.fold_col(header);
}
return;
}
if (header.path.length < header.root.groupby.length) {
this.$row_clicked = $(event.target).closest('tr');
this.expand(id);
return;
}
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};
});
if (this.dropdown) {
this.dropdown.remove();
}
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 (header.root === self.pivot.rows) {
// expanding rows can be done by only inserting in the dom
// console.log(event.target);
var rows = self.build_rows(header.children);
var doc_fragment = $(document.createDocumentFragment());
rows.map(function (row) {
doc_fragment.append(self.draw_row(row));
});
self.$row_clicked.after(doc_fragment);
} else {
// expanding cols will redraw the full table
self.display_data();
}
if (update_groupby && self.graph_view) {
self.graph_view.register_groupby(self.pivot.rows.groupby, self.pivot.cols.groupby);
}
});
},
fold_row: function (header, event) {
var rows_before = this.pivot.rows.headers.length,
update_groupby = this.pivot.fold(header),
rows_after = this.pivot.rows.headers.length,
rows_removed = rows_before - rows_after;
if (rows_after === 1) {
// probably faster to redraw the unique row instead of removing everything
this.display_data();
} else {
var $row = $(event.target).parent().parent();
$row.nextAll().slice(0,rows_removed).remove();
}
if (update_groupby && this.graph_view) {
this.graph_view.register_groupby(this.pivot.rows.groupby, this.pivot.cols.groupby);
}
},
fold_col: 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);
},
// ----------------------------------------------------------------------
// Convert Pivot data structure into table structure :
// compute rows, cols, colors, cell width, cell height, ...
// ----------------------------------------------------------------------
build_table: function(raw) {
return {
headers: this.build_headers(),
measure_row: this.build_measure_row(),
rows: this.build_rows(this.pivot.rows.headers,raw),
nbr_measures: this.pivot.measures.length,
title: this.title,
};
},
build_headers: function () {
var pivot = this.pivot,
nbr_measures = pivot.measures.length,
height = _.max(_.map(pivot.cols.headers, function(g) {return g.path.length;})),
rows = [];
_.each(pivot.cols.headers, function (col) {
if (col.path.length === 0) { return;}
var cell_width = nbr_measures * (col.expanded ? pivot.get_ancestor_leaves(col).length : 1),
cell_height = col.expanded ? 1 : height - col.path.length + 1,
cell = {width: cell_width, height: cell_height, title: col.title, id: col.id, expanded: col.expanded};
if (rows[col.path.length - 1]) {
rows[col.path.length - 1].push(cell);
} else {
rows[col.path.length - 1] = [cell];
}
});
if (pivot.get_cols_leaves().length > 1) {
rows[0].push({width: nbr_measures, height: height, title: _t('Total'), id: pivot.main_col().id });
}
if (pivot.cols.headers.length === 1) {
rows = [[{width: nbr_measures, height: 1, title: _t('Total'), id: pivot.main_col().id, expanded: false}]];
}
return rows;
},
build_measure_row: function () {
var nbr_leaves = this.pivot.get_cols_leaves().length,
nbr_cols = nbr_leaves + ((nbr_leaves > 1) ? 1 : 0),
result = [],
add_total = this.pivot.get_cols_leaves().length > 1,
i, m;
for (i = 0; i < nbr_cols; i++) {
for (m = 0; m < this.pivot.measures.length; m++) {
result.push({
text:this.pivot.measures[m].string,
is_bold: add_total && (i === nbr_cols - 1)
});
}
}
return result;
},
make_cell: function (row, col, value, index, raw) {
var formatted_value = raw && !_.isUndefined(value) ? value : openerp.web.format_value(value, {type:this.pivot.measures[index].type}),
cell = {value:formatted_value};
if (this.heatmap_mode === 'none') { return cell; }
var total = (this.heatmap_mode === 'both') ? this.pivot.get_total()[index]
: (this.heatmap_mode === 'row') ? this.pivot.get_total(row)[index]
: this.pivot.get_total(col)[index];
var color = Math.floor(90 + 165*(total - Math.abs(value))/total);
if (color < 255) {
cell.color = color;
}
return cell;
},
build_rows: function (headers, raw) {
var self = this,
pivot = this.pivot,
m, i, j, k, cell, row;
var rows = [];
var cells, pivot_cells, values;
var nbr_of_rows = headers.length;
var col_headers = pivot.get_cols_leaves();
for (i = 0; i < nbr_of_rows; i++) {
row = headers[i];
cells = [];
pivot_cells = [];
for (j = 0; j < pivot.cells.length; j++) {
if (pivot.cells[j].x == row.id || pivot.cells[j].y == row.id) {
pivot_cells.push(pivot.cells[j]);
}
}
for (j = 0; j < col_headers.length; j++) {
values = undefined;
for (k = 0; k < pivot_cells.length; k++) {
if (pivot_cells[k].x == col_headers[j].id || pivot_cells[k].y == col_headers[j].id) {
values = pivot_cells[k].values;
break;
}
}
if (!values) { values = new Array(pivot.measures.length);}
for (m = 0; m < pivot.measures.length; m++) {
cells.push(self.make_cell(row,col_headers[j],values[m], m, raw));
}
}
if (col_headers.length > 1) {
var totals = pivot.get_total(row);
for (m = 0; m < pivot.measures.length; m++) {
cell = self.make_cell(row, pivot.cols.headers[0], totals[m], m, raw);
cell.is_bold = 'true';
cells.push(cell);
}
}
rows.push({
id: row.id,
indent: row.path.length,
title: row.title,
expanded: row.expanded,
cells: cells,
});
}
return rows;
},
// ----------------------------------------------------------------------
// Main display method
// ----------------------------------------------------------------------
display_data: function () {
var scroll = $(window).scrollTop();
this.$('.graph_main_content svg').remove();
this.$('.graph_main_content div').remove();
this.table.empty();
this.table.toggleClass('heatmap', this.heatmap_mode !== 'none');
this.$('.graph_options_selection label').last().toggleClass('disabled', this.pivot.no_data);
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();
$(window).scrollTop(scroll);
} else {
this.$('.graph_main_content').append($('