|
1 | | -var content = (penColor, backgroundColor, dataURL, penMinWidth, penMaxWidth, useFont, name, height, width) => ` |
2 | | -
|
3 | | - var showSignaturePad = function (signaturePadCanvas, bodyWidth, bodyHeight) { |
4 | | - var width = bodyWidth; |
5 | | - var height = bodyHeight; |
6 | | -
|
7 | | - var sizeSignaturePad = function () { |
8 | | - var devicePixelRatio = window.devicePixelRatio || 1; |
9 | | - var canvasWidth = width * devicePixelRatio; |
10 | | - var canvasHeight = height * devicePixelRatio; |
11 | | - signaturePadCanvas.width = canvasWidth; |
12 | | - signaturePadCanvas.height = canvasHeight; |
13 | | - signaturePadCanvas.getContext("2d").scale(devicePixelRatio, devicePixelRatio); |
14 | | - }; |
| 1 | +var content = (penColor, backgroundColor, dataURL, penMinWidth, penMaxWidth, useFont, name, height, width, initTimeout) => ` |
15 | 2 |
|
16 | | - var enableSignaturePadFunctionality = function () { |
17 | | - var signaturePad = new SignaturePad(signaturePadCanvas, { |
18 | | - penColor: "${penColor || "black"}", |
19 | | - backgroundColor: "${backgroundColor || "white"}", |
20 | | - onEnd: function() { finishedStroke(signaturePad.toDataURL()); } |
21 | | - }); |
22 | | - signaturePad.minWidth = ${penMinWidth || 1}; |
23 | | - signaturePad.maxWidth = ${penMaxWidth || 4}; |
24 | | - if ("${dataURL}") { |
25 | | - signaturePad.fromDataURL("${dataURL}"); |
26 | | - } |
27 | | - }; |
| 3 | +var showSignaturePad = function (signaturePadCanvas, bodyWidth, bodyHeight) { |
| 4 | + var width = bodyWidth; |
| 5 | + var height = bodyHeight; |
28 | 6 |
|
29 | | - reportSize(width, height); |
30 | | - sizeSignaturePad(); |
31 | | - enableSignaturePadFunctionality(); |
| 7 | + var sizeSignaturePad = function () { |
| 8 | + var devicePixelRatio = window.devicePixelRatio || 1; |
| 9 | + var canvasWidth = width * devicePixelRatio; |
| 10 | + var canvasHeight = height * devicePixelRatio; |
| 11 | + signaturePadCanvas.width = canvasWidth; |
| 12 | + signaturePadCanvas.height = canvasHeight; |
| 13 | + signaturePadCanvas.getContext("2d").scale(devicePixelRatio, devicePixelRatio); |
32 | 14 | }; |
33 | 15 |
|
34 | | - var bodyWidth = document.body.clientWidth * 2; |
35 | | - var bodyHeight = document.body.clientHeight * 2; |
36 | | - if(!bodyWidth) { |
37 | | - bodyWidth = window.innerWidth ? window.innerWidth : ${width}; |
| 16 | + var enableSignaturePadFunctionality = function () { |
| 17 | + var signaturePad = new SignaturePad(signaturePadCanvas, { |
| 18 | + penColor: "${penColor || "black"}", |
| 19 | + backgroundColor: "${backgroundColor || "white"}", |
| 20 | + onEnd: function() { finishedStroke(signaturePad.toDataURL()); } |
| 21 | + }); |
| 22 | + signaturePad.minWidth = ${penMinWidth || 1}; |
| 23 | + signaturePad.maxWidth = ${penMaxWidth || 4}; |
| 24 | + if ("${dataURL}") { |
| 25 | + signaturePad.fromDataURL("${dataURL}"); |
| 26 | + } |
| 27 | + }; |
| 28 | +
|
| 29 | + reportSize(width, height); |
| 30 | + sizeSignaturePad(); |
| 31 | + enableSignaturePadFunctionality(); |
| 32 | +}; |
| 33 | +
|
| 34 | +var canvasElement = document.querySelector("canvas"); |
| 35 | +
|
| 36 | +var reportSize = function(width, height) { |
| 37 | + if (postMessage.length === 1) { |
| 38 | + window.postMessage(JSON.stringify({ width: width, height: height })); |
| 39 | + } else { |
| 40 | + setTimeout(function() { reportSize(width, height) }, 100); |
38 | 41 | } |
39 | | - if(!bodyHeight) { |
40 | | - bodyHeight = window.innerHeight ? window.innerHeight : ${height}; |
| 42 | +} |
| 43 | +
|
| 44 | +var finishedStroke = function(base64DataUrl) { |
| 45 | + window.postMessage(JSON.stringify({ base64DataUrl: base64DataUrl })); |
| 46 | +}; |
| 47 | +
|
| 48 | +var getBodyWidth = function() { |
| 49 | + var bodyWidth = document && document.body && document.body.clientWidth ? document.body.clientWidth * 2 : 0; |
| 50 | + if(!bodyWidth) { |
| 51 | + bodyWidth = window && window.innerWidth ? window.innerWidth : ${width || 0}; |
41 | 52 | } |
42 | 53 |
|
43 | | - var canvasElement = document.querySelector("canvas"); |
| 54 | + return bodyWidth; |
| 55 | +}; |
44 | 56 |
|
45 | | - var reportSize = function(width, height) { |
46 | | - if (postMessage.length === 1) { |
47 | | - window.postMessage(JSON.stringify({ width: width, height: height })); |
48 | | - } else { |
49 | | - setTimeout(function() { reportSize(width, height) }, 100); |
50 | | - } |
| 57 | +var getBodyHeight = function() { |
| 58 | + var bodyHeight = document && document.body && document.body.clientHeight ? document.body.clientHeight * 2 : 0; |
| 59 | + if(!bodyHeight) { |
| 60 | + bodyHeight = window && window.innerHeight ? window.innerHeight : ${height || 0}; |
51 | 61 | } |
52 | 62 |
|
53 | | - var finishedStroke = function(base64DataUrl) { |
54 | | - window.postMessage(JSON.stringify({ base64DataUrl: base64DataUrl })); |
55 | | - }; |
| 63 | + return bodyHeight; |
| 64 | +}; |
56 | 65 |
|
| 66 | +var initSignaturePad = function(bodyWidth, bodyHeight) { |
57 | 67 | if (${useFont}) { |
58 | 68 | var context = canvasElement.getContext("2d"); |
59 | 69 | var devicePixelRatio = 1; /* window.devicePixelRatio || 1; */ |
@@ -88,8 +98,60 @@ var content = (penColor, backgroundColor, dataURL, penMinWidth, penMaxWidth, use |
88 | 98 | finishedStroke(canvasElement.toDataURL()); |
89 | 99 | }, 75); |
90 | 100 | } else { |
91 | | - showSignaturePad(canvasElement, bodyWidth / 2, bodyHeight / 2); |
| 101 | + showSignaturePad(canvasElement, bodyWidth / 2, bodyHeight / 2); |
92 | 102 | } |
| 103 | +}; |
| 104 | +
|
| 105 | +var whileDocumentSizeNotSet = function(timeout, maximumWaitTime) { |
| 106 | + try { |
| 107 | + if ( typeof whileDocumentSizeNotSet.counter == 'undefined' ) { |
| 108 | + whileDocumentSizeNotSet.counter = 0; |
| 109 | + whileDocumentSizeNotSet.bodyHeight = 0; |
| 110 | + whileDocumentSizeNotSet.bodyWidth = 0; |
| 111 | + } else { |
| 112 | + whileDocumentSizeNotSet.counter++; |
| 113 | + } |
| 114 | +
|
| 115 | +
|
| 116 | + const maxAttemts = Math.floor(maximumWaitTime / timeout); |
| 117 | + const attempt = whileDocumentSizeNotSet.counter; |
| 118 | +
|
| 119 | + const previousBodyHeight = whileDocumentSizeNotSet.bodyHeight; |
| 120 | + const previousBodyWidth = whileDocumentSizeNotSet.bodyWidth; |
| 121 | + |
| 122 | + const bodyHeight = getBodyHeight(); |
| 123 | + const bodyWidth = getBodyWidth(); |
| 124 | +
|
| 125 | + whileDocumentSizeNotSet.bodyHeight = bodyHeight; |
| 126 | + whileDocumentSizeNotSet.bodyWidth = bodyWidth; |
| 127 | + |
| 128 | + if (bodyHeight === 0 || bodyWidth === 0 || previousBodyWidth !== bodyWidth || previousBodyHeight !== bodyHeight) { |
| 129 | + if (attempt <= maxAttemts) { |
| 130 | + setTimeout(whileDocumentSizeNotSet, timeout, timeout, maximumWaitTime); |
| 131 | + |
| 132 | + return false; |
| 133 | + } else { |
| 134 | + window.alert('Timed out trying to load SignaturePad, tried ' + attempt + ' times in ' + maximumWaitTime + 'ms.'); |
| 135 | + // of maximumWaitTime:' + maximumWaitTime + ', timeout:' + timeout + ', maxAttemts' + maxAttemts + ' times'); |
| 136 | +
|
| 137 | + initSignaturePad(700, 700); |
| 138 | + } |
| 139 | + } else { |
| 140 | + initSignaturePad(bodyWidth, bodyHeight); |
| 141 | +
|
| 142 | + // window.alert('Had to wait ' + attempt + ' times, width: ' + bodyWidth + ', height: ' + bodyHeight); |
| 143 | + } |
| 144 | + } catch (e) { |
| 145 | + if (window) { |
| 146 | + window.alert(e.message); |
| 147 | + } |
| 148 | + } |
| 149 | +
|
| 150 | + return true; |
| 151 | +}; |
| 152 | +
|
| 153 | +whileDocumentSizeNotSet(250, ${initTimeout || 3000}); |
| 154 | +
|
93 | 155 | `; |
94 | 156 |
|
95 | 157 | export default content; |
0 commit comments