|
5 | 5 | import { base } from '$app/paths';
|
6 | 6 | import { constraintsAll, constraintsColumns } from '../../stores/constraints';
|
7 | 7 | import effects from '../../utilities/effects';
|
8 |
| - import { tooltip } from '../../utilities/tooltip'; |
9 | 8 | import Input from '../form/Input.svelte';
|
10 | 9 | import Chip from '../ui/Chip.svelte';
|
11 | 10 | import CssGrid from '../ui/CssGrid.svelte';
|
12 | 11 | import CssGridGutter from '../ui/CssGridGutter.svelte';
|
| 12 | + import DataGrid from '../ui/DataGrid.svelte'; |
| 13 | + import DataGridActions from '../ui/DataGridActions.svelte'; |
13 | 14 | import Panel from '../ui/Panel.svelte';
|
14 |
| - import Table from '../ui/Table.svelte'; |
15 | 15 | import ConstraintEditor from './ConstraintEditor.svelte';
|
16 | 16 |
|
| 17 | + type CellRendererParams = { |
| 18 | + deleteConstraint: (constraint: Constraint) => void; |
| 19 | + editConstraint: (constraint: Constraint) => void; |
| 20 | + }; |
| 21 | + type ConstraintsCellRendererParams = ICellRendererParams & CellRendererParams; |
| 22 | +
|
17 | 23 | export let initialPlans: PlanList[] = [];
|
18 | 24 |
|
| 25 | + const columnDefs: DataGridColumnDef[] = [ |
| 26 | + { field: 'id', headerName: 'ID', sortable: true, suppressAutoSize: true, width: 60 }, |
| 27 | + { field: 'name', headerName: 'Name', resizable: true, sortable: true }, |
| 28 | + { field: 'model_id', headerName: 'Model ID', sortable: true, width: 120 }, |
| 29 | + { field: 'plan_id', headerName: 'Plan ID', sortable: true, width: 110 }, |
| 30 | + { |
| 31 | + cellRenderer: (params: ConstraintsCellRendererParams) => { |
| 32 | + const actionsDiv = document.createElement('div'); |
| 33 | + actionsDiv.className = 'actions-cell'; |
| 34 | + new DataGridActions({ |
| 35 | + props: { |
| 36 | + deleteCallback: params.deleteConstraint, |
| 37 | + deleteTooltip: { |
| 38 | + content: 'Delete Constraint', |
| 39 | + placement: 'bottom', |
| 40 | + }, |
| 41 | + editCallback: params.editConstraint, |
| 42 | + editTooltip: { |
| 43 | + content: 'Edit Constraint', |
| 44 | + placement: 'bottom', |
| 45 | + }, |
| 46 | + rowData: params.data, |
| 47 | + }, |
| 48 | + target: actionsDiv, |
| 49 | + }); |
| 50 | +
|
| 51 | + return actionsDiv; |
| 52 | + }, |
| 53 | + cellRendererParams: { |
| 54 | + deleteConstraint, |
| 55 | + editConstraint, |
| 56 | + } as CellRendererParams, |
| 57 | + field: 'actions', |
| 58 | + headerName: '', |
| 59 | + resizable: false, |
| 60 | + sortable: false, |
| 61 | + width: 90, |
| 62 | + }, |
| 63 | + ]; |
| 64 | +
|
19 | 65 | let constraintModelId: number | null = null;
|
20 | 66 | let filterText: string = '';
|
21 | 67 | let filteredConstraints: Constraint[] = [];
|
|
35 | 81 | }
|
36 | 82 | $: constraintModelId = getConstraintModelId(selectedConstraint);
|
37 | 83 |
|
38 |
| - async function deleteConstraint(id: number) { |
| 84 | + async function deleteConstraint({ id }: Constraint) { |
39 | 85 | const success = await effects.deleteConstraint(id);
|
40 | 86 |
|
41 | 87 | if (success) {
|
|
64 | 110 | return null;
|
65 | 111 | }
|
66 | 112 |
|
67 |
| - function toggleConstraint(event: CustomEvent<Constraint>) { |
68 |
| - const { detail: clickedConstraint } = event; |
| 113 | + function editConstraint({ id }: Constraint) { |
| 114 | + goto(`${base}/constraints/edit/${id}`); |
| 115 | + } |
69 | 116 |
|
| 117 | + function toggleConstraint(clickedConstraint: Constraint) { |
70 | 118 | if (selectedConstraint?.id === clickedConstraint.id) {
|
71 | 119 | selectedConstraint = null;
|
72 | 120 | } else {
|
|
91 | 139 |
|
92 | 140 | <svelte:fragment slot="body">
|
93 | 141 | {#if filteredConstraints.length}
|
94 |
| - <Table |
95 |
| - let:currentRow |
96 |
| - columnDefs={[ |
97 |
| - { field: 'id', name: 'ID', sortable: true }, |
98 |
| - { field: 'name', name: 'Name', sortable: true }, |
99 |
| - { field: 'model_id', name: 'Model ID', sortable: true }, |
100 |
| - { field: 'plan_id', name: 'Plan ID', sortable: true }, |
101 |
| - ]} |
102 |
| - rowActions |
| 142 | + <DataGrid |
| 143 | + {columnDefs} |
103 | 144 | rowData={filteredConstraints}
|
104 |
| - rowSelectionMode="single" |
105 |
| - selectedRowId={selectedConstraint?.id} |
106 |
| - on:rowClick={toggleConstraint} |
107 |
| - > |
108 |
| - <div slot="actions-cell"> |
109 |
| - <button |
110 |
| - class="st-button icon" |
111 |
| - on:click|stopPropagation={() => goto(`${base}/constraints/edit/${currentRow.id}`)} |
112 |
| - use:tooltip={{ content: 'Edit Constraint', placement: 'bottom' }} |
113 |
| - > |
114 |
| - <i class="bi bi-pencil" /> |
115 |
| - </button> |
116 |
| - <button |
117 |
| - class="st-button icon" |
118 |
| - on:click|stopPropagation={() => deleteConstraint(currentRow.id)} |
119 |
| - use:tooltip={{ content: 'Delete Constraint', placement: 'bottom' }} |
120 |
| - > |
121 |
| - <i class="bi bi-trash" /> |
122 |
| - </button> |
123 |
| - </div> |
124 |
| - </Table> |
| 145 | + rowSelection="single" |
| 146 | + on:rowSelected={({ detail }) => toggleConstraint(detail.data)} |
| 147 | + /> |
125 | 148 | {:else}
|
126 | 149 | No Constraints Found
|
127 | 150 | {/if}
|
|
0 commit comments