LP#1775466 Grid column position improvements
authorBill Erickson <berickxx@gmail.com>
Tue, 17 Jul 2018 17:05:12 +0000 (13:05 -0400)
committerBill Erickson <berickxx@gmail.com>
Wed, 25 Jul 2018 17:11:45 +0000 (13:11 -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.ts

index a8556f4..dffede1 100644 (file)
@@ -50,6 +50,7 @@ export class GridColumnComponent implements OnInit {
         col.isSortable = this.sortable;
         col.isMultiSortable = this.multiSortable;
         col.datatype = this.datatype;
+        col.isAuto = false;
         this.grid.context.columnSet.add(col);
     }
 }
index eda329a..9262c28 100644 (file)
@@ -30,6 +30,10 @@ export class GridColumn {
     isDragTarget: boolean;
     isSortable: boolean;
     isMultiSortable: boolean;
+
+    // True if the column was automatically generated.
+    isAuto: boolean;
+
     flesher: (obj: any, col: GridColumn, item: any) => any;
 
     getCellContext(row: any) {
@@ -62,8 +66,11 @@ export class GridColumnSet {
     add(col: GridColumn) {
 
         this.applyColumnDefaults(col);
-
-        if (this.getColByName(col.name)) { return; } // avoid dupes
+        
+        if (!this.insertColumn(col)) { 
+            // Column was rejected as a duplicate.
+            return; 
+        }
 
         if (col.isIndex) { this.indexColumn = col; }
 
@@ -73,8 +80,60 @@ export class GridColumnSet {
         }
 
         this.applyColumnSortability(col);
+    }
+
+    // Returns true if the new column was inserted, false otherwise.
+    // Declared columns take precedence over auto-generated columns
+    // when collisions occur.
+    // Declared columns are inserted in front of auto columns.
+    insertColumn(col: GridColumn): boolean {
+
+        if (col.isAuto) {
+            if (this.getColByName(col.name)) {
+                // New auto-generated column conflicts with existing 
+                // column.  Skip it.
+                return false;
+            } else {
+                // No collisions.  Add to the end of the list
+                this.columns.push(col);
+                return true;
+            }
+        }
+
+        // Adding a declared column.
+
+        // Check for dupes.
+        for (let idx = 0; idx < this.columns.length; idx++) {
+            const testCol = this.columns[idx];
+            if (testCol.name === col.name) { // match found
+                if (testCol.isAuto) { 
+                    // new column takes precedence, remove the existing column.
+                    this.columns.splice(idx, 1);
+                    break;
+                } else {
+                    // New column does not take precedence.  Avoid
+                    // inserting it.
+                    return false;
+                }
+            }
+        }
 
+        // Delcared columns are inserted just before the first auto-column
+        for (let idx = 0; idx < this.columns.length; idx++) {
+            const testCol = this.columns[idx];
+            if (testCol.isAuto) {
+                if (idx === 0) {
+                    this.columns.unshift(col);
+                } else {
+                    this.columns.splice(idx - 1, 0, col);
+                }
+                return true;
+            }
+        }
+
+        // No insertion point found.  Toss the new column on the end.
         this.columns.push(col);
+        return true;
     }
 
     getColByName(name: string): GridColumn {
@@ -712,6 +771,7 @@ export class GridContext {
             col.idlFieldDef = field;
             col.datatype = field.datatype;
             col.isIndex = (field.name === pkeyField);
+            col.isAuto = true;
             this.columnSet.add(col);
         });
     }