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.
|
|
|
|
*/
|
|
|
|
|
2019-09-08 13:49:16 +05:30
|
|
|
import {SortedArray} from "../observable/index.js";
|
2020-05-04 22:53:11 +05:30
|
|
|
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
|
|
|
|
2020-05-04 22:53:11 +05:30
|
|
|
class SessionItemViewModel extends ViewModel {
|
2020-10-09 20:32:53 +05:30
|
|
|
constructor(options, pickerVM) {
|
|
|
|
super(options);
|
2019-10-13 00:46:48 +05:30
|
|
|
this._pickerVM = pickerVM;
|
2020-10-09 20:32:53 +05:30
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete() {
|
|
|
|
this._isDeleting = true;
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange("isDeleting");
|
2019-10-13 00:46:48 +05:30
|
|
|
try {
|
|
|
|
await this._pickerVM.delete(this.id);
|
|
|
|
} catch(err) {
|
|
|
|
this._error = err;
|
|
|
|
console.error(err);
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange("error");
|
2019-10-13 00:46:48 +05:30
|
|
|
} finally {
|
|
|
|
this._isDeleting = false;
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange("isDeleting");
|
2019-10-13 00:46:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 11:18:49 +05:30
|
|
|
async clear() {
|
|
|
|
this._isClearing = true;
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange();
|
2019-10-13 11:18:49 +05:30
|
|
|
try {
|
|
|
|
await this._pickerVM.clear(this.id);
|
|
|
|
} catch(err) {
|
|
|
|
this._error = err;
|
|
|
|
console.error(err);
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange("error");
|
2019-10-13 11:18:49 +05:30
|
|
|
} finally {
|
|
|
|
this._isClearing = false;
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange("isClearing");
|
2019-10-13 11:18:49 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 00:46:48 +05:30
|
|
|
get isDeleting() {
|
|
|
|
return this._isDeleting;
|
|
|
|
}
|
|
|
|
|
2019-10-13 11:18:49 +05:30
|
|
|
get isClearing() {
|
|
|
|
return this._isClearing;
|
|
|
|
}
|
|
|
|
|
2019-10-13 00:46:48 +05:30
|
|
|
get id() {
|
|
|
|
return this._sessionInfo.id;
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:32:53 +05:30
|
|
|
get openUrl() {
|
2020-10-16 16:32:21 +05:30
|
|
|
return this.urlCreator.urlForSegment("session", this.id);
|
2020-10-09 20:32:53 +05:30
|
|
|
}
|
|
|
|
|
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-10-13 11:59:23 +05:30
|
|
|
|
2019-12-14 22:59:35 +05:30
|
|
|
get exportDataUrl() {
|
|
|
|
return this._exportDataUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
async export() {
|
|
|
|
try {
|
|
|
|
const data = await this._pickerVM._exportData(this._sessionInfo.id);
|
|
|
|
const json = JSON.stringify(data, undefined, 2);
|
|
|
|
const blob = new Blob([json], {type: "application/json"});
|
|
|
|
this._exportDataUrl = URL.createObjectURL(blob);
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange("exportDataUrl");
|
2019-12-14 22:59:35 +05:30
|
|
|
} catch (err) {
|
|
|
|
alert(err.message);
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clearExport() {
|
|
|
|
if (this._exportDataUrl) {
|
|
|
|
URL.revokeObjectURL(this._exportDataUrl);
|
|
|
|
this._exportDataUrl = null;
|
2020-05-04 22:53:11 +05:30
|
|
|
this.emitChange("exportDataUrl");
|
2019-10-13 11:59:23 +05:30
|
|
|
}
|
|
|
|
}
|
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
|
|
|
}
|
2019-07-30 02:09:56 +05:30
|
|
|
|
2020-04-23 01:23:55 +05:30
|
|
|
|
2020-05-04 22:53:11 +05:30
|
|
|
export class SessionPickerViewModel extends ViewModel {
|
2020-05-06 02:44:58 +05:30
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
2019-10-13 11:46:08 +05:30
|
|
|
this._sessions = new SortedArray((s1, s2) => s1.id.localeCompare(s2.id));
|
2020-04-23 01:23:55 +05:30
|
|
|
this._loadViewModel = null;
|
|
|
|
this._error = null;
|
2019-07-30 02:09:56 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 02:19:14 +05:30
|
|
|
// this loads all the sessions
|
2019-07-30 02:09:56 +05:30
|
|
|
async load() {
|
2020-10-26 20:14:11 +05:30
|
|
|
const sessions = await this.platform.sessionInfoStorage.getAll();
|
2020-10-09 20:32:53 +05:30
|
|
|
this._sessions.setManyUnsorted(sessions.map(s => {
|
|
|
|
return new SessionItemViewModel(this.childOptions({sessionInfo: s}), this);
|
|
|
|
}));
|
2019-07-30 02:09:56 +05:30
|
|
|
}
|
|
|
|
|
2020-04-23 01:23:55 +05:30
|
|
|
// for the loading of 1 picked session
|
|
|
|
get loadViewModel() {
|
|
|
|
return this._loadViewModel;
|
2020-04-21 02:19:14 +05:30
|
|
|
}
|
|
|
|
|
2019-12-14 22:59:35 +05:30
|
|
|
async _exportData(id) {
|
2020-10-26 20:14:11 +05:30
|
|
|
const sessionInfo = await this.platform.sessionInfoStorage.get(id);
|
|
|
|
const stores = await this.platform.storageFactory.export(id);
|
2019-12-14 22:59:35 +05:30
|
|
|
const data = {sessionInfo, stores};
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-10-13 11:46:08 +05:30
|
|
|
async import(json) {
|
2020-06-27 02:56:24 +05:30
|
|
|
try {
|
|
|
|
const data = JSON.parse(json);
|
|
|
|
const {sessionInfo} = data;
|
|
|
|
sessionInfo.comment = `Imported on ${new Date().toLocaleString()} from id ${sessionInfo.id}.`;
|
|
|
|
sessionInfo.id = this._createSessionContainer().createNewSessionId();
|
2020-10-26 20:14:11 +05:30
|
|
|
await this.platform.storageFactory.import(sessionInfo.id, data.stores);
|
|
|
|
await this.platform.sessionInfoStorage.add(sessionInfo);
|
2020-06-27 02:56:24 +05:30
|
|
|
this._sessions.set(new SessionItemViewModel(sessionInfo, this));
|
|
|
|
} catch (err) {
|
|
|
|
alert(err.message);
|
|
|
|
console.error(err);
|
|
|
|
}
|
2019-10-13 11:46:08 +05:30
|
|
|
}
|
|
|
|
|
2019-10-13 00:46:48 +05:30
|
|
|
async delete(id) {
|
|
|
|
const idx = this._sessions.array.findIndex(s => s.id === id);
|
2020-10-26 20:14:11 +05:30
|
|
|
await this.platform.sessionInfoStorage.delete(id);
|
|
|
|
await this.platform.storageFactory.delete(id);
|
2019-10-13 00:46:48 +05:30
|
|
|
this._sessions.remove(idx);
|
|
|
|
}
|
|
|
|
|
2019-10-13 11:18:49 +05:30
|
|
|
async clear(id) {
|
2020-10-26 20:14:11 +05:30
|
|
|
await this.platform.storageFactory.delete(id);
|
2019-10-13 11:18:49 +05:30
|
|
|
}
|
|
|
|
|
2019-07-30 02:09:56 +05:30
|
|
|
get sessions() {
|
|
|
|
return this._sessions;
|
|
|
|
}
|
|
|
|
|
2020-10-09 20:32:53 +05:30
|
|
|
get cancelUrl() {
|
2020-10-16 16:32:21 +05:30
|
|
|
return this.urlCreator.urlForSegment("login");
|
2019-07-30 02:09:56 +05:30
|
|
|
}
|
|
|
|
}
|