11import {
22 execPythonMessage ,
3- evalPythonMessage
3+ evalPythonMessage ,
44} from '@geppettoengine/geppetto-client/js/communication/geppettoJupyter/GeppettoJupyterUtils' ;
55import React from 'react' ;
66
7-
87const Utils = {
98
10- getAvailableKey : function ( model , prefix ) {
9+ getAvailableKey ( model , prefix ) {
1110 if ( model == undefined ) {
1211 return prefix ;
1312 }
1413 // Get New Available ID
15- var id = prefix ;
14+ let id = prefix ;
1615 if ( model [ id + 0 ] == undefined ) {
17- return id + 0
16+ return id + 0 ;
1817 }
19- var i = 0 ;
20- id = prefix + i ++
18+ let i = 0 ;
19+ id = prefix + i ++ ;
2120 while ( model [ id ] != undefined ) {
2221 id = prefix + i ++ ;
2322 }
2423 return id ;
2524 } ,
2625
27- getMetadataField : function ( key , field ) {
26+ getMetadataField ( key , field ) {
2827 if ( key == undefined ) {
2928 return ;
3029 }
31- var currentObject ;
32- var nextObject = window . metadata ;
33- key . split ( '.' ) . forEach ( item => {
34- if ( item in nextObject ) {
35- currentObject = nextObject [ item ] ;
36- if ( "children" in currentObject ) {
37- nextObject = currentObject [ "children" ] ;
30+ let currentObject ;
31+ let nextObject = window . metadata ;
32+ key . split ( '.' )
33+ . forEach ( ( item ) => {
34+ if ( item in nextObject ) {
35+ currentObject = nextObject [ item ] ;
36+ if ( 'children' in currentObject ) {
37+ nextObject = currentObject . children ;
38+ }
39+ } else {
40+ currentObject = undefined ;
3841 }
39- } else {
40- currentObject = undefined ;
41- return ;
42- }
43- } ) ;
42+ } ) ;
4443 return ( currentObject == undefined ) ? currentObject : currentObject [ field ] ;
4544 } ,
4645
47- getHTMLType : function ( key ) {
48- var type = this . getMetadataField ( key , " type" )
46+ getHTMLType ( key ) {
47+ const type = this . getMetadataField ( key , ' type' ) ;
4948
5049 switch ( type ) {
51- case " int" :
52- var htmlType = " number"
53- break ;
54- default :
55- var htmlType = " text"
56- break ;
50+ case ' int' :
51+ var htmlType = ' number' ;
52+ break ;
53+ default :
54+ var htmlType = ' text' ;
55+ break ;
5756 }
5857 return htmlType ;
59-
6058 } ,
6159
62- isObject : function ( item ) {
60+ isObject ( item ) {
6361 return ( item && typeof item === 'object' && ! Array . isArray ( item ) ) ;
6462 } ,
6563
66- mergeDeep : function ( target , source ) {
67- let output = Object . assign ( { } , target ) ;
64+ mergeDeep ( target , source ) {
65+ const output = { ... target } ;
6866 if ( this . isObject ( target ) && this . isObject ( source ) ) {
69- Object . keys ( source ) . forEach ( key => {
70- if ( this . isObject ( source [ key ] ) ) {
71- if ( ! ( key in target ) ) {
72- Object . assign ( output , { [ key ] : source [ key ] } ) ;
67+ Object . keys ( source )
68+ . forEach ( ( key ) => {
69+ if ( this . isObject ( source [ key ] ) ) {
70+ if ( ! ( key in target ) ) {
71+ Object . assign ( output , { [ key ] : source [ key ] } ) ;
72+ } else {
73+ output [ key ] = this . mergeDeep ( target [ key ] , source [ key ] ) ;
74+ }
7375 } else {
74- output [ key ] = this . mergeDeep ( target [ key ] , source [ key ] ) ;
76+ Object . assign ( output , { [ key ] : source [ key ] } ) ;
7577 }
76- } else {
77- Object . assign ( output , { [ key ] : source [ key ] } ) ;
78- }
79- } ) ;
78+ } ) ;
8079 }
8180 return output ;
8281 } ,
8382
84- getFieldsFromMetadataTree : function ( tree , callback ) {
83+ getFieldsFromMetadataTree ( tree , callback ) {
8584 function iterate ( object , path ) {
8685 if ( Array . isArray ( object ) ) {
87- object . forEach ( function ( a , i ) {
86+ object . forEach ( ( a , i ) => {
8887 iterate ( a , path . concat ( i ) ) ;
8988 } ) ;
9089 return ;
9190 }
9291 if ( object !== null && typeof object === 'object' ) {
93- Object . keys ( object ) . forEach ( function ( k ) {
94- // Don't add the leaf to path
95- iterate ( object [ k ] , ( typeof object [ k ] === 'object' ) ? path . concat ( k ) : path ) ;
96-
97- } ) ;
92+ Object . keys ( object )
93+ . forEach ( ( k ) => {
94+ // Don't add the leaf to path
95+ iterate ( object [ k ] , ( typeof object [ k ] === 'object' ) ? path . concat ( k ) : path ) ;
96+ } ) ;
9897 return ;
9998 }
10099
101100 // Push to array of field id. Remove children and create id string
102- modelFieldsIds . push ( path . filter ( path => path != 'children' ) . join ( '.' ) ) ;
101+ modelFieldsIds . push ( path . filter ( ( path ) => path != 'children' )
102+ . join ( '.' ) ) ;
103103 }
104104
105105 // Iterate the array extracting the fields Ids
106106 var modelFieldsIds = [ ] ;
107107 iterate ( tree , [ ] ) ;
108108
109109 // Generate model fields based on ids
110- var modelFields = [ ] ;
111- modelFieldsIds . filter ( ( v , i , a ) => a . indexOf ( v ) === i ) . map ( id => modelFields . push ( callback ( id , 0 ) ) )
110+ const modelFields = [ ] ;
111+ modelFieldsIds . filter ( ( v , i , a ) => a . indexOf ( v ) === i )
112+ . map ( ( id ) => modelFields . push ( callback ( id , 0 ) ) ) ;
112113 return modelFields ;
113114 } ,
114115
115116 renameKey ( path , oldValue , newValue , callback ) {
116- this . execPythonMessage ( ' netpyne_geppetto.rename("' + path + ' ","' + oldValue + ' ","' + newValue + '")' )
117- . then ( response => {
117+ this . execPythonMessage ( ` netpyne_geppetto.rename("${ path } ","${ oldValue } ","${ newValue } ")` )
118+ . then ( ( response ) => {
118119 callback ( response , newValue ) ;
119- } )
120+ } ) ;
120121 } ,
121122
122123 nameValidation ( name ) {
123124 // Remove spaces
124125 if ( ( / \s / . test ( name ) ) ) {
125- name = name . replace ( / \s + / g, "" ) . replace ( / ^ \d + / g, "" ) ;
126+ name = name . replace ( / \s + / g, '' )
127+ . replace ( / ^ \d + / g, '' ) ;
126128 } else if ( ( / ^ [ 0 - 9 ] / . test ( name ) ) ) { // Remove number at the beginning
127- name = name . replace ( / \s + / g, "" ) . replace ( / ^ \d + / g, "" ) ;
129+ name = name . replace ( / \s + / g, '' )
130+ . replace ( / ^ \d + / g, '' ) ;
128131 }
129132 return name ;
130-
131133 } ,
132134
133135 // FIXME: Hack to remove scaped chars (\\ -> \ and \' -> ') manually
134- convertToJSON ( data ) {
135- if ( typeof data === 'string' || data instanceof String ) {
136- return JSON . parse ( data . replace ( / \\ \\ / g, '\\' ) . replace ( / \\ ' / g, '\'' ) )
136+ convertToJSON ( data ) {
137+ if ( typeof data === 'string' || data instanceof String ) {
138+ return JSON . parse ( data . replace ( / \\ \\ / g, '\\' )
139+ . replace ( / \\ ' / g, '\'' ) ) ;
137140 }
138- return data
141+ return data ;
139142 } ,
140143
141144 getPlainStackTrace ( stackTrace ) {
142145 return stackTrace . replace ( / \u001b \[ .* ?m / g, '' ) ;
143146 } ,
144147
145148 getErrorResponse ( data ) {
146- var parsedData = this . convertToJSON ( data )
147- if ( parsedData . type && parsedData [ ' type' ] == 'ERROR' ) {
148- const error = { details : parsedData [ ' details' ] }
149- if ( Object . prototype . hasOwnProperty . call ( parsedData , " message" ) ) {
150- error [ " message" ] = parsedData [ ' message' ]
151- } else if ( Object . prototype . hasOwnProperty . call ( parsedData , " websocket" ) ) {
152- error [ " message" ] = parsedData [ ' websocket' ]
149+ const parsedData = this . convertToJSON ( data ) ;
150+ if ( parsedData . type && parsedData . type == 'ERROR' ) {
151+ const error = { details : parsedData . details } ;
152+ if ( Object . prototype . hasOwnProperty . call ( parsedData , ' message' ) ) {
153+ error . message = parsedData . message ;
154+ } else if ( Object . prototype . hasOwnProperty . call ( parsedData , ' websocket' ) ) {
155+ error . message = parsedData . websocket ;
153156 }
154- return error
157+ return error ;
155158 }
156159 return null ;
157160 } ,
@@ -164,34 +167,33 @@ const Utils = {
164167 */
165168 context . setState ( {
166169 currentName : newValue ,
167- errorMessage : " Error" ,
168- errorDetails : " Leading digits or whitespaces are not allowed in " + componentName + " names."
170+ errorMessage : ' Error' ,
171+ errorDetails : ` Leading digits or whitespaces are not allowed in ${ componentName } names.` ,
169172 } ) ;
170173 return true ;
171- } else if ( ( updateCondition ) && ( newValue == originalValue ) ) {
174+ } if ( ( updateCondition ) && ( newValue == originalValue ) ) {
172175 context . setState ( { currentName : newValue } ) ;
173176 return true ;
174- } else if ( ! ( updateCondition ) && ( newValue == originalValue ) ) {
177+ } if ( ! ( updateCondition ) && ( newValue == originalValue ) ) {
175178 context . setState ( {
176179 currentName : newValue ,
177- errorMessage : " Error" ,
178- errorDetails : " Name collision detected, the name " + newValue
179- + " is already used in this model, please pick another name."
180+ errorMessage : ' Error' ,
181+ errorDetails : ` Name collision detected, the name ${ newValue
182+ } is already used in this model, please pick another name.` ,
180183 } ) ;
181184 return false ;
182- } else if ( ! ( updateCondition ) && ( newValue != originalValue ) ) {
185+ } if ( ! ( updateCondition ) && ( newValue != originalValue ) ) {
183186 context . setState ( {
184187 currentName : newValue ,
185- errorMessage : " Error" ,
186- errorDetails : " Leading digits or whitespaces are not allowed in " + componentName + " names."
188+ errorMessage : ' Error' ,
189+ errorDetails : ` Leading digits or whitespaces are not allowed in ${ componentName } names.` ,
187190 } ) ;
188191 return false ;
189192 }
190193 } ,
191194
192- execPythonMessage : execPythonMessage ,
193- evalPythonMessage : evalPythonMessage
194- }
195-
195+ execPythonMessage,
196+ evalPythonMessage,
197+ } ;
196198
197- export default Utils
199+ export default Utils ;
0 commit comments