-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
story(playground): Add gravity playground. Add some fixes and feature…
…s for playground
- Loading branch information
Showing
28 changed files
with
885 additions
and
102 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1,87 +1,86 @@ | ||
import { Component } from "../lib/Component"; | ||
import { Constructor } from "../lib/tshelpers"; | ||
|
||
export function withEvent<T extends Constructor<Component>>(Base: T): T & Constructor<IWithEvent> { | ||
return class WithEvent extends Base implements IWithEvent { | ||
private _listenEvents: object = {}; | ||
export class EventedComponent extends Component { | ||
private _listenEvents: object = {}; | ||
|
||
public unmount() { | ||
super.unmount(); | ||
this._listenEvents = {}; | ||
} | ||
public readonly cursor?: string; | ||
|
||
public hasEventListener(type: string) { | ||
return Object.prototype.hasOwnProperty.call(this._listenEvents, type); | ||
} | ||
protected unmount() { | ||
super.unmount(); | ||
this._listenEvents = {}; | ||
} | ||
|
||
public addEventListener(type: string, cbOrObject: any) { | ||
if (Array.isArray(this._listenEvents[type])) { | ||
this._listenEvents[type].push(cbOrObject); | ||
} else { | ||
this._listenEvents[type] = [cbOrObject]; | ||
} | ||
public hasEventListener(type: string) { | ||
return Object.prototype.hasOwnProperty.call(this._listenEvents, type); | ||
} | ||
|
||
public addEventListener(type: string, cbOrObject: any) { | ||
if (Array.isArray(this._listenEvents[type])) { | ||
this._listenEvents[type].push(cbOrObject); | ||
} else { | ||
this._listenEvents[type] = [cbOrObject]; | ||
} | ||
return () => this.removeEventListener(type, cbOrObject); | ||
} | ||
|
||
public removeEventListener(type: string, cbOrObject: any) { | ||
if (Array.isArray(this._listenEvents[type])) { | ||
const i = this._listenEvents[type].indexOf(cbOrObject); | ||
public removeEventListener(type: string, cbOrObject: any) { | ||
if (Array.isArray(this._listenEvents[type])) { | ||
const i = this._listenEvents[type].indexOf(cbOrObject); | ||
|
||
if (i !== -1) { | ||
this._listenEvents[type].splice(i, 1); | ||
} | ||
if (i !== -1) { | ||
this._listenEvents[type].splice(i, 1); | ||
} | ||
} | ||
} | ||
|
||
public _fireEvent(cmp: any, event: Event) { | ||
if (!this._hasListener(cmp, event.type)) { | ||
return; | ||
} | ||
public _fireEvent(cmp: any, event: Event) { | ||
if (!this._hasListener(cmp, event.type)) { | ||
return; | ||
} | ||
|
||
const fnsOrObjects = cmp._listenEvents[event.type]; | ||
const fnsOrObjects = cmp._listenEvents[event.type]; | ||
|
||
for (let i = 0; i < fnsOrObjects.length; i += 1) { | ||
if (typeof fnsOrObjects[i] === "function") { | ||
fnsOrObjects[i](event); | ||
} else if (typeof fnsOrObjects[i] === "object" && typeof fnsOrObjects[i].handleEvent === "function") { | ||
fnsOrObjects[i].handleEvent(event); | ||
} | ||
for (let i = 0; i < fnsOrObjects.length; i += 1) { | ||
if (typeof fnsOrObjects[i] === "function") { | ||
fnsOrObjects[i](event); | ||
} else if (typeof fnsOrObjects[i] === "object" && typeof fnsOrObjects[i].handleEvent === "function") { | ||
fnsOrObjects[i].handleEvent(event); | ||
} | ||
} | ||
} | ||
|
||
public dispatchEvent(event: Event): boolean { | ||
const bubbles = event.bubbles || false; | ||
public dispatchEvent(event: Event): boolean { | ||
const bubbles = event.bubbles || false; | ||
|
||
if (bubbles) { | ||
return this._dipping(this, event); | ||
} else if (this._hasListener(this, event.type)) { | ||
this._fireEvent(this, event); | ||
return false; | ||
} | ||
if (bubbles) { | ||
return this._dipping(this, event); | ||
} else if (this._hasListener(this, event.type)) { | ||
this._fireEvent(this, event); | ||
return false; | ||
} | ||
return false; | ||
} | ||
|
||
public _dipping(startParent: Component, event: Event) { | ||
let stopPropagation = false; | ||
let parent: Component | undefined = startParent; | ||
event.stopPropagation = () => { | ||
stopPropagation = true; | ||
}; | ||
|
||
do { | ||
this._fireEvent(parent, event); | ||
if (stopPropagation) { | ||
return false; | ||
} | ||
parent = parent.getParent() as Component; | ||
} while (parent); | ||
|
||
public _dipping(startParent: Component, event: Event) { | ||
let stopPropagation = false; | ||
let parent: Component | undefined = startParent; | ||
event.stopPropagation = () => { | ||
stopPropagation = true; | ||
}; | ||
|
||
do { | ||
this._fireEvent(parent, event); | ||
if (stopPropagation) { | ||
return false; | ||
} | ||
parent = parent.getParent() as Component; | ||
} while (parent); | ||
|
||
return true; | ||
} | ||
return true; | ||
} | ||
|
||
public _hasListener(comp: any, type: string) { | ||
return comp._listenEvents !== undefined && comp._listenEvents[type] !== undefined; | ||
} | ||
}; | ||
public _hasListener(comp: any, type: string) { | ||
return comp._listenEvents !== undefined && comp._listenEvents[type] !== undefined; | ||
} | ||
} | ||
|
||
export class EventedComponent extends withEvent(Component) {} |
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
Oops, something went wrong.