hydrogen-web/src/domain/SessionPickerViewModel.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

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.
*/
import {SortedArray} from "../observable/index.js";
import {ViewModel} from "./ViewModel.js";
2020-08-14 18:04:39 +05:30
import {avatarInitials, getIdentifierColorNumber} from "./avatar.js";
2019-10-13 00:46:48 +05:30
class SessionItemViewModel extends ViewModel {
constructor(options, pickerVM) {
super(options);
2019-10-13 00:46:48 +05:30
this._pickerVM = pickerVM;
this._sessionInfo = options.sessionInfo;
2019-10-13 00:46:48 +05:30
this._isDeleting = false;
2019-10-13 11:18:49 +05:30
this._isClearing = false;
2019-10-13 00:46:48 +05:30
this._error = null;
2019-12-14 22:59:35 +05:30
this._exportDataUrl = null;
2019-10-13 00:46:48 +05:30
}
get error() {
return this._error && this._error.message;
}
get id() {
return this._sessionInfo.id;
}
get openUrl() {
return this.urlCreator.urlForSegment("session", this.id);
}
2019-12-14 22:59:35 +05:30
get label() {
const {userId, comment} = this._sessionInfo;
if (comment) {
return `${userId} (${comment})`;
} else {
return userId;
}
2019-10-13 00:46:48 +05:30
}
2019-10-13 01:48:08 +05:30
get sessionInfo() {
return this._sessionInfo;
2019-10-13 00:46:48 +05:30
}
2019-12-14 22:59:35 +05:30
get exportDataUrl() {
return this._exportDataUrl;
}
2020-08-14 18:04:39 +05:30
get avatarColorNumber() {
return getIdentifierColorNumber(this._sessionInfo.userId);
}
get avatarInitials() {
return avatarInitials(this._sessionInfo.userId);
}
2019-10-13 00:46:48 +05:30
}
export class SessionPickerViewModel extends ViewModel {
constructor(options) {
super(options);
2019-10-13 11:46:08 +05:30
this._sessions = new SortedArray((s1, s2) => s1.id.localeCompare(s2.id));
this._loadViewModel = null;
this._error = null;
}
// this loads all the sessions
async load() {
const sessions = await this.platform.sessionInfoStorage.getAll();
this._sessions.setManyUnsorted(sessions.map(s => {
return new SessionItemViewModel(this.childOptions({sessionInfo: s}), this);
}));
}
// for the loading of 1 picked session
get loadViewModel() {
return this._loadViewModel;
}
get sessions() {
return this._sessions;
}
get cancelUrl() {
return this.urlCreator.urlForSegment("login");
}
}