2023-10-29 20:22:30 -04:00
|
|
|
|
import {LitElement, html} from './lit-all.min.js';
|
|
|
|
|
import * as tfrpc from '/static/tfrpc.js';
|
|
|
|
|
|
2023-11-01 18:21:42 -04:00
|
|
|
|
class TfCollectionElement extends LitElement {
|
2023-10-29 20:22:30 -04:00
|
|
|
|
static get properties() {
|
|
|
|
|
return {
|
|
|
|
|
whoami: {type: String},
|
2023-11-01 19:39:34 -04:00
|
|
|
|
ids: {type: Array},
|
|
|
|
|
collections: {type: Array},
|
2023-10-29 20:22:30 -04:00
|
|
|
|
collections_loading: {type: Number},
|
|
|
|
|
type: {type: String},
|
|
|
|
|
parent: {type: String},
|
2023-11-01 18:21:42 -04:00
|
|
|
|
selectname: {type: String},
|
2023-11-01 19:39:34 -04:00
|
|
|
|
selectid: {type: String},
|
|
|
|
|
is_creating: {type: Boolean},
|
2023-11-01 20:29:07 -04:00
|
|
|
|
is_renaming: {type: Boolean},
|
2023-10-29 20:22:30 -04:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
|
super();
|
|
|
|
|
this.ids = [];
|
2023-11-01 18:21:42 -04:00
|
|
|
|
this.loaded = this.load();
|
2023-10-29 20:22:30 -04:00
|
|
|
|
this.type = 'collection';
|
|
|
|
|
this.collections_loading = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 20:45:20 -04:00
|
|
|
|
async process_message(message) {
|
2023-11-01 18:21:42 -04:00
|
|
|
|
let content = JSON.parse(message.content);
|
2023-11-01 20:45:20 -04:00
|
|
|
|
if (typeof content == 'string') {
|
|
|
|
|
let x = await tfrpc.rpc.try_decrypt(this.whoami, content);
|
|
|
|
|
if (x) {
|
|
|
|
|
content = JSON.parse(x);
|
|
|
|
|
content.draft = true;
|
|
|
|
|
} else {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-11-01 21:19:14 -04:00
|
|
|
|
if (content.type !== this.type ||
|
|
|
|
|
(this.parent && content.parent !== this.parent)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
content.draft = false;
|
2023-11-01 20:45:20 -04:00
|
|
|
|
}
|
2023-11-01 18:21:42 -04:00
|
|
|
|
if (content?.key) {
|
|
|
|
|
if (content?.tombstone) {
|
|
|
|
|
delete this.by_id[content.key];
|
|
|
|
|
} else if (this.by_id[content.key]) {
|
|
|
|
|
this.by_id[content.key] = Object.assign(this.by_id[content.key], content);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
this.by_id[message.id] = Object.assign(content, {id: message.id});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-29 20:22:30 -04:00
|
|
|
|
async load() {
|
|
|
|
|
try {
|
|
|
|
|
this.collections_loading++;
|
2023-11-01 19:39:34 -04:00
|
|
|
|
if (this.ids) {
|
|
|
|
|
let visible = this.ids;
|
|
|
|
|
this.visible = visible;
|
2023-11-01 18:21:42 -04:00
|
|
|
|
if (this.type) {
|
2023-10-29 20:22:30 -04:00
|
|
|
|
let collections = await tfrpc.rpc.query(`
|
|
|
|
|
SELECT messages.id, author, content, timestamp
|
|
|
|
|
FROM messages JOIN json_each(?1) AS id ON messages.author = id.value
|
|
|
|
|
WHERE
|
|
|
|
|
json_extract(content, '$.type') = ?2 AND
|
2023-11-01 20:45:20 -04:00
|
|
|
|
(?3 IS NULL OR json_extract(content, '$.parent') = ?3) OR
|
|
|
|
|
content LIKE '"%'
|
2023-10-29 20:22:30 -04:00
|
|
|
|
ORDER BY timestamp
|
2023-11-01 19:39:34 -04:00
|
|
|
|
`, [JSON.stringify(visible), this.type, this.parent]);
|
2023-11-01 18:21:42 -04:00
|
|
|
|
this.by_id = {};
|
2023-10-29 20:22:30 -04:00
|
|
|
|
for (let collection of collections) {
|
2023-11-01 20:45:20 -04:00
|
|
|
|
await this.process_message(collection);
|
2023-10-29 20:22:30 -04:00
|
|
|
|
}
|
2023-11-01 18:21:42 -04:00
|
|
|
|
this.collections = Object.values(this.by_id);
|
2023-10-29 20:22:30 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
this.collections_loading--;
|
|
|
|
|
}
|
2023-11-01 18:21:42 -04:00
|
|
|
|
if (this.selectname) {
|
|
|
|
|
this.set_selected_by_name(this.selectname);
|
|
|
|
|
}
|
2023-10-29 20:22:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async create(name, editors) {
|
|
|
|
|
let message = {
|
|
|
|
|
type: this.type,
|
|
|
|
|
name: name,
|
|
|
|
|
parent: this.parent,
|
|
|
|
|
editors: editors,
|
|
|
|
|
};
|
2023-11-01 18:21:42 -04:00
|
|
|
|
print('will append', message);
|
2023-10-29 20:22:30 -04:00
|
|
|
|
await tfrpc.rpc.appendMessage(this.whoami, message);
|
2023-11-01 18:21:42 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
notify_new_message(message) {
|
2023-11-01 19:39:34 -04:00
|
|
|
|
if (this.visible &&
|
|
|
|
|
this.visible.indexOf(message.author) != -1 &&
|
2023-11-01 18:21:42 -04:00
|
|
|
|
JSON.parse(message.content).type == this.type) {
|
2023-11-01 21:19:14 -04:00
|
|
|
|
let self = this;
|
2023-11-01 20:45:20 -04:00
|
|
|
|
this.process_message(message).then(function() {
|
2023-11-01 21:19:14 -04:00
|
|
|
|
self.collections = [...Object.values(self.by_id)];
|
2023-11-01 20:45:20 -04:00
|
|
|
|
});
|
2023-11-01 18:21:42 -04:00
|
|
|
|
}
|
2023-10-29 20:22:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async on_create(event) {
|
|
|
|
|
let name = this.shadowRoot.getElementById('create_name').value;
|
|
|
|
|
if (name) {
|
|
|
|
|
await this.create(name, [this.whoami]);
|
|
|
|
|
}
|
2023-11-01 21:43:32 -04:00
|
|
|
|
this.is_creating = false;
|
2023-10-29 20:22:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 20:29:07 -04:00
|
|
|
|
async on_rename(id) {
|
|
|
|
|
let name = this.shadowRoot.getElementById('rename_name').value;
|
2023-10-29 20:22:30 -04:00
|
|
|
|
let message = {
|
|
|
|
|
type: this.type,
|
2023-11-01 20:29:07 -04:00
|
|
|
|
key: this.selectid,
|
2023-11-01 18:21:42 -04:00
|
|
|
|
parent: this.parent,
|
2023-11-01 20:29:07 -04:00
|
|
|
|
name: name,
|
2023-10-29 20:22:30 -04:00
|
|
|
|
};
|
|
|
|
|
print(message);
|
|
|
|
|
await tfrpc.rpc.appendMessage(this.whoami, message);
|
2023-11-01 21:43:32 -04:00
|
|
|
|
this.is_renaming = false;
|
2023-11-01 20:29:07 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async on_tombstone(event) {
|
|
|
|
|
if (confirm(`Are you sure you want to delete this ${this.type}?`)) {
|
|
|
|
|
let message = {
|
|
|
|
|
type: this.type,
|
|
|
|
|
key: this.selectid,
|
|
|
|
|
parent: this.parent,
|
|
|
|
|
tombstone: {
|
|
|
|
|
date: new Date().valueOf(),
|
|
|
|
|
reason: 'archived',
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
print(message);
|
|
|
|
|
await tfrpc.rpc.appendMessage(this.whoami, message);
|
|
|
|
|
}
|
2023-10-29 20:22:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 18:21:42 -04:00
|
|
|
|
set_selected(id, value, by_user) {
|
2023-11-01 20:29:07 -04:00
|
|
|
|
this.selectid = id;
|
|
|
|
|
console.log('SEND', id, value?.name);
|
2023-10-29 20:22:30 -04:00
|
|
|
|
this.dispatchEvent(new CustomEvent('selected', {
|
|
|
|
|
bubbles: true,
|
|
|
|
|
detail: {
|
|
|
|
|
id: id,
|
|
|
|
|
value: value,
|
2023-11-01 18:21:42 -04:00
|
|
|
|
by_user: by_user,
|
2023-10-29 20:22:30 -04:00
|
|
|
|
},
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 18:21:42 -04:00
|
|
|
|
set_selected_by_name(name) {
|
2023-11-01 20:29:07 -04:00
|
|
|
|
console.log('set selected by name', name);
|
2023-11-01 18:21:42 -04:00
|
|
|
|
if (this.collections) {
|
|
|
|
|
for (let collection of this.collections) {
|
|
|
|
|
if (collection.name === name) {
|
|
|
|
|
this.set_selected(collection.id, collection);
|
2023-11-01 20:29:07 -04:00
|
|
|
|
this._select_by_name = undefined;
|
|
|
|
|
return;
|
2023-11-01 18:21:42 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this._select_by_name = name;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 19:39:34 -04:00
|
|
|
|
on_selected(event) {
|
|
|
|
|
if (this.collections) {
|
|
|
|
|
for (let collection of this.collections) {
|
|
|
|
|
if (collection.id === event.srcElement.value) {
|
|
|
|
|
this.set_selected(collection.id, collection, true);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-29 20:22:30 -04:00
|
|
|
|
render() {
|
2023-11-01 19:39:34 -04:00
|
|
|
|
let self = this;
|
2023-11-01 18:21:42 -04:00
|
|
|
|
let state = JSON.stringify([this.whoami, this.ids, this.parent]);
|
2023-10-29 20:22:30 -04:00
|
|
|
|
if (state !== this.loaded_for) {
|
|
|
|
|
this.loaded_for = state;
|
2023-11-01 18:21:42 -04:00
|
|
|
|
this.loaded = this.load();
|
2023-10-29 20:22:30 -04:00
|
|
|
|
}
|
2023-11-01 20:29:07 -04:00
|
|
|
|
if (this.collections) {
|
|
|
|
|
if (this.selectname) {
|
|
|
|
|
for (let collection of this.collections) {
|
|
|
|
|
if (collection.name === this.selectname) {
|
|
|
|
|
this.set_selected(collection.id, collection);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
for (let collection of this.collections) {
|
|
|
|
|
if (collection.id === this.selectid) {
|
|
|
|
|
this.set_selected(collection.id, collection);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-29 20:22:30 -04:00
|
|
|
|
return html`
|
2023-11-01 20:29:07 -04:00
|
|
|
|
<span style="display: inline-flex; flex-direction: row">
|
|
|
|
|
${this.collections_loading > 0 ? html`<div>Loading...</div>` : html`
|
|
|
|
|
<select @change=${this.on_selected}>
|
|
|
|
|
<option>(select)</option>
|
|
|
|
|
${this.collections?.map(x => html`<option value=${x.id} ?selected=${this.selectid === x.id}>${x.name}</option>`)}
|
|
|
|
|
</select>
|
|
|
|
|
`}
|
|
|
|
|
<span ?hidden=${!this.is_renaming}>
|
|
|
|
|
<label for="rename_name">Rename to:</label>
|
|
|
|
|
<input type="text" id="rename_name"></input>
|
|
|
|
|
<button @click=${this.on_rename}>Rename ${this.type}</button>
|
|
|
|
|
<button @click=${() => self.is_renaming = false}>x</button>
|
|
|
|
|
</span>
|
|
|
|
|
<button @click=${() => self.is_renaming = true} ?hidden=${this.is_renaming}>✏️</button>
|
|
|
|
|
<button @click=${self.on_tombstone}>🪦</button>
|
2023-11-01 21:43:32 -04:00
|
|
|
|
<span ?hidden=${!this.is_creating}>
|
|
|
|
|
<label for="create_name">New ${this.type} name:</label>
|
|
|
|
|
<input type="text" id="create_name"></input>
|
|
|
|
|
<button @click=${this.on_create}>Create ${this.type}</button>
|
|
|
|
|
<button @click=${() => self.is_creating = false}>x</button>
|
|
|
|
|
</span>
|
2023-11-01 20:29:07 -04:00
|
|
|
|
<button @click=${() => self.is_creating = true} ?hidden=${this.is_creating}>+</button>
|
2023-11-01 19:39:34 -04:00
|
|
|
|
</span>
|
2023-10-29 20:22:30 -04:00
|
|
|
|
`;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-01 18:21:42 -04:00
|
|
|
|
customElements.define('tf-collection', TfCollectionElement);
|