1
1
export const crel = ( tagName , attributes = null , children = [ ] ) => {
2
2
const dom = document . createElement ( tagName )
3
+ dom . append ( ...children )
3
4
if ( attributes ) {
4
5
for ( const [ name , value ] of Object . entries ( attributes ) ) {
5
6
if ( / ^ d a t a - | ^ a r i a - | ^ r o l e / . test ( name ) ) dom . setAttribute ( name , value )
6
7
else dom [ name ] = value
7
8
}
8
9
}
9
- dom . append ( ...children )
10
10
return dom
11
11
}
12
12
13
13
export const gettext = window . gettext || ( ( s ) => s )
14
14
15
- const formFieldForProperty = ( [ name , config ] ) => {
15
+ const formFieldForProperty = ( name , config , value ) => {
16
+ const label = crel ( "label" , { textContent : config . title || name } )
16
17
let widget
17
18
19
+ if ( config . type === "boolean" ) {
20
+ return crel ( "label" , { } , [
21
+ crel ( "input" , { name, type : "checkbox" , checked : ! ! value } ) ,
22
+ config . title || name ,
23
+ ] )
24
+ }
18
25
if ( config . format === "textarea" ) {
19
- widget = crel ( "textarea" , { name, cols : 80 , rows : 30 } )
26
+ widget = crel ( "textarea" , { name, value , cols : 80 , rows : 30 } )
20
27
} else if ( config . enum ) {
21
28
widget = crel (
22
29
"select" ,
23
- { name } ,
24
- config . enum . map ( ( value ) => crel ( "option" , { textContent : value } ) ) ,
30
+ { name, value } ,
31
+ config . enum . map ( ( option ) => crel ( "option" , { textContent : option } ) ) ,
25
32
)
26
33
} else {
27
34
// Create input with appropriate attributes
28
35
const attrs = {
29
36
name,
37
+ value,
30
38
type : config . format || "text" ,
31
39
size : 50 ,
32
40
}
@@ -39,10 +47,14 @@ const formFieldForProperty = ([name, config]) => {
39
47
widget = crel ( "input" , attrs )
40
48
}
41
49
42
- return crel ( "p" , { } , [
43
- crel ( "label" , { textContent : config . title || name } ) ,
44
- widget ,
45
- ] )
50
+ return crel ( "p" , { } , [ label , widget ] )
51
+ }
52
+
53
+ const valueForFormField = ( name , config , form ) => {
54
+ if ( config . type === "boolean" ) {
55
+ return ! ! form [ name ] . checked
56
+ }
57
+ return form [ name ] . value
46
58
}
47
59
48
60
export const updateAttrsDialog =
@@ -74,8 +86,8 @@ export const updateAttrsDialog =
74
86
75
87
// Add form fields with dynamic enum support
76
88
formElements . push (
77
- ...Object . entries ( properties ) . map ( ( entry ) =>
78
- formFieldForProperty ( entry ) ,
89
+ ...Object . entries ( properties ) . map ( ( [ name , config ] ) =>
90
+ formFieldForProperty ( name , config , attrs [ name ] ) ,
79
91
) ,
80
92
)
81
93
@@ -92,10 +104,6 @@ export const updateAttrsDialog =
92
104
const dialog = div . querySelector ( "dialog" )
93
105
const form = div . querySelector ( "form" )
94
106
95
- for ( const [ name , config ] of Object . entries ( properties ) ) {
96
- form [ name ] . value = attrs [ name ] || config . default || ""
97
- }
98
-
99
107
cancel . addEventListener ( "click" , ( ) => {
100
108
dialog . close ( )
101
109
} )
@@ -109,7 +117,10 @@ export const updateAttrsDialog =
109
117
div . remove ( )
110
118
resolve (
111
119
Object . fromEntries (
112
- Object . keys ( properties ) . map ( ( name ) => [ name , form [ name ] . value ] ) ,
120
+ Object . entries ( properties ) . map ( ( [ name , config ] ) => [
121
+ name ,
122
+ valueForFormField ( name , config , form ) ,
123
+ ] ) ,
113
124
) ,
114
125
)
115
126
}
0 commit comments