This repository has been archived by the owner on Jun 28, 2020. It is now read-only.
forked from Breizhtorm/Contact-Form-7-Signature-Addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
143 lines (106 loc) · 3.45 KB
/
scripts.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var signatures = [];
//window.onresize = sigFieldsResize;
document.addEventListener("DOMContentLoaded", function(){
var forms = document.querySelectorAll("form.wpcf7-form");
Array.prototype.forEach.call(forms, function(form, i){
var wrappers = document.querySelectorAll(".wpcf7-form-control-signature-global-wrap");
Array.prototype.forEach.call(wrappers, function(wrapper, i){
var canvas = wrapper.querySelector("canvas");
var clear = wrapper.querySelector("input[type=button]");
var submit = form.querySelector("input.wpcf7-submit");
var id = wrapper.getAttribute("data-field-id");
var input = document.getElementById("wpcf7_input_" + id);
// Resize field (for pixel ratio issues)
sigFieldRatio(canvas);
// Options
var options = [];
if (canvas.hasAttribute("data-color")){
options['penColor'] = canvas.getAttribute("data-color");
}
if (canvas.hasAttribute("data-background")){
options['backgroundColor'] = canvas.getAttribute("data-background");
}
// Canvas init
var signature = new SignaturePad(canvas, options);
// Push field elements into global var
signatures.push({
signature: signature,
input: input,
canvas: canvas,
options: options
});
sigFieldInit(i);
// Clear event listener
clear.addEventListener("click", function(){
sigFieldClear(i);
});
// Trigger change event on input field when signature changed
canvas.addEventListener("mouseup", function(){
sigFieldChange(i);
});
// Submit Event Listener
submit.addEventListener("click", function(){
sigFieldBeforeSubmit(i);
}, false);
});
});
});
// Init Canvas value if needed
function sigFieldInit(index){
if(signatures[index].input.value != ''){
signatures[index].signature.fromDataURL(signatures[index].input.value);
}
}
// Trigger Change event
function sigFieldChange(index){
sigFieldBeforeSubmit(index);
if (document.createEvent) {
var changeEvent = document.createEvent("HTMLEvents");
changeEvent.initEvent("change", false, true);
signatures[index].input.dispatchEvent(changeEvent);
} else {
signatures[index].input.fireEvent("onchange");
}
}
// Copy sig value to input field
function sigFieldBeforeSubmit(index){
if (!signatures[index].signature.isEmpty()){
signatures[index].input.value = signatures[index].signature.toDataURL();
}else{
signatures[index].input.value = "";
}
}
// Clear a single signature field
function sigFieldClear(index){
signatures[index].signature.clear();
signatures[index].input.value= "";
}
// Dealing with window size and device ratio
function sigFieldRatio(canvas){
var ratio = Math.max(window.devicePixelRatio || 1, 1);
canvas.width = canvas.offsetWidth * ratio;
canvas.height = canvas.offsetHeight * ratio;
canvas.getContext("2d").scale(ratio, ratio);
}
// Dealing with window size and device ratio
function sigFieldResize(index, clear){
var canvas = signatures[index].canvas;
sigFieldRatio(canvas)
if (clear){
sigFieldClear(index);
}
}
// Global resize management
function sigFieldsResize(){
var elements = document.querySelectorAll(".wpcf7-form-control-signature-input-wrap");
Array.prototype.forEach.call(elements, function(el, i){
sigFieldResize(i, true);
});
}
// Globally clear fields on form submit
function sigFieldsClear(){
var elements = document.querySelectorAll(".wpcf7-form-control-signature-input-wrap");
Array.prototype.forEach.call(elements, function(el, i){
sigFieldClear(i);
});
}