-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Lexical: Started converting drawio to TS
Converted events service to TS as part of this.
- Loading branch information
1 parent
5002a89
commit 634b0aa
Showing
9 changed files
with
143 additions
and
113 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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import {ComponentStore} from "./services/components"; | ||
import {EventManager} from "./services/events"; | ||
|
||
declare global { | ||
interface Window { | ||
$components: ComponentStore, | ||
$events: EventManager, | ||
} | ||
} |
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
export class EventManager { | ||
protected listeners: Record<string, ((data: {}) => void)[]> = {}; | ||
protected stack: {name: string, data: {}}[] = []; | ||
|
||
/** | ||
* Emit a custom event for any handlers to pick-up. | ||
*/ | ||
emit(eventName: string, eventData: {}): void { | ||
this.stack.push({name: eventName, data: eventData}); | ||
|
||
const listenersToRun = this.listeners[eventName] || []; | ||
for (const listener of listenersToRun) { | ||
listener(eventData); | ||
} | ||
} | ||
|
||
/** | ||
* Listen to a custom event and run the given callback when that event occurs. | ||
*/ | ||
listen(eventName: string, callback: (data: {}) => void): void { | ||
if (typeof this.listeners[eventName] === 'undefined') this.listeners[eventName] = []; | ||
this.listeners[eventName].push(callback); | ||
} | ||
|
||
/** | ||
* Emit an event for public use. | ||
* Sends the event via the native DOM event handling system. | ||
*/ | ||
emitPublic(targetElement: Element, eventName: string, eventData: {}): void { | ||
const event = new CustomEvent(eventName, { | ||
detail: eventData, | ||
bubbles: true, | ||
}); | ||
targetElement.dispatchEvent(event); | ||
} | ||
|
||
/** | ||
* Emit a success event with the provided message. | ||
*/ | ||
success(message: string): void { | ||
this.emit('success', message); | ||
} | ||
|
||
/** | ||
* Emit an error event with the provided message. | ||
*/ | ||
error(message: string): void { | ||
this.emit('error', message); | ||
} | ||
|
||
/** | ||
* Notify of standard server-provided validation errors. | ||
*/ | ||
showValidationErrors(responseErr: {status?: number, data?: object}): void { | ||
if (!responseErr.status) return; | ||
if (responseErr.status === 422 && responseErr.data) { | ||
const message = Object.values(responseErr.data).flat().join('\n'); | ||
this.error(message); | ||
} | ||
} | ||
|
||
/** | ||
* Notify standard server-provided error messages. | ||
*/ | ||
showResponseError(responseErr: {status?: number, data?: {message?: string}}): void { | ||
if (!responseErr.status) return; | ||
if (responseErr.status >= 400 && responseErr.data && responseErr.data.message) { | ||
this.error(responseErr.data.message); | ||
} | ||
} | ||
} |
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