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