matrix.to/src/open/ServerConsentView.js

124 lines
4.4 KiB
JavaScript
Raw Normal View History

2020-12-04 06:34:31 -05: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) {
const useAnotherServer = t.button({
className: "text",
onClick: () => vm.setShowServers()}, "use another server");
const continueWithoutPreview = t.button({
className: "text",
onClick: () => vm.continueWithoutConsent()
}, "continue without a preview");
return t.div({className: "ServerConsentView"}, [
t.p([
"View this link using ",
t.strong(vm => vm.selectedServer || "…"),
t.span({className: {hidden: vm => !vm.selectedServer}}, [
" (",
t.a({
href: vm => `#/policy/${vm.selectedServer}`,
target: "_blank",
}, "privacy policy"),
") ",
]),
t.span({className: {hidden: vm => vm.showSelectServer}}, [
" to preview content, or your can ",
useAnotherServer,
]),
" or ",
continueWithoutPreview,
"."
]),
t.form({action: "#", onSubmit: evt => this._onSubmit(evt)}, [
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 06:34:31 -05:00
t.input({type: "submit", value: "Continue", className: "primary fullwidth"})
])
])
]);
}
_onSubmit(evt) {
evt.preventDefault();
const {askEveryTime} = evt.target.elements;
this.value.continueWithSelection(askEveryTime.checked);
2020-12-04 06:34:31 -05:00
}
}
class ServerOptions extends TemplateView {
render(t, vm) {
const options = vm.servers.map(server => {
return t.div(t.label([t.input({type: "radio", name: "selectedServer", value: server}), t.span(server)]))
});
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 07:09:37 -05:00
const textField = evt.target;
if(!this.value.selectOtherServer(value)) {
textField.setCustomValidity("Please enter a valid domain name");
} else {
textField.setCustomValidity("");
}
2020-12-04 06:34:31 -05: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 07:09:37 -05:00
otherServer.setCustomValidity("");
2020-12-04 06:34:31 -05:00
}
this.value.selectServer(value);
}
}