Skip to content

Commit 889fbb0

Browse files
committed
handle submit event and pass to slotted <seed-transform-rest>
1 parent 033860c commit 889fbb0

File tree

1 file changed

+66
-4
lines changed

1 file changed

+66
-4
lines changed

src/seed/components/seed-choose-transform-rest.ts

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { html, css, HTMLTemplateResult, CSSResult } from 'lit'
22
import { customElement, property, state } from 'lit/decorators.js'
33
import { DefaultApiFactory, TransformationInfo, XsltParameterDetailsValue } from 'seed-xml-transformer-ts-client/api.ts'
44
import { TransformRESTElement } from './transform-rest.ts'
5+
import { SeedTransformREST } from './seed-transform-rest.ts'
56

67
// define the web component
78
@customElement("seed-choose-transform-rest")
@@ -27,13 +28,24 @@ export class SeedChooseTransformREST extends TransformRESTElement {
2728

2829
protected noPrintTransformationInfo: Array<String> = ["parameterDescriptors"];
2930

31+
@state()
32+
protected _formError: string | null = null;
33+
3034

3135
render(): HTMLTemplateResult {
32-
return html`<div class="transformation-container">${this.renderTransformationChooser()}${this.renderError()}${this.renderTransformationInfo()}</div>`;
36+
return html`<div class="transformation-container"><form @submit="${this.submit}" autocomplete="off">${this.renderTransformationChooser()}${this.renderError()}${this.renderTransformationInfo()}${this.renderSourceForm()}${this.renderSubmit()}</form></div><slot></slot>`;
37+
}
38+
39+
renderSubmit(): HTMLTemplateResult {
40+
if (this._transformation == null) {
41+
return html`<div class="inputfield source"><input type="submit" disabled="true" value="Transform"/></div>`;
42+
} else {
43+
return html`<div class="inputfield source"><input type="submit" value="Transform"/></div>`;
44+
}
3345
}
3446

3547
renderTransformationChooser(): HTMLTemplateResult {
36-
return html`<label for="transformations" @change="${this.notifySelect}">Choose a transformation:<label><select name="transformations" id="transformations">${this._transformations.map(t => html`<option value="${t}">${t}</option>`)}${this.renderTransformationInfo()}</select>`;
48+
return html`<div class="inputfield transformation"><label for="transformation">Transformation ID<label><select name="transformation" id="transformation" @change="${this.transformationSelected}" required><option value="" disabled selected hidden>Please choose...</option>${this._transformations.map(t => html`<option value="${t}">${t}</option>`)}${this.renderTransformationInfo()}</select></div>`;
3749
}
3850

3951
renderError(): HTMLTemplateResult {
@@ -90,15 +102,64 @@ export class SeedChooseTransformREST extends TransformRESTElement {
90102
}
91103
}
92104

105+
renderSourceForm(): HTMLTemplateResult {
106+
return html`<div class="inputfield source"><label for="source">XML Source Document</label><input type="file" id="source" name="source" accept="text/xml, application/xml, text/xhtml"/></div><div class="inputfield systemId"><label for="systemId">URL / System ID</label><input type="text" id="systemId" name="systemId"/></div>${this.renderSourceFormError()}`;
107+
}
93108

109+
renderSourceFormError(): HTMLTemplateResult {
110+
if (this._formError === null) {
111+
return html``;
112+
} else {
113+
return html`<div class="error">${this._formError}</div>`;
114+
}
115+
}
94116

95-
protected async notifySelect(e: Event) {
117+
118+
119+
120+
protected async transformationSelected(e: Event) {
96121
let transformation = (e?.target as HTMLSelectElement)?.value;
97122
console.log("selected", transformation);
98-
// this._transformation = transformation;
123+
this._transformation = transformation;
124+
this._formError = null;
99125
this.collectTransformationInformation(transformation);
100126
}
101127

128+
protected async submit(e: SubmitEvent) {
129+
console.log("form submitted");
130+
// we stop default handling of the submit event, i.e. sending
131+
// form data to the server
132+
// https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault
133+
e.preventDefault();
134+
// get the form data from the shadow dom
135+
const systemId: string = (this.shadowRoot?.querySelector("#systemId") as HTMLInputElement)?.value;
136+
console.log("systemId", systemId);
137+
const files: FileList | null = (this.shadowRoot?.querySelector("#source") as HTMLInputElement)?.files;
138+
console.log("files", files);
139+
140+
// validate input
141+
if ((this._transformationInfo?.requiresSource ?? false) && systemId.length === 0 && files !== null && files[0] === undefined) {
142+
console.log("either URL or file must be entered");
143+
this._formError = "An XML source file or an URL is required.";
144+
} else {
145+
146+
// run the transformation
147+
const slot = this.shadowRoot?.querySelector("slot");
148+
console.log(slot?.assignedElements({flatten: true}));
149+
const transformer: SeedTransformREST | null = slot?.assignedElements({flatten: true})?.[0] as SeedTransformREST;
150+
console.log(transformer);
151+
if (transformer === null) {
152+
console.log("no rest transformer in slotted children");
153+
this._error = "HTML Error: no transformer in slotted children";
154+
} else {
155+
// properties down
156+
transformer["transformation"] = this._transformation;
157+
transformer.href = systemId;
158+
transformer.src = files?.[0] ?? null;
159+
}
160+
}
161+
}
162+
102163
protected async collectTransformationInformation(transformation: string) {
103164
this._transformationInfo = await this.getTransformationInfo(transformation);
104165
this._parameterDetails = await this.getTransformationParameterDescriptors(transformation) ?? {};
@@ -177,6 +238,7 @@ font-style: italic;
177238
.error {
178239
color: red;
179240
}
241+
select:invalid { color: darkgray; }
180242
div.transformation-info-container {
181243
border: 1px solid lightblue;
182244
}

0 commit comments

Comments
 (0)