2020-08-05 18:38:55 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
2022-02-14 17:50:17 +01:00
|
|
|
Copyright 2022 The Matrix.org Foundation C.I.C.
|
2020-08-05 18:38:55 +02:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-04-18 19:16:16 +02:00
|
|
|
// ViewModel should just be an eventemitter, not an ObservableValue
|
|
|
|
// as in some cases it would really be more convenient to have multiple events (like telling the timeline to scroll down)
|
|
|
|
// we do need to return a disposable from EventEmitter.on, or at least have a method here to easily track a subscription to an EventEmitter
|
|
|
|
|
2021-09-16 10:23:03 +02:00
|
|
|
import {EventEmitter} from "../utils/EventEmitter";
|
2021-11-16 09:43:35 +01:00
|
|
|
import {Disposables} from "../utils/Disposables";
|
2020-05-04 19:23:11 +02:00
|
|
|
|
2022-02-14 17:50:17 +01:00
|
|
|
import type {Disposable} from "../utils/Disposables";
|
|
|
|
import type {Platform} from "../platform/web/Platform";
|
|
|
|
import type {Clock} from "../platform/web/dom/Clock";
|
|
|
|
import type {ILogger} from "../logging/types";
|
|
|
|
import type {Navigation} from "./navigation/Navigation";
|
2022-02-22 17:53:52 +01:00
|
|
|
import type {SegmentType} from "./navigation/index";
|
2022-05-27 19:08:53 +02:00
|
|
|
import type {IURLRouter} from "./navigation/URLRouter";
|
|
|
|
|
|
|
|
export type Options<T extends object = SegmentType> = {
|
|
|
|
platform: Platform;
|
|
|
|
logger: ILogger;
|
|
|
|
urlCreator: IURLRouter<T>;
|
|
|
|
navigation: Navigation<T>;
|
|
|
|
emitChange?: (params: any) => void;
|
2022-02-14 17:50:17 +01:00
|
|
|
}
|
|
|
|
|
2022-03-03 15:47:15 +01:00
|
|
|
|
|
|
|
export class ViewModel<N extends object = SegmentType, O extends Options<N> = Options<N>> extends EventEmitter<{change: never}> {
|
2022-02-14 17:50:17 +01:00
|
|
|
private disposables?: Disposables;
|
|
|
|
private _isDisposed = false;
|
2022-05-09 14:12:31 +02:00
|
|
|
private _options: Readonly<O>;
|
2022-02-14 17:50:17 +01:00
|
|
|
|
2022-05-09 14:12:31 +02:00
|
|
|
constructor(options: Readonly<O>) {
|
2020-04-09 23:19:49 +02:00
|
|
|
super();
|
2020-10-06 18:05:02 +02:00
|
|
|
this._options = options;
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:47:51 +01:00
|
|
|
childOptions<T extends Object>(explicitOptions: T): T & Options<N> {
|
2022-02-14 17:50:17 +01:00
|
|
|
return Object.assign({}, this._options, explicitOptions);
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
|
|
|
|
2022-05-09 14:12:31 +02:00
|
|
|
get options(): Readonly<O> { return this._options; }
|
2022-02-17 09:24:18 +01:00
|
|
|
|
2020-10-16 18:06:20 +02:00
|
|
|
// makes it easier to pass through dependencies of a sub-view model
|
2022-02-14 17:50:17 +01:00
|
|
|
getOption<N extends keyof O>(name: N): O[N] {
|
2020-10-16 18:06:20 +02:00
|
|
|
return this._options[name];
|
|
|
|
}
|
|
|
|
|
2022-03-07 07:03:51 +01:00
|
|
|
observeNavigation(type: string, onChange: (value: string | true | undefined, type: string) => void) {
|
2022-03-03 11:06:25 +01:00
|
|
|
const segmentObservable = this.navigation.observe(type);
|
2022-03-07 07:03:51 +01:00
|
|
|
const unsubscribe = segmentObservable.subscribe((value: string | true | undefined) => {
|
2022-03-03 11:06:25 +01:00
|
|
|
onChange(value, type);
|
|
|
|
})
|
|
|
|
this.track(unsubscribe);
|
|
|
|
}
|
|
|
|
|
2022-02-14 17:50:17 +01:00
|
|
|
track<D extends Disposable>(disposable: D): D {
|
2020-05-04 19:23:11 +02:00
|
|
|
if (!this.disposables) {
|
|
|
|
this.disposables = new Disposables();
|
|
|
|
}
|
2020-09-10 16:40:30 +02:00
|
|
|
return this.disposables.track(disposable);
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
|
|
|
|
2022-02-14 17:50:17 +01:00
|
|
|
untrack(disposable: Disposable): undefined {
|
2020-10-07 12:25:03 +02:00
|
|
|
if (this.disposables) {
|
|
|
|
return this.disposables.untrack(disposable);
|
|
|
|
}
|
2022-02-14 17:50:17 +01:00
|
|
|
return undefined;
|
2020-10-07 12:25:03 +02:00
|
|
|
}
|
|
|
|
|
2022-02-14 17:50:17 +01:00
|
|
|
dispose(): void {
|
2020-05-04 19:23:11 +02:00
|
|
|
if (this.disposables) {
|
|
|
|
this.disposables.dispose();
|
|
|
|
}
|
2020-09-14 17:43:06 +02:00
|
|
|
this._isDisposed = true;
|
|
|
|
}
|
|
|
|
|
2022-02-14 17:50:17 +01:00
|
|
|
get isDisposed(): boolean {
|
2020-09-14 17:43:06 +02:00
|
|
|
return this._isDisposed;
|
2020-05-04 19:23:11 +02:00
|
|
|
}
|
|
|
|
|
2022-02-14 17:50:17 +01:00
|
|
|
disposeTracked(disposable: Disposable | undefined): undefined {
|
2020-05-05 23:14:58 +02:00
|
|
|
if (this.disposables) {
|
|
|
|
return this.disposables.disposeTracked(disposable);
|
|
|
|
}
|
2022-02-14 17:50:17 +01:00
|
|
|
return undefined;
|
2020-05-05 23:14:58 +02:00
|
|
|
}
|
|
|
|
|
2020-05-04 19:23:11 +02:00
|
|
|
// TODO: this will need to support binding
|
|
|
|
// if any of the expr is a function, assume the function is a binding, and return a binding function ourselves
|
2020-05-05 23:14:58 +02:00
|
|
|
//
|
|
|
|
// translated string should probably always be bindings, unless we're fine with a refresh when changing the language?
|
|
|
|
// we probably are, if we're using routing with a url, we could just refresh.
|
2022-02-25 12:15:07 +01:00
|
|
|
i18n(parts: TemplateStringsArray, ...expr: any[]) {
|
2020-05-04 19:23:11 +02:00
|
|
|
// just concat for now
|
2021-02-16 15:38:43 +01:00
|
|
|
let result = "";
|
|
|
|
for (let i = 0; i < parts.length; ++i) {
|
|
|
|
result = result + parts[i];
|
|
|
|
if (i < expr.length) {
|
|
|
|
result = result + expr[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
2020-05-04 19:23:11 +02:00
|
|
|
}
|
|
|
|
|
2022-02-14 17:50:17 +01:00
|
|
|
emitChange(changedProps: any): void {
|
2022-02-17 09:24:18 +01:00
|
|
|
if (this._options.emitChange) {
|
2020-08-12 17:39:11 +02:00
|
|
|
this._options.emitChange(changedProps);
|
|
|
|
} else {
|
|
|
|
this.emit("change", changedProps);
|
|
|
|
}
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
2020-05-05 23:14:58 +02:00
|
|
|
|
2022-02-14 17:50:17 +01:00
|
|
|
get platform(): Platform {
|
2020-10-26 15:44:11 +01:00
|
|
|
return this._options.platform;
|
|
|
|
}
|
|
|
|
|
2022-02-14 17:50:17 +01:00
|
|
|
get clock(): Clock {
|
2020-10-26 15:44:11 +01:00
|
|
|
return this._options.platform.clock;
|
2020-05-05 23:14:58 +02:00
|
|
|
}
|
2020-10-06 18:05:02 +02:00
|
|
|
|
2022-02-14 17:50:17 +01:00
|
|
|
get logger(): ILogger {
|
2021-02-12 18:06:14 +01:00
|
|
|
return this.platform.logger;
|
2021-02-11 21:07:18 +01:00
|
|
|
}
|
|
|
|
|
2022-05-27 19:08:53 +02:00
|
|
|
get urlCreator(): IURLRouter<N> {
|
|
|
|
return this._options.urlCreator;
|
2020-10-06 18:05:02 +02:00
|
|
|
}
|
|
|
|
|
2022-02-22 15:47:51 +01:00
|
|
|
get navigation(): Navigation<N> {
|
2022-03-03 15:47:15 +01:00
|
|
|
// typescript needs a little help here
|
|
|
|
return this._options.navigation as unknown as Navigation<N>;
|
2020-10-06 18:05:02 +02:00
|
|
|
}
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|