]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/AutoGrid.js
edit/create pane can now be loaded as a inline grid deck-swap in addition to dialog
[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.EditPane');
7     dojo.require('openils.widget.EditDialog');
8     dojo.require('openils.Util');
9
10     dojo.declare(
11         'openils.widget.AutoGrid',
12         [dojox.grid.DataGrid, openils.widget.AutoWidget],
13         {
14
15             /* if true, pop up an edit dialog when user hits Enter on a give row */
16             editOnEnter : false, 
17             defaultCellWidth : null,
18
19             /* by default, don't show auto-generated (sequence) fields */
20             showSequenceFields : false, 
21
22             startup : function() {
23                 this.selectionMode = 'single';
24                 this.inherited(arguments);
25                 this.initAutoEnv();
26                 this.setStructure(this._compileStructure());
27                 this.setStore(this.buildAutoStore());
28                 this.overrideEditWidgets = {};
29                 if(this.editOnEnter) 
30                     this._applyEditOnEnter();
31                 else if(this.singleEditStyle) 
32                     this._applySingleEditStyle();
33             },
34
35             _compileStructure : function() {
36                 var existing = (this.structure && this.structure[0].cells[0]) ? 
37                     this.structure[0].cells[0] : [];
38                 var fields = [];
39
40                 var self = this;
41                 function pushEntry(entry) {
42                     if(!entry.get) 
43                         entry.get = openils.widget.AutoGrid.defaultGetter
44                     if(!entry.width && self.defaultCellWidth)
45                         entry.width = self.defaultCellWidth;
46                     fields.push(entry);
47                 }
48
49                 if(!this.fieldOrder) {
50                     /* no order defined, start with any explicit grid fields */
51                     for(var e in existing) {
52                         var entry = existing[e];
53                         var field = this.fmIDL.fields.filter(
54                             function(i){return (i.name == entry.field)})[0];
55                         if(field) entry.name = entry.name || field.label;
56                         pushEntry(entry);
57                     }
58                 }
59
60                 for(var f in this.sortedFieldList) {
61                     var field = this.sortedFieldList[f];
62                     if(!field || field.virtual) continue;
63                     
64                     // field was already added above
65                     if(fields.filter(function(i){return (i.field == field.name)})[0]) 
66                         continue;
67
68
69                     if(!this.showSequenceFields && field.name == this.fmIDL.pkey && this.fmIDL.pkey_sequence)
70                         continue; 
71                     var entry = existing.filter(function(i){return (i.field == field.name)})[0];
72                     if(entry) entry.name = field.label;
73                     else entry = {field:field.name, name:field.label};
74                     pushEntry(entry);
75                 }
76
77                 return [{cells: [fields]}];
78             },
79
80             _applySingleEditStyle : function() {
81                 this.onMouseOverRow = function(e) {};
82                 this.onMouseOutRow = function(e) {};
83                 this.onCellFocus = function(cell, rowIndex) { 
84                     this.selection.deselectAll();
85                     this.selection.select(this.focus.rowIndex);
86                 };
87             },
88
89             /* capture keydown and launch edit dialog on enter */
90             _applyEditOnEnter : function() {
91                 this._applySingleEditStyle();
92
93                 dojo.connect(this, 'onRowDblClick',
94                     function(e) {
95                         this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
96                     }
97                 );
98
99                 dojo.connect(this, 'onKeyDown',
100                     function(e) {
101                         if(e.keyCode == dojo.keys.ENTER) {
102                             this.selection.deselectAll();
103                             this.selection.select(this.focus.rowIndex);
104                             this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
105                         }
106                     }
107                 );
108             },
109
110             _makeEditPane : function(storeItem, rowIndex, onPostSubmit, onCancel) {
111                 var grid = this;
112                 var fmObject = new fieldmapper[this.fmClass]().fromStoreItem(storeItem);
113                 var idents = grid.store.getIdentityAttributes();
114
115                 var pane = new openils.widget.EditPane({
116                     fmObject:fmObject,
117                     overrideWidgets : this.overrideEditWidgets,
118                     onPostSubmit : function() {
119                         for(var i in fmObject._fields) {
120                             var field = fmObject._fields[i];
121                             if(idents.filter(function(j){return (j == field)})[0])
122                                 continue; // don't try to edit an identifier field
123                             grid.store.setValue(storeItem, field, fmObject[field]());
124                         }
125                         if(self.onPostUpdate)
126                             self.onPostUpdate(storeItem, rowIndex);
127                         setTimeout(
128                             function(){
129                                 try { 
130                                     grid.views.views[0].getCellNode(rowIndex, 0).focus(); 
131                                 } catch (E) {}
132                             },200
133                         );
134                         if(onPostSubmit) onPostSubmit();
135                     },
136                     onCancel : function() {
137                         setTimeout(function(){
138                             grid.views.views[0].getCellNode(rowIndex, 0).focus();},200);
139                         if(onCancel) onCancel();
140                     }
141                 });
142
143                 pane.fieldOrder = this.fieldOrder;
144                 pane.mode = 'update';
145                 return pane;
146             },
147
148             _makeCreatePane : function(onPostSubmit, onCancel) {
149                 var grid = this;
150                 var pane = new openils.widget.EditPane({
151                     fmClass : this.fmClass,
152                     overrideWidgets : this.overrideEditWidgets,
153                     onPostSubmit : function(r) {
154                         var fmObject = openils.Util.readResponse(r);
155                         if(fmObject) 
156                             grid.store.newItem(fmObject.toStoreItem());
157                         if(grid.onPostCreate)
158                             grid.onPostCreate(fmObject);
159                         setTimeout(function(){
160                             try {
161                                 grid.selection.select(grid.rowCount-1);
162                                 grid.views.views[0].getCellNode(grid.rowCount-1, 1).focus();
163                             } catch (E) {}
164                         },200);
165                         if(onPostSubmit)
166                             onPostSubmit();
167                     },
168                     onCancel : function() {
169                         if(onCancel) onCancel();
170                     }
171                 });
172                 pane.fieldOrder = this.fieldOrder;
173                 pane.mode = 'create';
174                 return pane;
175             },
176
177
178             _drawEditDialog : function(storeItem, rowIndex) {
179                 var self = this;
180                 var done = function() { self.hideDialog(); };
181                 var pane = this._makeEditPane(storeItem, rowIndex, done, done);
182                 this.editDialog = new openils.widget.EditDialog({editPane:pane});
183                 this.editDialog.startup();
184                 this.editDialog.show();
185             },
186
187             showCreateDialog : function() {
188                 var self = this;
189                 var done = function() { self.hideDialog(); };
190                 var pane = this._makeCreatePane(done, done);
191                 this.editDialog = new openils.widget.EditDialog({editPane:pane});
192                 this.editDialog.startup();
193                 this.editDialog.show();
194             },
195
196             showCreatePane : function() {
197                 var self = this;
198                 var done = function() { self.hidePane(); };
199                 this.editPane = this._makeCreatePane(done, done);
200                 this.editPane.startup();
201                 this.domNode.parentNode.insertBefore(this.editPane.domNode, this.domNode);
202                 dojo.style(this.domNode, 'display', 'none');
203             },
204
205             hideDialog : function() {
206                 this.editDialog.hide(); 
207                 this.editDialog.destroy(); 
208                 delete this.editDialog;
209             },
210
211             hidePane : function() {
212                 this.domNode.parentNode.removeChild(this.editPane.domNode);
213                 this.editPane.destroy();
214                 delete this.editPane;
215                 dojo.style(this.domNode, 'display', 'block');
216             },
217             
218             resetStore : function() {
219                 this.setStore(this.buildAutoStore());
220             },
221
222             loadAll : function(opts, search) {
223                 dojo.require('openils.PermaCrud');
224                 if(!opts) opts = {};
225                 var self = this;
226                 opts = dojo.mixin(opts, {
227                     async : true,
228                     streaming : true,
229                     onresponse : function(r) {
230                         var item = openils.Util.readResponse(r);
231                         self.store.newItem(item.toStoreItem());
232                     }
233                 });
234                 if(search)
235                     new openils.PermaCrud().search(this.fmClass, search, opts);
236                 else
237                     new openils.PermaCrud().retrieveAll(this.fmClass, opts);
238             }
239         } 
240     );
241     openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
242
243     openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
244         if(!item) return '';
245         var val = this.grid.store.getValue(item, this.field);
246         var autoWidget = new openils.widget.AutoFieldWidget({
247             fmClass: this.grid.fmClass,
248             fmField: this.field,
249             widgetValue : val,
250         });
251         return autoWidget.getDisplayString();
252     }
253 }
254