Debian Codegraph @

dojox/grid/DataGrid.js

raw symbols SCIP
1 define([
2 "../main",
3 "dojo/_base/array",
4 "dojo/_base/lang",
5 "dojo/_base/json",
6 "dojo/_base/sniff",
7 "dojo/_base/declare",
8 "./_Grid",
9 "./DataSelection",
10 "dojo/_base/html",
11 "dojo/has",
12 "dojo/has!dojo-bidi?./bidi/_BidiMixin"
13 ], function(dojox, array, lang, json, has, declare, _Grid, DataSelection, html){
14
15 /*=====
16 declare("dojox.grid.__DataCellDef", dojox.grid.__CellDef, {
17 constructor: function(){
18 // field: String?
19 // The attribute to read from the dojo.data item for the row.
20 // fields: String[]?
21 // An array of fields to grab the values of and pass as an array to the grid
22 // get: Function?
23 // function(rowIndex, item?){} rowIndex is of type Integer, item is of type
24 // Object. This function will be called when a cell requests data. Returns
25 // the unformatted data for the cell.
26 }
27 });
28 =====*/
29
30 /*=====
31 declare("dojox.grid.__DataViewDef", dojox.grid.__ViewDef, {
32 constructor: function(){
33 // cells: dojox.grid.__DataCellDef[]|Array[dojox.grid.__DataCellDef[]]?
34 // The structure of the cells within this grid.
35 // defaultCell: dojox.grid.__DataCellDef?
36 // A cell definition with default values for all cells in this view. If
37 // a property is defined in a cell definition in the "cells" array and
38 // this property, the cell definition's property will override this
39 // property's property.
40 }
41 });
42 =====*/
43
44 var DataGrid = declare("dojox.grid.DataGrid", _Grid, {
45 store: null,
46 query: null,
47 queryOptions: null,
48 fetchText: '...',
49 sortFields: null,
50
51 // updateDelay: int
52 // Time, in milliseconds, to delay updates automatically so that multiple
53 // calls to onSet/onNew/onDelete don't keep rerendering the grid. Set
54 // to 0 to immediately cause updates. A higher value will result in
55 // better performance at the expense of responsiveness of the grid.
56 updateDelay: 1,
57
58 /*=====
59 // structure: dojox.grid.__DataViewDef|dojox.grid.__DataViewDef[]|dojox.grid.__DataCellDef[]|Array[dojox.grid.__DataCellDef[]]
60 // View layout definition.
61 structure: '',
62 =====*/
63
64 // You can specify items instead of a query, if you like. They do not need
65 // to be loaded - but the must be items in the store
66 items: null,
67
68 _store_connects: null,
69 _by_idty: null,
70 _by_idx: null,
71 _cache: null,
72 _pages: null,
73 _pending_requests: null,
74 _bop: -1,
75 _eop: -1,
76 _requests: 0,
77 rowCount: 0,
78
79 _isLoaded: false,
80 _isLoading: false,
81
82 // keepSelection: Boolean
83 // Whether keep selection after sort, filter etc.
84 keepSelection: false,
85
86 postCreate: function(){
87 this._pages = [];
88 this._store_connects = [];
89 this._by_idty = {};
90 this._by_idx = [];
91 this._cache = [];
92 this._pending_requests = {};
93
94 this._setStore(this.store);
95 this.inherited(arguments);
96 },
97
98 destroy: function(){
99 this.selection.destroy();
100 this.inherited(arguments);
101 },
102
103 createSelection: function(){
104 this.selection = new DataSelection(this);
105 },
106
107 get: function(inRowIndex, inItem){
108 // summary:
109 // Default data getter.
110 // description:
111 // Provides data to display in a grid cell. Called in grid cell context.
112 // So this.cell.index is the column index.
113 // inRowIndex: Integer
114 // Row for which to provide data
115 // returns:
116 // Data to display for a given grid cell.
117
118 if(inItem && this.field == "_item" && !this.fields){
119 return inItem;
120 }else if(inItem && this.fields){
121 var ret = [];
122 var s = this.grid.store;
123 array.forEach(this.fields, function(f){
124 ret = ret.concat(s.getValues(inItem, f));
125 });
126 return ret;
127 }else if(!inItem && typeof inRowIndex === "string"){
128 return this.inherited(arguments);
129 }
130 return (!inItem ? this.defaultValue : (!this.field ? this.value : (this.field == "_item" ? inItem : this.grid.store.getValue(inItem, this.field))));
131 },
132
133 _checkUpdateStatus: function(){
134 if(this.updateDelay > 0){
135 var iStarted = false;
136 if(this._endUpdateDelay){
137 clearTimeout(this._endUpdateDelay);
138 delete this._endUpdateDelay;
139 iStarted = true;
140 }
141 if(!this.updating){
142 this.beginUpdate();
143 iStarted = true;
144 }
145 if(iStarted){
146 var _this = this;
147 this._endUpdateDelay = setTimeout(function(){
148 delete _this._endUpdateDelay;
149 _this.endUpdate();
150 }, this.updateDelay);
151 }
152 }
153 },
154
155 _onSet: function(item, attribute, oldValue, newValue){
156 this._checkUpdateStatus();
157 var idx = this.getItemIndex(item);
158 if(idx>-1){
159 this.updateRow(idx);
160 }
161 },
162
163 _createItem: function(item, index){
164 var idty = this._hasIdentity ? this.store.getIdentity(item) : json.toJson(this.query) + ":idx:" + index + ":sort:" + json.toJson(this.getSortProps());
165 var o = this._by_idty[idty] = { idty: idty, item: item };
166 return o;
167 },
168
169 _addItem: function(item, index, noUpdate){
170 this._by_idx[index] = this._createItem(item, index);
171 if(!noUpdate){
172 this.updateRow(index);
173 }
174 },
175
176 _onNew: function(item, parentInfo){
177 this._checkUpdateStatus();
178 var rowCount = this.get('rowCount');
179 this._addingItem = true;
180 this.updateRowCount(rowCount+1);
181 this._addingItem = false;
182 this._addItem(item, rowCount);
183 this.showMessage();
184 },
185
186 _onDelete: function(item){
187 this._checkUpdateStatus();
188 var idx = this._getItemIndex(item, true);
189
190 if(idx >= 0){
191 // When a row is deleted, all rest rows are shifted down,
192 // and migrate from page to page. If some page is not
193 // loaded yet empty rows can migrate to initialized pages
194 // without refreshing. It causes empty rows in some pages, see:
195 // http://bugs.dojotoolkit.org/ticket/6818
196 // this code fix this problem by reseting loaded page info
197 this._pages = [];
198 this._bop = -1;
199 this._eop = -1;
200
201 var o = this._by_idx[idx];
202 this._by_idx.splice(idx, 1);
203 delete this._by_idty[o.idty];
204 this.updateRowCount(this.get('rowCount')-1);
205 if(this.get('rowCount') === 0){
206 this.showMessage(this.noDataMessage);
207 }
208 }
209 if(this.selection.isSelected(idx)){
210 this.selection.deselect(idx);
211 this.selection.selected.splice(idx, 1);
212 }
213 },
214
215 _onRevert: function(){
216 this._refresh();
217 },
218
219 setStore: function(store, query, queryOptions){
220 if(this._requestsPending(0)){
221 return;
222 }
223 this._setQuery(query, queryOptions);
224 this._setStore(store);
225 this._refresh(true);
226 },
227
228 setQuery: function(query, queryOptions){
229 if(this._requestsPending(0)){
230 return;
231 }
232 this._setQuery(query, queryOptions);
233 this._refresh(true);
234 },
235
236 setItems: function(items){
237 this.items = items;
238 this._setStore(this.store);
239 this._refresh(true);
240 },
241
242 _setQuery: function(query, queryOptions){
243 this.query = query;
244 this.queryOptions = queryOptions || this.queryOptions;
245 },
246
247 _setStore: function(store){
248 if(this.store && this._store_connects){
249 array.forEach(this._store_connects, this.disconnect, this);
250 }
251 this.store = store;
252
253 if(this.store){
254 var f = this.store.getFeatures();
255 var h = [];
256
257 this._canEdit = !!f["dojo.data.api.Write"] && !!f["dojo.data.api.Identity"];
258 this._hasIdentity = !!f["dojo.data.api.Identity"];
259
260 if(!!f["dojo.data.api.Notification"] && !this.items){
261 h.push(this.connect(this.store, "onSet", "_onSet"));
262 h.push(this.connect(this.store, "onNew", "_onNew"));
263 h.push(this.connect(this.store, "onDelete", "_onDelete"));
264 }
265 if(this._canEdit){
266 h.push(this.connect(this.store, "revert", "_onRevert"));
267 }
268
269 this._store_connects = h;
270 }
271 },
272
273 _onFetchBegin: function(size, req){
274 if(!this.scroller){ return; }
275 if(this.rowCount != size){
276 if(req.isRender){
277 this.scroller.init(size, this.keepRows, this.rowsPerPage);
278 this.rowCount = size;
279 this._setAutoHeightAttr(this.autoHeight, true);
280 this._skipRowRenormalize = true;
281 this.prerender();
282 this._skipRowRenormalize = false;
283 }else{
284 this.updateRowCount(size);
285 }
286 }
287 if(!size){
288 this.views.render();
289 this._resize();
290 this.showMessage(this.noDataMessage);
291 this.focus.initFocusView();
292 }else{
293 this.showMessage();
294 }
295 },
296
297 _onFetchComplete: function(items, req){
298 if(!this.scroller){ return; }
299 if(items && items.length > 0){
300 //console.log(items);
301 array.forEach(items, function(item, idx){
302 this._addItem(item, req.start+idx, true);
303 }, this);
304 this.updateRows(req.start, items.length);
305 if(req.isRender){
306 this.setScrollTop(0);
307 this.postrender();
308 }else if(this._lastScrollTop){
309 this.setScrollTop(this._lastScrollTop);
310 }
311 if(has("ie")){
312 html.setSelectable(this.domNode, this.selectable);
313 }
314 }
315 delete this._lastScrollTop;
316 if(!this._isLoaded){
317 this._isLoading = false;
318 this._isLoaded = true;
319 }
320 this._pending_requests[req.start] = false;
321 },
322
323 _onFetchError: function(err, req){
324 console.log(err);
325 delete this._lastScrollTop;
326 if(!this._isLoaded){
327 this._isLoading = false;
328 this._isLoaded = true;
329 this.showMessage(this.errorMessage);
330 }
331 this._pending_requests[req.start] = false;
332 this.onFetchError(err, req);
333 },
334
335 onFetchError: function(err, req){
336 },
337
338 _fetch: function(start, isRender){
339 start = start || 0;
340 if(this.store && !this._pending_requests[start]){
341 if(!this._isLoaded && !this._isLoading){
342 this._isLoading = true;
343 this.showMessage(this.loadingMessage);
344 }
345 this._pending_requests[start] = true;
346 //console.log("fetch: ", start);
347 try{
348 if(this.items){
349 var items = this.items;
350 var store = this.store;
351 this.rowsPerPage = items.length;
352 var req = {
353 start: start,
354 count: this.rowsPerPage,
355 isRender: isRender
356 };
357 this._onFetchBegin(items.length, req);
358
359 // Load them if we need to
360 var waitCount = 0;
361 array.forEach(items, function(i){
362 if(!store.isItemLoaded(i)){ waitCount++; }
363 });
364 if(waitCount === 0){
365 this._onFetchComplete(items, req);
366 }else{
367 var onItem = function(item){
368 waitCount--;
369 if(waitCount === 0){
370 this._onFetchComplete(items, req);
371 }
372 };
373 array.forEach(items, function(i){
374 if(!store.isItemLoaded(i)){
375 store.loadItem({item: i, onItem: onItem, scope: this});
376 }
377 }, this);
378 }
379 }else{
380 this.store.fetch({
381 start: start,
382 count: this.rowsPerPage,
383 query: this.query,
384 sort: this.getSortProps(),
385 queryOptions: this.queryOptions,
386 isRender: isRender,
387 onBegin: lang.hitch(this, "_onFetchBegin"),
388 onComplete: lang.hitch(this, "_onFetchComplete"),
389 onError: lang.hitch(this, "_onFetchError")
390 });
391 }
392 }catch(e){
393 this._onFetchError(e, {start: start, count: this.rowsPerPage});
394 }
395 }
396 },
397
398 _clearData: function(){
399 this.updateRowCount(0);
400 this._by_idty = {};
401 this._by_idx = [];
402 this._pages = [];
403 this._bop = this._eop = -1;
404 this._isLoaded = false;
405 this._isLoading = false;
406 },
407
408 getItem: function(idx){
409 var data = this._by_idx[idx];
410 if(!data||(data&&!data.item)){
411 this._preparePage(idx);
412 return null;
413 }
414 return data.item;
415 },
416
417 getItemIndex: function(item){
418 return this._getItemIndex(item, false);
419 },
420
421 _getItemIndex: function(item, isDeleted){
422 if(!isDeleted && !this.store.isItem(item)){
423 return -1;
424 }
425
426 var idty = this._hasIdentity ? this.store.getIdentity(item) : null;
427
428 for(var i=0, l=this._by_idx.length; i<l; i++){
429 var d = this._by_idx[i];
430 if(d && ((idty && d.idty == idty) || (d.item === item))){
431 return i;
432 }
433 }
434 return -1;
435 },
436
437 filter: function(query, reRender){
438 this.query = query;
439 if(reRender){
440 this._clearData();
441 }
442 this._fetch();
443 },
444
445 _getItemAttr: function(idx, attr){
446 var item = this.getItem(idx);
447 return (!item ? this.fetchText : this.store.getValue(item, attr));
448 },
449
450 // rendering
451 _render: function(){
452 if(this.domNode.parentNode){
453 this.scroller.init(this.get('rowCount'), this.keepRows, this.rowsPerPage);
454 this.prerender();
455 this._fetch(0, true);
456 }
457 },
458
459 // paging
460 _requestsPending: function(inRowIndex){
461 return this._pending_requests[inRowIndex];
462 },
463
464 _rowToPage: function(inRowIndex){
465 return (this.rowsPerPage ? Math.floor(inRowIndex / this.rowsPerPage) : inRowIndex);
466 },
467
468 _pageToRow: function(inPageIndex){
469 return (this.rowsPerPage ? this.rowsPerPage * inPageIndex : inPageIndex);
470 },
471
472 _preparePage: function(inRowIndex){
473 if((inRowIndex < this._bop || inRowIndex >= this._eop) && !this._addingItem){
474 var pageIndex = this._rowToPage(inRowIndex);
475 this._needPage(pageIndex);
476 this._bop = pageIndex * this.rowsPerPage;
477 this._eop = this._bop + (this.rowsPerPage || this.get('rowCount'));
478 }
479 },
480
481 _needPage: function(inPageIndex){
482 if(!this._pages[inPageIndex]){
483 this._pages[inPageIndex] = true;
484 this._requestPage(inPageIndex);
485 }
486 },
487
488 _requestPage: function(inPageIndex){
489 var row = this._pageToRow(inPageIndex);
490 var count = Math.min(this.rowsPerPage, this.get('rowCount') - row);
491 if(count > 0){
492 this._requests++;
493 if(!this._requestsPending(row)){
494 setTimeout(lang.hitch(this, "_fetch", row, false), 1);
495 //this.requestRows(row, count);
496 }
497 }
498 },
499
500 getCellName: function(inCell){
501 return inCell.field;
502 //console.log(inCell);
503 },
504
505 _refresh: function(isRender){
506 this._clearData();
507 this._fetch(0, isRender);
508 },
509
510 sort: function(){
511 this.edit.apply();
512 this._lastScrollTop = this.scrollTop;
513 this._refresh();
514 },
515
516 canSort: function(){
517 return (!this._isLoading);
518 },
519
520 getSortProps: function(){
521 var c = this.getCell(this.getSortIndex());
522 if(!c){
523 if(this.sortFields){
524 return this.sortFields;
525 }
526 return null;
527 }else{
528 var desc = c["sortDesc"];
529 var si = !(this.sortInfo>0);
530 if(typeof desc == "undefined"){
531 desc = si;
532 }else{
533 desc = si ? !desc : desc;
534 }
535 return [{ attribute: c.field, descending: desc }];
536 }
537 },
538
539 styleRowState: function(inRow){
540 // summary:
541 // Perform row styling
542 if(this.store && this.store.getState){
543 var states=this.store.getState(inRow.index), c='';
544 for(var i=0, ss=["inflight", "error", "inserting"], s; s=ss[i]; i++){
545 if(states[s]){
546 c = ' dojoxGridRow-' + s;
547 break;
548 }
549 }
550 inRow.customClasses += c;
551 }
552 },
553
554 onStyleRow: function(inRow){
555 this.styleRowState(inRow);
556 this.inherited(arguments);
557 },
558
559 // editing
560 canEdit: function(inCell, inRowIndex){
561 return this._canEdit;
562 },
563
564 _copyAttr: function(idx, attr){
565 var row = {};
566 var backstop = {};
567 var src = this.getItem(idx);
568 return this.store.getValue(src, attr);
569 },
570
571 doStartEdit: function(inCell, inRowIndex){
572 if(!this._cache[inRowIndex]){
573 this._cache[inRowIndex] = this._copyAttr(inRowIndex, inCell.field);
574 }
575 this.onStartEdit(inCell, inRowIndex);
576 },
577
578 doApplyCellEdit: function(inValue, inRowIndex, inAttrName){
579 this.store.fetchItemByIdentity({
580 identity: this._by_idx[inRowIndex].idty,
581 onItem: lang.hitch(this, function(item){
582 var oldValue = this.store.getValue(item, inAttrName);
583 if(typeof oldValue == 'number'){
584 inValue = isNaN(inValue) ? inValue : parseFloat(inValue);
585 }else if(typeof oldValue == 'boolean'){
586 inValue = inValue == 'true' ? true : inValue == 'false' ? false : inValue;
587 }else if(oldValue instanceof Date){
588 var asDate = new Date(inValue);
589 inValue = isNaN(asDate.getTime()) ? inValue : asDate;
590 }
591 this.store.setValue(item, inAttrName, inValue);
592 this.onApplyCellEdit(inValue, inRowIndex, inAttrName);
593 })
594 });
595 },
596
597 doCancelEdit: function(inRowIndex){
598 var cache = this._cache[inRowIndex];
599 if(cache){
600 this.updateRow(inRowIndex);
601 delete this._cache[inRowIndex];
602 }
603 this.onCancelEdit.apply(this, arguments);
604 },
605
606 doApplyEdit: function(inRowIndex, inDataAttr){
607 var cache = this._cache[inRowIndex];
608 /*if(cache){
609 var data = this.getItem(inRowIndex);
610 if(this.store.getValue(data, inDataAttr) != cache){
611 this.update(cache, data, inRowIndex);
612 }
613 delete this._cache[inRowIndex];
614 }*/
615 this.onApplyEdit(inRowIndex);
616 },
617
618 removeSelectedRows: function(){
619 // summary:
620 // Remove the selected rows from the grid.
621 if(this._canEdit){
622 this.edit.apply();
623 var fx = lang.hitch(this, function(items){
624 if(items.length){
625 array.forEach(items, this.store.deleteItem, this.store);
626 this.selection.clear();
627 }
628 });
629 if(this.allItemsSelected){
630 this.store.fetch({
631 query: this.query,
632 queryOptions: this.queryOptions,
633 onComplete: fx});
634 }else{
635 fx(this.selection.getSelected());
636 }
637 }
638 }
639 });
640
641 DataGrid.cell_markupFactory = function(cellFunc, node, cellDef){
642 var field = lang.trim(html.attr(node, "field")||"");
643 if(field){
644 cellDef.field = field;
645 }
646 cellDef.field = cellDef.field||cellDef.name;
647 var fields = lang.trim(html.attr(node, "fields")||"");
648 if(fields){
649 cellDef.fields = fields.split(",");
650 }
651 if(cellFunc){
652 cellFunc(node, cellDef);
653 }
654 };
655
656 DataGrid.markupFactory = function(props, node, ctor, cellFunc){
657 return _Grid.markupFactory(props, node, ctor,
658 lang.partial(DataGrid.cell_markupFactory, cellFunc));
659 };
660
661 return DataGrid;
662
663 });