UNPKG

2.92 kBJavaScriptView Raw
1/*!
2Event object based on jQuery events, MIT license
3
4https://jquery.org/license/
5https://tldrlegal.com/license/mit-license
6https://github.com/jquery/jquery/blob/master/src/event.js
7*/
8
9let Event = function( src, props ){
10 this.recycle( src, props );
11};
12
13function returnFalse(){
14 return false;
15}
16
17function returnTrue(){
18 return true;
19}
20
21// http://www.w3.org/TR/2003/WD-DOM-Level-3-Events-20030331/ecma-script-binding.html
22Event.prototype = {
23 instanceString: function(){
24 return 'event';
25 },
26
27 recycle: function( src, props ){
28 this.isImmediatePropagationStopped = this.isPropagationStopped = this.isDefaultPrevented = returnFalse;
29
30 if( src != null && src.preventDefault ){ // Browser Event object
31 this.type = src.type;
32
33 // Events bubbling up the document may have been marked as prevented
34 // by a handler lower down the tree; reflect the correct value.
35 this.isDefaultPrevented = ( src.defaultPrevented ) ? returnTrue : returnFalse;
36
37 } else if( src != null && src.type ){ // Plain object containing all event details
38 props = src;
39
40 } else { // Event string
41 this.type = src;
42 }
43
44 // Put explicitly provided properties onto the event object
45 if( props != null ){
46 // more efficient to manually copy fields we use
47 this.originalEvent = props.originalEvent;
48 this.type = props.type != null ? props.type : this.type;
49 this.cy = props.cy;
50 this.target = props.target;
51 this.position = props.position;
52 this.renderedPosition = props.renderedPosition;
53 this.namespace = props.namespace;
54 this.layout = props.layout;
55 }
56
57 if( this.cy != null && this.position != null && this.renderedPosition == null ){
58 // create a rendered position based on the passed position
59 let pos = this.position;
60 let zoom = this.cy.zoom();
61 let pan = this.cy.pan();
62
63 this.renderedPosition = {
64 x: pos.x * zoom + pan.x,
65 y: pos.y * zoom + pan.y
66 };
67 }
68
69 // Create a timestamp if incoming event doesn't have one
70 this.timeStamp = src && src.timeStamp || Date.now();
71 },
72
73 preventDefault: function(){
74 this.isDefaultPrevented = returnTrue;
75
76 let e = this.originalEvent;
77 if( !e ){
78 return;
79 }
80
81 // if preventDefault exists run it on the original event
82 if( e.preventDefault ){
83 e.preventDefault();
84 }
85 },
86
87 stopPropagation: function(){
88 this.isPropagationStopped = returnTrue;
89
90 let e = this.originalEvent;
91 if( !e ){
92 return;
93 }
94
95 // if stopPropagation exists run it on the original event
96 if( e.stopPropagation ){
97 e.stopPropagation();
98 }
99 },
100
101 stopImmediatePropagation: function(){
102 this.isImmediatePropagationStopped = returnTrue;
103 this.stopPropagation();
104 },
105
106 isDefaultPrevented: returnFalse,
107 isPropagationStopped: returnFalse,
108 isImmediatePropagationStopped: returnFalse
109};
110
111export default Event;