added option to launch creation dialog from auto grid. passing final pcrud response...
[working/Evergreen.git] / Open-ILS / web / js / dojo / openils / widget / AutoGrid.js
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');
8
9     dojo.declare(
10         'openils.widget.AutoGrid',
11         [dojox.grid.DataGrid, openils.widget.AutoWidget],
12         {
13
14             /* if true, pop up an edit dialog when user hits Enter on a give row */
15             editOnEnter : false, 
16
17             startup : function() {
18
19                 this.selectionMode = 'single';
20                 this.inherited(arguments);
21                 this.initAutoEnv();
22                 var existing = (this.structure && this.structure[0].cells[0]) ? 
23                     this.structure[0].cells[0] : [];
24                 var fields = [];
25
26                 for(var f in this.sortedFieldList) {
27                     var field = this.sortedFieldList[f];
28                     if(!field || field.virtual) continue;
29                     var entry = existing.filter(
30                         function(i){return (i.field == field.name)})[0];
31                     if(entry) entry.name = field.label;
32                     else entry = {field:field.name, name:field.label};
33                     fields.push(entry);
34                     if(!entry.get) 
35                         entry.get = openils.widget.AutoGrid.defaultGetter
36                 }
37
38                 this.setStructure([{cells: [fields]}]);
39                 this.setStore(this.buildAutoStore());
40                 if(this.editOnEnter) 
41                     this._applyEditOnEnter();
42             },
43
44             /* capture keydown and launch edit dialog on enter */
45             _applyEditOnEnter : function() {
46
47                 this.onMouseOverRow = function(e) {};
48                 this.onMouseOutRow = function(e) {};
49                 this.onCellFocus = function(cell, rowIndex) { 
50                     this.selection.deselectAll();
51                     this.selection.select(this.focus.rowIndex);
52                 };
53
54                 dojo.connect(this, 'onKeyDown',
55                     function(e) {
56                         if(e.keyCode == dojo.keys.ENTER) {
57                             this.selection.deselectAll();
58                             this.selection.select(this.focus.rowIndex);
59                             this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
60                         }
61                     }
62                 );
63             },
64
65             _drawEditDialog : function(storeItem, rowIndex) {
66                 var grid = this;
67                 var fmObject = new fieldmapper[this.fmClass]().fromStoreItem(storeItem);
68                 var idents = grid.store.getIdentityAttributes();
69                 var dialog = new openils.widget.EditDialog({
70                     fmObject:fmObject,
71                     onPostSubmit : function() {
72                         for(var i in fmObject._fields) {
73                             var field = fmObject._fields[i];
74                             if(idents.filter(function(j){return (j == field)})[0])
75                                 continue; // don't try to edit an identifier field
76                             grid.store.setValue(storeItem, field, fmObject[field]());
77                         }
78                         grid.update();
79                         dialog.destroy();
80                     }
81                 });
82                 dialog.editPane.fieldOrder = this.fieldOrder;
83                 dialog.editPane.mode = 'update';
84                 dialog.startup();
85                 dialog.show();
86             },
87
88             showCreateDialog : function() {
89                 var grid = this;
90                 var dialog = new openils.widget.EditDialog({
91                     fmClass : this.fmClass,
92                     onPostSubmit : function(r) {
93                         var fmObject = openils.Util.readResponse(r);
94                         if(fmObject) {
95                             grid.store.newItem(fmObject.toStoreItem());
96                             grid.update();
97                         }
98                         dialog.destroy();
99                     }
100                 });
101                 dialog.editPane.fieldOrder = this.fieldOrder;
102                 dialog.editPane.mode = 'create';
103                 dialog.startup();
104                 dialog.show();
105             }
106         } 
107     );
108     openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
109
110     openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
111         if(!item) return '';
112         var val = this.grid.store.getValue(item, this.field);
113         var autoWidget = new openils.widget.AutoFieldWidget({
114             fmClass: this.grid.fmClass,
115             fmField: this.field,
116             widgetValue : val,
117         });
118         return autoWidget.getDisplayString();
119     }
120 }
121