forked from denoland/deno
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Web API: CustomEvent (denoland#1505)
- Loading branch information
Showing
8 changed files
with
132 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// Copyright 2018 the Deno authors. All rights reserved. MIT license. | ||
import * as domTypes from "./dom_types"; | ||
import * as event from "./event"; | ||
import { getPrivateValue } from "./util"; | ||
|
||
// WeakMaps are recommended for private attributes (see MDN link below) | ||
// tslint:disable-next-line:max-line-length | ||
// https://developer.mozilla.org/en-US/docs/Archive/Add-ons/Add-on_SDK/Guides/Contributor_s_Guide/Private_Properties#Using_WeakMaps | ||
export const customEventAttributes = new WeakMap(); | ||
|
||
export class CustomEventInit extends event.EventInit | ||
implements domTypes.CustomEventInit { | ||
// tslint:disable-next-line:no-any | ||
detail: any; | ||
|
||
constructor({ | ||
bubbles = false, | ||
cancelable = false, | ||
composed = false, | ||
detail = null | ||
}: domTypes.CustomEventInit) { | ||
super({ bubbles, cancelable, composed }); | ||
this.detail = detail; | ||
} | ||
} | ||
|
||
export class CustomEvent extends event.Event implements domTypes.CustomEvent { | ||
constructor( | ||
type: string, | ||
customEventInitDict: domTypes.CustomEventInit = {} | ||
) { | ||
super(type, customEventInitDict); | ||
const { detail = null } = customEventInitDict; | ||
customEventAttributes.set(this, { detail }); | ||
} | ||
|
||
// tslint:disable-next-line:no-any | ||
get detail(): any { | ||
return getPrivateValue(this, customEventAttributes, "detail"); | ||
} | ||
|
||
initCustomEvent( | ||
type: string, | ||
bubbles?: boolean, | ||
cancelable?: boolean, | ||
// tslint:disable-next-line:no-any | ||
detail?: any | ||
) { | ||
if (this.dispatched) { | ||
return; | ||
} | ||
|
||
customEventAttributes.set(this, { detail }); | ||
} | ||
} | ||
|
||
/** Built-in objects providing `get` methods for our | ||
* interceptable JavaScript operations. | ||
*/ | ||
Reflect.defineProperty(CustomEvent.prototype, "detail", { enumerable: true }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2018 the Deno authors. All rights reserved. MIT license. | ||
import { test, assertEqual } from "./test_util.ts"; | ||
|
||
test(function customEventInitializedWithDetail() { | ||
const type = "touchstart"; | ||
const detail = { message: "hello" }; | ||
const customEventDict = new CustomEventInit({ | ||
bubbles: true, | ||
cancelable: true, | ||
detail | ||
}); | ||
const event = new CustomEvent(type, customEventDict); | ||
|
||
assertEqual(event.bubbles, true); | ||
assertEqual(event.cancelable, true); | ||
assertEqual(event.currentTarget, null); | ||
assertEqual(event.detail, detail); | ||
assertEqual(event.isTrusted, false); | ||
assertEqual(event.target, null); | ||
assertEqual(event.type, type); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters