]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/AutoGrid.js
when no field order is defined but fields existin in the markup, start there with...
[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             defaultCellWidth : null,
17
18             /* by default, don't show auto-generated (sequence) fields */
19             showSequenceFields : false, 
20
21             startup : function() {
22                 this.selectionMode = 'single';
23                 this.inherited(arguments);
24                 this.initAutoEnv();
25                 this.setStructure(this._compileStructure());
26                 this.setStore(this.buildAutoStore());
27                 if(this.editOnEnter) 
28                     this._applyEditOnEnter();
29             },
30
31             _compileStructure : function() {
32                 var existing = (this.structure && this.structure[0].cells[0]) ? 
33                     this.structure[0].cells[0] : [];
34                 var fields = [];
35
36                 var self = this;
37                 function pushEntry(entry) {
38                     if(!entry.get) 
39                         entry.get = openils.widget.AutoGrid.defaultGetter
40                     if(!entry.width && self.defaultCellWidth)
41                         entry.width = self.defaultCellWidth;
42                     fields.push(entry);
43                 }
44
45                 if(!this.fieldOrder) {
46                     /* no order defined, start with any explicit grid fields */
47                     for(var e in existing) {
48                         var entry = existing[e];
49                         var field = this.fmIDL.fields.filter(
50                             function(i){return (i.name == entry.field)})[0];
51                         if(field) entry.name = entry.name || field.label;
52                         pushEntry(entry);
53                     }
54                 }
55
56                 for(var f in this.sortedFieldList) {
57                     var field = this.sortedFieldList[f];
58                     if(!field || field.virtual) continue;
59                     
60                     // field was already added above
61                     if(fields.filter(function(i){return (i.field == field.name)})[0]) 
62                         continue;
63
64
65                     if(!this.showSequenceFields && field.name == this.fmIDL.pkey && this.fmIDL.pkey_sequence)
66                         continue; 
67                     var entry = existing.filter(function(i){return (i.field == field.name)})[0];
68                     if(entry) entry.name = field.label;
69                     else entry = {field:field.name, name:field.label};
70                     pushEntry(entry);
71                 }
72
73                 return [{cells: [fields]}];
74             },
75
76
77             /* capture keydown and launch edit dialog on enter */
78             _applyEditOnEnter : function() {
79
80                 this.onMouseOverRow = function(e) {};
81                 this.onMouseOutRow = function(e) {};
82                 this.onCellFocus = function(cell, rowIndex) { 
83                     this.selection.deselectAll();
84                     this.selection.select(this.focus.rowIndex);
85                 };
86
87                 dojo.connect(this, 'onRowDblClick',
88                     function(e) {
89                         this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
90                     }
91                 );
92
93                 dojo.connect(this, 'onKeyDown',
94                     function(e) {
95                         if(e.keyCode == dojo.keys.ENTER) {
96                             this.selection.deselectAll();
97                             this.selection.select(this.focus.rowIndex);
98                             this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
99                         }
100                     }
101                 );
102             },
103
104             _drawEditDialog : function(storeItem, rowIndex) {
105                 var grid = this;
106                 var fmObject = new fieldmapper[this.fmClass]().fromStoreItem(storeItem);
107                 var idents = grid.store.getIdentityAttributes();
108                 var dialog = new openils.widget.EditDialog({
109                     fmObject:fmObject,
110                     onPostSubmit : function() {
111                         for(var i in fmObject._fields) {
112                             var field = fmObject._fields[i];
113                             if(idents.filter(function(j){return (j == field)})[0])
114                                 continue; // don't try to edit an identifier field
115                             grid.store.setValue(storeItem, field, fmObject[field]());
116                         }
117                         dialog.destroy();
118
119                         if(self.onPostUpdate)
120                             self.onPostUpdate(storeItem, rowIndex);
121
122                         setTimeout(
123                             function(){
124                                 try { 
125                                     grid.views.views[0].getCellNode(rowIndex, 0).focus(); 
126                                 } catch (E) {}
127                             },200
128                         );
129                     },
130                     onCancel : function() {
131                         setTimeout(function(){
132                             grid.views.views[0].getCellNode(rowIndex, 0).focus();},200);
133                     }
134                 });
135                 dialog.editPane.fieldOrder = this.fieldOrder;
136                 dialog.editPane.mode = 'update';
137                 dialog.startup();
138                 dialog.show();
139             },
140
141             showCreateDialog : function() {
142                 var grid = this;
143                 var dialog = new openils.widget.EditDialog({
144                     fmClass : this.fmClass,
145                     onPostSubmit : function(r) {
146                         var fmObject = openils.Util.readResponse(r);
147                         if(fmObject) 
148                             grid.store.newItem(fmObject.toStoreItem());
149                         dialog.destroy();
150                         if(grid.onPostCreate)
151                             grid.onPostCreate(fmObject);
152                         setTimeout(function(){
153                             try {
154                                 grid.selection.select(grid.rowCount-1);
155                                 grid.views.views[0].getCellNode(grid.rowCount-1, 1).focus();
156                             } catch (E) {}
157                         },200);
158                     },
159                 });
160                 dialog.editPane.fieldOrder = this.fieldOrder;
161                 dialog.editPane.mode = 'create';
162                 dialog.startup();
163                 dialog.show();
164             },
165
166             loadAll : function(opts) {
167                 dojo.require('openils.PermaCrud');
168                 if(!opts) opts = {};
169                 var self = this;
170                 opts = dojo.mixin(opts, {
171                     async : true,
172                     streaming : true,
173                     onresponse : function(r) {
174                         var item = openils.Util.readResponse(r);
175                         self.store.newItem(item.toStoreItem());
176                     }
177                 });
178                 new openils.PermaCrud().retrieveAll(this.fmClass, opts);
179             }
180         } 
181     );
182     openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
183
184     openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
185         if(!item) return '';
186         var val = this.grid.store.getValue(item, this.field);
187         var autoWidget = new openils.widget.AutoFieldWidget({
188             fmClass: this.grid.fmClass,
189             fmField: this.field,
190             widgetValue : val,
191         });
192         return autoWidget.getDisplayString();
193     }
194 }
195