]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/AutoGrid.js
allow for definition of default cell width. (can still be overridden by defining...
[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             defaultCellWidth : null,
17
18             startup : function() {
19
20                 this.selectionMode = 'single';
21                 this.inherited(arguments);
22                 this.initAutoEnv();
23                 var existing = (this.structure && this.structure[0].cells[0]) ? 
24                     this.structure[0].cells[0] : [];
25                 var fields = [];
26
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};
34                     fields.push(entry);
35                     if(!entry.get) 
36                         entry.get = openils.widget.AutoGrid.defaultGetter
37                     if(!entry.width && this.defaultCellWidth)
38                         entry.width = this.defaultCellWidth;
39                 }
40
41                 this.setStructure([{cells: [fields]}]);
42                 this.setStore(this.buildAutoStore());
43                 if(this.editOnEnter) 
44                     this._applyEditOnEnter();
45             },
46
47             /* capture keydown and launch edit dialog on enter */
48             _applyEditOnEnter : function() {
49
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);
55                 };
56
57                 dojo.connect(this, 'onKeyDown',
58                     function(e) {
59                         if(e.keyCode == dojo.keys.ENTER) {
60                             this.selection.deselectAll();
61                             this.selection.select(this.focus.rowIndex);
62                             this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
63                         }
64                     }
65                 );
66             },
67
68             _drawEditDialog : function(storeItem, rowIndex) {
69                 var grid = this;
70                 var fmObject = new fieldmapper[this.fmClass]().fromStoreItem(storeItem);
71                 var idents = grid.store.getIdentityAttributes();
72                 var dialog = new openils.widget.EditDialog({
73                     fmObject:fmObject,
74                     onPostSubmit : function() {
75                         for(var i in fmObject._fields) {
76                             var field = fmObject._fields[i];
77                             if(idents.filter(function(j){return (j == field)})[0])
78                                 continue; // don't try to edit an identifier field
79                             grid.store.setValue(storeItem, field, fmObject[field]());
80                         }
81                         grid.update();
82                         dialog.destroy();
83                     }
84                 });
85                 dialog.editPane.fieldOrder = this.fieldOrder;
86                 dialog.editPane.mode = 'update';
87                 dialog.startup();
88                 dialog.show();
89             },
90
91             showCreateDialog : function() {
92                 var grid = this;
93                 var dialog = new openils.widget.EditDialog({
94                     fmClass : this.fmClass,
95                     onPostSubmit : function(r) {
96                         var fmObject = openils.Util.readResponse(r);
97                         if(fmObject) {
98                             grid.store.newItem(fmObject.toStoreItem());
99                             grid.update();
100                         }
101                         dialog.destroy();
102                     }
103                 });
104                 dialog.editPane.fieldOrder = this.fieldOrder;
105                 dialog.editPane.mode = 'create';
106                 dialog.startup();
107                 dialog.show();
108             }
109         } 
110     );
111     openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
112
113     openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
114         if(!item) return '';
115         var val = this.grid.store.getValue(item, this.field);
116         var autoWidget = new openils.widget.AutoFieldWidget({
117             fmClass: this.grid.fmClass,
118             fmField: this.field,
119             widgetValue : val,
120         });
121         return autoWidget.getDisplayString();
122     }
123 }
124