env.esq/src/open/ServerConsentView.js

135 lines
4.7 KiB
JavaScript
Raw Normal View History

2020-12-04 12:34:31 +01:00
/*
Copyright 2020 The Matrix.org Foundation C.I.C.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
import {TemplateView} from "../utils/TemplateView.js";
import {ClientListView} from "./ClientListView.js";
import {PreviewView} from "../preview/PreviewView.js";
export class ServerConsentView extends TemplateView {
render(t, vm) {
2020-12-04 12:34:31 +01:00
const useAnotherServer = t.button({
className: "text",
onClick: () => vm.setShowServers()}, "use another server");
const continueWithoutPreview = t.button({
className: "text",
onClick: () => vm.continueWithoutConsent(this._askEveryTimeChecked)
2020-12-04 12:34:31 +01:00
}, "continue without a preview");
2021-07-23 13:25:21 +02:00
return t.div({className: "ServerConsentView"}, [
2020-12-04 12:34:31 +01:00
t.p([
2020-12-07 16:19:59 +01:00
"Preview this link using the ",
2020-12-04 12:34:31 +01:00
t.strong(vm => vm.selectedServer || "…"),
2020-12-07 16:19:59 +01:00
" homeserver ",
2020-12-04 12:34:31 +01:00
t.span({className: {hidden: vm => !vm.selectedServer}}, [
" (",
t.a({
href: vm => `#/policy/${vm.selectedServer}`,
target: "_blank",
}, "privacy policy"),
2020-12-07 16:19:59 +01:00
")",
2020-12-04 12:34:31 +01:00
]),
t.span({className: {hidden: vm => vm.showSelectServer}}, [
2020-12-07 16:19:59 +01:00
", ",
2020-12-04 12:34:31 +01:00
useAnotherServer,
]),
" or ",
continueWithoutPreview,
"."
]),
t.form({action: "#", id: "serverConsentForm", onSubmit: evt => this._onSubmit(evt)}, [
2020-12-04 12:34:31 +01:00
t.mapView(vm => vm.showSelectServer, show => show ? new ServerOptions(vm) : null),
t.div({className: "actions"}, [
t.label([t.input({type: "checkbox", name: "askEveryTime"}), "Ask every time"]),
2020-12-04 12:34:31 +01:00
t.input({type: "submit", value: "Continue", className: "primary fullwidth"})
])
])
]);
2021-07-23 13:25:21 +02:00
}
2020-12-04 12:34:31 +01:00
_onSubmit(evt) {
evt.preventDefault();
this.value.continueWithSelection(this._askEveryTimeChecked);
}
get _askEveryTimeChecked() {
const form = document.getElementById("serverConsentForm");
const {askEveryTime} = form.elements;
return askEveryTime.checked;
2020-12-04 12:34:31 +01:00
}
}
class ServerOptions extends TemplateView {
render(t, vm) {
const options = vm.servers.map(server => {
2020-12-07 16:20:17 +01:00
return t.div(t.label([t.input({
type: "radio",
name: "selectedServer",
value: server,
checked: server === vm.selectedServer
}), t.span(server)]))
2020-12-04 12:34:31 +01:00
});
options.push(t.div({className: "other"}, t.label([
t.input({type: "radio", name: "selectedServer", value: "other"}),
t.input({
type: "text",
className: "line",
placeholder: "Other",
name: "otherServer",
onClick: evt => this._onClickOther(evt),
})
])));
return t.div({
className: "ServerOptions",
onChange: evt => this._onChange(evt),
}, options);
}
_onClickOther(evt) {
const textField = evt.target;
const radio = Array.from(textField.form.elements.selectedServer).find(r => r.value === "other");
if (!radio.checked) {
radio.checked = true;
this._onChangeServerRadio(radio);
}
}
_onChange(evt) {
let {name, value} = evt.target;
if (name === "selectedServer") {
this._onChangeServerRadio(evt.target);
} else if (name === "otherServer") {
2020-12-04 13:09:37 +01:00
const textField = evt.target;
if(!this.value.selectOtherServer(value)) {
textField.setCustomValidity("Please enter a valid domain name");
} else {
textField.setCustomValidity("");
}
2020-12-04 12:34:31 +01:00
}
}
_onChangeServerRadio(radio) {
let {value, form} = radio;
const {otherServer} = form.elements;
if (value === "other") {
otherServer.required = true;
value = otherServer.value;
} else {
otherServer.required = false;
2020-12-04 13:09:37 +01:00
otherServer.setCustomValidity("");
2020-12-04 12:34:31 +01:00
}
this.value.selectServer(value);
}
}