Skip to content

Commit e3387d7

Browse files
authored
Merge pull request #40 from yossydev/feat/event-constructor
feat: Event() constructor
2 parents 5caeec8 + b9fc36f commit e3387d7

File tree

3 files changed

+199
-1
lines changed

3 files changed

+199
-1
lines changed

examples/event/constructor.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Create a new event instance
2+
const myEvent = new Event("customEvent", {
3+
bubbles: true,
4+
cancelable: true,
5+
composed: false,
6+
});
7+
8+
console.log("Event Type:", myEvent.type);
9+
console.log("Bubbles:", myEvent.bubbles);
10+
console.log("Cancelable:", myEvent.cancelable);
11+
console.log("Composed:", myEvent.composed);
12+
console.log("Event Phase:", myEvent.eventPhase);
13+
console.log("Before stopPropagation:", myEvent.cancelBubble);
14+
myEvent.stopPropagation();
15+
console.log("After stopPropagation:", myEvent.cancelBubble);
16+
console.log("Before preventDefault:", myEvent.defaultPrevented);
17+
myEvent.preventDefault();
18+
console.log("After preventDefault:", myEvent.defaultPrevented);
19+
console.log("Target:", myEvent.target);
20+
console.log("Current Target:", myEvent.currentTarget);
21+
console.log("NONE:", myEvent.NONE);
22+
console.log("CAPTURING_PHASE:", myEvent.CAPTURING_PHASE);
23+
console.log("AT_TARGET:", myEvent.AT_TARGET);
24+
console.log("BUBBLING_PHASE:", myEvent.BUBBLING_PHASE);
25+
console.log("Return Value (before change):", myEvent.returnValue);
26+
myEvent.returnValue = false;
27+
console.log("Return Value (after change):", myEvent.returnValue);
28+
console.log("Time Stamp:", myEvent.timeStamp);

runtime/src/ext/web/event.ts

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// deno-lint-ignore-file no-unused-vars
2+
class Event {
3+
constructor(type, eventInitDict = { __proto__: null }) {
4+
// @ts-ignore - this is a hack to make the URL object work
5+
this.SymbolToStringTag = "Event";
6+
// @ts-ignore - this is a hack to make the URL object work
7+
this.canceledFlag = false;
8+
// @ts-ignore - this is a hack to make the URL object work
9+
this.stopPropagationFlag = false;
10+
// @ts-ignore - this is a hack to make the URL object work
11+
this._stopImmediatePropagationFlag = false;
12+
// @ts-ignore - this is a hack to make the URL object work
13+
this._inPassiveListener = false;
14+
// @ts-ignore - this is a hack to make the URL object work
15+
this.dispatched = false;
16+
// @ts-ignore - this is a hack to make the URL object work
17+
this.isTrusted = false;
18+
// @ts-ignore - this is a hack to make the URL object work
19+
this.path = [];
20+
21+
// @ts-ignore - this is a hack to make the URL object work
22+
this.attributes = {
23+
type,
24+
// @ts-ignore - this is a hack to make the URL object work
25+
bubbles: !!eventInitDict.bubbles,
26+
// @ts-ignore - this is a hack to make the URL object work
27+
cancelable: !!eventInitDict.cancelable,
28+
// @ts-ignore - this is a hack to make the URL object work
29+
composed: !!eventInitDict.composed,
30+
currentTarget: null,
31+
eventPhase: Event.NONE,
32+
target: null,
33+
timeStamp: 0,
34+
};
35+
}
36+
37+
get type() {
38+
// @ts-ignore - this is a hack to make the URL object work
39+
return this.attributes.type;
40+
}
41+
42+
// TODO: Null is not returned
43+
get target() {
44+
// @ts-ignore - this is a hack to make the URL object work
45+
return this.attributes.target;
46+
}
47+
48+
get srcElement() {
49+
return null;
50+
}
51+
52+
set srcElement(_) {
53+
// this member is deprecated
54+
}
55+
56+
// TODO: Null is not returned
57+
get currentTarget() {
58+
// @ts-ignore - this is a hack to make the URL object work
59+
return this.attributes.currentTarget;
60+
}
61+
62+
get NONE() {
63+
return Event.NONE;
64+
}
65+
66+
get CAPTURING_PHASE() {
67+
return Event.CAPTURING_PHASE;
68+
}
69+
70+
get AT_TARGET() {
71+
return Event.AT_TARGET;
72+
}
73+
74+
get BUBBLING_PHASE() {
75+
return Event.BUBBLING_PHASE;
76+
}
77+
78+
static get NONE() {
79+
return 0;
80+
}
81+
82+
static get CAPTURING_PHASE() {
83+
return 1;
84+
}
85+
86+
static get AT_TARGET() {
87+
return 2;
88+
}
89+
90+
static get BUBBLING_PHASE() {
91+
return 3;
92+
}
93+
94+
get eventPhase() {
95+
// @ts-ignore - this is a hack to make the URL object work
96+
return this.attributes.eventPhase;
97+
}
98+
99+
stopPropagation() {
100+
// @ts-ignore - this is a hack to make the URL object work
101+
this.stopPropagationFlag = true;
102+
}
103+
104+
/** @deprecated */
105+
get cancelBubble() {
106+
// @ts-ignore - this is a hack to make the URL object work
107+
return this.stopPropagationFlag;
108+
}
109+
110+
set cancelBubble(value) {
111+
// TODO
112+
// this.stopPropagationFlag = webidl.converters.boolean(value);
113+
}
114+
115+
stopImmediatePropagation() {
116+
// @ts-ignore - this is a hack to make the URL object work
117+
118+
this.stopPropagationFlag = true;
119+
// @ts-ignore - this is a hack to make the URL object work
120+
this.stopImmediatePropagationFlag = true;
121+
}
122+
123+
get bubbles() {
124+
// @ts-ignore - this is a hack to make the URL object work
125+
return this.attributes.bubbles;
126+
}
127+
128+
get cancelable() {
129+
// @ts-ignore - this is a hack to make the URL object work
130+
return this.attributes.cancelable;
131+
}
132+
133+
get returnValue() {
134+
// @ts-ignore - this is a hack to make the URL object work
135+
return !this.canceledFlag;
136+
}
137+
138+
set returnValue(value) {
139+
// if (!webidl.converters.boolean(value)) {
140+
// @ts-ignore - this is a hack to make the URL object work
141+
this.canceledFlag = true;
142+
// }
143+
}
144+
145+
preventDefault() {
146+
// if (this.attributes.cancelable && !this.inPassiveListener) {
147+
// @ts-ignore - this is a hack to make the URL object work
148+
this.canceledFlag = true;
149+
// }
150+
}
151+
152+
get defaultPrevented() {
153+
// @ts-ignore - this is a hack to make the URL object work
154+
return this.canceledFlag;
155+
}
156+
157+
get composed() {
158+
// @ts-ignore - this is a hack to make the URL object work
159+
return this.attributes.composed;
160+
}
161+
162+
get initialized() {
163+
return true;
164+
}
165+
166+
get timeStamp() {
167+
// @ts-ignore - this is a hack to make the URL object work
168+
return this.attributes.timeStamp;
169+
}
170+
}

runtime/src/ext/web/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ impl WebExt {
2020
ExtensionOp::new("internal_atob", Self::internal_atob, 1),
2121
],
2222
storage: None,
23-
files: vec![],
23+
files: vec![include_str!("./event.ts")],
2424
}
2525
}
2626

0 commit comments

Comments
 (0)