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 {
|
2023-11-03 22:00:35 -04:00
|
|
|
collection: {type: Object},
|
|
|
|
selected_id: {type: String},
|
2023-11-01 19:39:34 -04:00
|
|
|
is_creating: {type: Boolean},
|
2023-11-01 20:29:07 -04:00
|
|
|
is_renaming: {type: Boolean},
|
2023-10-29 20:22:30 -04:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-11-03 22:00:35 -04:00
|
|
|
on_create(event) {
|
2023-10-29 20:22:30 -04:00
|
|
|
let name = this.shadowRoot.getElementById('create_name').value;
|
2023-11-03 22:00:35 -04:00
|
|
|
this.dispatchEvent(new CustomEvent('create', {
|
|
|
|
bubbles: true,
|
|
|
|
detail: {
|
|
|
|
name: name,
|
|
|
|
},
|
|
|
|
}));
|
2023-11-01 21:43:32 -04:00
|
|
|
this.is_creating = false;
|
2023-10-29 20:22:30 -04:00
|
|
|
}
|
|
|
|
|
2023-11-03 22:00:35 -04:00
|
|
|
on_rename(event) {
|
|
|
|
let id = this.shadowRoot.getElementById('select').value;
|
2023-11-01 20:29:07 -04:00
|
|
|
let name = this.shadowRoot.getElementById('rename_name').value;
|
2023-11-03 22:00:35 -04:00
|
|
|
this.dispatchEvent(new CustomEvent('rename', {
|
|
|
|
bubbles: true,
|
|
|
|
detail: {
|
|
|
|
id: id,
|
|
|
|
value: this.collection[id],
|
|
|
|
name: name,
|
|
|
|
},
|
|
|
|
}));
|
2023-11-01 21:43:32 -04:00
|
|
|
this.is_renaming = false;
|
2023-11-01 20:29:07 -04:00
|
|
|
}
|
|
|
|
|
2023-11-03 22:00:35 -04:00
|
|
|
on_tombstone(event) {
|
|
|
|
let id = this.shadowRoot.getElementById('select').value;
|
|
|
|
if (confirm(`Are you sure you want to delete '${this.collection[id].name}'?`)) {
|
|
|
|
this.dispatchEvent(new CustomEvent('tombstone', {
|
|
|
|
bubbles: true,
|
|
|
|
detail: {
|
|
|
|
id: id,
|
|
|
|
value: this.collection[id],
|
2023-11-01 20:29:07 -04:00
|
|
|
},
|
2023-11-03 22:00:35 -04:00
|
|
|
}));
|
2023-11-01 20:29:07 -04:00
|
|
|
}
|
2023-10-29 20:22:30 -04:00
|
|
|
}
|
|
|
|
|
2023-11-03 22:00:35 -04:00
|
|
|
on_selected(event) {
|
|
|
|
let id = event.srcElement.value;
|
|
|
|
this.selected_id = id != '' ? id : undefined;
|
|
|
|
this.dispatchEvent(new CustomEvent('change', {
|
2023-10-29 20:22:30 -04:00
|
|
|
bubbles: true,
|
|
|
|
detail: {
|
|
|
|
id: id,
|
2023-11-03 22:00:35 -04:00
|
|
|
value: this.collection[id],
|
2023-10-29 20:22:30 -04:00
|
|
|
},
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2023-11-01 19:39:34 -04:00
|
|
|
let self = this;
|
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">
|
2023-11-04 12:32:21 -04:00
|
|
|
<select @change=${this.on_selected} id="select" value=${this.selected_id}>
|
|
|
|
<option value="" ?selected=${this.selected_id === ''} disabled hidden>(select)</option>
|
|
|
|
${Object.values(this.collection ?? {}).map(x => html`<option value=${x.id} ?selected=${this.selected_id === x.id}>${x.name}</option>`)}
|
2023-11-03 22:00:35 -04:00
|
|
|
</select>
|
2023-11-01 20:29:07 -04:00
|
|
|
<span ?hidden=${!this.is_renaming}>
|
2023-11-04 12:32:21 -04:00
|
|
|
<span style="display: inline-flex; flex-direction: row; margin-left: 8px; margin-right: 8px">
|
|
|
|
<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>
|
2023-11-01 20:29:07 -04:00
|
|
|
</span>
|
2023-11-04 12:32:21 -04:00
|
|
|
<button @click=${() => self.is_renaming = true} ?disabled=${this.is_renaming || !this.selected_id}>🏷</button>
|
2023-11-03 22:00:35 -04:00
|
|
|
<button @click=${self.on_tombstone} ?disabled=${!this.selected_id}>🪦</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);
|