1 | define([ |
---|
2 | "dojo/_base/declare", |
---|
3 | "dojo/_base/array", |
---|
4 | "dojo/_base/lang", |
---|
5 | "../_Plugin", |
---|
6 | "../../_RowSelector", |
---|
7 | "../../EnhancedGrid", |
---|
8 | "../../cells/_base" |
---|
9 | ], function(declare, array, lang, _Plugin, _RowSelector, EnhancedGrid){ |
---|
10 | |
---|
11 | var gridCells = lang.getObject("dojox.grid.cells"); |
---|
12 | |
---|
13 | var Exporter = declare("dojox.grid.enhanced.plugins.Exporter", _Plugin, { |
---|
14 | // summary: |
---|
15 | // Provide functions to export the grid data into a given format. |
---|
16 | // |
---|
17 | // Acceptable plugin parameters: |
---|
18 | // |
---|
19 | // 1. exportFormatter: function(data, cell, rowIndex, item) |
---|
20 | // Provide a way to customize how data should look in exported string. |
---|
21 | // Note that usually the formatter of grid cell should not be used here (it can return HTML or even widget). |
---|
22 | // example: |
---|
23 | // | function onExported(exported_text){ |
---|
24 | // | //custom code here... |
---|
25 | // | } |
---|
26 | // | dijit.byId("my_grid_id").exportTo("csv", //registered export format, mandatory |
---|
27 | // | { //the whole object is optional. |
---|
28 | // | fetchArgs: {start:0,count:1000}, //keywordArgs for fetch, optional |
---|
29 | // | writerArgs: {separator:';'}, //export writer specific arguments, optional |
---|
30 | // | }, |
---|
31 | // | function(str){ |
---|
32 | // | //call back function, mandatory |
---|
33 | // | }); |
---|
34 | // | var result = dijit.byId("my_grid_id").exportSelectedTo("table", //registered export format, mandatory |
---|
35 | // | {separator:'|'} //export writer specific arguments, optional |
---|
36 | // | ); |
---|
37 | // |
---|
38 | |
---|
39 | // name: String |
---|
40 | // Plugin name. |
---|
41 | name: "exporter", |
---|
42 | |
---|
43 | constructor: function(grid, args){ |
---|
44 | // summary: |
---|
45 | // only newed by _Plugin |
---|
46 | // grid: EnhancedGrid |
---|
47 | // The grid to plug in to. |
---|
48 | this.grid = grid; |
---|
49 | this.formatter = (args && lang.isObject(args)) && args.exportFormatter; |
---|
50 | this._mixinGrid(); |
---|
51 | }, |
---|
52 | _mixinGrid: function(){ |
---|
53 | var g = this.grid; |
---|
54 | g.exportTo = lang.hitch(this, this.exportTo); |
---|
55 | g.exportGrid = lang.hitch(this, this.exportGrid); |
---|
56 | g.exportSelected = lang.hitch(this, this.exportSelected); |
---|
57 | g.setExportFormatter = lang.hitch(this, this.setExportFormatter); |
---|
58 | }, |
---|
59 | setExportFormatter: function(formatter){ |
---|
60 | this.formatter = formatter; |
---|
61 | }, |
---|
62 | exportGrid: function(type, args, onExported){ |
---|
63 | // summary: |
---|
64 | // Export required rows(fetchArgs) to a kind of format(type) |
---|
65 | // using the corresponding writer with given arguments(writerArgs), |
---|
66 | // then pass the exported text to a given function(onExported). |
---|
67 | // tags: |
---|
68 | // public |
---|
69 | // type: String |
---|
70 | // A registered export format name |
---|
71 | // args: Object? |
---|
72 | // includes: |
---|
73 | // | { |
---|
74 | // | fetchArgs: object? |
---|
75 | // | Any arguments for store.fetch |
---|
76 | // | writerArgs: object? |
---|
77 | // | Arguments for the given format writer |
---|
78 | // | } |
---|
79 | // onExported: Function(string) |
---|
80 | // Call back function when export result is ready |
---|
81 | if(lang.isFunction(args)){ |
---|
82 | onExported = args; |
---|
83 | args = {}; |
---|
84 | } |
---|
85 | if(!lang.isString(type) || !lang.isFunction(onExported)){ |
---|
86 | return; |
---|
87 | } |
---|
88 | args = args || {}; |
---|
89 | var g = this.grid, _this = this, |
---|
90 | writer = this._getExportWriter(type, args.writerArgs), |
---|
91 | fetchArgs = (args.fetchArgs && lang.isObject(args.fetchArgs)) ? args.fetchArgs : {}, |
---|
92 | oldFunc = fetchArgs.onComplete; |
---|
93 | if(g.store){ |
---|
94 | fetchArgs.onComplete = function(items, request){ |
---|
95 | if(oldFunc){ |
---|
96 | oldFunc(items, request); |
---|
97 | } |
---|
98 | onExported(_this._goThroughGridData(items, writer)); |
---|
99 | }; |
---|
100 | fetchArgs.sort = fetchArgs.sort || g.getSortProps(); |
---|
101 | g._storeLayerFetch(fetchArgs); |
---|
102 | }else{ |
---|
103 | //Data is defined directly in the structure; |
---|
104 | var start = fetchArgs.start || 0, |
---|
105 | count = fetchArgs.count || -1, |
---|
106 | items = []; |
---|
107 | for(var i = start; i != start + count && i < g.rowCount; ++i){ |
---|
108 | items.push(g.getItem(i)); |
---|
109 | } |
---|
110 | onExported(this._goThroughGridData(items, writer)); |
---|
111 | } |
---|
112 | }, |
---|
113 | exportSelected: function(type, writerArgs, onExported){ |
---|
114 | // summary: |
---|
115 | // Only export selected rows. |
---|
116 | // tags: |
---|
117 | // public |
---|
118 | // type: string |
---|
119 | // A registered export format name |
---|
120 | // writerArgs: object? |
---|
121 | // Arguments for the given format writer |
---|
122 | // returns: string |
---|
123 | // The exported string |
---|
124 | if(!lang.isString(type)){ |
---|
125 | return ""; |
---|
126 | } |
---|
127 | var writer = this._getExportWriter(type, writerArgs); |
---|
128 | return onExported(this._goThroughGridData(this.grid.selection.getSelected(), writer)); //String |
---|
129 | }, |
---|
130 | _buildRow: function(/* object */arg_obj,/* ExportWriter */writer){ |
---|
131 | // summary: |
---|
132 | // Use the given export writer(writer) to go through a single row |
---|
133 | // which is given in the context object(arg_obj). |
---|
134 | // tags: |
---|
135 | // private |
---|
136 | // returns: |
---|
137 | // undefined |
---|
138 | var _this = this; |
---|
139 | array.forEach(arg_obj._views, function(view, vIdx){ |
---|
140 | arg_obj.view = view; |
---|
141 | arg_obj.viewIdx = vIdx; |
---|
142 | if(writer.beforeView(arg_obj)){ |
---|
143 | array.forEach(view.structure.cells, function(subrow, srIdx){ |
---|
144 | arg_obj.subrow = subrow; |
---|
145 | arg_obj.subrowIdx = srIdx; |
---|
146 | if(writer.beforeSubrow(arg_obj)){ |
---|
147 | array.forEach(subrow, function(cell, cIdx){ |
---|
148 | if(arg_obj.isHeader && _this._isSpecialCol(cell)){ |
---|
149 | arg_obj.spCols.push(cell.index); |
---|
150 | } |
---|
151 | arg_obj.cell = cell; |
---|
152 | arg_obj.cellIdx = cIdx; |
---|
153 | writer.handleCell(arg_obj); |
---|
154 | }); |
---|
155 | writer.afterSubrow(arg_obj); |
---|
156 | } |
---|
157 | }); |
---|
158 | writer.afterView(arg_obj); |
---|
159 | } |
---|
160 | }); |
---|
161 | }, |
---|
162 | _goThroughGridData: function(/* Array */items,/* ExportWriter */writer){ |
---|
163 | // summary: |
---|
164 | // Use the given export writer(writer) to go through the grid structure |
---|
165 | // and the given rows(items), then return the writer output. |
---|
166 | // tags: |
---|
167 | // private |
---|
168 | var grid = this.grid, |
---|
169 | views = array.filter(grid.views.views, function(view){ |
---|
170 | return !(view instanceof _RowSelector); |
---|
171 | }), |
---|
172 | arg_obj = { |
---|
173 | 'grid': grid, |
---|
174 | 'isHeader': true, |
---|
175 | 'spCols': [], |
---|
176 | '_views': views, |
---|
177 | 'colOffset': (views.length < grid.views.views.length ? -1 : 0) |
---|
178 | }; |
---|
179 | //go through header |
---|
180 | if(writer.beforeHeader(grid)){ |
---|
181 | this._buildRow(arg_obj,writer); |
---|
182 | writer.afterHeader(); |
---|
183 | } |
---|
184 | //go through content |
---|
185 | arg_obj.isHeader = false; |
---|
186 | if(writer.beforeContent(items)){ |
---|
187 | array.forEach(items, function(item, rIdx){ |
---|
188 | arg_obj.row = item; |
---|
189 | arg_obj.rowIdx = rIdx; |
---|
190 | if(writer.beforeContentRow(arg_obj)){ |
---|
191 | this._buildRow(arg_obj, writer); |
---|
192 | writer.afterContentRow(arg_obj); |
---|
193 | } |
---|
194 | }, this); |
---|
195 | writer.afterContent(); |
---|
196 | } |
---|
197 | return writer.toString(); |
---|
198 | }, |
---|
199 | _isSpecialCol: function(/* dojox.grid.__CellDef */header_cell){ |
---|
200 | // summary: |
---|
201 | // Row selectors and row indexes should be recognized and handled separately. |
---|
202 | // tags: |
---|
203 | // private |
---|
204 | return header_cell.isRowSelector || header_cell instanceof gridCells.RowIndex; //Boolean |
---|
205 | }, |
---|
206 | _getExportWriter: function(/* string */ fileType, /* object? */ writerArgs){ |
---|
207 | // summary: |
---|
208 | // Use the given export format type(fileType) |
---|
209 | // and writer arguments(writerArgs) to create |
---|
210 | // a ExportWriter and return it. |
---|
211 | // tags: |
---|
212 | // private |
---|
213 | var writerName, cls, |
---|
214 | expCls = Exporter; |
---|
215 | if(expCls.writerNames){ |
---|
216 | writerName = expCls.writerNames[fileType.toLowerCase()]; |
---|
217 | cls = lang.getObject(writerName); |
---|
218 | if(cls){ |
---|
219 | var writer = new cls(writerArgs); |
---|
220 | writer.formatter = this.formatter; |
---|
221 | return writer; //ExportWriter |
---|
222 | }else{ |
---|
223 | throw new Error('Please make sure class "' + writerName + '" is required.'); |
---|
224 | } |
---|
225 | } |
---|
226 | throw new Error('The writer for "' + fileType + '" has not been registered.'); |
---|
227 | } |
---|
228 | }); |
---|
229 | |
---|
230 | Exporter.registerWriter = function(/* string */fileType,/* string */writerClsName){ |
---|
231 | // summary: |
---|
232 | // Register a writer(writerClsName) to a export format type(fileType). |
---|
233 | // This function separates the Exporter from all kinds of writers. |
---|
234 | // tags: |
---|
235 | // public |
---|
236 | Exporter.writerNames = Exporter.writerNames || {}; |
---|
237 | Exporter.writerNames[fileType] = writerClsName; |
---|
238 | }; |
---|
239 | |
---|
240 | EnhancedGrid.registerPlugin(Exporter/*name:'exporter'*/); |
---|
241 | |
---|
242 | return Exporter; |
---|
243 | }); |
---|