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');
10 'openils.widget.AutoGrid',
11 [dojox.grid.DataGrid, openils.widget.AutoWidget],
14 /* if true, pop up an edit dialog when user hits Enter on a give row */
16 defaultCellWidth : null,
18 /* by default, don't show auto-generated (sequence) fields */
19 showSequenceFields : false,
21 startup : function() {
22 this.selectionMode = 'single';
23 this.inherited(arguments);
25 this.setStructure(this._compileStructure());
26 this.setStore(this.buildAutoStore());
27 this.overrideEditWidgets = {};
29 this._applyEditOnEnter();
30 else if(this.singleEditStyle)
31 this._applySingleEditStyle();
34 _compileStructure : function() {
35 var existing = (this.structure && this.structure[0].cells[0]) ?
36 this.structure[0].cells[0] : [];
40 function pushEntry(entry) {
42 entry.get = openils.widget.AutoGrid.defaultGetter
43 if(!entry.width && self.defaultCellWidth)
44 entry.width = self.defaultCellWidth;
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;
59 for(var f in this.sortedFieldList) {
60 var field = this.sortedFieldList[f];
61 if(!field || field.virtual) continue;
63 // field was already added above
64 if(fields.filter(function(i){return (i.field == field.name)})[0])
68 if(!this.showSequenceFields && field.name == this.fmIDL.pkey && this.fmIDL.pkey_sequence)
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};
76 return [{cells: [fields]}];
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);
88 /* capture keydown and launch edit dialog on enter */
89 _applyEditOnEnter : function() {
90 this._applySingleEditStyle();
92 dojo.connect(this, 'onRowDblClick',
94 this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
98 dojo.connect(this, 'onKeyDown',
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);
109 _drawEditDialog : function(storeItem, rowIndex) {
111 var fmObject = new fieldmapper[this.fmClass]().fromStoreItem(storeItem);
112 var idents = grid.store.getIdentityAttributes();
113 var dialog = new openils.widget.EditDialog({
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]());
125 if(self.onPostUpdate)
126 self.onPostUpdate(storeItem, rowIndex);
131 grid.views.views[0].getCellNode(rowIndex, 0).focus();
136 onCancel : function() {
137 setTimeout(function(){
138 grid.views.views[0].getCellNode(rowIndex, 0).focus();},200);
141 dialog.editPane.fieldOrder = this.fieldOrder;
142 dialog.editPane.mode = 'update';
147 showCreateDialog : function() {
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);
155 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();
167 dialog.editPane.fieldOrder = this.fieldOrder;
168 dialog.editPane.mode = 'create';
173 resetStore : function() {
174 this.setStore(this.buildAutoStore());
177 loadAll : function(opts, search) {
178 dojo.require('openils.PermaCrud');
181 opts = dojo.mixin(opts, {
184 onresponse : function(r) {
185 var item = openils.Util.readResponse(r);
186 self.store.newItem(item.toStoreItem());
190 new openils.PermaCrud().search(this.fmClass, search, opts);
192 new openils.PermaCrud().retrieveAll(this.fmClass, opts);
196 openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
198 openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
200 var val = this.grid.store.getValue(item, this.field);
201 var autoWidget = new openils.widget.AutoFieldWidget({
202 fmClass: this.grid.fmClass,
206 return autoWidget.getDisplayString();