]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/AutoGrid.js
441f6fc0734b4a14c697d20027c41df7d9f735d9
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / widget / AutoGrid.js
1 if(!dojo._hasResource['openils.widget.AutoGrid']) {
2     dojo.provide('openils.widget.AutoGrid');
3     dojo.require('dojox.grid.DataGrid');
4     dojo.require('openils.widget.AutoWidget');
5     dojo.require('openils.widget.AutoFieldWidget');
6     dojo.require('openils.widget.EditDialog');
7     dojo.require('openils.Util');
8
9     dojo.declare(
10         'openils.widget.AutoGrid',
11         [dojox.grid.DataGrid, openils.widget.AutoWidget],
12         {
13
14             /* if true, pop up an edit dialog when user hits Enter on a give row */
15             editOnEnter : false, 
16
17             startup : function() {
18
19                 this.selectionMode = 'single';
20                 this.inherited(arguments);
21                 this.initAutoEnv();
22                 var existing = (this.structure && this.structure[0].cells[0]) ? 
23                     this.structure[0].cells[0] : [];
24                 var fields = [];
25
26                 for(var f in this.sortedFieldList) {
27                     var field = this.sortedFieldList[f];
28                     if(!field || field.virtual) continue;
29                     var entry = existing.filter(function(i){return (i.field == field.name)})[0];
30                     if(entry) entry.name = field.label;
31                     else entry = {field:field.name, name:field.label};
32                     fields.push(entry);
33                     if(!entry.get) 
34                         entry.get = openils.widget.AutoGrid.defaultGetter
35                 }
36
37                 this.setStructure([{cells: [fields]}]);
38                 this.setStore(this.buildAutoStore());
39                 if(this.editOnEnter) 
40                     this._applyEditOnEnter();
41             },
42
43             /* capture keydown and launch edit dialog on enter */
44             _applyEditOnEnter : function() {
45
46                 this.onMouseOverRow = function(e) {};
47                 this.onMouseOutRow = function(e) {};
48                 this.onCellFocus = function(cell, rowIndex) { 
49                     this.selection.deselectAll();
50                     this.selection.select(this.focus.rowIndex);
51                 };
52
53                 dojo.connect(this, 'onKeyDown',
54                     function(e) {
55                         if(e.keyCode == dojo.keys.ENTER) {
56                             this.selection.deselectAll();
57                             this.selection.select(this.focus.rowIndex);
58                             this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
59                         }
60                     }
61                 );
62             },
63
64             _drawEditDialog : function(storeItem, rowIndex) {
65                 var grid = this;
66                 var fmObject = new fieldmapper[this.fmClass]().fromStoreItem(storeItem);
67                 var idents = grid.store.getIdentityAttributes();
68                 var dialog = new openils.widget.EditDialog({
69                     fmObject:fmObject,
70                     onPostApply : function() {
71                         for(var i in fmObject._fields) {
72                             var field = fmObject._fields[i];
73                             if(idents.filter(function(j){return (j == field)})[0])
74                                 continue; // don't try to edit an identifier field
75                             grid.store.setValue(storeItem, field, fmObject[field]());
76                         }
77                         grid.update();
78                         dialog.destroy();
79                     }
80                 });
81                 dialog.editPane.fieldOrder = this.fieldOrder;
82                 dialog.startup();
83                 dialog.show();
84             }
85         }
86     );
87     openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
88
89     openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
90         if(!item) return '';
91         var val = this.grid.store.getValue(item, this.field);
92         var autoWidget = new openils.widget.AutoFieldWidget({
93             fmClass: this.grid.fmClass,
94             fmField: this.field,
95             widgetValue : val,
96         });
97         return autoWidget.getDisplayString();
98     }
99 }
100