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 startup : function() {
20 this.selectionMode = 'single';
21 this.inherited(arguments);
23 var existing = (this.structure && this.structure[0].cells[0]) ?
24 this.structure[0].cells[0] : [];
27 for(var f in this.sortedFieldList) {
28 var field = this.sortedFieldList[f];
29 if(!field || field.virtual) continue;
30 var entry = existing.filter(
31 function(i){return (i.field == field.name)})[0];
32 if(entry) entry.name = field.label;
33 else entry = {field:field.name, name:field.label};
36 entry.get = openils.widget.AutoGrid.defaultGetter
37 if(!entry.width && this.defaultCellWidth)
38 entry.width = this.defaultCellWidth;
41 this.setStructure([{cells: [fields]}]);
42 this.setStore(this.buildAutoStore());
44 this._applyEditOnEnter();
47 /* capture keydown and launch edit dialog on enter */
48 _applyEditOnEnter : function() {
50 this.onMouseOverRow = function(e) {};
51 this.onMouseOutRow = function(e) {};
52 this.onCellFocus = function(cell, rowIndex) {
53 this.selection.deselectAll();
54 this.selection.select(this.focus.rowIndex);
57 dojo.connect(this, 'onRowDblClick',
59 this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
63 dojo.connect(this, 'onKeyDown',
65 if(e.keyCode == dojo.keys.ENTER) {
66 this.selection.deselectAll();
67 this.selection.select(this.focus.rowIndex);
68 this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
74 _drawEditDialog : function(storeItem, rowIndex) {
76 var fmObject = new fieldmapper[this.fmClass]().fromStoreItem(storeItem);
77 var idents = grid.store.getIdentityAttributes();
78 var dialog = new openils.widget.EditDialog({
80 onPostSubmit : function() {
81 for(var i in fmObject._fields) {
82 var field = fmObject._fields[i];
83 if(idents.filter(function(j){return (j == field)})[0])
84 continue; // don't try to edit an identifier field
85 grid.store.setValue(storeItem, field, fmObject[field]());
90 self.onPostUpdate(storeItem, rowIndex);
95 grid.views.views[0].getCellNode(rowIndex, 0).focus();
100 onCancel : function() {
101 setTimeout(function(){
102 grid.views.views[0].getCellNode(rowIndex, 0).focus();},200);
105 dialog.editPane.fieldOrder = this.fieldOrder;
106 dialog.editPane.mode = 'update';
111 showCreateDialog : function() {
113 var dialog = new openils.widget.EditDialog({
114 fmClass : this.fmClass,
115 onPostSubmit : function(r) {
116 var fmObject = openils.Util.readResponse(r);
118 grid.store.newItem(fmObject.toStoreItem());
120 if(grid.onPostCreate)
121 grid.onPostCreate(fmObject);
122 setTimeout(function(){
124 grid.selection.select(grid.rowCount-1);
125 grid.views.views[0].getCellNode(grid.rowCount-1, 1).focus();
130 dialog.editPane.fieldOrder = this.fieldOrder;
131 dialog.editPane.mode = 'create';
136 loadAll : function(opts) {
137 dojo.require('openils.PermaCrud');
140 opts = dojo.mixin(opts, {
143 onresponse : function(r) {
144 var item = openils.Util.readResponse(r);
145 self.store.newItem(item.toStoreItem());
148 new openils.PermaCrud().retrieveAll(this.fmClass, opts);
152 openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
154 openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
156 var val = this.grid.store.getValue(item, this.field);
157 var autoWidget = new openils.widget.AutoFieldWidget({
158 fmClass: this.grid.fmClass,
162 return autoWidget.getDisplayString();