Cleanup view switching code a bit

This commit is contained in:
Danila Fedorin 2021-08-30 11:08:58 -07:00
parent 878663ca07
commit 628e99ba2d

View file

@ -35,10 +35,13 @@ export class RootViewModel extends ViewModel {
}); });
} }
_updateChildVMs(oldLink) { _updateChildVMs(newLink, oldLink) {
if (!oldLink || !oldLink.equals(this.link)) { this.link = newLink;
if (!newLink) {
this.openLinkViewModel = null;
} else if (!oldLink || !oldLink.equals(newLink)) {
this.openLinkViewModel = new OpenLinkViewModel(this.childOptions({ this.openLinkViewModel = new OpenLinkViewModel(this.childOptions({
link: this.link, link: newLink,
clients: createClients(), clients: createClients(),
})); }));
} }
@ -51,28 +54,27 @@ export class RootViewModel extends ViewModel {
} }
updateHash(hash) { updateHash(hash) {
// All view models except openLink are re-created anyway. // All view models except openLink are re-created anyway. Might as well
// Might as well clear them to avoid having to // clear them to avoid having to manually reset (n-1)/n view models in every case.
// manually reset (n-1)/n view models in every case. // That just doesn't scale well when we add new views.
const oldLink = this.link; const oldLink = this.link;
this.link = null;
this.showDisclaimer = false; this.showDisclaimer = false;
this.loadServerPolicyViewModel = null; this.loadServerPolicyViewModel = null;
this.createLinkViewModel = null; this.createLinkViewModel = null;
let newLink;
if (hash.startsWith("#/policy/")) { if (hash.startsWith("#/policy/")) {
const server = hash.substr(9); const server = hash.substr(9);
this.openLinkViewModel = null; this._updateChildVMs(null, oldLink);
this.loadServerPolicyViewModel = new LoadServerPolicyViewModel(this.childOptions({server})); this.loadServerPolicyViewModel = new LoadServerPolicyViewModel(this.childOptions({server}));
this.loadServerPolicyViewModel.load(); this.loadServerPolicyViewModel.load();
} else if (hash.startsWith("#/disclaimer/")) { } else if (hash.startsWith("#/disclaimer/")) {
this.openLinkViewModel = null; this._updateChildVMs(null, oldLink);
this.showDisclaimer = true; this.showDisclaimer = true;
} else if (hash === "" || hash === "#" || hash === "#/") { } else if (hash === "" || hash === "#" || hash === "#/") {
this.openLinkViewModel = null; this._updateChildVMs(null, oldLink);
this.createLinkViewModel = new CreateLinkViewModel(this.childOptions()); this.createLinkViewModel = new CreateLinkViewModel(this.childOptions());
} else if (this.link = Link.parse(hash)) { } else if (newLink = Link.parse(hash)) {
this.link = Link.parse(hash); this._updateChildVMs(newLink, oldLink);
this._updateChildVMs(oldLink);
} }
this.emitChange(); this.emitChange();
} }