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.EditPane');
7 dojo.require('openils.widget.EditDialog');
8 dojo.require('openils.Util');
11 'openils.widget.AutoGrid',
12 [dojox.grid.DataGrid, openils.widget.AutoWidget],
15 /* if true, pop up an edit dialog when user hits Enter on a give row */
17 defaultCellWidth : null,
19 suppressFields : null,
21 /* by default, don't show auto-generated (sequence) fields */
22 showSequenceFields : false,
24 startup : function() {
25 this.selectionMode = 'single';
26 this.inherited(arguments);
28 this.setStructure(this._compileStructure());
29 this.setStore(this.buildAutoStore());
30 this.overrideEditWidgets = {};
31 this.overrideEditWidgetClass = {};
33 this._applyEditOnEnter();
34 else if(this.singleEditStyle)
35 this._applySingleEditStyle();
38 _compileStructure : function() {
39 var existing = (this.structure && this.structure[0].cells[0]) ?
40 this.structure[0].cells[0] : [];
44 function pushEntry(entry) {
45 if(self.suppressFields) {
46 if(dojo.indexOf(self.suppressFields, entry.field) != -1)
50 entry.get = openils.widget.AutoGrid.defaultGetter
51 if(!entry.width && self.defaultCellWidth)
52 entry.width = self.defaultCellWidth;
56 if(!this.fieldOrder) {
57 /* no order defined, start with any explicit grid fields */
58 for(var e in existing) {
59 var entry = existing[e];
60 var field = this.fmIDL.fields.filter(
61 function(i){return (i.name == entry.field)})[0];
62 if(field) entry.name = entry.name || field.label;
67 for(var f in this.sortedFieldList) {
68 var field = this.sortedFieldList[f];
69 if(!field || field.virtual) continue;
71 // field was already added above
72 if(fields.filter(function(i){return (i.field == field.name)})[0])
75 if(!this.showSequenceFields && field.name == this.fmIDL.pkey && this.fmIDL.pkey_sequence)
77 var entry = existing.filter(function(i){return (i.field == field.name)})[0];
78 if(entry) entry.name = field.label;
79 else entry = {field:field.name, name:field.label};
84 /* append any explicit non-IDL grid fields to the end */
85 for(var e in existing) {
86 var entry = existing[e];
87 var field = fields.filter(
88 function(i){return (i.field == entry.field)})[0];
89 if(field) continue; // don't duplicate
95 return [{cells: [fields]}];
98 _applySingleEditStyle : function() {
99 this.onMouseOverRow = function(e) {};
100 this.onMouseOutRow = function(e) {};
101 this.onCellFocus = function(cell, rowIndex) {
102 this.selection.deselectAll();
103 this.selection.select(this.focus.rowIndex);
107 /* capture keydown and launch edit dialog on enter */
108 _applyEditOnEnter : function() {
109 this._applySingleEditStyle();
111 dojo.connect(this, 'onRowDblClick',
113 if(this.editStyle == 'pane')
114 this._drawEditPane(this.selection.getFirstSelected(), this.focus.rowIndex);
116 this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
120 dojo.connect(this, 'onKeyDown',
122 if(e.keyCode == dojo.keys.ENTER) {
123 this.selection.deselectAll();
124 this.selection.select(this.focus.rowIndex);
125 if(this.editStyle == 'pane')
126 this._drawEditPane(this.selection.getFirstSelected(), this.focus.rowIndex);
128 this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
134 _makeEditPane : function(storeItem, rowIndex, onPostSubmit, onCancel) {
136 var fmObject = new fieldmapper[this.fmClass]().fromStoreItem(storeItem);
137 var idents = grid.store.getIdentityAttributes();
139 var pane = new openils.widget.EditPane({
141 overrideWidgets : this.overrideEditWidgets,
142 overrideWidgetClass : this.overrideEditWidgetClass,
143 onPostSubmit : function() {
144 for(var i in fmObject._fields) {
145 var field = fmObject._fields[i];
146 if(idents.filter(function(j){return (j == field)})[0])
147 continue; // don't try to edit an identifier field
148 grid.store.setValue(storeItem, field, fmObject[field]());
150 if(self.onPostUpdate)
151 self.onPostUpdate(storeItem, rowIndex);
155 grid.views.views[0].getCellNode(rowIndex, 0).focus();
159 if(onPostSubmit) onPostSubmit();
161 onCancel : function() {
162 setTimeout(function(){
163 grid.views.views[0].getCellNode(rowIndex, 0).focus();},200);
164 if(onCancel) onCancel();
168 pane.fieldOrder = this.fieldOrder;
169 pane.mode = 'update';
173 _makeCreatePane : function(onPostSubmit, onCancel) {
175 var pane = new openils.widget.EditPane({
176 fmClass : this.fmClass,
177 overrideWidgets : this.overrideEditWidgets,
178 overrideWidgetClass : this.overrideEditWidgetClass,
179 onPostSubmit : function(r) {
180 var fmObject = openils.Util.readResponse(r);
182 grid.store.newItem(fmObject.toStoreItem());
183 if(grid.onPostCreate)
184 grid.onPostCreate(fmObject);
185 setTimeout(function(){
187 grid.selection.select(grid.rowCount-1);
188 grid.views.views[0].getCellNode(grid.rowCount-1, 1).focus();
194 onCancel : function() {
195 if(onCancel) onCancel();
198 pane.fieldOrder = this.fieldOrder;
199 pane.mode = 'create';
203 // .startup() is called within
204 _makeClonePane : function(storeItem, rowIndex, onPostSubmit, onCancel) {
205 var clonePane = this._makeCreatePane(onPostSubmit, onCancel);
206 var origPane = this._makeEditPane(this.selection.getFirstSelected(), this.focus.rowIndex);
209 dojo.forEach(origPane.fieldList,
211 if(field.widget.widget.attr('disabled')) return;
212 var w = clonePane.fieldList.filter(
213 function(i) { return (i.name == field.name) })[0];
214 w.widget.baseWidgetValue(field.widget.widgetValue); // sync widgets
215 w.widget.onload = function(){w.widget.baseWidgetValue(field.widget.widgetValue)}; // async widgets
223 _drawEditDialog : function(storeItem, rowIndex) {
225 var done = function() { self.hideDialog(); };
226 var pane = this._makeEditPane(storeItem, rowIndex, done, done);
227 this.editDialog = new openils.widget.EditDialog({editPane:pane});
228 this.editDialog.startup();
229 this.editDialog.show();
232 showCreateDialog : function() {
234 var done = function() { self.hideDialog(); };
235 var pane = this._makeCreatePane(done, done);
236 this.editDialog = new openils.widget.EditDialog({editPane:pane});
237 this.editDialog.startup();
238 this.editDialog.show();
241 _drawEditPane : function(storeItem, rowIndex) {
243 var done = function() { self.hidePane(); };
244 dojo.style(this.domNode, 'display', 'none');
245 this.editPane = this._makeEditPane(storeItem, rowIndex, done, done);
246 this.editPane.startup();
247 this.domNode.parentNode.insertBefore(this.editPane.domNode, this.domNode);
250 showClonePane : function(storeItem, rowIndex) {
252 var done = function() { self.hidePane(); };
253 dojo.style(this.domNode, 'display', 'none');
254 this.editPane = this._makeClonePane(storeItem, rowIndex, done, done);
255 this.domNode.parentNode.insertBefore(this.editPane.domNode, this.domNode);
258 showCreatePane : function() {
260 var done = function() { self.hidePane(); };
261 dojo.style(this.domNode, 'display', 'none');
262 this.editPane = this._makeCreatePane(done, done);
263 this.editPane.startup();
264 this.domNode.parentNode.insertBefore(this.editPane.domNode, this.domNode);
267 hideDialog : function() {
268 this.editDialog.hide();
269 this.editDialog.destroy();
270 delete this.editDialog;
273 hidePane : function() {
274 this.domNode.parentNode.removeChild(this.editPane.domNode);
275 this.editPane.destroy();
276 delete this.editPane;
277 dojo.style(this.domNode, 'display', 'block');
281 resetStore : function() {
282 this.setStore(this.buildAutoStore());
285 loadAll : function(opts, search) {
286 dojo.require('openils.PermaCrud');
289 opts = dojo.mixin(opts, {
292 onresponse : function(r) {
293 var item = openils.Util.readResponse(r);
294 self.store.newItem(item.toStoreItem());
298 new openils.PermaCrud().search(this.fmClass, search, opts);
300 new openils.PermaCrud().retrieveAll(this.fmClass, opts);
304 openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
306 openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
308 var val = this.grid.store.getValue(item, this.field);
309 var autoWidget = new openils.widget.AutoFieldWidget({
310 fmClass: this.grid.fmClass,
314 return autoWidget.getDisplayString();