2020-08-05 22:08:55 +05:30
|
|
|
/*
|
|
|
|
Copyright 2020 Bruno Windels <bruno@windels.cloud>
|
|
|
|
|
|
|
|
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 22:46:16 +05:30
|
|
|
// 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 22:53:11 +05:30
|
|
|
import {EventEmitter} from "../utils/EventEmitter.js";
|
|
|
|
import {Disposables} from "../utils/Disposables.js";
|
|
|
|
|
|
|
|
export class ViewModel extends EventEmitter {
|
2020-10-06 21:35:02 +05:30
|
|
|
constructor(options = {}) {
|
2020-04-10 02:49:49 +05:30
|
|
|
super();
|
2020-05-04 22:53:11 +05:30
|
|
|
this.disposables = null;
|
2020-09-14 21:13:06 +05:30
|
|
|
this._isDisposed = false;
|
2020-10-06 21:35:02 +05:30
|
|
|
this._options = options;
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
childOptions(explicitOptions) {
|
2021-02-12 22:36:14 +05:30
|
|
|
const {navigation, urlCreator, platform} = this._options;
|
|
|
|
return Object.assign({navigation, urlCreator, platform}, explicitOptions);
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
|
|
|
|
2020-10-16 21:36:20 +05:30
|
|
|
// makes it easier to pass through dependencies of a sub-view model
|
|
|
|
getOption(name) {
|
|
|
|
return this._options[name];
|
|
|
|
}
|
|
|
|
|
2020-04-10 02:49:49 +05:30
|
|
|
track(disposable) {
|
2020-05-04 22:53:11 +05:30
|
|
|
if (!this.disposables) {
|
|
|
|
this.disposables = new Disposables();
|
|
|
|
}
|
2020-09-10 20:10:30 +05:30
|
|
|
return this.disposables.track(disposable);
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
|
|
|
|
2020-10-07 15:55:03 +05:30
|
|
|
untrack(disposable) {
|
|
|
|
if (this.disposables) {
|
|
|
|
return this.disposables.untrack(disposable);
|
|
|
|
}
|
2020-10-13 16:40:27 +05:30
|
|
|
return null;
|
2020-10-07 15:55:03 +05:30
|
|
|
}
|
|
|
|
|
2020-04-10 02:49:49 +05:30
|
|
|
dispose() {
|
2020-05-04 22:53:11 +05:30
|
|
|
if (this.disposables) {
|
|
|
|
this.disposables.dispose();
|
|
|
|
}
|
2020-09-14 21:13:06 +05:30
|
|
|
this._isDisposed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
get isDisposed() {
|
|
|
|
return this._isDisposed;
|
2020-05-04 22:53:11 +05:30
|
|
|
}
|
|
|
|
|
2020-05-06 02:44:58 +05:30
|
|
|
disposeTracked(disposable) {
|
|
|
|
if (this.disposables) {
|
|
|
|
return this.disposables.disposeTracked(disposable);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-05-04 22:53:11 +05:30
|
|
|
// 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-06 02:44:58 +05:30
|
|
|
//
|
|
|
|
// 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 22:53:11 +05:30
|
|
|
i18n(parts, ...expr) {
|
|
|
|
// just concat for now
|
2021-02-16 20:08:43 +05:30
|
|
|
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 22:53:11 +05:30
|
|
|
}
|
|
|
|
|
2020-08-17 18:41:39 +05:30
|
|
|
updateOptions(options) {
|
|
|
|
this._options = Object.assign(this._options, options);
|
|
|
|
}
|
|
|
|
|
2020-05-04 22:53:11 +05:30
|
|
|
emitChange(changedProps) {
|
2020-08-12 21:09:11 +05:30
|
|
|
if (this._options.emitChange) {
|
|
|
|
this._options.emitChange(changedProps);
|
|
|
|
} else {
|
|
|
|
this.emit("change", changedProps);
|
|
|
|
}
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|
2020-05-06 02:44:58 +05:30
|
|
|
|
2020-10-26 20:14:11 +05:30
|
|
|
get platform() {
|
|
|
|
return this._options.platform;
|
|
|
|
}
|
|
|
|
|
2020-05-06 02:44:58 +05:30
|
|
|
get clock() {
|
2020-10-26 20:14:11 +05:30
|
|
|
return this._options.platform.clock;
|
2020-05-06 02:44:58 +05:30
|
|
|
}
|
2020-10-06 21:35:02 +05:30
|
|
|
|
2021-02-12 01:37:18 +05:30
|
|
|
get logger() {
|
2021-02-12 22:36:14 +05:30
|
|
|
return this.platform.logger;
|
2021-02-12 01:37:18 +05:30
|
|
|
}
|
|
|
|
|
2020-10-16 16:32:21 +05:30
|
|
|
/**
|
|
|
|
* The url router, only meant to be used to create urls with from view models.
|
|
|
|
* @return {URLRouter}
|
|
|
|
*/
|
|
|
|
get urlCreator() {
|
|
|
|
return this._options.urlCreator;
|
2020-10-06 21:35:02 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
get navigation() {
|
|
|
|
return this._options.navigation;
|
|
|
|
}
|
2020-04-10 02:49:49 +05:30
|
|
|
}
|