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