Exporter.js 7.8 KB

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