@@ -43,18 +43,27 @@ class BotManagerWidget extends LitElement {
4343 spaceTitle : Common . getYjsRoom ( ) ,
4444 } ) ;
4545 const y = await instance . connect ( ) ;
46- var xhr = new XMLHttpRequest ( ) ;
46+ // wait 100ms necessary because the y fields are not immediately available. This seems to be a bug in yjs.
47+ await new Promise ( ( resolve ) => setTimeout ( resolve , 100 ) ) ;
48+
4749 var endpoint = y . getText ( "sbfManager" ) . toString ( ) ;
48- xhr . open ( "GET" , endpoint + "/models/" ) ;
49- xhr . onload = ( ) => {
50- if ( xhr . status == 200 ) {
51- var models ;
52- try {
53- models = JSON . parse ( xhr . response ) ;
54- } catch ( e ) {
55- console . error ( "error while parsing models" , e ) ;
56- return ;
50+
51+ if ( ! endpoint ) {
52+ return ;
53+ }
54+
55+ fetch ( endpoint + "/models/" )
56+ . then ( ( response ) => {
57+ if (
58+ response . ok &&
59+ response . headers . get ( "content-type" ) . includes ( "json" )
60+ ) {
61+ return response . json ( ) ;
62+ } else {
63+ throw new Error ( "Failed to fetch models." ) ;
5764 }
65+ } )
66+ . then ( ( models ) => {
5867 Object . values ( models ) . forEach ( ( model ) => {
5968 if ( ! this . botModels . includes ( model ) ) {
6069 const loadNameInput = document . querySelector ( "#loadNameInput" ) ;
@@ -65,101 +74,114 @@ class BotManagerWidget extends LitElement {
6574 this . botModels . push ( model ) ;
6675 }
6776 } ) ;
68- }
69- } ;
70- xhr . send ( null ) ;
77+ // select the first model if there is no model selected
78+ const loadNameInput = document . querySelector ( "#loadNameInput" ) ;
79+ if ( loadNameInput . value == "" ) {
80+ loadNameInput . value = this . botModels [ 0 ] ;
81+ }
82+ } )
83+ . catch ( ( error ) => {
84+ console . error ( "Error while fetching models:" , error ) ;
85+ } ) ;
7186 }
7287
7388 loadModel ( ) {
74- var name = document . querySelector ( "#storeNameInput " ) . val ( ) ;
89+ const name = $ ( "#loadNameInput " ) . val ( ) ;
7590 var endpoint = window . y . getText ( "sbfManager" ) . toString ( ) ;
7691 var loadStatus = $ ( "#loadStatus" ) ;
7792 const spinner = $ ( "#loadStatusSpinner" ) ;
7893 const btn = $ ( "#load-model" ) ;
7994 $ ( loadStatus ) . text ( "Loading..." ) ;
8095 spinner . show ( ) ;
8196 btn . prop ( "disabled" , true ) ;
97+ fetch ( endpoint + "/models/" + name )
98+ . then ( ( response ) => {
99+ if (
100+ response . ok &&
101+ response . headers . get ( "content-type" ) . includes ( "json" )
102+ ) {
103+ return response . json ( ) ;
104+ } else {
105+ throw new Error ( "The model could not be loaded." ) ;
106+ }
107+ } )
108+ . then ( ( data ) => {
109+ if ( data && name ) {
110+ this . initModel ( data , name ) ;
111+ y . getMap ( "canvas" ) . set ( "ReloadWidgetOperation" , "import" ) ;
112+ alert ( "The model was successfully loaded." ) ;
113+ cleanStatus ( "loadStatus" ) ;
114+ spinner . hide ( ) ;
115+ btn . prop ( "disabled" , false ) ;
116+ } else {
117+ $ ( loadStatus ) . text ( "Loading failed." ) ;
118+ cleanStatus ( "loadStatus" ) ;
119+ }
120+ } )
121+ . catch ( ( error ) => {
122+ alert ( "The model could not be loaded." ) ;
123+ cleanStatus ( "loadStatus" ) ;
124+ spinner . hide ( ) ;
125+ btn . prop ( "disabled" , false ) ;
126+ } ) ;
127+ }
82128
83- var xhr = new XMLHttpRequest ( ) ;
84- xhr . addEventListener ( "load" , ( ) => {
85- alert ( "The model was successfully loaded." ) ;
86- cleanStatus ( "loadStatus" ) ;
87- spinner . hide ( ) ;
88- btn . prop ( "disabled" , false ) ;
89- } ) ;
90- xhr . addEventListener ( "error" , ( ) => {
91- alert ( "The model could not be loaded." ) ;
92- cleanStatus ( "loadStatus" ) ;
93- spinner . hide ( ) ;
94- btn . prop ( "disabled" , false ) ;
95- } ) ;
96- xhr . open ( "GET" , endpoint + "/models/" + name ) ;
97- xhr . responseType = "json" ;
98- xhr . onload = function ( ) {
99- var data = xhr . response ;
100- if ( data && name ) {
101- var initAttributes = function ( attrs , map ) {
102- if ( attrs . hasOwnProperty ( "[attributes]" ) ) {
103- var attr = attrs [ "[attributes]" ] . list ;
104- for ( var key in attr ) {
105- if ( attr . hasOwnProperty ( key ) ) {
106- if ( attr [ key ] . hasOwnProperty ( "key" ) ) {
107- var ytext = map . set ( attr [ key ] . key . id , new YText ( ) ) ;
108- ytext . insert ( 0 , attr [ key ] . key . value ) ;
109- } else {
110- var ytext = map . set ( attr [ key ] . value . id , new YText ( ) ) ;
111- ytext . insert ( 0 , attr [ key ] . value . value ) ;
112- }
113- }
114- }
115- } else {
116- for ( var key in attrs ) {
117- if ( attrs . hasOwnProperty ( key ) ) {
118- var value = attrs [ key ] . value ;
119- if ( ! value . hasOwnProperty ( "option" ) ) {
120- if ( value . value instanceof String ) {
121- var ytext = map . set ( value . id , new YText ( ) ) ;
122- ytext . insert ( 0 , value . value ) ;
123- }
124- }
125- }
126- }
127- }
128- } ;
129- if ( this . guidance . isGuidanceEditor ( ) )
130- y . getMap ( "data" ) . set ( "guidancemodel" , data ) ;
131- else y . getMap ( "data" ) . set ( "model" , data ) ;
132- for ( var key in data . nodes ) {
133- if ( data . nodes . hasOwnProperty ( key ) ) {
134- var entity = data . nodes [ key ] ;
135- var map = y . getMap ( "nodes" ) . set ( key , new YMap ( ) ) ;
136- var attrs = entity . attributes ;
137- if ( entity . hasOwnProperty ( "label" ) ) {
138- var ytext = map . set ( entity . label . value . id , new YText ( ) ) ;
139- ytext . insert ( 0 , entity . label . value . value ) ;
129+ initModel ( data , name ) {
130+ const initAttributes = ( attrs , map ) => {
131+ if ( attrs . hasOwnProperty ( "[attributes]" ) ) {
132+ var attr = attrs [ "[attributes]" ] . list ;
133+ for ( var key in attr ) {
134+ if ( attr . hasOwnProperty ( key ) ) {
135+ if ( attr [ key ] . hasOwnProperty ( "key" ) ) {
136+ var ytext = map . set ( attr [ key ] . key . id , new YText ( ) ) ;
137+ ytext . insert ( 0 , attr [ key ] . key . value ) ;
138+ } else {
139+ var ytext = map . set ( attr [ key ] . value . id , new YText ( ) ) ;
140+ ytext . insert ( 0 , attr [ key ] . value . value ) ;
140141 }
141- initAttributes ( attrs , map ) ;
142142 }
143143 }
144- for ( var key in data . edges ) {
145- if ( data . edges . hasOwnProperty ( key ) ) {
146- var entity = data . edges [ key ] ;
147- var map = y . getMap ( "edges" ) . set ( key , new YMap ( ) ) ;
148- var attrs = entity . attributes ;
149- if ( entity . hasOwnProperty ( "label" ) ) {
150- var ytext = map . set ( entity . label . value . id , new YText ( ) ) ;
151- ytext . insert ( 0 , entity . label . value . value ) ;
144+ } else {
145+ for ( var key in attrs ) {
146+ if ( attrs . hasOwnProperty ( key ) ) {
147+ var value = attrs [ key ] . value ;
148+ if ( ! value . hasOwnProperty ( "option" ) ) {
149+ if ( value . value instanceof String ) {
150+ var ytext = map . set ( value . id , new YText ( ) ) ;
151+ ytext . insert ( 0 , value . value ) ;
152+ }
152153 }
153- initAttributes ( attrs , map ) ;
154154 }
155155 }
156- y . getMap ( "canvas" ) . set ( "ReloadWidgetOperation" , "import" ) ;
157- } else {
158- $ ( loadStatus ) . text ( "Loading failed." ) ;
159- cleanStatus ( "loadStatus" ) ;
160156 }
161157 } ;
162- xhr . send ( null ) ;
158+ if ( this . guidance . isGuidanceEditor ( ) )
159+ y . getMap ( "data" ) . set ( "guidancemodel" , data ) ;
160+ else y . getMap ( "data" ) . set ( "model" , data ) ;
161+ for ( var key in data . nodes ) {
162+ if ( data . nodes . hasOwnProperty ( key ) ) {
163+ var entity = data . nodes [ key ] ;
164+ var map = y . getMap ( "nodes" ) . set ( key , new YMap ( ) ) ;
165+ var attrs = entity . attributes ;
166+ if ( entity . hasOwnProperty ( "label" ) ) {
167+ var ytext = map . set ( entity . label . value . id , new YText ( ) ) ;
168+ ytext . insert ( 0 , entity . label . value . value ) ;
169+ }
170+ initAttributes ( attrs , map ) ;
171+ }
172+ }
173+ for ( var key in data . edges ) {
174+ if ( data . edges . hasOwnProperty ( key ) ) {
175+ var entity = data . edges [ key ] ;
176+ var map = y . getMap ( "edges" ) . set ( key , new YMap ( ) ) ;
177+ var attrs = entity . attributes ;
178+ if ( entity . hasOwnProperty ( "label" ) ) {
179+ var ytext = map . set ( entity . label . value . id , new YText ( ) ) ;
180+ ytext . insert ( 0 , entity . label . value . value ) ;
181+ }
182+ initAttributes ( attrs , map ) ;
183+ }
184+ }
163185 }
164186
165187 submitModel ( ) {
@@ -303,23 +325,32 @@ class BotManagerWidget extends LitElement {
303325 btn . prop ( "disabled" , true ) ;
304326
305327 if ( name && model ) {
306- var xhr = new XMLHttpRequest ( ) ;
307- xhr . onload = function ( ) {
308- if ( xhr . status == 200 ) {
309- alert ( "Your bot model has been successfully backed up" ) ;
310- updateMenu ( ) ;
311- } else {
312- alert (
313- "Your bot model could not be backed up. Make sure that the SBF endpoint is correct. "
314- ) ;
315- }
316- spinner . hide ( ) ;
317- btn . prop ( "disabled" , false ) ;
318- // cleanStatus("storeStatus");
319- } ;
320- xhr . open ( "POST" , endpoint + "/models/" + name ) ;
321- xhr . setRequestHeader ( "Content-Type" , "application/json" ) ;
322- xhr . send ( JSON . stringify ( model ) ) ;
328+ fetch ( endpoint + "/models/" + name , {
329+ method : "POST" ,
330+ headers : {
331+ "Content-Type" : "application/json" ,
332+ } ,
333+ body : JSON . stringify ( model ) ,
334+ } )
335+ . then ( ( response ) => {
336+ if ( response . ok ) {
337+ alert ( "Your bot model has been successfully backed up" ) ;
338+ this . updateMenu ( ) ;
339+ } else {
340+ throw new Error (
341+ "Your bot model could not be backed up. Make sure that the SBF endpoint is correct."
342+ ) ;
343+ }
344+ } )
345+ . catch ( ( error ) => {
346+ alert ( error . message ) ;
347+ } )
348+ . finally ( ( ) => {
349+ spinner . hide ( ) ;
350+ btn . prop ( "disabled" , false ) ;
351+ // cleanStatus("storeStatus");
352+ } ) ;
353+
323354 } else {
324355 if ( ! name ) {
325356 alert ( "The model name is invalid." ) ;
@@ -476,14 +507,13 @@ class BotManagerWidget extends LitElement {
476507 < div class ="input-group mb-3 ">
477508 < div id ="storeNameInput "> </ div >
478509
479- < button id ="store-model " class ="btn btn-outline-primary ">
510+ < button id ="store-model " class ="btn btn-outline-primary " @click =" ${ this . storeModel } " >
480511 < i class ="bi bi-cloud-arrow-up "> </ i > Store
481512 < div
482513 class ="spinner-border spinner-border-sm text-secondary "
483514 id ="storeStatusSpinner "
484515 style ="display: none "
485516 role ="status "
486- @click ="${ this . storeModel } "
487517 >
488518 < span class ="visually-hidden "> Loading...</ span >
489519 </ div >
0 commit comments