Merge lp:~openerp-dev/openerp-web/trunk-graphview-more-optim-ged into lp:openerp-web

Proposed by Géry Debongnie
Status: Merged
Merged at revision: 3994
Proposed branch: lp:~openerp-dev/openerp-web/trunk-graphview-more-optim-ged
Merge into: lp:openerp-web
Diff against target: 209 lines (+81/-54)
1 file modified
addons/web_graph/static/src/js/graph_widget.js (+81/-54)
To merge this branch: bzr merge lp:~openerp-dev/openerp-web/trunk-graphview-more-optim-ged
Reviewer Review Type Date Requested Status
Xavier (Open ERP) Pending
Review via email: mp+217730@code.launchpad.net

Description of the change

This branch implements two changes:

1) optimizations to the graph view table rendering, mainly by using a document fragment to render the table off the main document, then including it only once, which allows the browser to only compute the layout once instead of every time a row is added

2) fix a bug when the user reloads rapidly the graph view (problem was that by the time the code tries to find the searchview, it was already gone)

To post a comment you must log in.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'addons/web_graph/static/src/js/graph_widget.js'
2--- addons/web_graph/static/src/js/graph_widget.js 2014-04-28 09:02:21 +0000
3+++ addons/web_graph/static/src/js/graph_widget.js 2014-04-30 09:36:32 +0000
4@@ -43,6 +43,13 @@
5 this.$('.graph_main_content').addClass('graph_pivot_mode');
6 }
7
8+ // get search view
9+ var parent = this.getParent();
10+ while (!(parent instanceof openerp.web.ViewManager)) {
11+ parent = parent.getParent();
12+ }
13+ this.search_view = parent.searchview;
14+
15 openerp.session.rpc('/web_graph/check_xlwt').then(function (result) {
16 self.$('.graph_options_selection label').toggle(result);
17 });
18@@ -76,16 +83,9 @@
19 // this method gets the fields that appear in the search view, under the
20 // 'Groupby' heading
21 get_search_fields: function () {
22- var self = this,
23- parent = this.getParent();
24-
25- while (!(parent instanceof openerp.web.ViewManager)) {
26- parent = parent.getParent();
27- }
28-
29- var search_view = parent.searchview;
30-
31- var groupbygroups = _(search_view.inputs).select(function (g) {
32+ var self = this;
33+
34+ var groupbygroups = _(this.search_view.inputs).select(function (g) {
35 return g instanceof openerp.web.search.GroupbyGroup;
36 });
37
38@@ -444,45 +444,55 @@
39 build_rows: function (raw) {
40 var self = this,
41 pivot = this.pivot,
42- m, i, cell;
43-
44- return _.map(pivot.rows.headers, function (row) {
45- var cells = [];
46- var pivot_cells = [];
47- for (i = 0; i < pivot.cells.length; i++) {
48- if (pivot.cells[i].x == row.id || pivot.cells[i].y == row.id) {
49- pivot_cells.push(pivot.cells[i]);
50- }
51+ m, i, j, k, cell, row;
52+
53+ var rows = [];
54+ var cells, pivot_cells, values;
55+
56+ var nbr_of_rows = pivot.rows.headers.length;
57+ var col_headers = pivot.get_cols_leaves();
58+
59+ for (i = 0; i < nbr_of_rows; i++) {
60+ row = pivot.rows.headers[i];
61+ cells = [];
62+ pivot_cells = [];
63+ for (j = 0; j < pivot.cells.length; j++) {
64+ if (pivot.cells[j].x == row.id || pivot.cells[j].y == row.id) {
65+ pivot_cells.push(pivot.cells[j]);
66+ }
67 }
68- _.each(pivot.get_cols_leaves(), function (col) {
69- var values;
70- for (i = 0; i < pivot_cells.length; i++) {
71- if (pivot_cells[i].x == col.id || pivot_cells[i].y == col.id) {
72- values = pivot_cells[i].values;
73+
74+ for (j = 0; j < col_headers.length; j++) {
75+ values = undefined;
76+ for (k = 0; k < pivot_cells.length; k++) {
77+ if (pivot_cells[k].x == col_headers[j].id || pivot_cells[k].y == col_headers[j].id) {
78+ values = pivot_cells[k].values;
79 break;
80- }
81+ }
82 }
83 if (!values) { values = new Array(pivot.measures.length);}
84 for (m = 0; m < pivot.measures.length; m++) {
85- cells.push(self.make_cell(row,col,values[m], m, raw));
86+ cells.push(self.make_cell(row,col_headers[j],values[m], m, raw));
87 }
88- });
89- if (pivot.get_cols_leaves().length > 1) {
90+ }
91+ if (col_headers.length > 1) {
92 var totals = pivot.get_total(row);
93 for (m = 0; m < pivot.measures.length; m++) {
94- cell = self.make_cell(row, pivot.main_col(), totals[m], m, raw);
95+ cell = self.make_cell(row, pivot.cols.headers[0], totals[m], m, raw);
96 cell.is_bold = 'true';
97 cells.push(cell);
98 }
99 }
100- return {
101+ rows.push({
102 id: row.id,
103 indent: row.path.length,
104 title: row.title,
105 expanded: row.expanded,
106 cells: cells,
107- };
108- });
109+ });
110+ }
111+
112+ return rows;
113 },
114
115 // ----------------------------------------------------------------------
116@@ -515,9 +525,11 @@
117 // ----------------------------------------------------------------------
118 draw_table: function () {
119 var table = this.build_table();
120- this.draw_headers(table.headers);
121- this.draw_measure_row(table.measure_row);
122- this.draw_rows(table.rows);
123+ var doc_fragment = $(document.createDocumentFragment());
124+ this.draw_headers(table.headers, doc_fragment);
125+ this.draw_measure_row(table.measure_row, doc_fragment);
126+ this.draw_rows(table.rows, doc_fragment);
127+ this.table.append(doc_fragment);
128 },
129
130 make_header_cell: function (header) {
131@@ -540,7 +552,7 @@
132 return cell.append(content);
133 },
134
135- draw_headers: function (headers) {
136+ draw_headers: function (headers, doc_fragment) {
137 var make_cell = this.make_header_cell,
138 empty_cell = $('<th>').attr('rowspan', headers.length),
139 thead = $('<thead>');
140@@ -553,10 +565,11 @@
141 thead.append(html_row);
142 });
143 thead.children(':first').prepend(empty_cell);
144- this.table.append(thead);
145+ doc_fragment.append(thead);
146+ this.thead = thead;
147 },
148
149- draw_measure_row: function (measure_row) {
150+ draw_measure_row: function (measure_row, doc_fragment) {
151 if (this.pivot.measures.length === 1) { return; }
152 var html_row = $('<tr>').append('<th>');
153 _.each(measure_row, function (cell) {
154@@ -564,25 +577,39 @@
155 if (cell.is_bold) {measure_cell.css('font-weight', 'bold');}
156 html_row.append(measure_cell);
157 });
158- this.$('thead').append(html_row);
159+ this.thead.append(html_row);
160 },
161
162- draw_rows: function (rows) {
163- var table = this.table,
164- make_cell = this.make_header_cell;
165-
166- _.each(rows, function (row) {
167- var html_row = $('<tr>').append(make_cell(row));
168- _.each(row.cells, function (cell) {
169- var html_cell = $('<td>').text(cell.value);
170- if (_.has(cell, 'color')) {
171- html_cell.css('background-color', $.Color(255, cell.color, cell.color));
172+ draw_rows: function (rows, doc_fragment) {
173+ var make_cell = this.make_header_cell,
174+ cell,
175+ html_row, i, j;
176+
177+ var cells_list,
178+ hcell,
179+ rows_length, cells_length;
180+
181+ rows_length = rows.length;
182+ for (i = 0; i < rows_length; i++) {
183+ html_row = $('<tr>').append(make_cell(rows[i]));
184+ cells_length = rows[i].cells.length;
185+ cells_list = [];
186+
187+ for (j = 0; j < cells_length; j++) {
188+ cell = rows[i].cells[j];
189+ hcell = '<td';
190+ if (cell.is_bold || cell.color) {
191+ hcell += ' style="';
192+ if (cell.is_bold) hcell += 'font-weight: bold;';
193+ if (cell.color) hcell += 'background-color:' + $.Color(255, cell.color, cell.color) + ';';
194+ hcell += '"';
195 }
196- if (cell.is_bold) { html_cell.css('font-weight', 'bold'); }
197- html_row.append(html_cell);
198- });
199- table.append(html_row);
200- });
201+ hcell += '>' + cell.value + '</td>';
202+ cells_list[j] = hcell;
203+ }
204+ html_row.append(cells_list.join(''));
205+ doc_fragment.append($('<tbody>').append(html_row));
206+ }
207 },
208
209 // ----------------------------------------------------------------------