2020-11-27 20:28:54 +00: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 {Link} from "./Link.js";
|
2020-11-30 10:24:08 +00:00
|
|
|
import {ViewModel} from "./utils/ViewModel.js";
|
2020-12-02 14:36:54 +00:00
|
|
|
import {OpenLinkViewModel} from "./open/OpenLinkViewModel.js";
|
|
|
|
import {createClients} from "./open/clients/index.js";
|
|
|
|
import {CreateLinkViewModel} from "./create/CreateLinkViewModel.js";
|
2020-12-04 09:33:31 +00:00
|
|
|
import {LoadServerPolicyViewModel} from "./policy/LoadServerPolicyViewModel.js";
|
2021-08-30 21:14:31 +00:00
|
|
|
import {InvalidUrlViewModel} from "./InvalidUrlViewModel.js";
|
2020-12-02 14:36:54 +00:00
|
|
|
import {Platform} from "./Platform.js";
|
2020-11-27 20:28:54 +00:00
|
|
|
|
|
|
|
export class RootViewModel extends ViewModel {
|
2021-07-23 11:25:21 +00:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
this.link = null;
|
|
|
|
this.openLinkViewModel = null;
|
|
|
|
this.createLinkViewModel = null;
|
2020-12-04 09:33:31 +00:00
|
|
|
this.loadServerPolicyViewModel = null;
|
2021-08-30 21:14:31 +00:00
|
|
|
this.invalidUrlViewModel = null;
|
2021-08-25 21:47:42 +00:00
|
|
|
this.showDisclaimer = false;
|
2020-12-04 12:59:52 +00:00
|
|
|
this.preferences.on("canClear", () => {
|
|
|
|
this.emitChange();
|
|
|
|
});
|
2021-07-23 11:25:21 +00:00
|
|
|
}
|
2020-11-27 20:28:54 +00:00
|
|
|
|
2021-08-30 18:08:58 +00:00
|
|
|
_updateChildVMs(newLink, oldLink) {
|
|
|
|
this.link = newLink;
|
|
|
|
if (!newLink) {
|
|
|
|
this.openLinkViewModel = null;
|
|
|
|
} else if (!oldLink || !oldLink.equals(newLink)) {
|
2021-08-30 18:01:56 +00:00
|
|
|
this.openLinkViewModel = new OpenLinkViewModel(this.childOptions({
|
2021-08-30 18:08:58 +00:00
|
|
|
link: newLink,
|
2021-08-30 18:01:56 +00:00
|
|
|
clients: createClients(),
|
|
|
|
}));
|
2021-07-23 11:25:21 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-30 10:24:08 +00:00
|
|
|
|
2021-08-25 22:06:13 +00:00
|
|
|
_hideLinks() {
|
2021-08-25 21:47:42 +00:00
|
|
|
this.link = null;
|
|
|
|
this.openLinkViewModel = null;
|
|
|
|
this.createLinkViewModel = null;
|
|
|
|
}
|
|
|
|
|
2021-07-23 11:25:21 +00:00
|
|
|
updateHash(hash) {
|
2021-08-30 18:08:58 +00:00
|
|
|
// All view models except openLink are re-created anyway. Might as well
|
|
|
|
// clear them to avoid having to manually reset (n-1)/n view models in every case.
|
|
|
|
// That just doesn't scale well when we add new views.
|
2021-08-30 18:01:56 +00:00
|
|
|
const oldLink = this.link;
|
2021-08-30 21:14:31 +00:00
|
|
|
this.invalidUrlViewModel = null;
|
2021-08-25 21:47:42 +00:00
|
|
|
this.showDisclaimer = false;
|
2021-08-30 18:01:56 +00:00
|
|
|
this.loadServerPolicyViewModel = null;
|
|
|
|
this.createLinkViewModel = null;
|
2021-08-30 18:08:58 +00:00
|
|
|
let newLink;
|
2020-12-04 09:33:31 +00:00
|
|
|
if (hash.startsWith("#/policy/")) {
|
2022-04-13 23:22:33 +00:00
|
|
|
const server = hash.slice(9);
|
2021-08-30 18:08:58 +00:00
|
|
|
this._updateChildVMs(null, oldLink);
|
2020-12-04 09:33:31 +00:00
|
|
|
this.loadServerPolicyViewModel = new LoadServerPolicyViewModel(this.childOptions({server}));
|
|
|
|
this.loadServerPolicyViewModel.load();
|
2021-08-25 21:47:42 +00:00
|
|
|
} else if (hash.startsWith("#/disclaimer/")) {
|
2021-08-30 18:08:58 +00:00
|
|
|
this._updateChildVMs(null, oldLink);
|
2021-08-26 16:39:44 +00:00
|
|
|
this.showDisclaimer = true;
|
2021-08-30 18:01:56 +00:00
|
|
|
} else if (hash === "" || hash === "#" || hash === "#/") {
|
2021-08-30 18:08:58 +00:00
|
|
|
this._updateChildVMs(null, oldLink);
|
2021-08-30 18:01:56 +00:00
|
|
|
this.createLinkViewModel = new CreateLinkViewModel(this.childOptions());
|
2021-09-14 08:44:27 +00:00
|
|
|
} else if (newLink = Link.parseFragment(hash)) {
|
2021-08-30 18:08:58 +00:00
|
|
|
this._updateChildVMs(newLink, oldLink);
|
2021-08-30 19:05:42 +00:00
|
|
|
} else {
|
|
|
|
this._updateChildVMs(null, oldLink);
|
2021-08-30 21:14:31 +00:00
|
|
|
this.invalidUrlViewModel = new InvalidUrlViewModel(this.childOptions({
|
|
|
|
fragment: hash
|
|
|
|
}));
|
2020-12-04 09:33:31 +00:00
|
|
|
}
|
2021-08-30 18:01:56 +00:00
|
|
|
this.emitChange();
|
2021-07-23 11:25:21 +00:00
|
|
|
}
|
2020-11-30 09:57:39 +00:00
|
|
|
|
2021-07-23 11:25:21 +00:00
|
|
|
clearPreferences() {
|
|
|
|
this.preferences.clear();
|
|
|
|
this._updateChildVMs();
|
|
|
|
}
|
2020-12-01 11:06:37 +00:00
|
|
|
|
2021-07-23 11:25:21 +00:00
|
|
|
get hasPreferences() {
|
|
|
|
return this.preferences.canClear;
|
|
|
|
}
|
2020-12-01 11:06:37 +00:00
|
|
|
}
|