make login view enabled again if load view is not busy anymore

This commit is contained in:
Bruno Windels 2020-05-06 23:44:52 +02:00
parent e17fc57d9c
commit 06fc3101e8
4 changed files with 44 additions and 11 deletions

View File

@ -8,13 +8,26 @@ export class LoginViewModel extends ViewModel {
this._sessionCallback = sessionCallback; this._sessionCallback = sessionCallback;
this._defaultHomeServer = defaultHomeServer; this._defaultHomeServer = defaultHomeServer;
this._loadViewModel = null; this._loadViewModel = null;
this._loadViewModelSubscription = null;
} }
get defaultHomeServer() { return this._defaultHomeServer; } get defaultHomeServer() { return this._defaultHomeServer; }
get loadViewModel() {return this._loadViewModel; } get loadViewModel() {return this._loadViewModel; }
get isBusy() {
if (!this._loadViewModel) {
return false;
} else {
return this._loadViewModel.loading;
}
}
async login(username, password, homeserver) { async login(username, password, homeserver) {
this._loadViewModelSubscription = this.disposeTracked(this._loadViewModelSubscription);
if (this._loadViewModel) {
this._loadViewModel.cancel();
}
this._loadViewModel = new SessionLoadViewModel({ this._loadViewModel = new SessionLoadViewModel({
createAndStartSessionContainer: () => { createAndStartSessionContainer: () => {
const sessionContainer = this._createSessionContainer(); const sessionContainer = this._createSessionContainer();
@ -36,10 +49,16 @@ export class LoginViewModel extends ViewModel {
}); });
this._loadViewModel.start(); this._loadViewModel.start();
this.emitChange("loadViewModel"); this.emitChange("loadViewModel");
this._loadViewModelSubscription = this.track(this._loadViewModel.disposableOn("change", () => {
if (!this._loadViewModel.loading) {
this._loadViewModelSubscription = this.disposeTracked(this._loadViewModelSubscription);
}
this.emitChange("isBusy");
}));
} }
cancel() { cancel() {
if (!this._loadViewModel) { if (!this.isBusy) {
this._sessionCallback(); this._sessionCallback();
} }
} }

View File

@ -276,10 +276,9 @@ class TemplateBuilder {
return vm => new TemplateView(vm, render); return vm => new TemplateView(vm, render);
} }
// creates a conditional subtemplate // map a value to a view, every time the value changes
if(fn, viewCreator) { mapView(mapFn, viewCreator) {
const boolFn = value => !!fn(value); return this._addReplaceNodeBinding(mapFn, (prevNode) => {
return this._addReplaceNodeBinding(boolFn, (prevNode) => {
if (prevNode && prevNode.nodeType !== Node.COMMENT_NODE) { if (prevNode && prevNode.nodeType !== Node.COMMENT_NODE) {
const subViews = this._templateView._subViews; const subViews = this._templateView._subViews;
const viewIdx = subViews.findIndex(v => v.root() === prevNode); const viewIdx = subViews.findIndex(v => v.root() === prevNode);
@ -288,14 +287,22 @@ class TemplateBuilder {
view.unmount(); view.unmount();
} }
} }
if (boolFn(this._value)) { const view = viewCreator(mapFn(this._value));
const view = viewCreator(this._value); if (view) {
return this.view(view); return this.view(view);
} else { } else {
return document.createComment("if placeholder"); return document.createComment("node binding placeholder");
} }
}); });
} }
// creates a conditional subtemplate
if(fn, viewCreator) {
return this.mapView(
value => !!fn(value),
enabled => enabled ? viewCreator(this._value) : null
);
}
} }

View File

@ -4,7 +4,7 @@ import {SessionLoadView} from "./SessionLoadView.js";
export class LoginView extends TemplateView { export class LoginView extends TemplateView {
render(t, vm) { render(t, vm) {
const disabled = vm => !!vm.loadViewModel; const disabled = vm => !!vm.isBusy;
const username = t.input({type: "text", placeholder: vm.i18n`Username`, disabled}); const username = t.input({type: "text", placeholder: vm.i18n`Username`, disabled});
const password = t.input({type: "password", placeholder: vm.i18n`Password`, disabled}); const password = t.input({type: "password", placeholder: vm.i18n`Password`, disabled});
const homeserver = t.input({type: "text", placeholder: vm.i18n`Your matrix homeserver`, value: vm.defaultHomeServer, disabled}); const homeserver = t.input({type: "text", placeholder: vm.i18n`Your matrix homeserver`, value: vm.defaultHomeServer, disabled});
@ -18,8 +18,8 @@ export class LoginView extends TemplateView {
onClick: () => vm.login(username.value, password.value, homeserver.value), onClick: () => vm.login(username.value, password.value, homeserver.value),
disabled disabled
}, vm.i18n`Log In`)), }, vm.i18n`Log In`)),
t.div(t.button({onClick: () => vm.goBack(), disabled}, [vm.i18n`Pick an existing session`])), t.div(t.button({onClick: () => vm.cancel(), disabled}, [vm.i18n`Pick an existing session`])),
t.if(vm => vm.loadViewModel, vm => new SessionLoadView(vm.loadViewModel)), t.mapView(vm => vm.loadViewModel, loadViewModel => loadViewModel ? new SessionLoadView(loadViewModel) : null),
t.p(brawlGithubLink(t)) t.p(brawlGithubLink(t))
]); ]);
} }

View File

@ -12,6 +12,13 @@ export class EventEmitter {
} }
} }
disposableOn(name, callback) {
this.on(name, callback);
return () => {
this.off(name, callback);
}
}
on(name, callback) { on(name, callback) {
let handlers = this._handlersByName[name]; let handlers = this._handlersByName[name];
if (!handlers) { if (!handlers) {