Skip to content
Snippets Groups Projects
Commit 018c48c3 authored by legac's avatar legac
Browse files

Redesign the grid plugin Export using the mixins ExportCSV and ExportLatex

parent a9f0d999
No related branches found
No related tags found
No related merge requests found
......@@ -4,6 +4,8 @@
*
* A context menu gives access to the different possibilities.
*
* The grid should be instantiate with the mixin App.grid.mixin.ExportToLatex.
*
* @since 0.6.1.8
*
*/
......@@ -13,12 +15,6 @@ Ext.define('App.grid.plugin.Export', {
alias: 'plugin.pGridExport',
requires: 'Ext.form.action.StandardSubmit',
/**
* @cfg {String}
* The CSV delimiter for string.
*/
delimiter: '"',
/**
* @cfg {String}
* The new line separator used in the CSV / LaTeX file.
......@@ -36,13 +32,6 @@ Ext.define('App.grid.plugin.Export', {
'\\usepackage{longtable}'
],
/**
* @cfg {String}
* The CSV field separator.
*
*/
separator: ',',
// private properties for internationalization
textToCSV: 'Export to CSV...',
textToLaTeX: 'Export to LaTeX...',
......@@ -103,23 +92,6 @@ Ext.define('App.grid.plugin.Export', {
}, this, {single: true});
},
/**
* Surround the value with the CSV delimiter.
* Delimiter are only applied to non-numeric value.
*
* @param {String/Number} value
*
*/
delimit: function (value) {
"use strict";
if (!Ext.isNumeric(value)) {
value = this.delimiter + value + this.delimiter;
}
return value;
},
/**
* Destroy the plugin
*
......@@ -130,46 +102,29 @@ Ext.define('App.grid.plugin.Export', {
Ext.destroy(this.memu);
},
/**
* Get the label and the alignment of the visible columns.
*
* @return {Object}
* @return {String[]} return.alignments
* @return {Boolean} return.hidden
* @return {String[]} return.labels empty for hidden headers
*
*/
getColumnHeader: function () {
//
// Do latex
// @return {String[]} one string per line
//
doLaTeX: function () {
"use strict";
var alignments = [],
columns,
container,
labels = [],
i;
container = this.getCmp().getDockedItems('headercontainer');
if (container.length) {
columns = container[0].getVisibleGridColumns();
for (i = 0; i < columns.length; i += 1) {
labels.push(columns[i].text);
if (columns[i].align) {
alignments.push(columns[i].align);
} else {
alignments.push('left');
}
}
}
return {
alignments: alignments,
hidden: container[0].hiddenHeaders,
labels: labels
};
var latex = [];
// latex preamble
latex = latex.concat(this.preamble);
// begin document
latex.push('\\begin{document}');
// the table
latex = latex.concat(this.getCmp().toLaTeX());
// end document
latex.push('\\end{document}');
return latex;
},
/**
......@@ -180,63 +135,8 @@ Ext.define('App.grid.plugin.Export', {
"use strict";
var div,
grid = this.getCmp(),
header = this.getColumnHeader(),
i,
li,
lines = [],
total;
// convert the header
if (! header.hidden) {
for (i = 0; i < header.labels.length; i += 1) {
header.labels[i] = this.delimit(header.labels[i]);
}
lines.push(header.labels.join(this.separator));
}
//
// convert the HTML row associated to the Ext.grid.View
// The structure of the HTML table is complex because it contains
// several types of row depending on the grouping features.
//
grid.getView().getEl().select("table tr").each(function(trEl, cEl, index){
if(trEl.hasCls("x-grid-wrap-row")) {
div = trEl.down(".x-grid-group-title");
if (div) {
lines.push(this.delimit(div.dom.textContent));
}
} else if (trEl.hasCls("x-grid-data-row") || trEl.hasCls("x-grid-row-summary")) {
li = [];
trEl.select("td").each(function(tdEl){
li.push(this.delimit(tdEl.dom.textContent));
}, this);
li = li.join(this.separator);
// unfortunately the grand total appear as the first row (Ext JS 4.2)
if (index === 0 && trEl.hasCls("x-grid-row-summary")) {
total = li;
} else {
lines.push(li);
}
}
}, this);
// push the grand total at the end
if (total) {
lines.push(total);
}
// trigger the transfer using URI technique
App.save("text/csv", "utf-8", lines);
var csv = this.getCmp().toCSV();
App.save("text/csv", "utf-8", csv);
},
/**
......@@ -247,8 +147,8 @@ Ext.define('App.grid.plugin.Export', {
"use strict";
var lines = this.toLaTeX();
App.save("application/x-latex", "utf-8", lines);
var latex = this.doLaTeX();
App.save("application/x-latex", "utf-8", latex);
},
/**
......@@ -264,7 +164,7 @@ Ext.define('App.grid.plugin.Export', {
var data,
form;
data = this.toLaTeX().join(this.newLine);
data = this.doLaTeX().join(this.newLine);
// send the latex data to the server.
// the method used standardSubmit using a form.
......@@ -280,111 +180,4 @@ Ext.define('App.grid.plugin.Export', {
form.getForm().submit();
},
/**
* Convert the Ext.grid.View content into a string.
* the information are encoded using LaTeX standard.
*
* @return {String[]} One string per line.
*
*/
toLaTeX: function () {
"use strict";
var cmd,
div,
grid = this.getCmp(),
header = this.getColumnHeader(),
li,
lines = [],
nColumns = header.alignments.length,
total;
// begin document
lines = lines.concat(this.preamble);
lines.push('\\begin{document}');
// columns alignment
header.alignments.forEach(function(value, index, array){
array[index] = value[0];
});
// begin table
cmd = '\\begin{longtable}{' + header.alignments.join('') + '}';
lines.push(cmd);
// the header label of the RowNumberer column is a non breaking space
// replace it by an empty space
header.labels.forEach(function(element, index, array){
if (element === "&#160") {
array[index] = "";
}
});
// table header
if (! header.hidden) {
lines.push(header.labels.join(' & ') + ' \\\\');
lines.push('\\hline\\hline');
}
// table content
//
// convert the HTML row associated to the Ext.grid.View.
// The structure of the HTML table is complex because it contains
// several type of row depending on the grouping features.
//
grid.getView().getEl().select("table tr").each(function(trEl, cEl, index){
if(trEl.hasCls("x-grid-wrap-row")) {
div = trEl.down(".x-grid-group-title");
if (div) {
cmd = '\\multicolumn{' + nColumns + '}{l}{' + div.dom.textContent + '} \\\\';
lines.push(cmd);
lines.push('\\hline');
}
} else if (trEl.hasCls("x-grid-data-row") || trEl.hasCls("x-grid-row-summary")) {
li = [];
trEl.select("td").each(function(tdEl){
var val;
// shrink empty string otherwise latex processing failed
val = tdEl.dom.textContent;
if (val.length === 1) {
val = val.trim();
}
li.push(val);
}, this);
li = li.join(' & ') + ' \\\\';
// unfortunately the grand total appear as the first row (Ext JS 4.2)
if (index === 0 && trEl.hasCls("x-grid-row-summary")) {
total = li;
} else {
lines.push(li);
}
}
}, this);
// push the grand total at the end
if (total) {
lines.push('\\hline\\hline');
lines.push(total);
}
//end table and document
lines.push('\\end{longtable}');
lines.push('\\end{document}');
return lines;
}
});
\ No newline at end of file
......@@ -49,7 +49,7 @@
}
// instantiate the grid
Ext.create('Ext.grid.Panel', {
Ext.create('App.grid.Panel', {
plugins: ['pGridExport'],
store: Ext.create('Ext.data.ArrayStore', cfgStore),
columns: [
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment