@@ -19,51 +19,25 @@ function promptCancel() {
19
19
}
20
20
21
21
function promptSubmit ( ) {
22
- let data = null ;
23
-
24
- if ( promptOptions . fields ) {
25
- data = { } ;
26
- promptOptions . fields . forEach ( field => {
27
- const inputElement = document . getElementById ( field . id ) ;
28
- data [ field . id ] = inputElement . value ;
29
- } ) ;
30
-
31
- } else {
32
- const dataElement = document . querySelector ( '#data' ) ;
33
-
34
- if ( promptOptions . type === 'input' ) {
35
- data = dataElement . value ;
36
- } else if ( promptOptions . type === 'select' ) {
37
- if ( promptOptions . selectMultiple ) {
38
- data = dataElement . querySelectorAll ( 'option[selected]' ) . map ( o => o . getAttribute ( 'value' ) ) ;
39
- } else {
40
- data = dataElement . value ;
41
- }
42
- }
43
- }
44
-
22
+ const dataElement = document . querySelector ( '#data' ) ;
23
+ const data = dataElement . value ;
45
24
window . api . ipc . sendSync ( 'prompt-post-data:' + promptId , data ) ;
46
25
}
47
26
48
- function promptCreateInput ( promptOptions ) {
49
- const dataElement = document . createElement ( 'input' ) ;
50
- dataElement . setAttribute ( 'type' , 'text' ) ;
27
+ function promptRegister ( ) {
28
+ promptId = document . location . hash . replace ( '#' , '' ) ;
51
29
52
- if ( promptOptions . value ) {
53
- dataElement . value = promptOptions . value ;
54
- } else {
55
- dataElement . value = '' ;
30
+ try {
31
+ promptOptions = JSON . parse ( window . api . ipc . sendSync ( 'prompt-get-options:' + promptId ) ) ;
32
+ } catch ( error ) {
33
+ return promptError ( error ) ;
56
34
}
57
35
58
- if ( promptOptions . inputAttrs && typeof ( promptOptions . inputAttrs ) === 'object' ) {
59
- for ( const k in promptOptions . inputAttrs ) {
60
- if ( ! Object . prototype . hasOwnProperty . call ( promptOptions . inputAttrs , k ) ) {
61
- continue ;
62
- }
36
+ document . querySelector ( '#form' ) . addEventListener ( 'submit' , promptSubmit ) ;
37
+ document . querySelector ( '#cancel' ) . addEventListener ( 'click' , promptCancel ) ;
63
38
64
- dataElement . setAttribute ( k , promptOptions . inputAttrs [ k ] ) ;
65
- }
66
- }
39
+ const dataElement = document . querySelector ( '#data' ) ;
40
+ dataElement . value = promptOptions . value ? promptOptions . value : '' ;
67
41
68
42
dataElement . addEventListener ( 'keyup' , event => {
69
43
if ( event . key === 'Escape' ) {
@@ -78,114 +52,17 @@ function promptCreateInput(promptOptions) {
78
52
}
79
53
} ) ;
80
54
81
- return dataElement ;
82
- }
83
-
84
- function promptCreateSelect ( ) {
85
- const dataElement = document . createElement ( 'select' ) ;
86
- let optionElement ;
87
-
88
- for ( const k in promptOptions . selectOptions ) {
89
- if ( ! Object . prototype . hasOwnProperty . call ( promptOptions . selectOptions , k ) ) {
90
- continue ;
91
- }
92
-
93
- optionElement = document . createElement ( 'option' ) ;
94
- optionElement . setAttribute ( 'value' , k ) ;
95
- optionElement . textContent = promptOptions . selectOptions [ k ] ;
96
- if ( k === promptOptions . value ) {
97
- optionElement . setAttribute ( 'selected' , 'selected' ) ;
98
- }
99
-
100
- dataElement . append ( optionElement ) ;
101
- }
102
-
103
- return dataElement ;
104
- }
105
-
106
- function promptRegister ( ) {
107
- promptId = document . location . hash . replace ( '#' , '' ) ;
108
-
109
- try {
110
- promptOptions = JSON . parse ( window . api . ipc . sendSync ( 'prompt-get-options:' + promptId ) ) ;
111
- } catch ( error ) {
112
- return promptError ( error ) ;
113
- }
55
+ dataElement . focus ( ) ;
56
+ dataElement . select ( ) ;
114
57
115
- const labelContainer = document . querySelector ( '#label' ) ;
116
- if ( labelContainer ) {
117
- if ( promptOptions . useHtmlLabel ) {
118
- labelContainer . innerHTML = promptOptions . label ;
119
- } else {
120
- labelContainer . textContent = promptOptions . label ;
121
- }
122
- }
123
-
124
- if ( promptOptions . buttonLabels && promptOptions . buttonLabels . ok ) {
125
- document . querySelector ( '#ok' ) . textContent = promptOptions . buttonLabels . ok ;
126
- }
127
-
128
- if ( promptOptions . buttonLabels && promptOptions . buttonLabels . cancel ) {
129
- document . querySelector ( '#cancel' ) . textContent = promptOptions . buttonLabels . cancel ;
130
- }
131
-
132
- document . querySelector ( '#form' ) . addEventListener ( 'submit' , promptSubmit ) ;
133
- document . querySelector ( '#cancel' ) . addEventListener ( 'click' , promptCancel ) ;
58
+ window . api . i18n . ready . then ( ( ) => {
59
+ document . querySelector ( '#ok' ) . textContent = window . api . i18n . t ( 'dialog-button-ok' ) ;
60
+ document . querySelector ( '#cancel' ) . textContent = window . api . i18n . t ( 'dialog-button-cancel' ) ;
61
+ document . querySelector ( '#label' ) . textContent = window . api . i18n . t ( 'dialog-passphrase-prompt-label' ) ;
134
62
135
- if ( promptOptions . fields ) {
136
- const formElement = document . querySelector ( '#form' ) ;
137
- const buttonContainer = document . querySelector ( '#buttons' ) ;
138
-
139
- promptOptions . fields . forEach ( field => {
140
- if ( field . label ) {
141
- const labelContainer = document . createElement ( 'div' ) ;
142
- labelContainer . setAttribute ( 'class' , 'label' ) ;
143
- labelContainer . innerHTML = field . label ;
144
- formElement . insertBefore ( labelContainer , buttonContainer ) ;
145
- }
146
-
147
- const dataElement = createDataElement ( field ) ;
148
- if ( dataElement ) {
149
- dataElement . setAttribute ( 'id' , field . id ) ;
150
- dataElement . setAttribute ( 'class' , 'data' ) ;
151
- const dataContainer = document . createElement ( 'div' ) ;
152
- dataContainer . setAttribute ( 'class' , 'data-container' ) ;
153
- dataContainer . append ( dataElement ) ;
154
- formElement . insertBefore ( dataContainer , buttonContainer ) ;
155
- }
156
- } ) ;
157
-
158
- } else {
159
- const dataContainerElement = document . querySelector ( '#data-container' ) ;
160
-
161
- const dataElement = createDataElement ( promptOptions ) ;
162
- if ( ! dataElement ) {
163
- return promptError ( `Unhandled input type '${ promptOptions . type } '` ) ;
164
- }
165
-
166
- dataContainerElement . append ( dataElement ) ;
167
- dataElement . setAttribute ( 'id' , 'data' ) ;
168
-
169
- dataElement . focus ( ) ;
170
- if ( promptOptions . type === 'input' ) {
171
- dataElement . select ( ) ;
172
- }
173
- }
174
-
175
- const height = document . querySelector ( 'body' ) . offsetHeight ;
176
- window . api . ipc . sendSync ( 'prompt-size:' + promptId , height ) ;
177
- }
178
-
179
- function createDataElement ( promptOptions ) {
180
- let dataElement ;
181
- if ( promptOptions . type === 'input' ) {
182
- dataElement = promptCreateInput ( promptOptions ) ;
183
- } else if ( promptOptions . type === 'select' ) {
184
- dataElement = promptCreateSelect ( ) ;
185
- } else {
186
- dataElement = null ;
187
- }
188
- return dataElement ;
63
+ const height = document . querySelector ( 'body' ) . offsetHeight ;
64
+ window . api . ipc . sendSync ( 'prompt-size:' + promptId , height ) ;
65
+ } ) ;
189
66
}
190
67
191
68
window . addEventListener ( 'error' , error => {
0 commit comments