forked from MicrosoftDX/Vorlonjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vorlon.dashboardPlugin.ts
122 lines (104 loc) · 5.63 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
module VORLON {
declare var vorlonBaseURL: string;
declare var $: any;
export class DashboardPlugin extends BasePlugin {
public htmlFragmentUrl;
public cssStyleSheetUrl;
public JavascriptSheetUrl;
public DashboardCommands: any;
constructor(name: string, htmlFragmentUrl: string, cssStyleSheetUrl?: (string | string[]), JavascriptSheetUrl?: (string | string[])) {
super(name);
this.htmlFragmentUrl = htmlFragmentUrl;
this.cssStyleSheetUrl = (cssStyleSheetUrl instanceof Array) ? cssStyleSheetUrl : (typeof cssStyleSheetUrl === 'undefined') ? [] : [cssStyleSheetUrl];
this.JavascriptSheetUrl = (JavascriptSheetUrl instanceof Array) ? JavascriptSheetUrl : (typeof JavascriptSheetUrl === 'undefined') ? [] : [JavascriptSheetUrl];
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) {
if (Core.Messenger) {
this.trace(this.getID() + ' send command to client ' + command);
Core.Messenger.sendRealtimeMessage(this.getID(), data, RuntimeSide.Dashboard, "message", command);
}
}
public sendCommandToPluginClient(pluginId: string, command: string, data: any = null) {
if (Core.Messenger) {
this.trace(this.getID() + ' send command to plugin client ' + command);
Core.Messenger.sendRealtimeMessage(pluginId, data, RuntimeSide.Dashboard, "protocol", command);
}
}
public sendCommandToPluginDashboard(pluginId : string, command: string, data: any = null) {
if (Core.Messenger) {
this.trace(this.getID() + ' send command to plugin dashboard ' + command);
Core.Messenger.sendRealtimeMessage(pluginId, data, RuntimeSide.Client, "protocol", command);
}
}
public _insertHtmlContentAsync(divContainer: HTMLDivElement, callback: (filledDiv: HTMLDivElement) => void): void {
var basedUrl = vorlonBaseURL + "/" + 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) {
var headID = document.getElementsByTagName("head")[0];
for (var i = 0; i < this.cssStyleSheetUrl.length; i++) {
var cssNode = document.createElement('link');
cssNode.type = "text/css";
cssNode.rel = "stylesheet";
cssNode.href = basedUrl + this.cssStyleSheetUrl[i];
cssNode.media = "screen";
headID.appendChild(cssNode);
}
for (var i = 0; i < this.JavascriptSheetUrl.length; i++) {
var jsNode = document.createElement('script');
jsNode.type = "text/javascript";
jsNode.src = basedUrl + this.JavascriptSheetUrl[i];
headID.appendChild(jsNode);
}
divContainer.innerHTML = this._stripContent(request.responseText);
if($(divContainer).find('.split').length && $(divContainer).find('.split').is(":visible") && !$(divContainer).find('.vsplitter').length) {
$(divContainer).find('.split').split({
orientation: $(divContainer).find('.split').data('orientation'),
limit: $(divContainer).find('.split').data('limit'),
position: $(divContainer).find('.split').data('position'),
});
}
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;
}
}
}