Added literallycanvas and react libraries.
[openemr.git] / library / js / literallycanvas / js / core / bindEvents.js
blobd3455284b6fb7c9fea5133ac0d2e178ce7fbe763
1 var bindEvents, buttonIsDown, coordsForTouchEvent, position;
3 coordsForTouchEvent = function(el, e) {
4   var p, tx, ty;
5   tx = e.changedTouches[0].clientX;
6   ty = e.changedTouches[0].clientY;
7   p = el.getBoundingClientRect();
8   return [tx - p.left, ty - p.top];
9 };
11 position = function(el, e) {
12   var p;
13   p = el.getBoundingClientRect();
14   return {
15     left: e.clientX - p.left,
16     top: e.clientY - p.top
17   };
20 buttonIsDown = function(e) {
21   if (e.buttons != null) {
22     return e.buttons === 1;
23   } else {
24     return e.which > 0;
25   }
28 module.exports = bindEvents = function(lc, canvas, panWithKeyboard) {
29   var listener, mouseMoveListener, mouseUpListener, touchEndListener, touchMoveListener, unsubs;
30   if (panWithKeyboard == null) {
31     panWithKeyboard = false;
32   }
33   unsubs = [];
34   mouseMoveListener = (function(_this) {
35     return function(e) {
36       var p;
37       e.preventDefault();
38       p = position(canvas, e);
39       return lc.pointerMove(p.left, p.top);
40     };
41   })(this);
42   mouseUpListener = (function(_this) {
43     return function(e) {
44       var p;
45       e.preventDefault();
46       canvas.onselectstart = function() {
47         return true;
48       };
49       p = position(canvas, e);
50       lc.pointerUp(p.left, p.top);
51       document.removeEventListener('mousemove', mouseMoveListener);
52       document.removeEventListener('mouseup', mouseUpListener);
53       return canvas.addEventListener('mousemove', mouseMoveListener);
54     };
55   })(this);
56   canvas.addEventListener('mousedown', (function(_this) {
57     return function(e) {
58       var down, p;
59       if (e.target.tagName.toLowerCase() !== 'canvas') {
60         return;
61       }
62       down = true;
63       e.preventDefault();
64       canvas.onselectstart = function() {
65         return false;
66       };
67       p = position(canvas, e);
68       lc.pointerDown(p.left, p.top);
69       canvas.removeEventListener('mousemove', mouseMoveListener);
70       document.addEventListener('mousemove', mouseMoveListener);
71       return document.addEventListener('mouseup', mouseUpListener);
72     };
73   })(this));
74   touchMoveListener = function(e) {
75     e.preventDefault();
76     return lc.pointerMove.apply(lc, coordsForTouchEvent(canvas, e));
77   };
78   touchEndListener = function(e) {
79     e.preventDefault();
80     lc.pointerUp.apply(lc, coordsForTouchEvent(canvas, e));
81     document.removeEventListener('touchmove', touchMoveListener);
82     document.removeEventListener('touchend', touchEndListener);
83     return document.removeEventListener('touchcancel', touchEndListener);
84   };
85   canvas.addEventListener('touchstart', function(e) {
86     if (e.target.tagName.toLowerCase() !== 'canvas') {
87       return;
88     }
89     e.preventDefault();
90     if (e.touches.length === 1) {
91       lc.pointerDown.apply(lc, coordsForTouchEvent(canvas, e));
92       document.addEventListener('touchmove', touchMoveListener);
93       document.addEventListener('touchend', touchEndListener);
94       return document.addEventListener('touchcancel', touchEndListener);
95     } else {
96       return lc.pointerMove.apply(lc, coordsForTouchEvent(canvas, e));
97     }
98   });
99   if (panWithKeyboard) {
100     console.warn("Keyboard panning is deprecated.");
101     listener = function(e) {
102       switch (e.keyCode) {
103         case 37:
104           lc.pan(-10, 0);
105           break;
106         case 38:
107           lc.pan(0, -10);
108           break;
109         case 39:
110           lc.pan(10, 0);
111           break;
112         case 40:
113           lc.pan(0, 10);
114       }
115       return lc.repaintAllLayers();
116     };
117     document.addEventListener('keydown', listener);
118     unsubs.push(function() {
119       return document.removeEventListener(listener);
120     });
121   }
122   return function() {
123     var f, i, len, results;
124     results = [];
125     for (i = 0, len = unsubs.length; i < len; i++) {
126       f = unsubs[i];
127       results.push(f());
128     }
129     return results;
130   };