2021-11-30 15:22:16 +00:00
|
|
|
class App {
|
|
|
|
constructor() {
|
|
|
|
this.data = {users: {}};
|
|
|
|
this.eventListeners = {'users.patch': []};
|
2021-12-01 16:52:09 +00:00
|
|
|
this.promises = {users: {}};
|
2021-11-30 15:22:16 +00:00
|
|
|
this.socket = io({transports: ['websocket'], upgrade: false});
|
|
|
|
this.socket.on('users.patch', patch => this.usersPatchHandler(patch));
|
|
|
|
}
|
|
|
|
|
2021-12-01 13:15:20 +00:00
|
|
|
get users() {
|
|
|
|
return this.data.users;
|
|
|
|
}
|
2021-11-30 15:22:16 +00:00
|
|
|
|
|
|
|
addEventListener(type, listener) {
|
2021-12-01 13:15:20 +00:00
|
|
|
if (!(type in this.eventListeners)) {
|
|
|
|
throw `Unknown event type: ${type}`;
|
|
|
|
}
|
2021-11-30 15:22:16 +00:00
|
|
|
this.eventListeners[type].push(listener);
|
|
|
|
}
|
|
|
|
|
|
|
|
flash(message, category) {
|
2021-12-01 13:15:20 +00:00
|
|
|
let iconPrefix;
|
|
|
|
let toast;
|
|
|
|
let toastCloseActionElement;
|
|
|
|
|
2021-11-30 15:22:16 +00:00
|
|
|
switch (category) {
|
2021-12-01 13:15:20 +00:00
|
|
|
case 'corpus':
|
|
|
|
iconPrefix = '<i class="left material-icons">book</i>';
|
2021-11-30 15:22:16 +00:00
|
|
|
break;
|
2021-12-01 13:15:20 +00:00
|
|
|
case 'error':
|
|
|
|
iconPrefix = '<i class="error-color-text left material-icons">error</i>';
|
2021-11-30 15:22:16 +00:00
|
|
|
break;
|
2021-12-01 13:15:20 +00:00
|
|
|
case 'job':
|
2022-02-08 11:26:20 +00:00
|
|
|
iconPrefix = '<i class="left nopaque-icon">J</i>';
|
2021-11-30 15:22:16 +00:00
|
|
|
break;
|
|
|
|
default:
|
2021-12-01 13:15:20 +00:00
|
|
|
iconPrefix = '<i class="left material-icons">notifications</i>';
|
|
|
|
break;
|
2021-11-30 15:22:16 +00:00
|
|
|
}
|
2021-12-01 13:15:20 +00:00
|
|
|
toast = M.toast(
|
|
|
|
{
|
|
|
|
html: `
|
|
|
|
<span>${iconPrefix}${message}</span>
|
|
|
|
<button class="btn-flat toast-action white-text" data-action="close">
|
|
|
|
<i class="material-icons">close</i>
|
|
|
|
</button>
|
|
|
|
`.trim()
|
|
|
|
}
|
|
|
|
);
|
2021-11-30 15:22:16 +00:00
|
|
|
toastCloseActionElement = toast.el.querySelector('.toast-action[data-action="close"]');
|
|
|
|
toastCloseActionElement.addEventListener('click', () => {toast.dismiss();});
|
|
|
|
}
|
|
|
|
|
|
|
|
getUserById(userId) {
|
2021-12-01 16:52:09 +00:00
|
|
|
if (userId in this.promises.users) {
|
|
|
|
return this.promises.users[userId];
|
2021-12-01 16:07:05 +00:00
|
|
|
}
|
2021-12-01 16:52:09 +00:00
|
|
|
this.promises.users[userId] = new Promise((resolve, reject) => {
|
2021-11-30 15:22:16 +00:00
|
|
|
this.socket.emit('users.user.get', userId, response => {
|
|
|
|
if (response.code === 200) {
|
|
|
|
this.data.users[userId] = response.payload;
|
|
|
|
resolve(this.data.users[userId]);
|
|
|
|
} else {
|
|
|
|
reject(response);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2021-12-01 16:52:09 +00:00
|
|
|
return this.promises.users[userId];
|
2021-11-30 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
usersPatchHandler(patch) {
|
2021-12-01 13:15:20 +00:00
|
|
|
let listener;
|
|
|
|
|
2021-12-09 11:50:14 +00:00
|
|
|
this.data = jsonpatch.applyPatch(this.data, patch).newDocument;
|
|
|
|
//this.data = jsonpatch.apply_patch(this.data, patch);
|
2021-12-01 13:15:20 +00:00
|
|
|
for (listener of this.eventListeners['users.patch']) {listener(patch);}
|
2021-11-30 15:22:16 +00:00
|
|
|
}
|
|
|
|
}
|