1 | /* |
||
2 | * https://github.com/legalthings/signature-pad-angular |
||
3 | * Copyright (c) 2015 ; Licensed MIT |
||
4 | */ |
||
5 | |||
6 | angular.module('signature', []); |
||
7 | |||
8 | angular.module('signature').directive('signaturePad', ['$interval', '$timeout', '$window', |
||
9 | function ($interval, $timeout, $window) { |
||
10 | 'use strict'; |
||
11 | |||
12 | var signaturePad, element, EMPTY_IMAGE = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAjgAAADcCAQAAADXNhPAAAACIklEQVR42u3UIQEAAAzDsM+/6UsYG0okFDQHMBIJAMMBDAfAcADDATAcwHAAwwEwHMBwAAwHMBzAcAAMBzAcAMMBDAcwHADDAQwHwHAAwwEMB8BwAMMBMBzAcADDATAcwHAADAcwHADDAQwHMBwAwwEMB8BwAMMBDAfAcADDATAcwHAAwwEwHMBwAAwHMBzAcAAMBzAcAMMBDAcwHADDAQwHwHAAwwEwHMBwAMMBMBzAcAAMBzAcwHAADAcwHADDAQwHMBwAwwEMB8BwAMMBDAfAcADDATAcwHAAwwEwHMBwAAwHMBzAcCQADAcwHADDAQwHwHAAwwEMB8BwAMMBMBzAcADDATAcwHAADAcwHMBwAAwHMBwAwwEMBzAcAMMBDAfAcADDAQwHwHAAwwEwHMBwAAwHMBzAcAAMBzAcAMMBDAcwHADDAQwHwHAAwwEMB8BwAMMBMBzAcADDATAcwHAADAcwHMBwAAwHMBwAwwEMB8BwAMMBDAfAcADDATAcwHAAwwEwHMBwAAwHMBzAcAAMBzAcAMMBDAcwHADDAQwHwHAAwwEMB8BwAMMBMBzAcADDkQAwHMBwAAwHMBwAwwEMBzAcAMMBDAfAcADDAQwHwHAAwwEwHMBwAMMBMBzAcAAMBzAcwHAADAcwHADDAQwHMBwAwwEMB8BwAMMBMBzAcADDATAcwHAADAcwHMBwAAwHMBwAwwEMBzAcAMMBDAegeayZAN3dLgwnAAAAAElFTkSuQmCC'; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() As per coding-style, prefer block-scoped variables using
let or const which have better semantics than var .
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
Consider the following two pieces of code: if (true)
{
var x = "Hello, Stonehenge!";
}
console.log(x); //prints Hello, Stonehenge! to the console
and if (true)
{
let x = "Hello, Stonehenge!";
}
console.log(x); //ReferenceError: x is not defined
The variable is not defined otuside of its block. This limits bleeding of variables into other contexts. To know more about this ECMA6 feature, look at the MDN pages on let and const. ![]() |
|||
13 | |||
14 | return { |
||
15 | restrict: 'EA', |
||
16 | replace: true, |
||
17 | template: '<div class="signature" style="width: 100%; max-width:{{width}}px; height: 100%; max-height:{{height}}px;"><canvas style="display: block; margin: 0 auto;" ng-mouseup="onMouseup()" ng-mousedown="notifyDrawing({ drawing: true })"></canvas></div>', |
||
18 | scope: { |
||
19 | accept: '=?', |
||
20 | clear: '=?', |
||
21 | disabled: '=?', |
||
22 | dataurl: '=?', |
||
23 | height: '@', |
||
24 | width: '@', |
||
25 | notifyDrawing: '&onDrawing', |
||
26 | }, |
||
27 | controller: [ |
||
28 | '$scope', |
||
29 | function ($scope) { |
||
30 | $scope.accept = function () { |
||
31 | |||
32 | return { |
||
33 | isEmpty: $scope.dataurl === EMPTY_IMAGE, |
||
34 | dataUrl: $scope.dataurl |
||
35 | }; |
||
36 | }; |
||
37 | |||
38 | $scope.onMouseup = function () { |
||
39 | $scope.updateModel(); |
||
40 | |||
41 | // notify that drawing has ended |
||
42 | $scope.notifyDrawing({ drawing: false }); |
||
43 | }; |
||
44 | |||
45 | $scope.updateModel = function () { |
||
46 | /* |
||
47 | defer handling mouseup event until $scope.signaturePad handles |
||
48 | first the same event |
||
49 | */ |
||
50 | $timeout().then(function () { |
||
51 | $scope.dataurl = $scope.signaturePad.isEmpty() ? EMPTY_IMAGE : $scope.signaturePad.toDataURL(); |
||
52 | }); |
||
53 | }; |
||
54 | |||
55 | $scope.clear = function () { |
||
56 | $scope.signaturePad.clear(); |
||
57 | $scope.dataurl = EMPTY_IMAGE; |
||
58 | }; |
||
59 | |||
60 | $scope.$watch("dataurl", function (dataUrl) { |
||
61 | if (!dataUrl || $scope.signaturePad.toDataURL() === dataUrl) { |
||
62 | return; |
||
63 | } |
||
64 | |||
65 | $scope.setDataUrl(dataUrl); |
||
66 | }); |
||
67 | } |
||
68 | ], |
||
69 | link: function (scope, element, attrs) { |
||
70 | var canvas = element.find('canvas')[0]; |
||
0 ignored issues
–
show
As per coding-style, prefer block-scoped variables using
let or const which have better semantics than var .
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
Consider the following two pieces of code: if (true)
{
var x = "Hello, Stonehenge!";
}
console.log(x); //prints Hello, Stonehenge! to the console
and if (true)
{
let x = "Hello, Stonehenge!";
}
console.log(x); //ReferenceError: x is not defined
The variable is not defined otuside of its block. This limits bleeding of variables into other contexts. To know more about this ECMA6 feature, look at the MDN pages on let and const. ![]() |
|||
71 | var parent = canvas.parentElement; |
||
0 ignored issues
–
show
As per coding-style, prefer block-scoped variables using
let or const which have better semantics than var .
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
Consider the following two pieces of code: if (true)
{
var x = "Hello, Stonehenge!";
}
console.log(x); //prints Hello, Stonehenge! to the console
and if (true)
{
let x = "Hello, Stonehenge!";
}
console.log(x); //ReferenceError: x is not defined
The variable is not defined otuside of its block. This limits bleeding of variables into other contexts. To know more about this ECMA6 feature, look at the MDN pages on let and const. ![]() |
|||
72 | var scale = 0; |
||
73 | var ctx = canvas.getContext('2d'); |
||
0 ignored issues
–
show
As per coding-style, prefer block-scoped variables using
let or const which have better semantics than var .
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
Consider the following two pieces of code: if (true)
{
var x = "Hello, Stonehenge!";
}
console.log(x); //prints Hello, Stonehenge! to the console
and if (true)
{
let x = "Hello, Stonehenge!";
}
console.log(x); //ReferenceError: x is not defined
The variable is not defined otuside of its block. This limits bleeding of variables into other contexts. To know more about this ECMA6 feature, look at the MDN pages on let and const. ![]() |
|||
74 | |||
75 | var width = parseInt(scope.width, 10); |
||
0 ignored issues
–
show
As per coding-style, prefer block-scoped variables using
let or const which have better semantics than var .
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
Consider the following two pieces of code: if (true)
{
var x = "Hello, Stonehenge!";
}
console.log(x); //prints Hello, Stonehenge! to the console
and if (true)
{
let x = "Hello, Stonehenge!";
}
console.log(x); //ReferenceError: x is not defined
The variable is not defined otuside of its block. This limits bleeding of variables into other contexts. To know more about this ECMA6 feature, look at the MDN pages on let and const. ![]() |
|||
76 | var height = parseInt(scope.height, 10); |
||
0 ignored issues
–
show
As per coding-style, prefer block-scoped variables using
let or const which have better semantics than var .
Since ECMAScript 6, you can create block-scoped vars or constants with the keywords
Consider the following two pieces of code: if (true)
{
var x = "Hello, Stonehenge!";
}
console.log(x); //prints Hello, Stonehenge! to the console
and if (true)
{
let x = "Hello, Stonehenge!";
}
console.log(x); //ReferenceError: x is not defined
The variable is not defined otuside of its block. This limits bleeding of variables into other contexts. To know more about this ECMA6 feature, look at the MDN pages on let and const. ![]() |
|||
77 | |||
78 | canvas.width = width; |
||
79 | canvas.height = height; |
||
80 | |||
81 | scope.signaturePad = new SignaturePad(canvas); |
||
0 ignored issues
–
show
The variable
SignaturePad seems to be never declared. If this is a global, consider adding a /** global: SignaturePad */ comment.
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed. To learn more about declaring variables in Javascript, see the MDN. ![]() |
|||
82 | |||
83 | scope.setDataUrl = function(dataUrl) { |
||
84 | var ratio = Math.max(window.devicePixelRatio || 1, 1); |
||
85 | |||
86 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
||
87 | ctx.scale(ratio, ratio); |
||
88 | |||
89 | scope.signaturePad.clear(); |
||
90 | scope.signaturePad.fromDataURL(dataUrl); |
||
91 | |||
92 | $timeout().then(function() { |
||
93 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
||
94 | ctx.scale(1 / scale, 1 / scale); |
||
95 | }); |
||
96 | }; |
||
97 | |||
98 | scope.$watch('disabled', function (val) { |
||
99 | val ? scope.signaturePad.off() : scope.signaturePad.on(); |
||
100 | }); |
||
101 | |||
102 | var calculateScale = function() { |
||
103 | var scaleWidth = Math.min(parent.clientWidth / width, 1); |
||
104 | var scaleHeight = Math.min(parent.clientHeight / height, 1); |
||
105 | |||
106 | var newScale = Math.min(scaleWidth, scaleHeight); |
||
107 | |||
108 | if (newScale === scale) { |
||
109 | return; |
||
110 | } |
||
111 | |||
112 | var newWidth = width * newScale; |
||
113 | var newHeight = height * newScale; |
||
114 | canvas.style.height = Math.round(newHeight) + "px"; |
||
115 | canvas.style.width = Math.round(newWidth) + "px"; |
||
116 | |||
117 | scale = newScale; |
||
118 | ctx.setTransform(1, 0, 0, 1, 0, 0); |
||
119 | ctx.scale(1 / scale, 1 / scale); |
||
120 | }; |
||
121 | |||
122 | var resizeIH = $interval(calculateScale, 200); |
||
123 | scope.$on('$destroy', function () { |
||
124 | $interval.cancel(resizeIH); |
||
125 | resizeIH = null; |
||
126 | }); |
||
127 | |||
128 | angular.element($window).bind('resize', calculateScale); |
||
129 | scope.$on('$destroy', function () { |
||
130 | angular.element($window).unbind('resize', calculateScale); |
||
131 | }); |
||
132 | |||
133 | calculateScale(); |
||
134 | |||
135 | element.on('touchstart', onTouchstart); |
||
136 | element.on('touchend', onTouchend); |
||
137 | |||
138 | function onTouchstart(event) { |
||
139 | scope.$apply(function () { |
||
140 | // notify that drawing has started |
||
141 | scope.notifyDrawing({ drawing: true }); |
||
142 | }); |
||
143 | event.preventDefault(); |
||
144 | } |
||
145 | |||
146 | function onTouchend(event) { |
||
147 | scope.$apply(function () { |
||
148 | // updateModel |
||
149 | scope.updateModel(); |
||
150 | |||
151 | // notify that drawing has ended |
||
152 | scope.notifyDrawing({ drawing: false }); |
||
153 | }); |
||
154 | event.preventDefault(); |
||
155 | } |
||
156 | } |
||
157 | }; |
||
158 | } |
||
159 | ]); |
||
160 | |||
161 | // Backward compatibility |
||
162 | angular.module('ngSignaturePad', ['signature']); |
||
163 |