2020-11-27 15:28:54 -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 {Link} from "./Link.js";
|
2020-11-30 05:24:08 -05:00
|
|
|
import {ViewModel} from "./utils/ViewModel.js";
|
2020-12-02 09:36:54 -05:00
|
|
|
import {OpenLinkViewModel} from "./open/OpenLinkViewModel.js";
|
|
|
|
import {createClients} from "./open/clients/index.js";
|
|
|
|
import {CreateLinkViewModel} from "./create/CreateLinkViewModel.js";
|
2020-12-04 04:33:31 -05:00
|
|
|
import {LoadServerPolicyViewModel} from "./policy/LoadServerPolicyViewModel.js";
|
2021-08-30 17:14:31 -04:00
|
|
|
import {InvalidUrlViewModel} from "./InvalidUrlViewModel.js";
|
2020-12-02 09:36:54 -05:00
|
|
|
import {Platform} from "./Platform.js";
|
2020-11-27 15:28:54 -05:00
|
|
|
|
|
|
|
export class RootViewModel extends ViewModel {
|
2021-07-23 07:25:21 -04:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
this.link = null;
|
|
|
|
this.openLinkViewModel = null;
|
|
|
|
this.createLinkViewModel = null;
|
2020-12-04 04:33:31 -05:00
|
|
|
this.loadServerPolicyViewModel = null;
|
2021-08-30 17:14:31 -04:00
|
|
|
this.invalidUrlViewModel = null;
|
2021-08-25 17:47:42 -04:00
|
|
|
this.showDisclaimer = false;
|
2020-12-04 07:59:52 -05:00
|
|
|
this.preferences.on("canClear", () => {
|
|
|
|
this.emitChange();
|
|
|
|
});
|
2021-07-23 07:25:21 -04:00
|
|
|
}
|
2020-11-27 15:28:54 -05:00
|
|
|
|
2021-08-30 14:08:58 -04:00
|
|
|
_updateChildVMs(newLink, oldLink) {
|
|
|
|
this.link = newLink;
|
|
|
|
if (!newLink) {
|
|
|
|
this.openLinkViewModel = null;
|
|
|
|
} else if (!oldLink || !oldLink.equals(newLink)) {
|
2021-08-30 14:01:56 -04:00
|
|
|
this.openLinkViewModel = new OpenLinkViewModel(this.childOptions({
|
2021-08-30 14:08:58 -04:00
|
|
|
link: newLink,
|
2021-08-30 14:01:56 -04:00
|
|
|
clients: createClients(),
|
|
|
|
}));
|
2021-07-23 07:25:21 -04:00
|
|
|
}
|
|
|
|
}
|
2020-11-30 05:24:08 -05:00
|
|
|
|
2021-08-25 18:06:13 -04:00
|
|
|
_hideLinks() {
|
2021-08-25 17:47:42 -04:00
|
|
|
this.link = null;
|
|
|
|
this.openLinkViewModel = null;
|
|
|
|
this.createLinkViewModel = null;
|
|
|
|
}
|
|
|
|
|
2021-07-23 07:25:21 -04:00
|
|
|
updateHash(hash) {
|
2021-08-30 14:08:58 -04: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 14:01:56 -04:00
|
|
|
const oldLink = this.link;
|
2021-08-30 17:14:31 -04:00
|
|
|
this.invalidUrlViewModel = null;
|
2021-08-25 17:47:42 -04:00
|
|
|
this.showDisclaimer = false;
|
2021-08-30 14:01:56 -04:00
|
|
|
this.loadServerPolicyViewModel = null;
|
|
|
|
this.createLinkViewModel = null;
|
2021-08-30 14:08:58 -04:00
|
|
|
let newLink;
|
2020-12-04 04:33:31 -05:00
|
|
|
if (hash.startsWith("#/policy/")) {
|
|
|
|
const server = hash.substr(9);
|
2021-08-30 14:08:58 -04:00
|
|
|
this._updateChildVMs(null, oldLink);
|
2020-12-04 04:33:31 -05:00
|
|
|
this.loadServerPolicyViewModel = new LoadServerPolicyViewModel(this.childOptions({server}));
|
|
|
|
this.loadServerPolicyViewModel.load();
|
2021-08-25 17:47:42 -04:00
|
|
|
} else if (hash.startsWith("#/disclaimer/")) {
|
2021-08-30 14:08:58 -04:00
|
|
|
this._updateChildVMs(null, oldLink);
|
2021-08-26 12:39:44 -04:00
|
|
|
this.showDisclaimer = true;
|
2021-08-30 14:01:56 -04:00
|
|
|
} else if (hash === "" || hash === "#" || hash === "#/") {
|
2021-08-30 14:08:58 -04:00
|
|
|
this._updateChildVMs(null, oldLink);
|
2021-08-30 14:01:56 -04:00
|
|
|
this.createLinkViewModel = new CreateLinkViewModel(this.childOptions());
|
2021-09-14 04:44:27 -04:00
|
|
|
} else if (newLink = Link.parseFragment(hash)) {
|
2021-08-30 14:08:58 -04:00
|
|
|
this._updateChildVMs(newLink, oldLink);
|
2021-08-30 15:05:42 -04:00
|
|
|
} else {
|
|
|
|
this._updateChildVMs(null, oldLink);
|
2021-08-30 17:14:31 -04:00
|
|
|
this.invalidUrlViewModel = new InvalidUrlViewModel(this.childOptions({
|
|
|
|
fragment: hash
|
|
|
|
}));
|
2020-12-04 04:33:31 -05:00
|
|
|
}
|
2021-08-30 14:01:56 -04:00
|
|
|
this.emitChange();
|
2021-07-23 07:25:21 -04:00
|
|
|
}
|
2020-11-30 04:57:39 -05:00
|
|
|
|
2021-07-23 07:25:21 -04:00
|
|
|
clearPreferences() {
|
|
|
|
this.preferences.clear();
|
|
|
|
this._updateChildVMs();
|
|
|
|
}
|
2020-12-01 06:06:37 -05:00
|
|
|
|
2021-07-23 07:25:21 -04:00
|
|
|
get hasPreferences() {
|
|
|
|
return this.preferences.canClear;
|
|
|
|
}
|
2020-12-01 06:06:37 -05:00
|
|
|
}
|