Finished WindowAPI for the frontend
This commit is contained in:
22
WindowAPI/dist/WindowAPI.d.ts
vendored
Normal file
22
WindowAPI/dist/WindowAPI.d.ts
vendored
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Getters } from "./helper/functions/Getter.js";
|
||||
import { Setters } from "./helper/functions/Setter.js";
|
||||
import { WindowEventEmitter } from "./helper/functions/WindowEventEmitter.js";
|
||||
import { Actions } from "./helper/functions/Actions.js";
|
||||
export declare class WindowAPI {
|
||||
private currentEventListener;
|
||||
constructor();
|
||||
private sendRequest;
|
||||
private getMethod;
|
||||
private setMethod;
|
||||
get: Getters;
|
||||
set: Setters;
|
||||
action: Actions;
|
||||
event: WindowEventEmitter;
|
||||
private openWindow;
|
||||
private closeWindow;
|
||||
private sendDataToWindow;
|
||||
private close;
|
||||
private focus;
|
||||
private notification;
|
||||
private openPopup;
|
||||
}
|
||||
132
WindowAPI/dist/WindowAPI.js
vendored
Normal file
132
WindowAPI/dist/WindowAPI.js
vendored
Normal file
@@ -0,0 +1,132 @@
|
||||
import { WindowEventEmitter } from "./helper/functions/WindowEventEmitter.js";
|
||||
export class WindowAPI {
|
||||
constructor() {
|
||||
this.get = {
|
||||
title: this.getMethod('title'),
|
||||
size: this.getMethod('size'),
|
||||
position: this.getMethod('size'),
|
||||
maximized: this.getMethod('maximized'),
|
||||
minimized: this.getMethod('minimized'),
|
||||
draggable: this.getMethod('draggable'),
|
||||
resizable: this.getMethod('resizable'),
|
||||
};
|
||||
this.set = {
|
||||
title: this.setMethod('title'),
|
||||
size: this.setMethod('size'),
|
||||
position: this.setMethod('position'),
|
||||
maximized: this.setMethod('maximized'),
|
||||
minimized: this.setMethod('minimized'),
|
||||
draggable: this.setMethod('draggable'),
|
||||
resizable: this.setMethod('resizable'),
|
||||
};
|
||||
this.action = {
|
||||
openWindow: this.openWindow.bind(this),
|
||||
closeWindow: this.closeWindow.bind(this),
|
||||
sendDataToWindow: this.sendDataToWindow.bind(this),
|
||||
close: this.close.bind(this),
|
||||
focus: this.focus.bind(this),
|
||||
notification: this.notification.bind(this),
|
||||
openPopup: this.openPopup.bind(this)
|
||||
};
|
||||
this.event = new class extends WindowEventEmitter {
|
||||
emit(name, data) {
|
||||
if (!this._events[name]) {
|
||||
throw new Error(`Can't emit an event. Event "${name}" doesn't exits.`);
|
||||
}
|
||||
const fire = (callback) => callback(data);
|
||||
this._events[name].forEach(fire);
|
||||
}
|
||||
on(name, listener) {
|
||||
if (!this._events[name]) {
|
||||
this._events[name] = [];
|
||||
}
|
||||
this._events[name].push(listener);
|
||||
}
|
||||
removeListener(name, listenerToRemove) {
|
||||
if (!this._events[name]) {
|
||||
throw new Error(`Can't remove a listener. Event "${name}" doesn't exits.`);
|
||||
}
|
||||
const filter = (listener) => listener !== listenerToRemove;
|
||||
this._events[name] = this._events[name].filter(filter);
|
||||
}
|
||||
};
|
||||
window.addEventListener('message', (event) => {
|
||||
const data = event.data;
|
||||
if (this.currentEventListener !== undefined && data.method !== "event") {
|
||||
this.currentEventListener(data);
|
||||
delete this.currentEventListener;
|
||||
}
|
||||
if (data.method === 'event') {
|
||||
const eventData = data.content;
|
||||
this.event.emit(data.event, eventData);
|
||||
}
|
||||
}, false);
|
||||
}
|
||||
sendRequest(data) {
|
||||
parent.postMessage(data, '*');
|
||||
}
|
||||
getMethod(variable) {
|
||||
return () => new Promise(resolve => {
|
||||
this.currentEventListener = (data) => {
|
||||
resolve(data.content);
|
||||
};
|
||||
this.sendRequest({ method: 'get', variable });
|
||||
});
|
||||
}
|
||||
setMethod(variable) {
|
||||
return (content) => {
|
||||
this.sendRequest({ method: "set", variable, content });
|
||||
};
|
||||
}
|
||||
openWindow(identifier, args, asPopup) {
|
||||
return new Promise(resolve => {
|
||||
this.currentEventListener = (data) => {
|
||||
resolve(data.content);
|
||||
};
|
||||
this.sendRequest({ method: 'action', action: 'openWindow', content: { identifier, args, asPopup } });
|
||||
});
|
||||
}
|
||||
closeWindow(uuid) {
|
||||
return new Promise(resolve => {
|
||||
this.currentEventListener = (data) => {
|
||||
resolve(data.content);
|
||||
};
|
||||
this.sendRequest({ method: 'action', action: 'closeWindow', content: { uuid } });
|
||||
});
|
||||
}
|
||||
sendDataToWindow(uuid, data) {
|
||||
return new Promise(resolve => {
|
||||
this.currentEventListener = (data) => {
|
||||
resolve(data.content);
|
||||
};
|
||||
this.sendRequest({ method: 'action', action: 'messageWindow', content: { uuid, data } });
|
||||
});
|
||||
}
|
||||
close() {
|
||||
return new Promise(resolve => {
|
||||
this.currentEventListener = () => resolve();
|
||||
this.sendRequest({ method: 'action', action: 'closeSelf' });
|
||||
});
|
||||
}
|
||||
focus() {
|
||||
return new Promise(resolve => {
|
||||
this.currentEventListener = () => resolve();
|
||||
this.sendRequest({ method: 'action', action: 'focus' });
|
||||
});
|
||||
}
|
||||
notification(notification) {
|
||||
return new Promise(resolve => {
|
||||
this.currentEventListener = () => resolve();
|
||||
this.sendRequest({ method: 'action', action: 'notification', content: notification });
|
||||
});
|
||||
}
|
||||
openPopup(data) {
|
||||
return new Promise(resolve => {
|
||||
this.currentEventListener = (data) => {
|
||||
resolve(data.content);
|
||||
};
|
||||
this.sendRequest({ method: 'action', action: 'popup', content: data });
|
||||
});
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=WindowAPI.js.map
|
||||
1
WindowAPI/dist/WindowAPI.js.map
vendored
Normal file
1
WindowAPI/dist/WindowAPI.js.map
vendored
Normal file
File diff suppressed because one or more lines are too long
5
WindowAPI/dist/helper/EventData.d.ts
vendored
Normal file
5
WindowAPI/dist/helper/EventData.d.ts
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
import { WindowEventName } from "./PackageTypes.js";
|
||||
export interface WindowEvent {
|
||||
type: WindowEventName;
|
||||
data: any;
|
||||
}
|
||||
2
WindowAPI/dist/helper/EventData.js
vendored
Normal file
2
WindowAPI/dist/helper/EventData.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=EventData.js.map
|
||||
1
WindowAPI/dist/helper/EventData.js.map
vendored
Normal file
1
WindowAPI/dist/helper/EventData.js.map
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"EventData.js","sourceRoot":"","sources":["../../src/helper/EventData.ts"],"names":[],"mappings":""}
|
||||
2
WindowAPI/dist/helper/Notification.d.ts
vendored
Normal file
2
WindowAPI/dist/helper/Notification.d.ts
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export declare class Notification {
|
||||
}
|
||||
3
WindowAPI/dist/helper/Notification.js
vendored
Normal file
3
WindowAPI/dist/helper/Notification.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export class Notification {
|
||||
}
|
||||
//# sourceMappingURL=Notification.js.map
|
||||
1
WindowAPI/dist/helper/Notification.js.map
vendored
Normal file
1
WindowAPI/dist/helper/Notification.js.map
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"Notification.js","sourceRoot":"","sources":["../../src/helper/Notification.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,YAAY;CAExB"}
|
||||
8
WindowAPI/dist/helper/Package.d.ts
vendored
Normal file
8
WindowAPI/dist/helper/Package.d.ts
vendored
Normal file
@@ -0,0 +1,8 @@
|
||||
import { PackageActionType, PackageMethod, PackageVariable, WindowEventName } from "./PackageTypes.js";
|
||||
export interface Package {
|
||||
method: PackageMethod;
|
||||
variable?: PackageVariable;
|
||||
action?: PackageActionType;
|
||||
event?: WindowEventName;
|
||||
content?: any;
|
||||
}
|
||||
2
WindowAPI/dist/helper/Package.js
vendored
Normal file
2
WindowAPI/dist/helper/Package.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=Package.js.map
|
||||
1
WindowAPI/dist/helper/Package.js.map
vendored
Normal file
1
WindowAPI/dist/helper/Package.js.map
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"Package.js","sourceRoot":"","sources":["../../src/helper/Package.ts"],"names":[],"mappings":""}
|
||||
4
WindowAPI/dist/helper/PackageTypes.d.ts
vendored
Normal file
4
WindowAPI/dist/helper/PackageTypes.d.ts
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
export declare type PackageMethod = "get" | "set" | "event" | "action";
|
||||
export declare type PackageVariable = "title" | "size" | "position" | "maximized" | "minimized" | "draggable" | "resizable";
|
||||
export declare type PackageActionType = "openWindow" | "closeWindow" | "closeSelf" | "focus" | "messageWindow" | "notification" | "popup";
|
||||
export declare type WindowEventName = "resize" | "move" | "openAsPopup" | "open" | "close";
|
||||
2
WindowAPI/dist/helper/PackageTypes.js
vendored
Normal file
2
WindowAPI/dist/helper/PackageTypes.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=PackageTypes.js.map
|
||||
1
WindowAPI/dist/helper/PackageTypes.js.map
vendored
Normal file
1
WindowAPI/dist/helper/PackageTypes.js.map
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"PackageTypes.js","sourceRoot":"","sources":["../../src/helper/PackageTypes.ts"],"names":[],"mappings":""}
|
||||
2
WindowAPI/dist/helper/PopupData.d.ts
vendored
Normal file
2
WindowAPI/dist/helper/PopupData.d.ts
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export declare class PopupData {
|
||||
}
|
||||
3
WindowAPI/dist/helper/PopupData.js
vendored
Normal file
3
WindowAPI/dist/helper/PopupData.js
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
export class PopupData {
|
||||
}
|
||||
//# sourceMappingURL=PopupData.js.map
|
||||
1
WindowAPI/dist/helper/PopupData.js.map
vendored
Normal file
1
WindowAPI/dist/helper/PopupData.js.map
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"PopupData.js","sourceRoot":"","sources":["../../src/helper/PopupData.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,SAAS;CAErB"}
|
||||
11
WindowAPI/dist/helper/functions/Actions.d.ts
vendored
Normal file
11
WindowAPI/dist/helper/functions/Actions.d.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
import { Notification } from "../Notification.js";
|
||||
import { PopupData } from "../PopupData.js";
|
||||
export interface Actions {
|
||||
openWindow(identifier: string, args?: string[], asPopup?: boolean): Promise<number>;
|
||||
closeWindow(uuid: number): Promise<boolean>;
|
||||
sendDataToWindow(uuid: number, data: any): Promise<boolean>;
|
||||
close(): Promise<void>;
|
||||
focus(): Promise<void>;
|
||||
notification(notification: Notification): Promise<void>;
|
||||
openPopup<T>(data: PopupData): Promise<T>;
|
||||
}
|
||||
2
WindowAPI/dist/helper/functions/Actions.js
vendored
Normal file
2
WindowAPI/dist/helper/functions/Actions.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=Actions.js.map
|
||||
1
WindowAPI/dist/helper/functions/Actions.js.map
vendored
Normal file
1
WindowAPI/dist/helper/functions/Actions.js.map
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"Actions.js","sourceRoot":"","sources":["../../../src/helper/functions/Actions.ts"],"names":[],"mappings":""}
|
||||
15
WindowAPI/dist/helper/functions/Getter.d.ts
vendored
Normal file
15
WindowAPI/dist/helper/functions/Getter.d.ts
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
export interface Getters {
|
||||
title(): Promise<string>;
|
||||
size(): Promise<{
|
||||
width: number;
|
||||
height: number;
|
||||
}>;
|
||||
position(): Promise<{
|
||||
x: number;
|
||||
y: number;
|
||||
}>;
|
||||
maximized(): Promise<boolean>;
|
||||
minimized(): Promise<boolean>;
|
||||
draggable(): Promise<boolean>;
|
||||
resizable(): Promise<boolean>;
|
||||
}
|
||||
2
WindowAPI/dist/helper/functions/Getter.js
vendored
Normal file
2
WindowAPI/dist/helper/functions/Getter.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=Getter.js.map
|
||||
1
WindowAPI/dist/helper/functions/Getter.js.map
vendored
Normal file
1
WindowAPI/dist/helper/functions/Getter.js.map
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"Getter.js","sourceRoot":"","sources":["../../../src/helper/functions/Getter.ts"],"names":[],"mappings":""}
|
||||
15
WindowAPI/dist/helper/functions/Setter.d.ts
vendored
Normal file
15
WindowAPI/dist/helper/functions/Setter.d.ts
vendored
Normal file
@@ -0,0 +1,15 @@
|
||||
export interface Setters {
|
||||
title(value: string): void;
|
||||
size(value: {
|
||||
width: number;
|
||||
height: number;
|
||||
}): void;
|
||||
position(value: {
|
||||
x: number;
|
||||
y: number;
|
||||
}): void;
|
||||
maximized(value: boolean): void;
|
||||
minimized(value: boolean): void;
|
||||
draggable(value: boolean): void;
|
||||
resizable(value: boolean): void;
|
||||
}
|
||||
2
WindowAPI/dist/helper/functions/Setter.js
vendored
Normal file
2
WindowAPI/dist/helper/functions/Setter.js
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
export {};
|
||||
//# sourceMappingURL=Setter.js.map
|
||||
1
WindowAPI/dist/helper/functions/Setter.js.map
vendored
Normal file
1
WindowAPI/dist/helper/functions/Setter.js.map
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"Setter.js","sourceRoot":"","sources":["../../../src/helper/functions/Setter.ts"],"names":[],"mappings":""}
|
||||
11
WindowAPI/dist/helper/functions/WindowEventEmitter.d.ts
vendored
Normal file
11
WindowAPI/dist/helper/functions/WindowEventEmitter.d.ts
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
import { WindowEventName } from "../PackageTypes.js";
|
||||
import { WindowEvent } from "../EventData.js";
|
||||
export declare type WindowEventListener = (event: WindowEvent) => void;
|
||||
export declare abstract class WindowEventEmitter {
|
||||
protected _events: {
|
||||
[name: string]: WindowEventListener[];
|
||||
};
|
||||
abstract on(name: WindowEventName, listener: WindowEventListener): void;
|
||||
abstract removeListener(name: WindowEventName, listenerToRemove: WindowEventListener): void;
|
||||
abstract emit(name: WindowEventName, data: WindowEvent): void;
|
||||
}
|
||||
6
WindowAPI/dist/helper/functions/WindowEventEmitter.js
vendored
Normal file
6
WindowAPI/dist/helper/functions/WindowEventEmitter.js
vendored
Normal file
@@ -0,0 +1,6 @@
|
||||
export class WindowEventEmitter {
|
||||
constructor() {
|
||||
this._events = {};
|
||||
}
|
||||
}
|
||||
//# sourceMappingURL=WindowEventEmitter.js.map
|
||||
1
WindowAPI/dist/helper/functions/WindowEventEmitter.js.map
vendored
Normal file
1
WindowAPI/dist/helper/functions/WindowEventEmitter.js.map
vendored
Normal file
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"WindowEventEmitter.js","sourceRoot":"","sources":["../../../src/helper/functions/WindowEventEmitter.ts"],"names":[],"mappings":"AAKA,MAAM,OAAgB,kBAAkB;IAAxC;QAEc,YAAO,GAA4C,EAAE,CAAC;IAMpE,CAAC;CAAA"}
|
||||
Reference in New Issue
Block a user