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
|
|
|
|
|
2020-05-04 19:23:11 +02:00
|
|
|
import {EventEmitter} from "../utils/EventEmitter.js";
|
|
|
|
import {Disposables} from "../utils/Disposables.js";
|
|
|
|
|
|
|
|
export class ViewModel extends EventEmitter {
|
2020-05-05 23:14:58 +02:00
|
|
|
constructor({clock} = {}) {
|
2020-04-09 23:19:49 +02:00
|
|
|
super();
|
2020-05-04 19:23:11 +02:00
|
|
|
this.disposables = null;
|
2020-05-05 23:14:58 +02:00
|
|
|
this._options = {clock};
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
childOptions(explicitOptions) {
|
|
|
|
return Object.assign({}, this._options, explicitOptions);
|
|
|
|
}
|
|
|
|
|
|
|
|
track(disposable) {
|
2020-05-04 19:23:11 +02:00
|
|
|
if (!this.disposables) {
|
|
|
|
this.disposables = new Disposables();
|
|
|
|
}
|
2020-04-09 23:19:49 +02:00
|
|
|
this.disposables.track(disposable);
|
2020-05-05 23:14:58 +02:00
|
|
|
return disposable;
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dispose() {
|
2020-05-04 19:23:11 +02:00
|
|
|
if (this.disposables) {
|
|
|
|
this.disposables.dispose();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 23:14:58 +02:00
|
|
|
disposeTracked(disposable) {
|
|
|
|
if (this.disposables) {
|
|
|
|
return this.disposables.disposeTracked(disposable);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
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.
|
2020-05-04 19:23:11 +02:00
|
|
|
i18n(parts, ...expr) {
|
|
|
|
// just concat for now
|
|
|
|
let result = "";
|
|
|
|
for (let i = 0; i < parts.length; ++i) {
|
|
|
|
result = result + parts[i];
|
|
|
|
if (i < expr.length) {
|
|
|
|
result = result + expr[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
emitChange(changedProps) {
|
|
|
|
this.emit("change", changedProps);
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|
2020-05-05 23:14:58 +02:00
|
|
|
|
|
|
|
get clock() {
|
|
|
|
return this._options.clock;
|
|
|
|
}
|
2020-04-09 23:19:49 +02:00
|
|
|
}
|