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