LP1803787 Grid context retains selection; lint
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / share / grid / grid-toolbar.component.ts
index 5c8b523..a05aaa5 100644 (file)
@@ -17,13 +17,53 @@ export class GridToolbarComponent implements OnInit {
     @Input() colWidthConfig: GridColumnWidthComponent;
     @Input() gridPrinter: GridPrintComponent;
 
+    renderedGroups: {[group: string]: boolean};
+
     csvExportInProgress: boolean;
     csvExportUrl: SafeUrl;
     csvExportFileName: string;
 
-    constructor(private sanitizer: DomSanitizer) {}
+    constructor(private sanitizer: DomSanitizer) {
+        this.renderedGroups = {};
+    }
+
+    ngOnInit() {
+        this.sortActions();
+    }
+
+    sortActions() {
+        const actions = this.gridContext.toolbarActions;
+
+        const unGrouped = actions.filter(a => !a.group)
+        .sort((a, b) => {
+            return a.label < b.label ? -1 : 1;
+        });
+
+        const grouped = actions.filter(a => Boolean(a.group))
+        .sort((a, b) => {
+            if (a.group === b.group) {
+                return a.label < b.label ? -1 : 1;
+            } else {
+                return a.group < b.group ? -1 : 1;
+            }
+        });
+
+        // Insert group markers for rendering
+        const seen: any = {};
+        const grouped2: any[] = [];
+        grouped.forEach(action => {
+            if (!seen[action.group]) {
+                seen[action.group] = true;
+                const act = new GridToolbarAction();
+                act.label = action.group;
+                act.isGroup = true;
+                grouped2.push(act);
+            }
+            grouped2.push(action);
+        });
 
-    ngOnInit() {}
+        this.gridContext.toolbarActions = unGrouped.concat(grouped2);
+    }
 
     saveGridConfig() {
         // TODO: when server-side settings are supported, this operation
@@ -38,7 +78,25 @@ export class GridToolbarComponent implements OnInit {
     }
 
     performAction(action: GridToolbarAction) {
-        action.action(this.gridContext.getSelectedRows());
+        if (action.isGroup || action.separator) {
+            return; // These don't perform actions
+        }
+        const rows = this.gridContext.getSelectedRows();
+        action.onClick.emit(rows);
+        if (action.action) { action.action(rows); }
+    }
+
+    performButtonAction(button: GridToolbarButton) {
+        const rows = this.gridContext.getSelectedRows();
+        button.onClick.emit();
+        if (button.action) { button.action(); }
+    }
+
+    shouldDisableAction(action: GridToolbarAction) {
+        if (action.disableOnRows) {
+            return action.disableOnRows(this.gridContext.getSelectedRows());
+        }
+        return false;
     }
 
     printHtml() {