@@ -2,6 +2,7 @@ import { html, css, HTMLTemplateResult, CSSResult } from 'lit'
2
2
import { customElement , property , state } from 'lit/decorators.js'
3
3
import { DefaultApiFactory , TransformationInfo , XsltParameterDetailsValue } from 'seed-xml-transformer-ts-client/api.ts'
4
4
import { TransformRESTElement } from './transform-rest.ts'
5
+ import { SeedTransformREST } from './seed-transform-rest.ts'
5
6
6
7
// define the web component
7
8
@customElement ( "seed-choose-transform-rest" )
@@ -27,13 +28,24 @@ export class SeedChooseTransformREST extends TransformRESTElement {
27
28
28
29
protected noPrintTransformationInfo : Array < String > = [ "parameterDescriptors" ] ;
29
30
31
+ @state ( )
32
+ protected _formError : string | null = null ;
33
+
30
34
31
35
render ( ) : HTMLTemplateResult {
32
- return html `<div class= "transformation-container" > ${ this . renderTransformationChooser ( ) } ${ this . renderError ( ) } ${ this . renderTransformationInfo ( ) } </ div> ` ;
36
+ return html `<div class= "transformation-container" > <for m @submit = "${ this . submit } " autocomplete = "off" > ${ this . renderTransformationChooser ( ) } ${ this . renderError ( ) } ${ this . renderTransformationInfo ( ) } ${ this . renderSourceForm ( ) } ${ this . renderSubmit ( ) } </ for m> </ 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
+ }
33
45
}
34
46
35
47
renderTransformationChooser ( ) : HTMLTemplateResult {
36
- return html `<label f or = "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 f or = "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 > ` ;
37
49
}
38
50
39
51
renderError ( ) : HTMLTemplateResult {
@@ -90,15 +102,64 @@ export class SeedChooseTransformREST extends TransformRESTElement {
90
102
}
91
103
}
92
104
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
+ }
93
108
109
+ renderSourceFormError ( ) : HTMLTemplateResult {
110
+ if ( this . _formError === null ) {
111
+ return html `` ;
112
+ } else {
113
+ return html `<div class= "error" > ${ this . _formError } </ div> ` ;
114
+ }
115
+ }
94
116
95
- protected async notifySelect ( e : Event ) {
117
+
118
+
119
+
120
+ protected async transformationSelected ( e : Event ) {
96
121
let transformation = ( e ?. target as HTMLSelectElement ) ?. value ;
97
122
console . log ( "selected" , transformation ) ;
98
- // this._transformation = transformation;
123
+ this . _transformation = transformation ;
124
+ this . _formError = null ;
99
125
this . collectTransformationInformation ( transformation ) ;
100
126
}
101
127
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
+
102
163
protected async collectTransformationInformation ( transformation : string ) {
103
164
this . _transformationInfo = await this . getTransformationInfo ( transformation ) ;
104
165
this . _parameterDetails = await this . getTransformationParameterDescriptors ( transformation ) ?? { } ;
@@ -177,6 +238,7 @@ font-style: italic;
177
238
.error {
178
239
color : red;
179
240
}
241
+ select : invalid { color : darkgray; }
180
242
div .transformation-info-container {
181
243
border : 1px solid lightblue;
182
244
}
0 commit comments