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,
19 /* by default, don't show auto-generated (sequence) fields */
20 showSequenceFields : false,
22 startup : function() {
23 this.selectionMode = 'single';
24 this.inherited(arguments);
26 this.setStructure(this._compileStructure());
27 this.setStore(this.buildAutoStore());
28 this.overrideEditWidgets = {};
30 this._applyEditOnEnter();
31 else if(this.singleEditStyle)
32 this._applySingleEditStyle();
35 _compileStructure : function() {
36 var existing = (this.structure && this.structure[0].cells[0]) ?
37 this.structure[0].cells[0] : [];
41 function pushEntry(entry) {
43 entry.get = openils.widget.AutoGrid.defaultGetter
44 if(!entry.width && self.defaultCellWidth)
45 entry.width = self.defaultCellWidth;
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;
60 for(var f in this.sortedFieldList) {
61 var field = this.sortedFieldList[f];
62 if(!field || field.virtual) continue;
64 // field was already added above
65 if(fields.filter(function(i){return (i.field == field.name)})[0])
69 if(!this.showSequenceFields && field.name == this.fmIDL.pkey && this.fmIDL.pkey_sequence)
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};
77 return [{cells: [fields]}];
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);
89 /* capture keydown and launch edit dialog on enter */
90 _applyEditOnEnter : function() {
91 this._applySingleEditStyle();
93 dojo.connect(this, 'onRowDblClick',
95 this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
99 dojo.connect(this, 'onKeyDown',
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);
110 _makeEditPane : function(storeItem, rowIndex, onPostSubmit, onCancel) {
112 var fmObject = new fieldmapper[this.fmClass]().fromStoreItem(storeItem);
113 var idents = grid.store.getIdentityAttributes();
115 var pane = new openils.widget.EditPane({
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]());
125 if(self.onPostUpdate)
126 self.onPostUpdate(storeItem, rowIndex);
130 grid.views.views[0].getCellNode(rowIndex, 0).focus();
134 if(onPostSubmit) onPostSubmit();
136 onCancel : function() {
137 setTimeout(function(){
138 grid.views.views[0].getCellNode(rowIndex, 0).focus();},200);
139 if(onCancel) onCancel();
143 pane.fieldOrder = this.fieldOrder;
144 pane.mode = 'update';
148 _makeCreatePane : function(onPostSubmit, onCancel) {
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);
156 grid.store.newItem(fmObject.toStoreItem());
157 if(grid.onPostCreate)
158 grid.onPostCreate(fmObject);
159 setTimeout(function(){
161 grid.selection.select(grid.rowCount-1);
162 grid.views.views[0].getCellNode(grid.rowCount-1, 1).focus();
168 onCancel : function() {
169 if(onCancel) onCancel();
172 pane.fieldOrder = this.fieldOrder;
173 pane.mode = 'create';
178 _drawEditDialog : function(storeItem, rowIndex) {
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();
187 showCreateDialog : function() {
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();
196 showCreatePane : function() {
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');
205 hideDialog : function() {
206 this.editDialog.hide();
207 this.editDialog.destroy();
208 delete this.editDialog;
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');
218 resetStore : function() {
219 this.setStore(this.buildAutoStore());
222 loadAll : function(opts, search) {
223 dojo.require('openils.PermaCrud');
226 opts = dojo.mixin(opts, {
229 onresponse : function(r) {
230 var item = openils.Util.readResponse(r);
231 self.store.newItem(item.toStoreItem());
235 new openils.PermaCrud().search(this.fmClass, search, opts);
237 new openils.PermaCrud().retrieveAll(this.fmClass, opts);
241 openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
243 openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
245 var val = this.grid.store.getValue(item, this.field);
246 var autoWidget = new openils.widget.AutoFieldWidget({
247 fmClass: this.grid.fmClass,
251 return autoWidget.getDisplayString();