| 
									
										
										
										
											2023-10-30 00:22:30 +00:00
										 |  |  | import {LitElement, html} from './lit-all.min.js'; | 
					
						
							|  |  |  | import * as tfrpc from '/static/tfrpc.js'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-01 22:21:42 +00:00
										 |  |  | class TfCollectionElement extends LitElement { | 
					
						
							| 
									
										
										
										
											2023-10-30 00:22:30 +00:00
										 |  |  | 	static get properties() { | 
					
						
							|  |  |  | 		return { | 
					
						
							| 
									
										
										
										
											2023-11-07 23:34:55 +00:00
										 |  |  | 			whoami: {type: String}, | 
					
						
							| 
									
										
										
										
											2024-02-22 18:11:13 +01:00
										 |  |  | 			category: {type: String}, | 
					
						
							| 
									
										
										
										
											2023-11-04 02:00:35 +00:00
										 |  |  | 			collection: {type: Object}, | 
					
						
							|  |  |  | 			selected_id: {type: String}, | 
					
						
							| 
									
										
										
										
											2023-11-01 23:39:34 +00:00
										 |  |  | 			is_creating: {type: Boolean}, | 
					
						
							| 
									
										
										
										
											2023-11-02 00:29:07 +00:00
										 |  |  | 			is_renaming: {type: Boolean}, | 
					
						
							| 
									
										
										
										
											2023-10-30 00:22:30 +00:00
										 |  |  | 		}; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-04 02:00:35 +00:00
										 |  |  | 	on_create(event) { | 
					
						
							| 
									
										
										
										
											2023-10-30 00:22:30 +00:00
										 |  |  | 		let name = this.shadowRoot.getElementById('create_name').value; | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 		this.dispatchEvent( | 
					
						
							|  |  |  | 			new CustomEvent('create', { | 
					
						
							|  |  |  | 				bubbles: true, | 
					
						
							|  |  |  | 				detail: { | 
					
						
							|  |  |  | 					name: name, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2023-11-02 01:43:32 +00:00
										 |  |  | 		this.is_creating = false; | 
					
						
							| 
									
										
										
										
											2023-10-30 00:22:30 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-04 02:00:35 +00:00
										 |  |  | 	on_rename(event) { | 
					
						
							|  |  |  | 		let id = this.shadowRoot.getElementById('select').value; | 
					
						
							| 
									
										
										
										
											2023-11-02 00:29:07 +00:00
										 |  |  | 		let name = this.shadowRoot.getElementById('rename_name').value; | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 		this.dispatchEvent( | 
					
						
							|  |  |  | 			new CustomEvent('rename', { | 
					
						
							| 
									
										
										
										
											2023-11-04 02:00:35 +00:00
										 |  |  | 				bubbles: true, | 
					
						
							|  |  |  | 				detail: { | 
					
						
							|  |  |  | 					id: id, | 
					
						
							|  |  |  | 					value: this.collection[id], | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 					name: name, | 
					
						
							| 
									
										
										
										
											2023-11-02 00:29:07 +00:00
										 |  |  | 				}, | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 			}) | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		this.is_renaming = false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	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-02 00:29:07 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2023-10-30 00:22:30 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-04 02:00:35 +00:00
										 |  |  | 	on_selected(event) { | 
					
						
							|  |  |  | 		let id = event.srcElement.value; | 
					
						
							|  |  |  | 		this.selected_id = id != '' ? id : undefined; | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 		this.dispatchEvent( | 
					
						
							|  |  |  | 			new CustomEvent('change', { | 
					
						
							|  |  |  | 				bubbles: true, | 
					
						
							|  |  |  | 				detail: { | 
					
						
							|  |  |  | 					id: id, | 
					
						
							|  |  |  | 					value: this.collection[id], | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}) | 
					
						
							|  |  |  | 		); | 
					
						
							| 
									
										
										
										
											2023-10-30 00:22:30 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	render() { | 
					
						
							| 
									
										
										
										
											2023-11-01 23:39:34 +00:00
										 |  |  | 		let self = this; | 
					
						
							| 
									
										
										
										
											2023-10-30 00:22:30 +00:00
										 |  |  | 		return html`
 | 
					
						
							| 
									
										
										
										
											2024-02-22 16:11:49 +01:00
										 |  |  | 			<link rel="stylesheet" href="tildefriends.css"/> | 
					
						
							| 
									
										
										
										
											2024-02-22 13:03:21 +01:00
										 |  |  | 			<span class="inline-flex-row"> | 
					
						
							| 
									
										
										
										
											2023-11-04 16:32:21 +00:00
										 |  |  | 				<select @change=${this.on_selected} id="select" value=${this.selected_id}> | 
					
						
							| 
									
										
										
										
											2024-02-22 18:11:13 +01:00
										 |  |  | 					<option value="" ?selected=${this.selected_id === ''} disabled hidden>(select ${this.category})</option> | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 					${Object.values(this.collection ?? {}) | 
					
						
							|  |  |  | 						.sort((x, y) => x.name.localeCompare(y.name)) | 
					
						
							|  |  |  | 						.map( | 
					
						
							|  |  |  | 							(x) => | 
					
						
							|  |  |  | 								html`<option
 | 
					
						
							|  |  |  | 									value=${x.id} | 
					
						
							|  |  |  | 									?selected=${this.selected_id === x.id} | 
					
						
							|  |  |  | 								> | 
					
						
							|  |  |  | 									${x.name} | 
					
						
							|  |  |  | 								</option>` | 
					
						
							|  |  |  | 						)} | 
					
						
							| 
									
										
										
										
											2023-11-04 02:00:35 +00:00
										 |  |  | 				</select> | 
					
						
							| 
									
										
										
										
											2023-11-07 23:34:55 +00:00
										 |  |  | 				<span ?hidden=${!this.is_renaming || !this.whoami}> | 
					
						
							| 
									
										
										
										
											2024-02-22 13:03:21 +01:00
										 |  |  | 					<span class="inline-flex-row" style="margin-left: 8px; margin-right: 8px"> | 
					
						
							| 
									
										
										
										
											2023-11-04 16:32:21 +00:00
										 |  |  | 						<label for="rename_name">🏷Rename to:</label> | 
					
						
							|  |  |  | 						<input type="text" id="rename_name"></input> | 
					
						
							|  |  |  | 						<button @click=${this.on_rename}>Rename ${this.type}</button> | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 						<button @click=${() => (self.is_renaming = false)}>x</button> | 
					
						
							| 
									
										
										
										
											2023-11-04 16:32:21 +00:00
										 |  |  | 					</span> | 
					
						
							| 
									
										
										
										
											2023-11-02 00:29:07 +00:00
										 |  |  | 				</span> | 
					
						
							| 
									
										
										
										
											2024-02-25 15:37:13 -05:00
										 |  |  | 				<button class="yellow" @click=${() => (self.is_renaming = true)} ?disabled=${this.is_renaming || !this.selected_id} ?hidden=${!this.whoami}>🏷</button> | 
					
						
							| 
									
										
										
										
											2024-02-22 13:03:21 +01:00
										 |  |  | 				<button class="red" @click=${self.on_tombstone} ?disabled=${!this.selected_id} ?hidden=${!this.whoami}>🪦</button> | 
					
						
							| 
									
										
										
										
											2023-11-07 23:34:55 +00:00
										 |  |  | 				<span ?hidden=${!this.is_creating || !this.whoami}> | 
					
						
							| 
									
										
										
										
											2023-11-02 01:43:32 +00:00
										 |  |  | 					<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> | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | 					<button @click=${() => (self.is_creating = false)}>x</button> | 
					
						
							| 
									
										
										
										
											2023-11-02 01:43:32 +00:00
										 |  |  | 				</span> | 
					
						
							| 
									
										
										
										
											2024-02-25 15:37:13 -05:00
										 |  |  | 				<button class="green" @click=${() => (self.is_creating = true)} ?hidden=${this.is_creating || !this.whoami}>+</button> | 
					
						
							| 
									
										
										
										
											2023-11-01 23:39:34 +00:00
										 |  |  | 			</span> | 
					
						
							| 
									
										
										
										
											2023-10-30 00:22:30 +00:00
										 |  |  | 		`;
 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-02-24 11:09:34 -05:00
										 |  |  | customElements.define('tf-collection', TfCollectionElement); |