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());
28 this._applyEditOnEnter();
31 _compileStructure : function() {
32 var existing = (this.structure && this.structure[0].cells[0]) ?
33 this.structure[0].cells[0] : [];
37 function pushEntry(entry) {
39 entry.get = openils.widget.AutoGrid.defaultGetter
40 if(!entry.width && self.defaultCellWidth)
41 entry.width = self.defaultCellWidth;
45 if(!this.fieldOrder) {
46 /* no order defined, start with any explicit grid fields */
47 for(var e in existing) {
48 var entry = existing[e];
49 var field = this.fmIDL.fields.filter(
50 function(i){return (i.name == entry.field)})[0];
51 if(field) entry.name = entry.name || field.label;
56 for(var f in this.sortedFieldList) {
57 var field = this.sortedFieldList[f];
58 if(!field || field.virtual) continue;
60 // field was already added above
61 if(fields.filter(function(i){return (i.field == field.name)})[0])
65 if(!this.showSequenceFields && field.name == this.fmIDL.pkey && this.fmIDL.pkey_sequence)
67 var entry = existing.filter(function(i){return (i.field == field.name)})[0];
68 if(entry) entry.name = field.label;
69 else entry = {field:field.name, name:field.label};
73 return [{cells: [fields]}];
77 /* capture keydown and launch edit dialog on enter */
78 _applyEditOnEnter : 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);
87 dojo.connect(this, 'onRowDblClick',
89 this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
93 dojo.connect(this, 'onKeyDown',
95 if(e.keyCode == dojo.keys.ENTER) {
96 this.selection.deselectAll();
97 this.selection.select(this.focus.rowIndex);
98 this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
104 _drawEditDialog : function(storeItem, rowIndex) {
106 var fmObject = new fieldmapper[this.fmClass]().fromStoreItem(storeItem);
107 var idents = grid.store.getIdentityAttributes();
108 var dialog = new openils.widget.EditDialog({
110 onPostSubmit : function() {
111 for(var i in fmObject._fields) {
112 var field = fmObject._fields[i];
113 if(idents.filter(function(j){return (j == field)})[0])
114 continue; // don't try to edit an identifier field
115 grid.store.setValue(storeItem, field, fmObject[field]());
119 if(self.onPostUpdate)
120 self.onPostUpdate(storeItem, rowIndex);
125 grid.views.views[0].getCellNode(rowIndex, 0).focus();
130 onCancel : function() {
131 setTimeout(function(){
132 grid.views.views[0].getCellNode(rowIndex, 0).focus();},200);
135 dialog.editPane.fieldOrder = this.fieldOrder;
136 dialog.editPane.mode = 'update';
141 showCreateDialog : function() {
143 var dialog = new openils.widget.EditDialog({
144 fmClass : this.fmClass,
145 onPostSubmit : function(r) {
146 var fmObject = openils.Util.readResponse(r);
148 grid.store.newItem(fmObject.toStoreItem());
150 if(grid.onPostCreate)
151 grid.onPostCreate(fmObject);
152 setTimeout(function(){
154 grid.selection.select(grid.rowCount-1);
155 grid.views.views[0].getCellNode(grid.rowCount-1, 1).focus();
160 dialog.editPane.fieldOrder = this.fieldOrder;
161 dialog.editPane.mode = 'create';
166 loadAll : function(opts) {
167 dojo.require('openils.PermaCrud');
170 opts = dojo.mixin(opts, {
173 onresponse : function(r) {
174 var item = openils.Util.readResponse(r);
175 self.store.newItem(item.toStoreItem());
178 new openils.PermaCrud().retrieveAll(this.fmClass, opts);
182 openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
184 openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
186 var val = this.grid.store.getValue(item, this.field);
187 var autoWidget = new openils.widget.AutoFieldWidget({
188 fmClass: this.grid.fmClass,
192 return autoWidget.getDisplayString();