Debian Codegraph @

dojox/grid/enhanced/plugins/exporter/CSVWriter.js

raw symbols SCIP
1 define([
2 "dojo/_base/declare",
3 "dojo/_base/lang",
4 "dojo/_base/array",
5 "./_ExportWriter",
6 "../Exporter"
7 ], function(declare, lang, array, _ExportWriter, Exporter){
8
9 Exporter.registerWriter("csv", "dojox.grid.enhanced.plugins.exporter.CSVWriter");
10
11 return declare("dojox.grid.enhanced.plugins.exporter.CSVWriter", _ExportWriter, {
12 // summary:
13 // Export grid to CSV format.
14 _separator: ',',
15
16 _newline: "\r\n",
17
18 constructor: function(/* object? */writerArgs){
19 // summary:
20 // CSV default separator is ','.
21 // But we can also use our own.
22 // writerArgs: object?
23 // {separator:'...'}
24 if(writerArgs){
25 this._separator = writerArgs.separator ? writerArgs.separator : this._separator;
26 this._newline = writerArgs.newline ? writerArgs.newline : this._newline;
27 }
28 this._headers = [];
29 this._dataRows = [];
30 },
31
32 _formatCSVCell: function(/* string */cellValue){
33 // summary:
34 // Format cell value to follow CSV standard.
35 // See: http://en.wikipedia.org/wiki/Comma-separated_values
36 // tags:
37 // private
38 // cellValue: string
39 // The value in a cell.
40 // returns:
41 // The formatted content of a cell
42 if(cellValue === null || cellValue === undefined){
43 return '';
44 }
45 var result = String(cellValue).replace(/"/g, '""');
46 if(result.indexOf(this._separator) >= 0 || result.search(/[" \t\r\n]/) >= 0){
47 result = '"' + result + '"';
48 }
49 return result; //String
50 },
51
52 beforeContentRow: function(/* object */arg_obj){
53 // summary:
54 // Overrided from _ExportWriter
55 var row = [],
56 func = lang.hitch(this, this._formatCSVCell);
57 array.forEach(arg_obj.grid.layout.cells, function(cell){
58 //We are not interested in indirect selectors and row indexes.
59 if(!cell.hidden && array.indexOf(arg_obj.spCols,cell.index) < 0){
60 //We only need data here, not html
61 row.push(func(this._getExportDataForCell(arg_obj.rowIndex, arg_obj.row, cell, arg_obj.grid)));
62 }
63 }, this);
64 this._dataRows.push(row);
65 //We do not need to go into the row.
66 return false; //Boolean
67 },
68
69 handleCell: function(/* object */arg_obj){
70 // summary:
71 // Overrided from _ExportWriter
72 var cell = arg_obj.cell;
73 if(arg_obj.isHeader && !cell.hidden && array.indexOf(arg_obj.spCols,cell.index) < 0){
74 this._headers.push(cell.name || cell.field);
75 }
76 },
77
78 toString: function(){
79 // summary:
80 // Overrided from _ExportWriter
81 var result = this._headers.join(this._separator);
82 for(var i = this._dataRows.length - 1; i >= 0; --i){
83 this._dataRows[i] = this._dataRows[i].join(this._separator);
84 }
85 return result + this._newline + this._dataRows.join(this._newline); //String
86 }
87 });
88 });