@@ -9,66 +9,72 @@ export class SignatureComponent extends BaseComponent {
9
9
const wrapper = document . createElement ( "div" ) ;
10
10
wrapper . classList . add ( "signature-pad" ) ;
11
11
12
- const field = document . createElement ( "div" ) ;
13
- field . classList . add ( " field" ) ;
12
+ const field = U . createField ( this . config , [ ] , true ) ;
13
+ wrapper . appendChild ( field ) ;
14
14
15
15
const control = document . createElement ( "div" ) ;
16
16
control . classList . add ( "control" ) ;
17
17
18
- field . appendChild ( control ) ;
19
-
20
- const label = document . createElement ( "div" ) ;
21
- label . classList . add ( "label" ) ;
22
- if ( this . config . labelsmall ) {
23
- label . classList . add ( "label-smaller" ) ;
24
- }
25
- label . innerText = this . config . label + U . requiredMark ( this . config ) ;
26
- control . appendChild ( label ) ;
27
-
28
- const tooltip = U . tooltipHint ( this . config ) ;
29
- control . insertAdjacentHTML ( 'beforeend' , tooltip ) ;
30
-
31
-
32
-
18
+ // actual input field to be submitted with the form
33
19
const input = document . createElement ( "input" ) ;
34
20
input . type = "hidden" ;
35
21
input . name = this . name ;
36
22
control . appendChild ( input ) ;
37
23
38
- const canvas = document . createElement ( "canvas" ) ;
39
- control . appendChild ( canvas ) ;
40
- wrapper . appendChild ( field ) ;
24
+ field . appendChild ( control ) ;
41
25
26
+ // dynamic canvas to draw on
27
+ const canvas = document . createElement ( "canvas" ) ;
42
28
const signaturePad = new SignaturePad ( canvas ) ;
43
- if ( this . myState . value ) {
44
- signaturePad . fromDataURL ( this . myState . value ) ;
45
- }
46
29
// default input event is of no use here
47
30
signaturePad . addEventListener ( "endStroke" , ( ) => {
48
31
this . myState . value = signaturePad . toDataURL ( ) ;
32
+ input . value = this . myState . value ;
49
33
} ) ;
50
34
35
+ // static image with saved signature
36
+ const imageDiv = document . createElement ( "div" ) ;
37
+ const image = document . createElement ( "img" ) ;
38
+
39
+ // display canvas or image depending on available data
40
+ let signatureVisibleElement ;
41
+ if ( ! this . myState . value ) {
42
+ signatureVisibleElement = canvas ;
43
+ } else {
44
+ image . src = this . myState . value ;
45
+ imageDiv . appendChild ( image ) ;
46
+ signatureVisibleElement = imageDiv ;
47
+ }
48
+ control . appendChild ( signatureVisibleElement ) ;
51
49
52
- const fieldButton = document . createElement ( "div" ) ;
53
- fieldButton . classList . add ( "field" ) ;
50
+ // clear button
51
+ const clearButtonField = document . createElement ( "div" ) ;
52
+ clearButtonField . classList . add ( "field" ) ;
54
53
55
- const controlButton = document . createElement ( "div" ) ;
56
- controlButton . classList . add ( "control" ) ;
54
+ const clearButtonControl = document . createElement ( "div" ) ;
55
+ clearButtonControl . classList . add ( "control" ) ;
57
56
58
- fieldButton . appendChild ( controlButton ) ;
57
+ clearButtonField . appendChild ( clearButtonControl ) ;
59
58
60
59
const clearButton = document . createElement ( "button" ) ;
61
60
clearButton . classList . add ( "button" , "clear" ) ;
62
61
clearButton . type = "button" ;
63
62
clearButton . innerText = U . getLang ( 'label_signature_delete' ) ;
64
63
clearButton . addEventListener ( "click" , ( event ) => {
64
+ // clear all values
65
65
signaturePad . clear ( ) ;
66
66
this . myState . value = null ;
67
+ input . value = null ;
68
+
69
+ // restore canvas (signature may be an image at this point)
70
+ control . removeChild ( signatureVisibleElement ) ;
71
+ signatureVisibleElement = canvas ;
72
+ control . appendChild ( signatureVisibleElement ) ;
67
73
} ) ;
68
74
69
- controlButton . appendChild ( clearButton ) ;
75
+ clearButtonControl . appendChild ( clearButton ) ;
70
76
71
- wrapper . appendChild ( fieldButton ) ;
77
+ wrapper . appendChild ( clearButtonField ) ;
72
78
73
79
return wrapper ;
74
80
}
0 commit comments