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');
11 'openils.widget.AutoGrid',
12 [dojox.grid.DataGrid, openils.widget.AutoWidget],
15 /* if true, pop up an edit dialog when user hits Enter on a give row */
17 defaultCellWidth : null,
20 /* by default, don't show auto-generated (sequence) fields */
21 showSequenceFields : false,
23 startup : function() {
24 this.selectionMode = 'single';
25 this.inherited(arguments);
27 this.setStructure(this._compileStructure());
28 this.setStore(this.buildAutoStore());
29 this.overrideEditWidgets = {};
31 this._applyEditOnEnter();
32 else if(this.singleEditStyle)
33 this._applySingleEditStyle();
36 _compileStructure : function() {
37 var existing = (this.structure && this.structure[0].cells[0]) ?
38 this.structure[0].cells[0] : [];
42 function pushEntry(entry) {
44 entry.get = openils.widget.AutoGrid.defaultGetter
45 if(!entry.width && self.defaultCellWidth)
46 entry.width = self.defaultCellWidth;
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;
61 for(var f in this.sortedFieldList) {
62 var field = this.sortedFieldList[f];
63 if(!field || field.virtual) continue;
65 // field was already added above
66 if(fields.filter(function(i){return (i.field == field.name)})[0])
70 if(!this.showSequenceFields && field.name == this.fmIDL.pkey && this.fmIDL.pkey_sequence)
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};
78 return [{cells: [fields]}];
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);
90 /* capture keydown and launch edit dialog on enter */
91 _applyEditOnEnter : function() {
92 this._applySingleEditStyle();
94 dojo.connect(this, 'onRowDblClick',
96 if(this.editStyle == 'pane')
97 this._drawEditPane(this.selection.getFirstSelected(), this.focus.rowIndex);
99 this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
103 dojo.connect(this, 'onKeyDown',
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);
111 this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
117 _makeEditPane : function(storeItem, rowIndex, onPostSubmit, onCancel) {
119 var fmObject = new fieldmapper[this.fmClass]().fromStoreItem(storeItem);
120 var idents = grid.store.getIdentityAttributes();
122 var pane = new openils.widget.EditPane({
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]());
132 if(self.onPostUpdate)
133 self.onPostUpdate(storeItem, rowIndex);
137 grid.views.views[0].getCellNode(rowIndex, 0).focus();
141 if(onPostSubmit) onPostSubmit();
143 onCancel : function() {
144 setTimeout(function(){
145 grid.views.views[0].getCellNode(rowIndex, 0).focus();},200);
146 if(onCancel) onCancel();
150 pane.fieldOrder = this.fieldOrder;
151 pane.mode = 'update';
155 _makeCreatePane : function(onPostSubmit, onCancel) {
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);
163 grid.store.newItem(fmObject.toStoreItem());
164 if(grid.onPostCreate)
165 grid.onPostCreate(fmObject);
166 setTimeout(function(){
168 grid.selection.select(grid.rowCount-1);
169 grid.views.views[0].getCellNode(grid.rowCount-1, 1).focus();
175 onCancel : function() {
176 if(onCancel) onCancel();
179 pane.fieldOrder = this.fieldOrder;
180 pane.mode = 'create';
185 _drawEditDialog : function(storeItem, rowIndex) {
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();
194 showCreateDialog : function() {
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();
203 _drawEditPane : function(storeItem, rowIndex) {
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');
212 showCreatePane : function() {
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');
221 hideDialog : function() {
222 this.editDialog.hide();
223 this.editDialog.destroy();
224 delete this.editDialog;
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');
234 resetStore : function() {
235 this.setStore(this.buildAutoStore());
238 loadAll : function(opts, search) {
239 dojo.require('openils.PermaCrud');
242 opts = dojo.mixin(opts, {
245 onresponse : function(r) {
246 var item = openils.Util.readResponse(r);
247 self.store.newItem(item.toStoreItem());
251 new openils.PermaCrud().search(this.fmClass, search, opts);
253 new openils.PermaCrud().retrieveAll(this.fmClass, opts);
257 openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
259 openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
261 var val = this.grid.store.getValue(item, this.field);
262 var autoWidget = new openils.widget.AutoFieldWidget({
263 fmClass: this.grid.fmClass,
267 return autoWidget.getDisplayString();