1 if (!dojo._hasResource["openils.widget.FlattenerGrid"]) {
2 dojo.provide("openils.widget.FlattenerGrid");
4 dojo.require("DojoSRF");
5 dojo.require("dojox.grid.DataGrid");
6 dojo.require("openils.FlattenerStore");
7 dojo.require("openils.PermaCrud");
8 dojo.require("openils.widget.GridColumnPicker");
9 dojo.require("openils.widget.EditDialog"); /* includes EditPane */
10 dojo.require("openils.widget._GridHelperColumns");
13 "openils.widget.FlattenerGrid",
14 [dojox.grid.DataGrid, openils.widget._GridHelperColumns], {
15 /* These potential constructor arguments are useful to
16 * FlattenerGrid in their own right */
17 "columnReordering": true,
18 "columnPersistKey": null,
19 "showLoadFilter": false, /* use FlattenerFilterDialog */
21 /* These potential constructor arguments maybe useful to
22 * FlattenerGrid in their own right, and are passed to
27 "defaultSort": null, /* whatever any part of the UI says will
29 "baseSort": null, /* will contains what the columnpicker
30 dictates, and precedes whatever the column
33 /* These potential constructor arguments are for functionality
34 * copied from AutoGrid */
35 "editOnEnter": false, /* also implies edit-on-dblclick */
36 "editStyle": "dialog", /* "dialog" or "pane" */
37 "requiredFields": null, /* affects create/edit dialogs */
38 "suppressEditFields": null, /* affects create/edit dialogs */
40 /* _generateMap() lives to interpret the attributes of the
41 * FlattenerGrid dijit itself plus those definined in
45 * <th field="foo" ...>
46 * to build the map to hand to the FlattenerStore, which in turn
47 * uses it to query the flattener service.
49 "_generateMap": function() {
50 var map = this.mapClause = {};
51 var fields = this.structure[0].cells[0];
53 /* These are the fields defined in thead -> tr -> [th,th,...].
54 * For purposes of building the map, where each field has
55 * three boolean attributes "display", "sort" and "filter",
56 * assume "display" and "sort" are always true for these.
57 * That doesn't mean that at the UI level we can't hide a
60 * If you need extra fields in the map for which display
61 * or sort should *not* be true, use mapExtras.
64 fields, function(field) {
65 if (field.field.match(/^\+/))
66 return; /* special fields e.g. checkbox/line # */
70 "filter": (field.ffilter || false),
72 "path": field.fpath || field.field
74 /* The following attribute is not for the flattener
75 * service's benefit, but for other uses. We capture
76 * the hardcoded <th> value (the header label) if any.*/
78 map[field.field]._label = field.name;
83 /* It's not particularly useful to add simple fields, i.e.
84 * circ_lib: "circ_lib.name"
85 * to mapExtras, because by convention used elsewhere in
86 * Flattener, that gives all attributes, including
87 * display, a true value. Still, be consistent to avoid
90 for (var key in this.mapExtras) {
91 if (typeof this.mapExtras[key] != "object") {
92 this.mapExtras[key] = {
93 "path": this.mapExtras[key],
100 dojo.mixin(map, this.mapExtras);
103 /* Do this now, since we don't want a silently added
104 * identifier attribute in the terminii list (see its uses). */
105 this._calculateMapTerminii();
106 this._supplementHeaderNames();
108 /* make sure we always have a field for fm identifier */
109 if (!map[this.fmIdentifier]) {
110 map[this.fmIdentifier] = {
111 "path": this.fmIdentifier,
112 "display": true, /* Flattener displays it to us,
113 but we don't display to user. */
122 "_cleanMapForStore": function(map) {
123 var clean = dojo.clone(map);
125 for (var column in clean) {
126 openils.Util.objectProperties(clean[column]).filter(
127 function(k) { return k.match(/^_/); }
129 function(k) { delete clean[column][k]; }
136 /* The FlattenerStore doesn't need this, but it has at least two
137 * uses: 1) FlattenerFilterDialog, 2) setting column header labels
140 * To call these 'Terminii' can be misleading. In certain
141 * (actually probably common) cases, they won't really be the last
142 * field in a path, but the next-to-last. Read on. */
143 "_calculateMapTerminii": function() {
144 function _fm_is_selector_for_class(hint, field) {
145 var cl = fieldmapper.IDL.fmclasses[hint];
146 return (cl.field_map[cl.pkey].selector == field);
149 function _follow_to_end(hint, path) {
150 var last_field, last_hint;
151 var orig_path = dojo.clone(path);
154 while (field = path.shift()) {
155 /* XXX this assumes we have the whole IDL loaded. I
156 * guess we could teach this to work by loading classes
157 * on demand when we don't have the whole IDL loaded. */
159 fieldmapper.IDL.fmclasses[hint].field_map[field];
161 if (field_def["class"] && path.length) {
165 hint = field_def["class"];
166 } else if (path.length) {
167 /* There are more fields left but we can't follow
168 * the chain via IDL any further. */
170 "_calculateMapTerminii can't parse path " +
171 orig_path + " (at " + field + ")"
174 break; /* keeps field defined after loop */
178 var datatype = field_def.datatype;
179 var indirect = false;
180 /* Back off the last field in the path if it's a selector
181 * for its class, because the preceding field will be
182 * a better thing to hand to AutoFieldWidget.
184 if (orig_path.length > 1 &&
185 _fm_is_selector_for_class(hint, field)) {
195 "label": field_def.label,
196 "datatype": datatype,
201 this.mapTerminii = [];
202 for (var column in this.mapClause) {
203 var terminus = dojo.mixin(
206 this.mapClause[column].path.split(/\./)
208 "simple_name": column,
209 "isfilter": this.mapClause[column].filter
212 if (this.mapClause[column]._label)
213 terminus.label = this.mapClause[column]._label;
215 this.mapTerminii.push(terminus);
219 "_supplementHeaderNames": function() {
220 /* You'd be surprised how rarely this make sense in Flattener
221 * use cases, but if we didn't give a particular header cell
222 * (<th>) a display name (the innerHTML of that <th>), then
223 * use the IDL to provide the label of the terminus of the
224 * flattener path for that column. It may be better than using
225 * the raw field name. */
227 this.structure[0].cells[0].forEach(
230 header.name = self.mapTerminii.filter(
232 return t.simple_name == header.field;
240 "constructor": function(args) {
241 dojo.mixin(this, args);
243 this.fmIdentifier = this.fmIdentifier ||
244 fieldmapper.IDL.fmclasses[this.fmClass].pkey;
247 "startup": function() {
249 /* Save original query for further filtering later */
250 this._baseQuery = dojo.clone(this.query);
251 this._startupGridHelperColumns();
253 if (!this.columnPicker) {
255 new openils.widget.GridColumnPicker(
256 null, this.columnPersistKey, this);
257 this.columnPicker.onLoad = dojo.hitch(
258 this, function(opts) { this._finishStartup(opts.sortFields) });
260 this.columnPicker.onSortChange = dojo.hitch(this,
261 /* directly after, this.update() is called by the
262 column picker, causing a re-fetch */
264 this.store.baseSort = this._mapCPSortFields(fields)
268 this.columnPicker.load();
271 this.inherited(arguments);
274 /* Maps ColumnPicker sort fields to the correct format.
275 If no sort fields specified, falls back to defaultSort */
276 "_mapCPSortFields": function(sortFields) {
277 var sort = this.defaultSort;
278 if (sortFields.length) {
279 sort = sortFields.map(function(f) {
281 a[f.field] = f.direction;
288 "_finishStartup": function(sortFields) {
291 new openils.FlattenerStore({
292 "fmClass": this.fmClass,
293 "fmIdentifier": this.fmIdentifier,
294 "mapClause": (this.mapClause ||
295 this._cleanMapForStore(this._generateMap())),
296 "baseSort": this.baseSort,
297 "defaultSort": this._mapCPSortFields(sortFields)
301 this._showing_create_pane = false;
303 this.overrideEditWidgets = {};
304 this.overrideEditWidgetClass = {};
305 this.overrideWidgetArgs = {};
307 if (this.editOnEnter)
308 this._applyEditOnEnter();
309 else if (this.singleEditStyle)
310 this._applySingleEditStyle();
312 /* Like AutoGrid's paginator, but we'll never have Back/Next
313 * links. Just a place to hold misc links */
318 "_setupLinks": function() {
319 this.linkHolder = new dijit.layout.ContentPane();
320 dojo.place(this.linkHolder.domNode, this.domNode, "before");
322 if (this.showLoadFilter) {
323 dojo.require("openils.widget.FlattenerFilterDialog");
325 new openils.widget.FlattenerFilterDialog({
326 "fmClass": this.fmClass,
327 "mapTerminii": this.mapTerminii
330 this.filterDialog.onApply = dojo.hitch(
331 this, function(filter) {
333 dojo.mixin(filter, this._baseQuery),
339 this.filterDialog.startup();
342 "innerHTML": "Filter", /* XXX i18n */
343 "href": "javascript:void(0);",
344 "onclick": dojo.hitch(this, function() {
345 this.filterDialog.show();
347 }, this.linkHolder.domNode
352 /* ******** below are methods mostly copied but
353 * slightly changed from AutoGrid ******** */
355 "_applySingleEditStyle": function() {
356 this.onMouseOverRow = function(e) {};
357 this.onMouseOutRow = function(e) {};
358 this.onCellFocus = function(cell, rowIndex) {
359 this.selection.deselectAll();
360 this.selection.select(this.focus.rowIndex);
364 /* capture keydown and launch edit dialog on enter */
365 "_applyEditOnEnter": function() {
366 this._applySingleEditStyle();
369 this, "onRowDblClick", function(e) {
370 if (this.editStyle == "pane")
372 this.selection.getFirstSelected(),
376 this._drawEditDialog(
377 this.selection.getFirstSelected(),
384 this, "onKeyDown", function(e) {
385 if (e.keyCode == dojo.keys.ENTER) {
386 this.selection.deselectAll();
387 this.selection.select(this.focus.rowIndex);
388 if (this.editStyle == "pane")
390 this.selection.getFirstSelected(),
394 this._drawEditDialog(
395 this.selection.getFirstSelected(),
403 "_makeEditPane": function(storeItem, rowIndex, onPostSubmit, onCancel) {
405 var fmObject = (new openils.PermaCrud()).retrieve(
407 this.store.getIdentity(storeItem)
410 var pane = new openils.widget.EditPane({
411 "fmObject": fmObject,
412 "hideSaveButton": this.editReadOnly,
413 "readOnly": this.editReadOnly,
414 "overrideWidgets": this.overrideEditWidgets,
415 "overrideWidgetClass": this.overrideEditWidgetClass,
416 "overrideWidgetArgs": this.overrideWidgetArgs,
417 "disableWidgetTest": this.disableWidgetTest,
418 "requiredFields": this.requiredFields,
419 "suppressFields": this.suppressEditFields,
420 "onPostSubmit": function() {
421 /* ask the store to call flattener specially to get
422 * the flat row related to only this fmobj */
423 grid.store.loadItem({"force": true, "item": storeItem});
425 if (grid.onPostUpdate)
426 grid.onPostUpdate(storeItem, rowIndex);
431 grid.views.views[0].getCellNode(
440 "onCancel": function() {
443 grid.views.views[0].getCellNode(
453 if (typeof this.editPaneOnSubmit == "function")
454 pane.onSubmit = this.editPaneOnSubmit;
456 pane.fieldOrder = this.fieldOrder;
457 pane.mode = "update";
461 "_makeCreatePane": function(onPostSubmit, onCancel) {
463 var pane = new openils.widget.EditPane({
464 "fmClass": this.fmClass,
465 "overrideWidgets": this.overrideEditWidgets,
466 "overrideWidgetClass": this.overrideEditWidgetClass,
467 "overrideWidgetArgs": this.overrideWidgetArgs,
468 "disableWidgetTest": this.disableWidgetTest,
469 "requiredFields": this.requiredFields,
470 "suppressFields": this.suppressEditFields,
471 "onPostSubmit": function(req, cudResults) {
472 var fmObject = cudResults[0];
473 if (grid.onPostCreate)
474 grid.onPostCreate(fmObject);
476 grid.store.fetchItemByIdentity({
477 "identity": fmObject[grid.fmIdentifier](),
478 "onItem": function(item) {
479 grid.store.onNew(item);
487 grid.selection.select(grid.rowCount - 1);
488 grid.views.views[0].getCellNode(
496 onPostSubmit(fmObject);
498 "onCancel": function() { if (onCancel) onCancel(); }
501 if (typeof this.createPaneOnSubmit == "function")
502 pane.onSubmit = this.createPaneOnSubmit;
503 pane.fieldOrder = this.fieldOrder;
504 pane.mode = "create";
509 * Creates an EditPane with a copy of the data from the provided store
510 * item for cloning said item
511 * @param {Object} storeItem Dojo data item
512 * @param {Number} rowIndex The Grid row index of the item to be cloned
513 * @param {Function} onPostSubmit Optional callback for post-submit behavior
514 * @param {Function} onCancel Optional callback for clone cancelation
515 * @return {Object} The clone EditPane
517 "_makeClonePane": function(storeItem,rowIndex,onPostSubmit,onCancel) {
518 var clonePane = this._makeCreatePane(onPostSubmit, onCancel);
519 var origPane = this._makeEditPane(storeItem, rowIndex);
523 origPane.fieldList, function(field) {
524 if (field.widget.widget.attr('disabled'))
527 var w = clonePane.fieldList.filter(
528 function(i) { return (i.name == field.name) }
532 w.widget.baseWidgetValue(field.widget.widget.attr('value'));
535 w.widget.onload = function() {
536 w.widget.baseWidgetValue(
537 field.widget.widget.attr('value')
547 "_drawEditDialog": function(storeItem, rowIndex) {
548 var done = dojo.hitch(this, function() { this.hideDialog(); });
549 var pane = this._makeEditPane(storeItem, rowIndex, done, done);
550 this.editDialog = new openils.widget.EditDialog({editPane:pane});
551 this.editDialog.startup();
552 this.editDialog.show();
556 * Generates an EditDialog for object creation and displays it to the user
558 "showCreateDialog": function() {
559 var done = dojo.hitch(this, function() { this.hideDialog(); });
560 var pane = this._makeCreatePane(done, done);
561 this.editDialog = new openils.widget.EditDialog({editPane:pane});
562 this.editDialog.startup();
563 this.editDialog.show();
566 "_drawEditPane": function(storeItem, rowIndex) {
567 var done = dojo.hitch(this, function() { this.hidePane(); });
569 dojo.style(this.domNode, "display", "none");
571 this.editPane = this._makeEditPane(storeItem, rowIndex, done, done);
572 this.editPane.startup();
573 dojo.place(this.editPane.domNode, this.domNode, "before");
576 this.onEditPane(this.editPane);
579 "showClonePane": function(onPostSubmit) {
580 var done = dojo.hitch(this, function() { this.hidePane(); });
581 var row = this.getFirstSelectedRow();
587 postSubmit = dojo.hitch(
588 this, function(result) {
589 onPostSubmit(this.getItem(row), result);
597 dojo.style(this.domNode, "display", "none");
598 this.editPane = this._makeClonePane(
599 this.getItem(row), row, postSubmit, done
601 dojo.place(this.editPane.domNode, this.domNode, "before");
603 this.onEditPane(this.editPane);
606 "showCreatePane": function() {
607 if (this._showing_create_pane)
609 this._showing_create_pane = true;
611 var done = dojo.hitch(
613 this._showing_create_pane = false;
618 dojo.style(this.domNode, "display", "none");
620 this.editPane = this._makeCreatePane(done, done);
621 this.editPane.startup();
623 dojo.place(this.editPane.domNode, this.domNode, "before");
626 this.onEditPane(this.editPane);
629 "hideDialog": function() {
630 this.editDialog.hide();
631 this.editDialog.destroy();
632 delete this.editDialog;
636 "hidePane": function() {
637 this.domNode.parentNode.removeChild(this.editPane.domNode);
638 this.editPane.destroy();
639 delete this.editPane;
640 dojo.style(this.domNode, "display", "block");
644 "deleteSelected": function() {
647 this.getSelectedItems().forEach(
649 var fmobj = new fieldmapper[self.fmClass]();
650 fmobj[self.fmIdentifier](
651 self.store.getIdentity(item)
653 (new openils.PermaCrud()).eliminate(
655 "oncomplete": function() {
656 self.store.deleteItem(item);
666 /* monkey patch so we can get more attributes from each column in the
667 * markup that specifies grid columns (table->thead->tr->[td,...])
670 var b = dojox.grid.cells._Base;
671 var orig_mf = b.markupFactory;
673 b.markupFactory = function(node, cellDef) {
674 orig_mf(node, cellDef);
677 ["fpath", "ffilter"], function(a) {
678 var value = dojo.attr(node, a);
686 /* the secret to successfully subclassing dojox.grid.DataGrid */
687 openils.widget.FlattenerGrid.markupFactory =
688 dojox.grid.DataGrid.markupFactory;