2019-09-08 13:49:16 +05:30
|
|
|
import {SortedArray} from "../observable/index.js";
|
2020-04-21 01:05:53 +05:30
|
|
|
import {EventEmitter} from "../utils/EventEmitter.js";
|
2019-10-13 00:46:48 +05:30
|
|
|
|
|
|
|
class SessionItemViewModel extends EventEmitter {
|
|
|
|
constructor(sessionInfo, pickerVM) {
|
|
|
|
super();
|
|
|
|
this._pickerVM = pickerVM;
|
|
|
|
this._sessionInfo = sessionInfo;
|
|
|
|
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;
|
|
|
|
this.emit("change", "isDeleting");
|
|
|
|
try {
|
|
|
|
await this._pickerVM.delete(this.id);
|
|
|
|
} catch(err) {
|
|
|
|
this._error = err;
|
|
|
|
console.error(err);
|
|
|
|
this.emit("change", "error");
|
|
|
|
} finally {
|
|
|
|
this._isDeleting = false;
|
|
|
|
this.emit("change", "isDeleting");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 11:18:49 +05:30
|
|
|
async clear() {
|
|
|
|
this._isClearing = true;
|
2019-10-13 11:59:23 +05:30
|
|
|
this.emit("change");
|
2019-10-13 11:18:49 +05:30
|
|
|
try {
|
|
|
|
await this._pickerVM.clear(this.id);
|
|
|
|
} catch(err) {
|
|
|
|
this._error = err;
|
|
|
|
console.error(err);
|
|
|
|
this.emit("change", "error");
|
|
|
|
} finally {
|
|
|
|
this._isClearing = false;
|
|
|
|
this.emit("change", "isClearing");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
this.emit("change", "exportDataUrl");
|
|
|
|
} catch (err) {
|
|
|
|
alert(err.message);
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
clearExport() {
|
|
|
|
if (this._exportDataUrl) {
|
|
|
|
URL.revokeObjectURL(this._exportDataUrl);
|
|
|
|
this._exportDataUrl = null;
|
|
|
|
this.emit("change", "exportDataUrl");
|
2019-10-13 11:59:23 +05:30
|
|
|
}
|
|
|
|
}
|
2019-10-13 00:46:48 +05:30
|
|
|
}
|
2019-07-30 02:09:56 +05:30
|
|
|
|
2020-04-23 01:23:55 +05:30
|
|
|
|
|
|
|
export class SessionPickerViewModel extends EventEmitter {
|
2020-04-21 02:19:14 +05:30
|
|
|
constructor({storageFactory, sessionInfoStorage, sessionCallback, createSessionContainer}) {
|
2020-04-23 01:23:55 +05:30
|
|
|
super();
|
2019-10-13 00:46:48 +05:30
|
|
|
this._storageFactory = storageFactory;
|
2020-04-19 22:43:38 +05:30
|
|
|
this._sessionInfoStorage = sessionInfoStorage;
|
2019-07-30 02:09:56 +05:30
|
|
|
this._sessionCallback = sessionCallback;
|
2020-04-21 02:19:14 +05:30
|
|
|
this._createSessionContainer = createSessionContainer;
|
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-04-19 22:43:38 +05:30
|
|
|
const sessions = await this._sessionInfoStorage.getAll();
|
2019-10-13 00:46:48 +05:30
|
|
|
this._sessions.setManyUnsorted(sessions.map(s => new SessionItemViewModel(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
|
|
|
}
|
|
|
|
|
|
|
|
async pick(id) {
|
2020-04-23 01:23:55 +05:30
|
|
|
if (this._loadViewModel) {
|
|
|
|
return;
|
|
|
|
}
|
2019-10-13 01:48:08 +05:30
|
|
|
const sessionVM = this._sessions.array.find(s => s.id === id);
|
|
|
|
if (sessionVM) {
|
2020-04-23 01:23:55 +05:30
|
|
|
this._loadViewModel = new LoadViewModel({
|
2020-04-23 12:36:04 +05:30
|
|
|
createAndStartSessionContainer: () => {
|
|
|
|
const sessionContainer = this._createSessionContainer();
|
|
|
|
sessionContainer.startWithExistingSession(sessionVM.id);
|
|
|
|
return sessionContainer;
|
|
|
|
},
|
2020-04-23 01:23:55 +05:30
|
|
|
sessionCallback: sessionContainer => {
|
|
|
|
if (sessionContainer) {
|
|
|
|
// make parent view model move away
|
|
|
|
this._sessionCallback(sessionContainer);
|
|
|
|
} else {
|
|
|
|
// show list of session again
|
|
|
|
this._loadViewModel = null;
|
|
|
|
this.emit("change", "loadViewModel");
|
|
|
|
}
|
2020-04-23 12:36:04 +05:30
|
|
|
}
|
2020-04-21 02:19:14 +05:30
|
|
|
});
|
2020-04-23 01:23:55 +05:30
|
|
|
this._loadViewModel.start();
|
|
|
|
this.emit("change", "loadViewModel");
|
2019-07-30 02:09:56 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-14 22:59:35 +05:30
|
|
|
async _exportData(id) {
|
2020-04-19 22:43:38 +05:30
|
|
|
const sessionInfo = await this._sessionInfoStorage.get(id);
|
2019-12-14 22:59:35 +05:30
|
|
|
const stores = await this._storageFactory.export(id);
|
|
|
|
const data = {sessionInfo, stores};
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2019-10-13 11:46:08 +05:30
|
|
|
async import(json) {
|
2019-12-14 22:59:35 +05:30
|
|
|
const data = JSON.parse(json);
|
|
|
|
const {sessionInfo} = data;
|
|
|
|
sessionInfo.comment = `Imported on ${new Date().toLocaleString()} from id ${sessionInfo.id}.`;
|
2020-04-21 02:19:14 +05:30
|
|
|
sessionInfo.id = this._createSessionContainer().createNewSessionId();
|
2019-12-14 22:59:35 +05:30
|
|
|
await this._storageFactory.import(sessionInfo.id, data.stores);
|
2020-04-19 22:43:38 +05:30
|
|
|
await this._sessionInfoStorage.add(sessionInfo);
|
2019-10-13 11:46:08 +05:30
|
|
|
this._sessions.set(new SessionItemViewModel(sessionInfo, this));
|
|
|
|
}
|
|
|
|
|
2019-10-13 00:46:48 +05:30
|
|
|
async delete(id) {
|
|
|
|
const idx = this._sessions.array.findIndex(s => s.id === id);
|
2020-04-19 22:43:38 +05:30
|
|
|
await this._sessionInfoStorage.delete(id);
|
2019-10-13 00:46:48 +05:30
|
|
|
await this._storageFactory.delete(id);
|
|
|
|
this._sessions.remove(idx);
|
|
|
|
}
|
|
|
|
|
2019-10-13 11:18:49 +05:30
|
|
|
async clear(id) {
|
|
|
|
await this._storageFactory.delete(id);
|
|
|
|
}
|
|
|
|
|
2019-07-30 02:09:56 +05:30
|
|
|
get sessions() {
|
|
|
|
return this._sessions;
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel() {
|
|
|
|
this._sessionCallback();
|
|
|
|
}
|
|
|
|
}
|