2019-09-08 10:19:16 +02:00
|
|
|
import {SortedArray} from "../observable/index.js";
|
2019-10-12 21:16:48 +02:00
|
|
|
import EventEmitter from "../EventEmitter.js";
|
|
|
|
|
|
|
|
class SessionItemViewModel extends EventEmitter {
|
|
|
|
constructor(sessionInfo, pickerVM) {
|
|
|
|
super();
|
|
|
|
this._pickerVM = pickerVM;
|
|
|
|
this._sessionInfo = sessionInfo;
|
|
|
|
this._isDeleting = false;
|
2019-10-13 07:48:49 +02:00
|
|
|
this._isClearing = false;
|
2019-10-12 21:16:48 +02:00
|
|
|
this._error = null;
|
2019-10-13 08:29:23 +02:00
|
|
|
this._showJSON = false;
|
2019-10-12 21:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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 07:48:49 +02:00
|
|
|
async clear() {
|
|
|
|
this._isClearing = true;
|
2019-10-13 08:29:23 +02:00
|
|
|
this._showJSON = true;
|
|
|
|
this.emit("change");
|
2019-10-13 07:48:49 +02:00
|
|
|
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-12 21:16:48 +02:00
|
|
|
get isDeleting() {
|
|
|
|
return this._isDeleting;
|
|
|
|
}
|
|
|
|
|
2019-10-13 07:48:49 +02:00
|
|
|
get isClearing() {
|
|
|
|
return this._isClearing;
|
|
|
|
}
|
|
|
|
|
2019-10-12 21:16:48 +02:00
|
|
|
get id() {
|
|
|
|
return this._sessionInfo.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
get userId() {
|
|
|
|
return this._sessionInfo.userId;
|
|
|
|
}
|
2019-10-12 22:18:08 +02:00
|
|
|
|
|
|
|
get sessionInfo() {
|
|
|
|
return this._sessionInfo;
|
2019-10-12 21:16:48 +02:00
|
|
|
}
|
2019-10-13 08:29:23 +02:00
|
|
|
|
|
|
|
get json() {
|
|
|
|
if (this._showJSON) {
|
|
|
|
return JSON.stringify(this._sessionInfo, undefined, 2);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2019-10-12 21:16:48 +02:00
|
|
|
}
|
2019-07-29 22:39:56 +02:00
|
|
|
|
|
|
|
export default class SessionPickerViewModel {
|
2019-10-12 21:16:48 +02:00
|
|
|
constructor({storageFactory, sessionStore, sessionCallback}) {
|
|
|
|
this._storageFactory = storageFactory;
|
2019-09-08 10:19:16 +02:00
|
|
|
this._sessionStore = sessionStore;
|
2019-07-29 22:39:56 +02:00
|
|
|
this._sessionCallback = sessionCallback;
|
2019-10-13 08:16:08 +02:00
|
|
|
this._sessions = new SortedArray((s1, s2) => s1.id.localeCompare(s2.id));
|
2019-07-29 22:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async load() {
|
2019-09-08 10:19:16 +02:00
|
|
|
const sessions = await this._sessionStore.getAll();
|
2019-10-12 21:16:48 +02:00
|
|
|
this._sessions.setManyUnsorted(sessions.map(s => new SessionItemViewModel(s, this)));
|
2019-07-29 22:39:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pick(id) {
|
2019-10-12 22:18:08 +02:00
|
|
|
const sessionVM = this._sessions.array.find(s => s.id === id);
|
|
|
|
if (sessionVM) {
|
|
|
|
this._sessionCallback(sessionVM.sessionInfo);
|
2019-07-29 22:39:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-13 08:16:08 +02:00
|
|
|
async import(json) {
|
|
|
|
const sessionInfo = JSON.parse(json);
|
|
|
|
const sessionId = (Math.floor(Math.random() * Number.MAX_SAFE_INTEGER)).toString();
|
|
|
|
sessionInfo.id = sessionId;
|
|
|
|
sessionInfo.lastUsed = sessionId;
|
|
|
|
await this._sessionStore.add(sessionInfo);
|
|
|
|
this._sessions.set(new SessionItemViewModel(sessionInfo, this));
|
|
|
|
}
|
|
|
|
|
2019-10-12 21:16:48 +02:00
|
|
|
async delete(id) {
|
|
|
|
const idx = this._sessions.array.findIndex(s => s.id === id);
|
|
|
|
await this._sessionStore.delete(id);
|
|
|
|
await this._storageFactory.delete(id);
|
|
|
|
this._sessions.remove(idx);
|
|
|
|
}
|
|
|
|
|
2019-10-13 07:48:49 +02:00
|
|
|
async clear(id) {
|
|
|
|
await this._storageFactory.delete(id);
|
|
|
|
}
|
|
|
|
|
2019-07-29 22:39:56 +02:00
|
|
|
get sessions() {
|
|
|
|
return this._sessions;
|
|
|
|
}
|
|
|
|
|
|
|
|
cancel() {
|
|
|
|
this._sessionCallback();
|
|
|
|
}
|
|
|
|
}
|