forked from MicrosoftDX/Vorlonjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vorlon.dashboardPlugin.ts
100 lines (85 loc) · 4.36 KB
/
vorlon.dashboardPlugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
module VORLON {
export class DashboardPlugin extends BasePlugin {
public htmlFragmentUrl;
public cssStyleSheetUrl;
public DashboardCommands: any;
constructor(name: string, htmlFragmentUrl: string, cssStyleSheetUrl: string) {
super(name);
this.htmlFragmentUrl = htmlFragmentUrl;
this.cssStyleSheetUrl = cssStyleSheetUrl;
this.debug = Core.debug;
}
public startDashboardSide(div: HTMLDivElement): void { }
public onRealtimeMessageReceivedFromClientSide(receivedObject: any): void { }
public sendToClient(data: any){
if (Core.Messenger)
Core.Messenger.sendRealtimeMessage(this.getID(), data, RuntimeSide.Dashboard, "message");
}
public sendCommandToClient(command: string, data: any = null, incrementVisualIndicator: boolean = false) {
if (Core.Messenger) {
this.trace(this.getID() + ' send command to client ' + command);
Core.Messenger.sendRealtimeMessage(this.getID(), data, RuntimeSide.Dashboard, "message", incrementVisualIndicator, command);
}
}
public sendCommandToPluginClient(pluginId: string, command: string, data: any = null, incrementVisualIndicator: boolean = false) {
if (Core.Messenger) {
this.trace(this.getID() + ' send command to plugin client ' + command);
Core.Messenger.sendRealtimeMessage(pluginId, data, RuntimeSide.Dashboard, "protocol", incrementVisualIndicator, command);
}
}
public sendCommandToPluginDashboard(pluginId : string, command: string, data: any = null, incrementVisualIndicator: boolean = false) {
if (Core.Messenger) {
this.trace(this.getID() + ' send command to plugin dashboard ' + command);
Core.Messenger.sendRealtimeMessage(pluginId, data, RuntimeSide.Client, "protocol", incrementVisualIndicator, command);
}
}
public _insertHtmlContentAsync(divContainer: HTMLDivElement, callback: (filledDiv: HTMLDivElement) => void): void {
var basedUrl = "/" + this.loadingDirectory + "/" + this.name + "/";
var alone = false;
if (!divContainer) {
// Not emptyDiv provided, let's plug into the main DOM
divContainer = document.createElement("div");
document.body.appendChild(divContainer);
alone = true;
}
var request = new XMLHttpRequest();
request.open('GET', basedUrl + this.htmlFragmentUrl, true);
request.onreadystatechange = (ev: Event) => {
if (request.readyState === 4) {
if (request.status === 200) {
divContainer.innerHTML = this._stripContent(request.responseText);
var headID = document.getElementsByTagName("head")[0];
var cssNode = document.createElement('link');
cssNode.type = "text/css";
cssNode.rel = "stylesheet";
cssNode.href = basedUrl + this.cssStyleSheetUrl;
cssNode.media = "screen";
headID.appendChild(cssNode);
var firstDivChild = <HTMLDivElement>(divContainer.children[0]);
if (alone) {
firstDivChild.className = "alone";
}
callback(firstDivChild);
} else { // Failed
throw new Error("Error status: " + request.status + " - Unable to load " + basedUrl + this.htmlFragmentUrl);
}
}
};
request.send(null);
}
private _stripContent(content): string {
// in case of SVG injection
var xmlRegExp = /^\s*<\?xml(\s)+version=[\'\"](\d)*.(\d)*[\'\"](\s)*\?>/im;
// for HTML content
var bodyRegExp = /<body[^>]*>\s*([\s\S]+)\s*<\/body>/im;
if (content) {
content = content.replace(xmlRegExp, "");
var matches = content.match(bodyRegExp);
if (matches) {
content = matches[1];
}
}
return content;
}
}
}