LP#1775466 Grid default hidden fields; tidying
authorBill Erickson <berickxx@gmail.com>
Tue, 3 Jul 2018 21:37:40 +0000 (17:37 -0400)
committerBill Erickson <berickxx@gmail.com>
Wed, 5 Sep 2018 14:05:23 +0000 (10:05 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/src/eg2/src/app/share/grid/grid-column.component.ts
Open-ILS/src/eg2/src/app/share/grid/grid.component.ts
Open-ILS/src/eg2/src/app/share/grid/grid.ts

index dd8adb2..a8556f4 100644 (file)
@@ -16,8 +16,10 @@ export class GridColumnComponent implements OnInit {
     @Input() flex: number;
     // is this the index field?
     @Input() index: boolean;
-    @Input() visible: boolean;
+
+    // Columns are assumed to be visible unless hidden=true.
     @Input() hidden: boolean;
+
     @Input() sortable: boolean;
     @Input() datatype: string;
     @Input() multiSortable: boolean;
index 2de7922..a91e3d2 100644 (file)
@@ -58,6 +58,11 @@ export class GridComponent implements OnInit, AfterViewInit, OnDestroy {
     // a given cell or all cells in a column.
     @Input() cellClassCallback: (row: any, col: GridColumn) => string;
 
+    // comma-separated list of fields to hide.
+    // This is less verbose than having to define an <eg-grid-column/>
+    // just to mark it hidden.
+    @Input() hideFields: string;
+
     context: GridContext;
 
     // These events are emitted from our grid-body component.
@@ -86,6 +91,9 @@ export class GridComponent implements OnInit, AfterViewInit, OnDestroy {
         this.context.disableMultiSelect = this.disableMultiSelect === true;
         this.context.rowFlairIsEnabled = this.rowFlairIsEnabled  === true;
         this.context.rowFlairCallback = this.rowFlairCallback;
+        if (this.hideFields) {
+            this.context.defaultHiddenFields = this.hideFields.split(',');
+        }
 
         // TS doesn't seem to like: let foo = bar || () => '';
         this.context.rowClassCallback =
index ae5dab5..18c580c 100644 (file)
@@ -49,6 +49,7 @@ export class GridColumnSet {
     isMultiSortable: boolean;
     stockVisible: string[];
     idl: IdlService;
+    defaultHiddenFields: string[];
 
     constructor(idl: IdlService, idlClass?: string) {
         this.idl = idl;
@@ -231,7 +232,20 @@ export class GridColumnSet {
     }
 
     applyColumnSettings(conf: GridColumnPersistConf[]) {
-        if (!conf || conf.length === 0) { return; }
+
+        if (!conf || conf.length === 0) {
+
+            // If no configuration is available, but we have a list of
+            // fields to hide by default, hide them.
+            if (this.defaultHiddenFields) {
+                this.defaultHiddenFields.forEach(name => {
+                    const col = this.getColByName(name);
+                    if (col) col.visible = false;
+                });
+            }
+
+            return;
+        }
 
         const newCols = [];
 
@@ -334,6 +348,7 @@ export class GridContext {
     rowFlairCallback: (row: any) => GridRowFlairEntry;
     rowClassCallback: (row: any) => string;
     cellClassCallback: (row: any, col: GridColumn) => string;
+    defaultHiddenFields: string[];
 
     // Services injected by our grid component
     idl: IdlService;
@@ -360,13 +375,17 @@ export class GridContext {
         this.columnSet = new GridColumnSet(this.idl, this.idlClass);
         this.columnSet.isSortable = this.isSortable === true;
         this.columnSet.isMultiSortable = this.isMultiSortable === true;
-        this.generateColumns();
+        this.columnSet.defaultHiddenFields = this.defaultHiddenFields;
     }
 
     // Load initial settings and data.
     initData() {
-        this.getColumnsConfig(this.persistKey)
-        .then(conf => this.columnSet.applyColumnSettings(conf))
+
+        // run in timeout because we are modifying the state of the 
+        // grid column header component mid-digest
+        setTimeout(() => this.generateColumns());
+
+        this.applyColumnsConfig()
         .then(ok => this.dataSource.requestPage(this.pager))
         .then(ok => this.listenToPager());
     }
@@ -375,6 +394,11 @@ export class GridContext {
         this.ignorePager();
     }
 
+    applyColumnsConfig(): Promise<void> {
+        return this.getColumnsConfig(this.persistKey)
+        .then(conf => this.columnSet.applyColumnSettings(conf));
+    }
+
     reload() {
         // Give the UI time to settle before reloading grid data.
         // This can help when data retrieval depends on a value
@@ -658,23 +682,22 @@ export class GridContext {
     }
 
     generateColumns() {
+        if (!this.columnSet.idlClass) { return; }
+
+        const pkeyField = this.idl.classes[this.columnSet.idlClass].pkey;
 
         // generate columns for all non-virtual fields on the IDL class
-        if (this.columnSet.idlClass) {
-            this.idl.classes[this.columnSet.idlClass].fields.forEach(field => {
-                if (field.virtual) { return; }
-                const col = new GridColumn();
-                col.name = field.name;
-                col.label = field.label || field.name;
-                col.idlFieldDef = field;
-                col.datatype = field.datatype;
-                if (field.name ===
-                    this.idl.classes[this.columnSet.idlClass].pkey) {
-                    col.isIndex = true;
-                }
-                this.columnSet.add(col);
-            });
-        }
+        this.idl.classes[this.columnSet.idlClass].fields
+        .filter(field => !field.virtual)
+        .forEach(field => {
+            const col = new GridColumn();
+            col.name = field.name;
+            col.label = field.label || field.name;
+            col.idlFieldDef = field;
+            col.datatype = field.datatype;
+            col.isIndex = (field.name === pkeyField);
+            this.columnSet.add(col);
+        });
     }