forked from w3canvas/ascanvas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
EventTarget.as
209 lines (190 loc) · 6.19 KB
/
EventTarget.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
CanvasEvent by Jumis, Inc
Updated: 2009-05-18
Unless otherwise noted:
All source code is hereby released into public domain.
http://creativecommons.org/publicdomain/zero/1.0/
http://creativecommons.org/licenses/publicdomain/
Lead development by Charles Pritchard with thanks to:
The World Wide Web Consortium ( http://www.w3.org/TR/DOM-Level-3-Events/idl-definitions.html )
Wikipedia ( http://en.wikipedia.org/wiki/DOM_Events )
Some DOM Events target Adobe Flash 9 Event Constants
*/
package com.w3canvas.ascanvas {
import flash.utils.Proxy;
import flash.utils.flash_proxy;
import flash.events.MouseEvent;
// import flash.events.Event;
// import flash.events.MouseEvent;
// import flash.events.KeyboardEvent;
// import flash.events.EventDispatcher;
public class EventTarget {
public var public_vars = [];
public var public_call = ['addEventListener','removeEventListener','dispatchEvent','handleEvent','createEvent'];
private var element;
public function EventTarget(container = null) {
element = container === null ? this : container;
for(var i in dom_events) public_vars.push(i);
}
// EventTarget
public function addEventListener(type,fn,bubble=false) {
return element.addEventListener(getHandle(type),fn,false);
};
public function removeEventListener(type,fn,bubble=false) {
return element.removeEventListener(getHandle(type),fn,false);
};
public function dispatchEvent(e) {
return element.dispatchEvent(e);
};
// EventListener
public function handleEvent(e) {};
// DocumentEvent
public function createEvent(type) {};
// Actual implementation
// FIXME: Flash API useCapture vs DOM API bubble
private function getHandle(type) {
if(type in dom_events) type = dom_events[type][0];
if(type in flash_events) type = flash_events[type];
return type;
}
private const flash_events = {
'click': flash.events.MouseEvent.CLICK,
'dblclick': flash.events.MouseEvent.DOUBLE_CLICK,
'mousedown': flash.events.MouseEvent.MOUSE_DOWN,
'mouseup': flash.events.MouseEvent.MOUSE_UP,
'mouseover': flash.events.MouseEvent.MOUSE_OVER,
'mousemove': flash.events.MouseEvent.MOUSE_MOVE,
'mouseout': flash.events.MouseEvent.MOUSE_OUT
}
// attribute: [ type, bubbles, cancelable ]
private const dom_events = {
// Mouse
'onclick': ['click',true,true],
'ondblclick': ['dblclick',true,true],
'onmousedown': ['mousedown',true,true],
'onmouseup': ['mouseup',true,true],
'onmouseover': ['mouseover',true,true],
'onmousemove': ['mousemove',true,false],
'onmouseout': ['mouseout',true,true],
'oncontextmenu': ['contextmenu',true,true],
// Keyboard
'onkeypress': ['keypress',true,true],
'onkeydown': ['keydown',true,true],
'onkeyup': ['keyup',true,true],
// Object
'onload': ['load',false,false],
'onunload': ['unload',false,false],
'onabort': ['abort',true,false],
'onerror': ['error',true,false],
'onreadystatechange': ['readystatechange',false,false],
'onbeforeunload': ['beforeunload',false,true],
// Frame
'onstop':['stop',false,false],
// 'onresize': ['resize',true,false],
'onmove': ['move',true,false],
'onscroll': ['scroll',true,false],
'onbeforeprint': ['beforeprint',false,false],
'onafterprint': ['afterprint',false,false]
};
}
public class DOMEvent {
public var CAPTURING_PHASE = 1;
public var AT_TARGET = 2;
public var BUBBLING_PHASE = 3;
public var type;
public var target;
public var currentTarget;
public var eventPhase;
public var bubbles = false;
public var cancelable = false;
public var timeStamp;
public function stopPropagation() {};
public function preventDefault() {};
public function DOMEvent(eventTypeArg,canBubbleArg,cancelableArg) {
this.type = eventTypeArg;
this.bubbles = canBubbleArg;
this.cancelable = cancelableArg;
}
}
public class UIEvent extends DOMEvent {
public var view;
public var detail;
public function UIEvent(typeArg,canBubbleArg,cancelableArg,viewArg,detailArg) {
this.view = viewArg;
this.detail = detailArg;
super(typeArg,canBubbleArg,cancelableArg);
}
}
public class MouseEvent extends UIEvent {
public var screenX;
public var screenY;
public var clientX;
public var clientY;
public var ctrlKey = false;
public var shiftKey = false;
public var altKey = false;
public var metaKey = false;
public var button; // Key
public var relatedTarget;
public function MouseEvent(typeArg,
canBubbleArg,
cancelableArg,
viewArg,
detailArg,
screenXArg,
screenYArg,
clientXArg,
clientYArg,
ctrlKeyArg,
altKeyArg,
shiftKeyArg,
metaKeyArg,
buttonArg,
relatedTargetArg) {
super(typeArg,canBubbleArg,cancelableArg,viewArg,detailArg);
};
}
dynamic public class CanvasEvent extends Expando {
private var element;
private var sprite;
private var handler;
private var events = {};
public function get public_vars() { return handler.public_vars; };
public function get public_call() { return handler.public_call; };
public function CanvasEvent(container, target) {
handler = new EventTarget(target);
element = container;
}
public function addEventListener(type,fn,bubble=false) {
return handler.addEventListener(type,fn,false);
};
public function removeEventListener(type,fn,bubble=false) {
if(type in events) if(events[type] == fn) delete events[type];
return handler.removeEventListener(type,fn,false);
};
private function bind(scope,fn) {
if(typeof(fn) != 'function') return function(...args) { return fn; };
// if(scope == window.global) return fn;
return function(...args) { return fn.apply(scope,args); };
}
public function setter(type,fn) {
fn = bind(element,fn);
var flash_type = type;
if(type in events) this.removeEventListener(type,events[type]);
this.addEventListener(type,fn);
events[type] = fn;
}
override flash_proxy function setProperty(name:*, value:*):void {
if(!(name in trait)) sort.push(name);
_setProperty(name,value);
return;
}
private function _setProperty(name:*,value:*) {
setter(name,value);
return trait[name] = value;
}
override flash_proxy function callProperty(name:*, ... args):* {
if(name in handler) return handler[name].apply(this,args);
}
}
}