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]());
88 setTimeout(function(){
89 grid.views.views[1].getCellNode(rowIndex, 0).focus();},200);
91 onCancel : function() {
92 setTimeout(function(){
93 grid.views.views[1].getCellNode(rowIndex, 0).focus();},200);
96 dialog.editPane.fieldOrder = this.fieldOrder;
97 dialog.editPane.mode = 'update';
102 showCreateDialog : function() {
104 var dialog = new openils.widget.EditDialog({
105 fmClass : this.fmClass,
106 onPostSubmit : function(r) {
107 var fmObject = openils.Util.readResponse(r);
109 grid.store.newItem(fmObject.toStoreItem());
111 setTimeout(function(){
112 grid.selection.select(grid.rowCount-1);
113 grid.views.views[1].getCellNode(grid.rowCount-1, 1).focus();
117 dialog.editPane.fieldOrder = this.fieldOrder;
118 dialog.editPane.mode = 'create';
123 loadAll : function(opts) {
124 dojo.require('openils.PermaCrud');
127 opts = dojo.mixin(opts, {
130 onresponse : function(r) {
131 var item = openils.Util.readResponse(r);
132 self.store.newItem(item.toStoreItem());
135 new openils.PermaCrud().retrieveAll(this.fmClass, opts);
139 openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
141 openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
143 var val = this.grid.store.getValue(item, this.field);
144 var autoWidget = new openils.widget.AutoFieldWidget({
145 fmClass: this.grid.fmClass,
149 return autoWidget.getDisplayString();