2019-02-20 19:23:56 +00:00
|
|
|
export class ConnectionView {
|
2019-04-17 20:51:16 +00:00
|
|
|
constructor(client, provider) {
|
|
|
|
this._provider = provider;
|
2019-02-22 14:48:49 +00:00
|
|
|
this._client = client;
|
|
|
|
this._client.onopen = () => { this._onConnectionOpened(); };
|
|
|
|
this._client.onclose = () => { this._onConnectionClosed(); };
|
2019-01-28 21:08:37 +00:00
|
|
|
|
2019-02-13 20:09:43 +00:00
|
|
|
this.element = document.createElement("div");
|
2019-01-28 21:08:37 +00:00
|
|
|
|
2019-04-01 20:15:12 +00:00
|
|
|
const connectBox = document.createElement("div");
|
|
|
|
this.element.appendChild(connectBox);
|
|
|
|
|
2019-02-22 14:48:49 +00:00
|
|
|
const urlLabel = document.createElement("span");
|
2019-02-13 20:09:43 +00:00
|
|
|
urlLabel.textContent = "URL:";
|
2019-04-01 20:15:12 +00:00
|
|
|
connectBox.appendChild(urlLabel);
|
2019-01-28 21:08:37 +00:00
|
|
|
|
2019-02-13 20:09:43 +00:00
|
|
|
this.urlTextbox = document.createElement("input");
|
|
|
|
this.urlTextbox.type = "text";
|
2019-01-28 21:08:37 +00:00
|
|
|
this.urlTextbox.value = window.location.href.replace(/^http/, "ws");
|
2019-04-01 20:15:12 +00:00
|
|
|
connectBox.appendChild(this.urlTextbox);
|
2019-01-28 21:08:37 +00:00
|
|
|
|
2019-02-13 20:09:43 +00:00
|
|
|
this.connectButton = document.createElement("input");
|
|
|
|
this.connectButton.type = "button";
|
|
|
|
this.connectButton.value = "connect";
|
2019-02-22 14:48:49 +00:00
|
|
|
this.connectButton.addEventListener("click", () => { this._onConnectButtonClicked(); });
|
2019-04-01 20:15:12 +00:00
|
|
|
connectBox.appendChild(this.connectButton);
|
|
|
|
|
|
|
|
|
|
|
|
const authenticateBox = document.createElement("div");
|
|
|
|
this.element.appendChild(authenticateBox);
|
|
|
|
|
2019-04-17 20:51:16 +00:00
|
|
|
const authLabel = document.createElement("span");
|
|
|
|
authLabel.textContent = "use authentication:";
|
|
|
|
authenticateBox.appendChild(authLabel);
|
|
|
|
|
|
|
|
this.authenticateCheckbox = document.createElement("input");
|
|
|
|
this.authenticateCheckbox.type = "checkbox";
|
|
|
|
authenticateBox.appendChild(this.authenticateCheckbox);
|
|
|
|
|
2019-04-01 20:15:12 +00:00
|
|
|
const usernameLabel = document.createElement("span");
|
|
|
|
usernameLabel.textContent = "user:";
|
|
|
|
authenticateBox.appendChild(usernameLabel);
|
|
|
|
|
|
|
|
this.usernameTextbox = document.createElement("input");
|
|
|
|
this.usernameTextbox.type = "text";
|
|
|
|
this.usernameTextbox.value = "bob";
|
|
|
|
authenticateBox.appendChild(this.usernameTextbox);
|
|
|
|
|
|
|
|
const passwordLabel = document.createElement("span");
|
|
|
|
passwordLabel.textContent = "user:";
|
|
|
|
authenticateBox.appendChild(passwordLabel);
|
|
|
|
|
|
|
|
this.passwordTextbox = document.createElement("input");
|
|
|
|
this.passwordTextbox.type = "password";
|
|
|
|
this.passwordTextbox.value = "secret";
|
|
|
|
authenticateBox.appendChild(this.passwordTextbox);
|
2019-01-28 21:08:37 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 14:48:49 +00:00
|
|
|
_onConnectButtonClicked() {
|
|
|
|
if (!this._client.isConnected()) {
|
2019-01-28 21:08:37 +00:00
|
|
|
let url = this.urlTextbox.value;
|
2019-02-22 14:48:49 +00:00
|
|
|
this._client.connectTo(url);
|
2019-01-28 21:08:37 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-02-22 14:48:49 +00:00
|
|
|
this._client.disconnect();
|
2019-01-28 21:08:37 +00:00
|
|
|
}
|
|
|
|
}
|
2019-02-22 14:48:49 +00:00
|
|
|
|
2019-04-01 20:15:12 +00:00
|
|
|
_onAuthenticateButtonClicked() {
|
|
|
|
if (this._client.isConnected()) {
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 14:48:49 +00:00
|
|
|
_onConnectionOpened() {
|
2019-04-17 20:51:16 +00:00
|
|
|
if (this.authenticateCheckbox.checked) {
|
|
|
|
const username = this.usernameTextbox.value;
|
|
|
|
const password = this.passwordTextbox.value;
|
|
|
|
|
|
|
|
const promise = this._client.authenticate("username", { username, password });
|
|
|
|
promise.then(() => { this._client.addProvider("test", this._provider); });
|
|
|
|
} else {
|
|
|
|
this._client.addProvider("test", this._provider);
|
|
|
|
}
|
|
|
|
|
2019-02-13 20:09:43 +00:00
|
|
|
this.connectButton.value = "disconnect";
|
2019-01-28 21:08:37 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 14:48:49 +00:00
|
|
|
_onConnectionClosed() {
|
2019-02-13 20:09:43 +00:00
|
|
|
this.connectButton.value = "connect";
|
2019-01-28 21:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|