forked from MicrosoftDX/Vorlonjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vorlon.clientPlugin.ts
70 lines (59 loc) · 2.55 KB
/
vorlon.clientPlugin.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
module VORLON {
declare var vorlonBaseURL: string;
export class ClientPlugin extends BasePlugin {
public ClientCommands: any;
constructor(name: string) {
super(name);
}
public startClientSide(): void { }
public onRealtimeMessageReceivedFromDashboardSide(receivedObject: any): void { }
public sendToDashboard(data: any){
if (Core.Messenger)
Core.Messenger.sendRealtimeMessage(this.getID(), data, RuntimeSide.Client, "message");
}
public sendCommandToDashboard(command: string, data: any = null) {
if (Core.Messenger) {
this.trace(this.getID() + ' send command to dashboard ' + command);
Core.Messenger.sendRealtimeMessage(this.getID(), data, RuntimeSide.Client, "message", command);
}
}
public refresh(): void {
console.error("Please override plugin.refresh()");
}
public _loadNewScriptAsync(scriptName: string, callback: () => void, waitForDOMContentLoaded?: boolean) {
var basedUrl = "";
if(this.loadingDirectory.indexOf('http') === 0){
if (scriptName[0] == "/"){
basedUrl = "";
} else {
basedUrl = this.loadingDirectory + "/" + this.name + "/";
}
}
else{
if (scriptName[0] == "/"){
basedUrl = vorlonBaseURL;
} else {
basedUrl = vorlonBaseURL + "/" + this.loadingDirectory + "/" + this.name + "/";
}
}
if(Core.IsHttpsEnabled && basedUrl.indexOf('https://') === -1){
basedUrl = basedUrl.replace(/^http/, "https");
}
function loadScript() {
var scriptToLoad = document.createElement("script");
scriptToLoad.setAttribute("src", basedUrl + scriptName);
scriptToLoad.onload = callback;
var first = document.getElementsByTagName('script')[0];
first.parentNode.insertBefore(scriptToLoad, first);
}
if (!waitForDOMContentLoaded || document.body) {
loadScript();
}
else {
document.addEventListener("DOMContentLoaded", () => {
this._loadNewScriptAsync(scriptName, callback, waitForDOMContentLoaded);
});
}
}
}
}