Skip to content

Commit c0e9d97

Browse files
Object creation with dialogues
1 parent efc3b7d commit c0e9d97

File tree

2 files changed

+175
-102
lines changed

2 files changed

+175
-102
lines changed

packages/core/src/browser/new-data-model-dialog.ts renamed to packages/core/src/browser/grid-dialog.ts

Lines changed: 59 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,56 @@
55
import { inject } from '@theia/core/shared/inversify';
66
import { LabelProvider } from '@theia/core/lib/browser';
77
import { WorkspaceInputDialog, WorkspaceInputDialogProps } from '@theia/workspace/lib/browser/workspace-input-dialog';
8-
import { toPascal } from '@crossbreeze/protocol';
98

10-
const DataModelInputDialogProps = Symbol('DataModelInputDialogProps');
11-
interface DataModelInputDialogProps extends WorkspaceInputDialogProps {
12-
dataModelTypes: string[];
9+
const GridInputDialogProps = Symbol('GridInputDialogProps');
10+
interface GridInputDialogProps<T extends readonly InputOptions[] = readonly InputOptions[]> extends WorkspaceInputDialogProps {
11+
inputs: T;
1312
}
1413

15-
export class DataModelInputDialog extends WorkspaceInputDialog {
16-
protected readonly versionInput: HTMLInputElement;
17-
protected readonly typeSelector: HTMLSelectElement;
14+
export interface InputOptions {
15+
placeholder?: string;
16+
value?: string;
17+
id: string;
18+
label: string;
19+
options?: Record<string, string>;
20+
}
21+
22+
export type FieldValues<T extends readonly InputOptions[]> = Record<T[number]['id'], string>;
23+
24+
interface LabelOptions {
25+
text: string;
26+
for: string;
27+
}
28+
29+
export class GridInputDialog extends WorkspaceInputDialog {
1830
protected readonly grid: HTMLDivElement;
1931

2032
constructor(
21-
@inject(DataModelInputDialogProps) protected override readonly props: DataModelInputDialogProps,
33+
@inject(GridInputDialogProps) protected override readonly props: GridInputDialogProps,
2234
@inject(LabelProvider) protected override readonly labelProvider: LabelProvider
2335
) {
2436
super(props, labelProvider);
2537
this.grid = this.getGrid();
26-
this.contentNode.appendChild(this.grid);
38+
this.contentNode.insertBefore(this.grid, this.inputField);
39+
this.addInputs();
40+
}
41+
42+
protected addInputs(): void {
2743
const idBase = Date.now().toString(26);
28-
const nameInputId = idBase + '-name';
29-
this.grid.appendChild(createLabel({ text: 'Model Name', for: nameInputId }));
30-
this.inputField.id = nameInputId;
31-
this.grid.appendChild(this.inputField);
32-
const versionInputId = idBase + '-version';
33-
this.grid.appendChild(createLabel({ text: 'Version', for: versionInputId }));
34-
this.versionInput = createInput({ placeholder: '1.0.0', value: '1.0.0', id: versionInputId });
35-
this.grid.appendChild(this.versionInput);
36-
const typeInputId = idBase + '-type';
37-
this.grid.appendChild(createLabel({ text: 'Type', for: typeInputId }));
38-
this.typeSelector = createInput({
39-
value: 'logical',
40-
id: typeInputId,
41-
options: Object.fromEntries(props.dataModelTypes.map(key => [key, toPascal(key)]))
44+
let inputFieldSet = false;
45+
this.props.inputs.forEach(inputProps => {
46+
const computedId = idBase + '-' + inputProps.id;
47+
const label = createLabel({ text: inputProps.label, for: computedId });
48+
this.grid.appendChild(label);
49+
if (!inputFieldSet && !inputProps.options) {
50+
inputFieldSet = true;
51+
this.inputField.id = computedId;
52+
this.grid.appendChild(this.inputField);
53+
} else {
54+
const input = createInput({ ...inputProps, id: computedId });
55+
this.grid.appendChild(input);
56+
}
4257
});
43-
this.grid.appendChild(this.typeSelector);
4458
}
4559

4660
protected getGrid(): HTMLDivElement {
@@ -53,31 +67,31 @@ export class DataModelInputDialog extends WorkspaceInputDialog {
5367
}
5468

5569
override get value(): string {
56-
const name = this.inputField.value;
57-
const version = this.versionInput.value;
58-
const type = this.typeSelector.value;
59-
return JSON.stringify({ name, version, type });
70+
const data: Record<string, string> = {};
71+
for (let i = 0; i < this.grid.children.length; i++) {
72+
const child = this.grid.children[i];
73+
if (child instanceof HTMLLabelElement) {
74+
continue;
75+
}
76+
const value = (child as HTMLInputElement | HTMLSelectElement).value;
77+
const id = child.id.slice(child.id.indexOf('-') + 1);
78+
data[id] = value;
79+
}
80+
return JSON.stringify(data);
6081
}
6182
}
6283

63-
export async function getNewDataModelOptions(
64-
props: DataModelInputDialogProps,
84+
export async function getGridInputOptions<T extends readonly InputOptions[]>(
85+
props: GridInputDialogProps<T>,
6586
labelProvider: LabelProvider
66-
): Promise<{ name: string; version: string; type: string } | undefined> {
67-
const userSelection = await new DataModelInputDialog(props, labelProvider).open();
87+
): Promise<FieldValues<T> | undefined> {
88+
const userSelection = await new GridInputDialog(props, labelProvider).open();
6889
if (!userSelection) {
6990
return undefined;
7091
}
7192
return JSON.parse(userSelection);
7293
}
7394

74-
interface InputOptions {
75-
placeholder?: string;
76-
value?: string;
77-
id?: string;
78-
options?: Record<string, string>;
79-
}
80-
8195
function createInput<T extends InputOptions, U = T extends { options: Record<string, string> } ? HTMLSelectElement : HTMLInputElement>(
8296
options: T
8397
): U {
@@ -90,12 +104,6 @@ function createInput<T extends InputOptions, U = T extends { options: Record<str
90104
inputField.placeholder = options.placeholder || '';
91105
inputField.type = 'text';
92106
}
93-
if (options.value) {
94-
inputField.value = options.value;
95-
}
96-
if (options.id) {
97-
inputField.id = options.id;
98-
}
99107
if (options.options) {
100108
Object.entries(options.options).forEach(([key, value]) => {
101109
const option = document.createElement('option');
@@ -104,14 +112,15 @@ function createInput<T extends InputOptions, U = T extends { options: Record<str
104112
inputField.appendChild(option);
105113
});
106114
}
115+
if (options.value) {
116+
inputField.value = options.value;
117+
}
118+
if (options.id) {
119+
inputField.id = options.id;
120+
}
107121
return inputField as U;
108122
}
109123

110-
interface LabelOptions {
111-
text: string;
112-
for: string;
113-
}
114-
115124
function createLabel(options: LabelOptions): HTMLLabelElement {
116125
const label = document.createElement('label');
117126
label.setAttribute('for', options.for);

0 commit comments

Comments
 (0)