]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/web/js/dojo/openils/widget/AutoGrid.js
gave autogrid the ability to turn on the column picker
[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.EditPane');
7     dojo.require('openils.widget.EditDialog');
8     dojo.require('openils.widget.GridColumnPicker');
9     dojo.require('openils.Util');
10
11     dojo.declare(
12         'openils.widget.AutoGrid',
13         [dojox.grid.DataGrid, openils.widget.AutoWidget],
14         {
15
16             /* if true, pop up an edit dialog when user hits Enter on a give row */
17             editOnEnter : false, 
18             defaultCellWidth : null,
19             editStyle : 'dialog',
20             suppressFields : null,
21             hideSelector : false,
22             selectorWidth : '1.5',
23             showColumnPicker : false,
24             columnPickerPrefix : null,
25
26             /* by default, don't show auto-generated (sequence) fields */
27             showSequenceFields : false, 
28
29             startup : function() {
30                 this.selectionMode = 'single';
31                 this.sequence = openils.widget.AutoGrid.sequence++;
32                 openils.widget.AutoGrid.gridCache[this.sequence] = this;
33                 this.inherited(arguments);
34                 this.initAutoEnv();
35                 this.attr('structure', this._compileStructure());
36                 this.setStore(this.buildAutoStore());
37
38                 if(this.showColumnPicker) {
39                     if(!this.columnPickerPrefix) {
40                         console.error("No columnPickerPrefix defined");
41                     } else {
42                         new openils.widget.GridColumnPicker(
43                             openils.User.authtoken, this.columnPickerPrefix, this).load();
44                     }
45                 }
46
47                 this.overrideEditWidgets = {};
48                 this.overrideEditWidgetClass = {};
49
50                 if(this.editOnEnter) 
51                     this._applyEditOnEnter();
52                 else if(this.singleEditStyle) 
53                     this._applySingleEditStyle();
54
55                 if(!this.hideSelector) {
56                     var header = this.layout.cells[0].view.getHeaderCellNode(0);
57                     var self = this;
58                     header.onclick = function() { self.toggleSelectAll(); }
59                 }
60             },
61
62             /* Don't allow sorting on the selector column */
63             canSort : function(rowIdx) {
64                 if(rowIdx == 1 && !this.hideSelector)
65                     return false;
66                 return true;
67             },
68
69             _compileStructure : function() {
70                 var existing = (this.structure && this.structure[0].cells[0]) ? 
71                     this.structure[0].cells[0] : [];
72                 var fields = [];
73
74                 var self = this;
75                 function pushEntry(entry) {
76                     if(self.suppressFields) {
77                         if(dojo.indexOf(self.suppressFields, entry.field) != -1)
78                             return;
79                     }
80                     if(!entry.get) 
81                         entry.get = openils.widget.AutoGrid.defaultGetter
82                     if(!entry.width && self.defaultCellWidth)
83                         entry.width = self.defaultCellWidth;
84                     fields.push(entry);
85                 }
86
87                 if(!this.hideSelector) {
88                     // insert the selector column
89                     pushEntry({
90                         field : '+selector',
91                         formatter : function(rowIdx) { return self._formatRowSelectInput(rowIdx); },
92                         get : function(rowIdx, item) { if(item) return rowIdx; },
93                         width : this.selectorWidth,
94                         name : '&#x2713',
95                         nonSelectable : true
96                     });
97                 }
98
99
100                 if(!this.fieldOrder) {
101                     /* no order defined, start with any explicit grid fields */
102                     for(var e in existing) {
103                         var entry = existing[e];
104                         var field = this.fmIDL.fields.filter(
105                             function(i){return (i.name == entry.field)})[0];
106                         if(field) entry.name = entry.name || field.label;
107                         pushEntry(entry);
108                     }
109                 }
110
111                 for(var f in this.sortedFieldList) {
112                     var field = this.sortedFieldList[f];
113                     if(!field || field.virtual) continue;
114                     
115                     // field was already added above
116                     if(fields.filter(function(i){return (i.field == field.name)})[0]) 
117                         continue;
118
119                     var entry = existing.filter(function(i){return (i.field == field.name)})[0];
120                     if(entry) {
121                         entry.name = field.label;
122                     } else {
123                         // unless specifically requested, hide sequence fields
124                         if(!this.showSequenceFields && field.name == this.fmIDL.pkey && this.fmIDL.pkey_sequence)
125                             continue; 
126
127                         entry = {field:field.name, name:field.label};
128                     }
129                     pushEntry(entry);
130                 }
131
132                 if(this.fieldOrder) {
133                     /* append any explicit non-IDL grid fields to the end */
134                     for(var e in existing) {
135                         var entry = existing[e];
136                         var field = fields.filter(
137                             function(i){return (i.field == entry.field)})[0];
138                         if(field) continue; // don't duplicate
139                         pushEntry(entry);
140                     }
141                 }
142
143                 return [{cells: [fields]}];
144             },
145
146             toggleSelectAll : function() {
147                 var selected = this.getSelectedRows();
148                 for(var i = 0; i < this.rowCount; i++) {
149                     if(selected[0])
150                         this.deSelectRow(i);
151                     else
152                         this.selectRow(i);
153                 }
154             },
155
156             getSelectedRows : function() {
157                 var rows = []; 
158                 dojo.forEach(
159                     dojo.query('[name=autogrid.selector]', this.domNode),
160                     function(input) {
161                         if(input.checked)
162                             rows.push(input.getAttribute('row'));
163                     }
164                 );
165                 return rows;
166             },
167
168             getFirstSelectedRow : function() {
169                 return this.getSelectedRows()[0];
170             },
171
172             getSelectedItems : function() {
173                 var items = [];
174                 var self = this;
175                 dojo.forEach(this.getSelectedRows(), function(idx) { items.push(self.getItem(idx)); });
176                 return items;
177             },
178
179             selectRow : function(rowIdx) {
180                 var inputs = dojo.query('[name=autogrid.selector]', this.domNode);
181                 for(var i = 0; i < inputs.length; i++) {
182                     if(inputs[i].getAttribute('row') == rowIdx) {
183                         inputs[i].checked = true;
184                         break;
185                     }
186                 }
187             },
188
189             deSelectRow : function(rowIdx) {
190                 var inputs = dojo.query('[name=autogrid.selector]', this.domNode);
191                 for(var i = 0; i < inputs.length; i++) {
192                     if(inputs[i].getAttribute('row') == rowIdx) {
193                         inputs[i].checked = false;
194                         break;
195                     }
196                 }
197             },
198
199             deleteSelected : function() {
200                 var items = this.getSelectedItems();
201                 var total = items.length;
202                 var self = this;
203                 dojo.require('openils.PermaCrud');
204                 var pcrud = new openils.PermaCrud();
205                 dojo.forEach(items,
206                     function(item) {
207                         var fmObject = new fieldmapper[self.fmClass]().fromStoreItem(item);
208                         pcrud['delete'](fmObject, {oncomplete : function(r) { self.store.deleteItem(item) }});
209                     }
210                 );
211             },
212
213             _formatRowSelectInput : function(rowIdx) {
214                 if(rowIdx === null || rowIdx === undefined) return '';
215                 return "<input type='checkbox' name='autogrid.selector' row='" + rowIdx + "'/>";
216             },
217
218             _applySingleEditStyle : function() {
219                 this.onMouseOverRow = function(e) {};
220                 this.onMouseOutRow = function(e) {};
221                 this.onCellFocus = function(cell, rowIndex) { 
222                     this.selection.deselectAll();
223                     this.selection.select(this.focus.rowIndex);
224                 };
225             },
226
227             /* capture keydown and launch edit dialog on enter */
228             _applyEditOnEnter : function() {
229                 this._applySingleEditStyle();
230
231                 dojo.connect(this, 'onRowDblClick',
232                     function(e) {
233                         if(this.editStyle == 'pane')
234                             this._drawEditPane(this.selection.getFirstSelected(), this.focus.rowIndex);
235                         else
236                             this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
237                     }
238                 );
239
240                 dojo.connect(this, 'onKeyDown',
241                     function(e) {
242                         if(e.keyCode == dojo.keys.ENTER) {
243                             this.selection.deselectAll();
244                             this.selection.select(this.focus.rowIndex);
245                             if(this.editStyle == 'pane')
246                                 this._drawEditPane(this.selection.getFirstSelected(), this.focus.rowIndex);
247                             else
248                                 this._drawEditDialog(this.selection.getFirstSelected(), this.focus.rowIndex);
249                         }
250                     }
251                 );
252             },
253
254             _makeEditPane : function(storeItem, rowIndex, onPostSubmit, onCancel) {
255                 var grid = this;
256                 var fmObject = new fieldmapper[this.fmClass]().fromStoreItem(storeItem);
257                 var idents = grid.store.getIdentityAttributes();
258
259                 var pane = new openils.widget.EditPane({
260                     fmObject:fmObject,
261                     overrideWidgets : this.overrideEditWidgets,
262                     overrideWidgetClass : this.overrideEditWidgetClass,
263                     disableWidgetTest : this.disableWidgetTest,
264                     onPostSubmit : function() {
265                         for(var i in fmObject._fields) {
266                             var field = fmObject._fields[i];
267                             if(idents.filter(function(j){return (j == field)})[0])
268                                 continue; // don't try to edit an identifier field
269                             grid.store.setValue(storeItem, field, fmObject[field]());
270                         }
271                         if(self.onPostUpdate)
272                             self.onPostUpdate(storeItem, rowIndex);
273                         setTimeout(
274                             function(){
275                                 try { 
276                                     grid.views.views[0].getCellNode(rowIndex, 0).focus(); 
277                                 } catch (E) {}
278                             },200
279                         );
280                         if(onPostSubmit) onPostSubmit();
281                     },
282                     onCancel : function() {
283                         setTimeout(function(){
284                             grid.views.views[0].getCellNode(rowIndex, 0).focus();},200);
285                         if(onCancel) onCancel();
286                     }
287                 });
288
289                 pane.fieldOrder = this.fieldOrder;
290                 pane.mode = 'update';
291                 return pane;
292             },
293
294             _makeCreatePane : function(onPostSubmit, onCancel) {
295                 var grid = this;
296                 var pane = new openils.widget.EditPane({
297                     fmClass : this.fmClass,
298                     overrideWidgets : this.overrideEditWidgets,
299                     overrideWidgetClass : this.overrideEditWidgetClass,
300                     disableWidgetTest : this.disableWidgetTest,
301                     onPostSubmit : function(r) {
302                         var fmObject = openils.Util.readResponse(r);
303                         if(grid.onPostCreate)
304                             grid.onPostCreate(fmObject);
305                         if(fmObject) 
306                             grid.store.newItem(fmObject.toStoreItem());
307                         setTimeout(function(){
308                             try {
309                                 grid.selection.select(grid.rowCount-1);
310                                 grid.views.views[0].getCellNode(grid.rowCount-1, 1).focus();
311                             } catch (E) {}
312                         },200);
313                         if(onPostSubmit)
314                             onPostSubmit();
315                     },
316                     onCancel : function() {
317                         if(onCancel) onCancel();
318                     }
319                 });
320                 pane.fieldOrder = this.fieldOrder;
321                 pane.mode = 'create';
322                 return pane;
323             },
324
325             // .startup() is called within
326             _makeClonePane : function(storeItem, rowIndex, onPostSubmit, onCancel) {
327                 var clonePane = this._makeCreatePane(onPostSubmit, onCancel);
328                 var origPane = this._makeEditPane(storeItem, rowIndex);
329                 clonePane.startup();
330                 origPane.startup();
331                 dojo.forEach(origPane.fieldList,
332                     function(field) {
333                         if(field.widget.widget.attr('disabled')) return;
334                         var w = clonePane.fieldList.filter(
335                             function(i) { return (i.name == field.name) })[0];
336                         w.widget.baseWidgetValue(field.widget.widgetValue); // sync widgets
337                         w.widget.onload = function(){w.widget.baseWidgetValue(field.widget.widgetValue)}; // async widgets
338                     }
339                 );
340                 origPane.destroy();
341                 return clonePane;
342             },
343
344
345             _drawEditDialog : function(storeItem, rowIndex) {
346                 var self = this;
347                 var done = function() { self.hideDialog(); };
348                 var pane = this._makeEditPane(storeItem, rowIndex, done, done);
349                 this.editDialog = new openils.widget.EditDialog({editPane:pane});
350                 this.editDialog.startup();
351                 this.editDialog.show();
352             },
353
354             showCreateDialog : function() {
355                 var self = this;
356                 var done = function() { self.hideDialog(); };
357                 var pane = this._makeCreatePane(done, done);
358                 this.editDialog = new openils.widget.EditDialog({editPane:pane});
359                 this.editDialog.startup();
360                 this.editDialog.show();
361             },
362
363             _drawEditPane : function(storeItem, rowIndex) {
364                 var self = this;
365                 var done = function() { self.hidePane(); };
366                 dojo.style(this.domNode, 'display', 'none');
367                 this.editPane = this._makeEditPane(storeItem, rowIndex, done, done);
368                 this.editPane.startup();
369                 this.domNode.parentNode.insertBefore(this.editPane.domNode, this.domNode);
370             },
371
372             showClonePane : function() {
373                 var self = this;
374                 var done = function() { self.hidePane(); };
375                 var row = this.getFirstSelectedRow();
376                 if(!row) return;
377                 dojo.style(this.domNode, 'display', 'none');
378                 this.editPane = this._makeClonePane(this.getItem(row), row, done, done);
379                 this.domNode.parentNode.insertBefore(this.editPane.domNode, this.domNode);
380             },
381
382             showCreatePane : function() {
383                 var self = this;
384                 var done = function() { self.hidePane(); };
385                 dojo.style(this.domNode, 'display', 'none');
386                 this.editPane = this._makeCreatePane(done, done);
387                 this.editPane.startup();
388                 this.domNode.parentNode.insertBefore(this.editPane.domNode, this.domNode);
389             },
390
391             hideDialog : function() {
392                 this.editDialog.hide(); 
393                 this.editDialog.destroy(); 
394                 delete this.editDialog;
395                 this.update();
396             },
397
398             hidePane : function() {
399                 this.domNode.parentNode.removeChild(this.editPane.domNode);
400                 this.editPane.destroy();
401                 delete this.editPane;
402                 dojo.style(this.domNode, 'display', 'block');
403                 this.update();
404             },
405             
406             resetStore : function() {
407                 this.setStore(this.buildAutoStore());
408             },
409
410             loadAll : function(opts, search) {
411                 dojo.require('openils.PermaCrud');
412                 if(!opts) opts = {};
413                 var self = this;
414                 opts = dojo.mixin(opts, {
415                     async : true,
416                     streaming : true,
417                     onresponse : function(r) {
418                         var item = openils.Util.readResponse(r);
419                         self.store.newItem(item.toStoreItem());
420                     }
421                 });
422                 if(search)
423                     new openils.PermaCrud().search(this.fmClass, search, opts);
424                 else
425                     new openils.PermaCrud().retrieveAll(this.fmClass, opts);
426             }
427         } 
428     );
429
430     // static ID generater seed
431     openils.widget.AutoGrid.sequence = 0;
432     openils.widget.AutoGrid.gridCache = {};
433
434     openils.widget.AutoGrid.markupFactory = dojox.grid.DataGrid.markupFactory;
435
436     openils.widget.AutoGrid.defaultGetter = function(rowIndex, item) {
437         if(!item) return '';
438         var val = this.grid.store.getValue(item, this.field);
439         var autoWidget = new openils.widget.AutoFieldWidget({
440             fmClass: this.grid.fmClass,
441             fmField: this.field,
442             widgetValue : val,
443             readOnly : true,
444             forceSync : true
445         });
446         //autoWidget.build();
447         return autoWidget.getDisplayString();
448     }
449 }
450