@@ -2,13 +2,17 @@ import {customElement, html, LitElement, property, TemplateResult} from "lit-ele
2
2
import { get , translate } from "lit-translate" ;
3
3
import { TextFieldBase } from "@material/mwc-textfield/mwc-textfield-base" ;
4
4
5
- import { newLogEvent , newWizardEvent , Wizard , WizardInput } from "../foundation.js" ;
6
- import { OpenSCD } from "../open-scd.js" ;
5
+ import { newLogEvent , newPendingStateEvent , newWizardEvent , Wizard , WizardInput } from "../foundation.js" ;
7
6
8
7
import { CompasChangeSetRadiogroup } from "./CompasChangeSet.js" ;
9
8
import { CompasScltypeRadiogroup } from "./CompasScltypeRadiogroup.js" ;
10
- import { CompasSclDataService } from "./CompasSclDataService.js" ;
11
- import { getTypeFromDocName , stripExtensionFromName , updateDocumentInOpenSCD } from "./foundation.js" ;
9
+ import { CompasSclDataService } from "../compas-services/CompasSclDataService.js" ;
10
+ import {
11
+ getOpenScdElement ,
12
+ getTypeFromDocName ,
13
+ stripExtensionFromName ,
14
+ updateDocumentInOpenSCD
15
+ } from "./foundation.js" ;
12
16
13
17
import './CompasChangeSet.js' ;
14
18
import './CompasScltypeRadiogroup.js' ;
@@ -42,88 +46,97 @@ export class CompasSaveTo extends LitElement {
42
46
return this . getChangeSetRadiogroup ( ) . valid ( ) ;
43
47
}
44
48
45
- render ( ) : TemplateResult {
46
- if ( ! this . docId ) {
47
- return html `
48
- < mwc-textfield dialogInitialFocus id ="name " label ="${ translate ( 'scl.name' ) } "
49
- value ="${ this . docName } " required >
50
- </ mwc-textfield >
49
+ private async getSclDocument ( type : string , id : string ) : Promise < void > {
50
+ await CompasSclDataService ( ) . getSclDocument ( type , id )
51
+ . then ( response => {
52
+ // Copy the SCL Result from the Response and create a new Document from it.
53
+ const sclElement = response . querySelectorAll ( "SCL" ) . item ( 0 ) ;
54
+ const sclDocument = document . implementation . createDocument ( "" , "" , null ) ;
55
+ sclDocument . getRootNode ( ) . appendChild ( sclElement . cloneNode ( true ) ) ;
51
56
52
- < compas-scltype-radiogroup .value ="${ getTypeFromDocName ( this . docName ) } "> </ compas-scltype-radiogroup >
53
- ` ;
54
- }
55
- return html `
56
- < compas-changeset-radiogroup > </ compas-changeset-radiogroup >
57
- ` ;
57
+ updateDocumentInOpenSCD ( sclDocument ) ;
58
+ } ) ;
58
59
}
59
- }
60
-
61
- function getSclDocument ( type : string , id : string ) : void {
62
- CompasSclDataService ( ) . getSclDocument ( type , id )
63
- . then ( response => {
64
- // Copy the SCL Result from the Response and create a new Document from it.
65
- const sclElement = response . querySelectorAll ( "SCL" ) . item ( 0 ) ;
66
- const sclDocument = document . implementation . createDocument ( "" , "" , null ) ;
67
- sclDocument . getRootNode ( ) . appendChild ( sclElement . cloneNode ( true ) ) ;
68
-
69
- updateDocumentInOpenSCD ( sclDocument ) ;
70
- } ) ;
71
- }
72
60
73
- function addSclToCompas ( wizard : Element , compasSaveTo : CompasSaveTo , doc : XMLDocument ) {
74
- const name = stripExtensionFromName ( compasSaveTo . getNameField ( ) . value ) ;
75
- const docType = compasSaveTo . getSclTypeRadioGroup ( ) . getSelectedValue ( ) ?? '' ;
61
+ private async addSclToCompas ( wizard : Element , doc : XMLDocument ) : Promise < void > {
62
+ const name = stripExtensionFromName ( this . getNameField ( ) . value ) ;
63
+ const docType = this . getSclTypeRadioGroup ( ) . getSelectedValue ( ) ?? '' ;
76
64
77
- CompasSclDataService ( ) . addSclDocument ( docType , { sclName : name , doc : doc } )
78
- . then ( xmlResponse => {
79
- const id = Array . from ( xmlResponse . querySelectorAll ( '*|Id' ) ?? [ ] ) [ 0 ]
65
+ await CompasSclDataService ( ) . addSclDocument ( docType , { sclName : name , doc : doc } )
66
+ . then ( xmlResponse => {
67
+ const id = Array . from ( xmlResponse . querySelectorAll ( '*|Id' ) ?? [ ] ) [ 0 ]
80
68
81
- // Retrieve the document to fetch server-side updates.
82
- getSclDocument ( docType , id . textContent ?? '' ) ;
69
+ // Retrieve the document to fetch server-side updates.
70
+ this . getSclDocument ( docType , id . textContent ?? '' ) ;
83
71
84
- const openScd = < OpenSCD > document . querySelector ( 'open-scd' ) ;
85
- openScd . dispatchEvent (
72
+ const openScd = getOpenScdElement ( ) ;
73
+ openScd . dispatchEvent (
86
74
newLogEvent ( {
87
75
kind : 'info' ,
88
- title : get ( 'compas.saveTo.addSuccess' ) } ) ) ;
89
-
90
- // Close the Save Dialog.
91
- openScd . dispatchEvent ( newWizardEvent ( ) ) ;
92
- } )
93
- . catch ( ( ) => {
94
- const openScd = < OpenSCD > document . querySelector ( 'open-scd' ) ;
95
- openScd . dispatchEvent (
76
+ title : get ( 'compas.saveTo.addSuccess' )
77
+ } ) ) ;
78
+
79
+ // Close the Save Dialog.
80
+ openScd . dispatchEvent ( newWizardEvent ( ) ) ;
81
+ } ) . catch ( ( ) => {
82
+ const openScd = getOpenScdElement ( ) ;
83
+ openScd . dispatchEvent (
96
84
newLogEvent ( {
97
85
kind : 'error' ,
98
- title : get ( 'compas.saveTo.addError' ) } ) ) ;
86
+ title : get ( 'compas.saveTo.addError' )
87
+ } ) ) ;
99
88
} ) ;
100
- }
89
+ }
101
90
102
- function updateSclInCompas ( wizard : Element , compasSaveTo : CompasSaveTo , docId : string , docName : string , doc : XMLDocument ) {
103
- const changeSet = compasSaveTo . getChangeSetRadiogroup ( ) . getSelectedValue ( ) ;
104
- const docType = getTypeFromDocName ( docName ) ;
91
+ private async updateSclInCompas ( wizard : Element , docId : string , docName : string , doc : XMLDocument ) : Promise < void > {
92
+ const changeSet = this . getChangeSetRadiogroup ( ) . getSelectedValue ( ) ;
93
+ const docType = getTypeFromDocName ( docName ) ;
105
94
106
- CompasSclDataService ( ) . updateSclDocument ( docType . toUpperCase ( ) , docId , { changeSet : changeSet ! , doc : doc } )
107
- . then ( ( ) => {
108
- // Retrieve the document to fetch server-side updates.
109
- getSclDocument ( docType , docId ) ;
95
+ await CompasSclDataService ( ) . updateSclDocument ( docType . toUpperCase ( ) , docId , { changeSet : changeSet ! , doc : doc } )
96
+ . then ( ( ) => {
97
+ // Retrieve the document to fetch server-side updates.
98
+ this . getSclDocument ( docType , docId ) ;
110
99
111
- const openScd = < OpenSCD > document . querySelector ( 'open-scd' ) ;
112
- openScd . dispatchEvent (
100
+ const openScd = getOpenScdElement ( ) ;
101
+ openScd . dispatchEvent (
113
102
newLogEvent ( {
114
103
kind : 'info' ,
115
104
title : get ( 'compas.saveTo.updateSuccess' ) } ) ) ;
116
105
117
- // Close the Save Dialog.
118
- openScd . dispatchEvent ( newWizardEvent ( ) ) ;
119
- } )
120
- . catch ( ( ) => {
121
- const openScd = < OpenSCD > document . querySelector ( 'open-scd' ) ;
122
- openScd . dispatchEvent (
106
+ // Close the Save Dialog.
107
+ openScd . dispatchEvent ( newWizardEvent ( ) ) ;
108
+ } ) . catch ( ( ) => {
109
+ const openScd = getOpenScdElement ( ) ;
110
+ openScd . dispatchEvent (
123
111
newLogEvent ( {
124
112
kind : 'error' ,
125
113
title : get ( 'compas.saveTo.updateError' ) } ) ) ;
126
- } ) ;
114
+ } ) ;
115
+ }
116
+
117
+ async saveToCompas ( wizard : Element , docId : string , docName : string , doc : XMLDocument ) : Promise < void > {
118
+ if ( ! docId ) {
119
+ await this . addSclToCompas ( wizard , doc ) ;
120
+ } else {
121
+ await this . updateSclInCompas ( wizard , docId , docName , doc ) ;
122
+ }
123
+ }
124
+
125
+ render ( ) : TemplateResult {
126
+ if ( ! this . docId ) {
127
+ return html `
128
+ < mwc-textfield dialogInitialFocus id ="name " label ="${ translate ( 'scl.name' ) } "
129
+ value ="${ this . docName } " required >
130
+ </ mwc-textfield >
131
+
132
+ < compas-scltype-radiogroup .value ="${ getTypeFromDocName ( this . docName ) } "> </ compas-scltype-radiogroup >
133
+ ` ;
134
+ }
135
+
136
+ return html `
137
+ < compas-changeset-radiogroup > </ compas-changeset-radiogroup >
138
+ ` ;
139
+ }
127
140
}
128
141
129
142
function saveToCompas ( docId : string , docName : string , doc : XMLDocument ) {
@@ -133,11 +146,7 @@ function saveToCompas(docId: string, docName: string, doc: XMLDocument) {
133
146
return [ ] ;
134
147
}
135
148
136
- if ( ! docId ) {
137
- addSclToCompas ( wizard , compasSaveTo , doc ) ;
138
- } else {
139
- updateSclInCompas ( wizard , compasSaveTo , docId , docName , doc ) ;
140
- }
149
+ getOpenScdElement ( ) . dispatchEvent ( newPendingStateEvent ( compasSaveTo . saveToCompas ( wizard , docId , docName , doc ) ) ) ;
141
150
return [ ] ;
142
151
} ;
143
152
}
0 commit comments