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