EnhancedGrid.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. define("dojox/grid/EnhancedGrid", [
  2. "dojo/_base/kernel",
  3. "../main",
  4. "dojo/_base/declare",
  5. "dojo/_base/lang",
  6. "dojo/_base/array",
  7. "dojo/_base/sniff",
  8. "dojo/dom",
  9. "dojo/dom-geometry",
  10. "dojo/i18n",
  11. "./DataGrid",
  12. "./DataSelection",
  13. "./enhanced/_PluginManager",
  14. "./enhanced/plugins/_SelectionPreserver",//default loaded plugin
  15. "dojo/i18n!./enhanced/nls/EnhancedGrid"
  16. ], function(dojo, dojox, declare, lang, array, has, dom, domGeometry, i18n,
  17. DataGrid, DataSelection, _PluginManager, _SelectionPreserver){
  18. dojo.experimental("dojox.grid.EnhancedGrid");
  19. var EnhancedGrid = declare("dojox.grid.EnhancedGrid", DataGrid, {
  20. // summary:
  21. // Provides enhanced features based on DataGrid
  22. //
  23. // description:
  24. // EnhancedGrid features are implemented as plugins that could be loaded on demand.
  25. // Explicit dojo.require() is needed to use these feature plugins.
  26. //
  27. // example:
  28. // A quick sample to use EnhancedGrid features:
  29. //
  30. // Step 1. Load EnhancedGrid and required features
  31. // | <script type="text/javascript">
  32. // | dojo.require("dojox.grid.EnhancedGrid");
  33. // | dojo.require("dojox.grid.enhanced.plugins.DnD");
  34. // | dojo.require("dojox.grid.enhanced.plugins.Menu");
  35. // | dojo.require("dojox.grid.enhanced.plugins.NestedSorting");
  36. // | dojo.require("dojox.grid.enhanced.plugins.IndirectSelection");
  37. // | </script>
  38. //
  39. // Step 2. Use EnhancedGrid
  40. // - Via HTML markup
  41. // | <div dojoType="dojox.grid.EnhancedGrid" ...
  42. // | plugins="{nestedSorting: true, dnd: true, indirectSelection: true,
  43. // | menus:{headerMenu:"headerMenuId", rowMenu:"rowMenuId", cellMenu:"cellMenuId",
  44. // | selectedRegionMenu:"selectedRegionMenuId"}}">
  45. // | ...
  46. // | </div>
  47. //
  48. // - Or via JavaScript
  49. // | <script type="text/javascript">
  50. // | var grid = new dojox.grid.EnhancedGrid({plugins : {nestedSorting: true, dnd: true, indirectSelection: true,
  51. // | menus:{headerMenu:"headerMenuId", rowMenu:"rowMenuId", cellMenu:"cellMenuId",selectedRegionMenu:"selectedRegionMenuId"}},
  52. // | ... }, dojo.byId('gridDiv'));
  53. // | grid.startup();
  54. // | </script>
  55. //
  56. //
  57. // Plugin Support
  58. // [Note: Plugin support is still experimental]
  59. //
  60. // You can either customize the default plugins or add new ones, more details please see
  61. // - dojox.grid.enhanced._PluginManager
  62. // - dojox.grid.enhanced._Plugin
  63. // - dojox.grid.enhanced.plugins.*
  64. //plugins: Object
  65. // Plugin properties, e.g. {nestedSorting: true, dnd: true, ...}
  66. plugins: null,
  67. //pluginMgr: Object
  68. // Singleton plugin manager
  69. pluginMgr: null,
  70. //_pluginMgrClass: Object
  71. // Default plugin manager class
  72. _pluginMgrClass: _PluginManager,
  73. postMixInProperties: function(){
  74. //load nls bundle
  75. this._nls = i18n.getLocalization("dojox.grid.enhanced", "EnhancedGrid", this.lang);
  76. this.inherited(arguments);
  77. },
  78. postCreate: function(){
  79. //create plugin manager
  80. this.pluginMgr = new this._pluginMgrClass(this);
  81. this.pluginMgr.preInit();
  82. this.inherited(arguments);
  83. this.pluginMgr.postInit();
  84. },
  85. plugin: function(/*String*/name){
  86. // summary:
  87. // An easier way for getting a plugin, e.g. grid.plugin('dnd')
  88. return this.pluginMgr.getPlugin(name);
  89. },
  90. startup: function(){
  91. this.inherited(arguments);
  92. this.pluginMgr.startup();
  93. },
  94. createSelection: function(){
  95. this.selection = new dojox.grid.enhanced.DataSelection(this);
  96. },
  97. canSort: function(colIndex, field){
  98. // summary:
  99. // Overwritten
  100. return true;
  101. },
  102. doKeyEvent: function(e){
  103. // summary:
  104. // Overwritten, see _Grid.doKeyEvent()
  105. try{
  106. var view = this.focus.focusView;
  107. view.content.decorateEvent(e);
  108. if(!e.cell){ view.header.decorateEvent(e); }
  109. }catch(e){}
  110. this.inherited(arguments);
  111. },
  112. doApplyCellEdit: function(inValue, inRowIndex, inAttrName){
  113. // summary:
  114. // Overwritten, see DataGrid.doApplyCellEdit()
  115. if(!inAttrName){
  116. this.invalidated[inRowIndex] = true;
  117. return;
  118. }
  119. this.inherited(arguments);
  120. },
  121. mixin: function(target, source){
  122. var props = {};
  123. for(var p in source){
  124. if(p == '_inherited' || p == 'declaredClass' || p == 'constructor' ||
  125. source['privates'] && source['privates'][p]){
  126. continue;
  127. }
  128. props[p] = source[p];
  129. }
  130. lang.mixin(target, props);
  131. },
  132. _copyAttr: function(idx, attr){
  133. // summary:
  134. // Overwritten, see DataGrid._copyAttr()
  135. // Fix cell TAB navigation for single click editing
  136. if(!attr){ return; }
  137. return this.inherited(arguments);
  138. },
  139. _getHeaderHeight: function(){
  140. // summary:
  141. // Overwritten, see _Grid._getHeaderHeight()
  142. // Should include borders/margins of this.viewsHeaderNode
  143. this.inherited(arguments);
  144. return domGeometry.getMarginBox(this.viewsHeaderNode).h;
  145. },
  146. _fetch: function(start, isRender){
  147. // summary:
  148. // Overwritten, see DataGrid._fetch()
  149. if(this.items){
  150. return this.inherited(arguments);
  151. }
  152. start = start || 0;
  153. if(this.store && !this._pending_requests[start]){
  154. if(!this._isLoaded && !this._isLoading){
  155. this._isLoading = true;
  156. this.showMessage(this.loadingMessage);
  157. }
  158. this._pending_requests[start] = true;
  159. try{
  160. var req = {
  161. start: start,
  162. count: this.rowsPerPage,
  163. query: this.query,
  164. sort: this.getSortProps(),
  165. queryOptions: this.queryOptions,
  166. isRender: isRender,
  167. onBegin: lang.hitch(this, "_onFetchBegin"),
  168. onComplete: lang.hitch(this, "_onFetchComplete"),
  169. onError: lang.hitch(this, "_onFetchError")
  170. };
  171. this._storeLayerFetch(req);
  172. }catch(e){
  173. this._onFetchError(e, {start: start, count: this.rowsPerPage});
  174. }
  175. }
  176. return 0;
  177. },
  178. _storeLayerFetch: function(req){
  179. // summary:
  180. // Extracted fetch specifically for store layer use
  181. this.store.fetch(req);
  182. },
  183. getCellByField: function(field){
  184. return array.filter(this.layout.cells, function(cell){
  185. return cell.field == field;
  186. })[0];
  187. },
  188. onMouseUp: function(e){ },
  189. createView: function(){
  190. // summary
  191. // Overwrite: rewrite getCellX of view.header
  192. var view = this.inherited(arguments);
  193. if(has("mozilla")){
  194. var ascendDom = function(inNode, inWhile){
  195. for(var n = inNode; n && inWhile(n); n = n.parentNode){}
  196. return n;
  197. };//copied from dojox.grid._Builder
  198. var makeNotTagName = function(inTagName){
  199. var name = inTagName.toUpperCase();
  200. return function(node){ return node.tagName != name; };
  201. };//copied from dojox.grid._Builder
  202. var func = view.header.getCellX;
  203. view.header.getCellX = function(e){
  204. var x = func.call(view.header, e);
  205. var n = ascendDom(e.target, makeNotTagName("th"));
  206. if(n && n !== e.target && dom.isDescendant(e.target, n)){ x += n.firstChild.offsetLeft; }
  207. return x;
  208. };
  209. }
  210. return view;
  211. },
  212. destroy: function(){
  213. // summary:
  214. // Destroy all resources
  215. delete this._nls;
  216. this.pluginMgr.destroy();
  217. this.inherited(arguments);
  218. }
  219. });
  220. declare("dojox.grid.enhanced.DataSelection", DataSelection, {
  221. constructor: function(grid){
  222. if(grid.keepSelection){
  223. if(this.preserver){
  224. this.preserver.destroy();
  225. }
  226. this.preserver = new _SelectionPreserver(this);
  227. }
  228. },
  229. _range: function(inFrom, inTo){
  230. this.grid._selectingRange = true;
  231. this.inherited(arguments);
  232. this.grid._selectingRange = false;
  233. this.onChanged();
  234. },
  235. deselectAll: function(inItemOrIndex){
  236. this.grid._selectingRange = true;
  237. this.inherited(arguments);
  238. this.grid._selectingRange = false;
  239. this.onChanged();
  240. }
  241. });
  242. EnhancedGrid.markupFactory = function(props, node, ctor, cellFunc){
  243. return dojox.grid._Grid.markupFactory(props, node, ctor,
  244. lang.partial(DataGrid.cell_markupFactory, cellFunc));
  245. };
  246. EnhancedGrid.registerPlugin = function(clazz, props){
  247. _PluginManager.registerPlugin(clazz, props);
  248. };
  249. return EnhancedGrid;
  250. });