Issues (19)

src/signature.js (3 issues)

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
The variable element seems to be never used. Consider removing it.
Loading history...
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) {
0 ignored issues
show
The parameter attrs is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
70
        var canvas = element.find('canvas')[0];
71
        var parent = canvas.parentElement;
72
        var scale = 0;
73
        var ctx = canvas.getContext('2d');
74
75
        var width = parseInt(scope.width, 10);
76
        var height = parseInt(scope.height, 10);
77
78
        canvas.width = width;
79
        canvas.height = height;
80
81
        scope.signaturePad = new SignaturePad(canvas);
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();
0 ignored issues
show
Did you forget to assign or call a function?

This error message can for example pop up if you forget to assign the result of a function call to a variable or pass it to another function:

function someFunction(x) {
    (x > 0) ? callFoo() : callBar();
}

// JSHint expects you to assign the result to a variable:
function someFunction(x) {
    var rs = (x > 0) ? callFoo() : callBar();
}

// If you do not use the result, you could also use if statements in the
// case above.
function someFunction(x) {
    if (x > 0) {
        callFoo();
    } else {
        callBar();
    }
}
Loading history...
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