2024-05-13 00:14:22 +00:00
|
|
|
import { html, LitElement, TemplateResult } from 'lit'
|
|
|
|
import { repeat } from 'lit/directives/repeat.js'
|
|
|
|
import { customElement, property, state } from 'lit/decorators.js'
|
2024-05-13 14:40:36 +00:00
|
|
|
import { unsafeHTML } from 'lit/directives/unsafe-html.js'
|
|
|
|
import { ifDefined } from 'lit/directives/if-defined.js'
|
|
|
|
import { StaticValue, unsafeStatic } from 'lit/static-html.js'
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
type DynamicTableAcceptedTypes = number | string | boolean | Date
|
|
|
|
|
|
|
|
type DynamicTableAcceptedInputTypes = 'textarea'
|
|
|
|
| 'select'
|
|
|
|
| 'checkbox'
|
|
|
|
| 'range'
|
|
|
|
| 'color'
|
|
|
|
| 'date'
|
|
|
|
| 'datetime'
|
|
|
|
| 'datetime-local'
|
|
|
|
| 'email'
|
|
|
|
| 'file'
|
|
|
|
| 'image'
|
|
|
|
| 'month'
|
|
|
|
| 'number'
|
|
|
|
| 'password'
|
|
|
|
| 'tel'
|
|
|
|
| 'text'
|
|
|
|
| 'time'
|
|
|
|
| 'url'
|
|
|
|
| 'week'
|
|
|
|
|
|
|
|
|
|
|
|
interface CellDataSchema {
|
|
|
|
min?: number
|
|
|
|
max?: number
|
|
|
|
minlength?: number
|
|
|
|
maxlength?: number
|
|
|
|
size?: number
|
|
|
|
label?: string
|
|
|
|
options?: { [key: string]: string }
|
|
|
|
inputType?: DynamicTableAcceptedInputTypes
|
|
|
|
default?: DynamicTableAcceptedTypes
|
|
|
|
}
|
|
|
|
|
|
|
|
@customElement('dynamic-table-form')
|
|
|
|
export class DynamicTableFormElement extends LitElement {
|
|
|
|
|
|
|
|
@property({ attribute: false })
|
2024-05-13 14:40:36 +00:00
|
|
|
public header: { [key : string]: { colName: TemplateResult, description: TemplateResult } } = {}
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
@property({ attribute: false })
|
|
|
|
public schema: { [key : string]: CellDataSchema } = {}
|
|
|
|
|
|
|
|
|
|
|
|
@property({ reflect: true })
|
|
|
|
public rows: { _id: number; [key : string]: DynamicTableAcceptedTypes }[] = []
|
|
|
|
|
|
|
|
|
|
|
|
@property({ attribute: false })
|
|
|
|
public formName: string = ''
|
|
|
|
|
|
|
|
|
|
|
|
@state()
|
|
|
|
private _lastRowId = 1
|
|
|
|
|
|
|
|
createRenderRoot = () => {
|
|
|
|
return this
|
|
|
|
}
|
|
|
|
|
|
|
|
private _getDefaultRow = () => {
|
|
|
|
return Object.fromEntries([...Object.entries(this.schema).map((entry) => [entry[0], entry[1].default ?? '']), ['_id', this._lastRowId++]])
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private _addRow = () => {
|
|
|
|
this.rows.push(this._getDefaultRow())
|
|
|
|
|
|
|
|
this.requestUpdate('rows')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private _removeRow = (rowId: number) => {
|
|
|
|
this.rows = this.rows.filter((x) => x._id != rowId)
|
|
|
|
|
|
|
|
this.requestUpdate('rows')
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
render = () => {
|
|
|
|
const inputId = `peertube-livechat-${this.formName.replaceAll('_','-')}-table`
|
|
|
|
|
2024-05-13 14:40:36 +00:00
|
|
|
for(let row of this.rows) {
|
|
|
|
if (!row._id) {
|
|
|
|
row._id = this._lastRowId++
|
|
|
|
}
|
|
|
|
}
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
return html`
|
2024-05-13 14:40:36 +00:00
|
|
|
<table class="table table-striped table-hover table-sm" id=${inputId}>
|
|
|
|
${this._renderHeader()}
|
|
|
|
<tbody>
|
|
|
|
${repeat(this.rows,(row) => row._id, this._renderDataRow)}
|
|
|
|
</tbody>
|
|
|
|
<tfoot>
|
|
|
|
<tr><td><button @click=${this._addRow}>Add Row</button></td></tr>
|
|
|
|
</tfoot>
|
|
|
|
</table>
|
2024-05-13 00:14:22 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
private _renderHeader = () => {
|
2024-05-13 14:40:36 +00:00
|
|
|
return html`<thead>
|
|
|
|
<tr>
|
|
|
|
<!-- <th scope="col">#</th> -->
|
|
|
|
${Object.values(this.header).map(this._renderHeaderCell)}
|
|
|
|
<th scope="col">Remove Row</th>
|
|
|
|
</tr>
|
|
|
|
</thead>`
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-05-13 14:40:36 +00:00
|
|
|
private _renderHeaderCell = (headerCellData: { colName: TemplateResult, description: TemplateResult }) => {
|
|
|
|
return html`<th scope="col">
|
|
|
|
<div data-toggle="tooltip" data-placement="bottom" data-html="true" title="${headerCellData.description}">${headerCellData.colName}</div>
|
|
|
|
</th>`
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private _renderDataRow = (rowData: { _id: number; [key : string]: DynamicTableAcceptedTypes }) => {
|
|
|
|
const inputId = `peertube-livechat-${this.formName.replaceAll('_','-')}-row-${rowData._id}`
|
|
|
|
|
2024-05-13 14:40:36 +00:00
|
|
|
return html`<tr id=${inputId}>
|
|
|
|
<!-- <td class="form-group">${rowData._id}</td> -->
|
|
|
|
${Object.entries(rowData).filter(([k,v]) => k != '_id').map((data) => this.renderDataCell(data, rowData._id))}
|
|
|
|
<td class="form-group"><button @click=${() => this._removeRow(rowData._id)}>Remove</button></td>
|
|
|
|
</tr>`
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
renderDataCell = (property: [string, DynamicTableAcceptedTypes], rowId: number) => {
|
|
|
|
const [propertyName, propertyValue] = property
|
|
|
|
const propertySchema = this.schema[propertyName] ?? {}
|
|
|
|
|
|
|
|
let formElement
|
|
|
|
|
|
|
|
const inputName = `${this.formName.replaceAll('-','_')}_${propertyName.toString().replaceAll('-','_')}_${rowId}`
|
|
|
|
const inputId = `peertube-livechat-${this.formName.replaceAll('_','-')}-${propertyName.toString().replaceAll('_','-')}-${rowId}`
|
|
|
|
|
|
|
|
switch (propertyValue.constructor) {
|
|
|
|
case String:
|
|
|
|
switch (propertySchema.inputType) {
|
|
|
|
case undefined:
|
|
|
|
propertySchema.inputType = 'text'
|
|
|
|
|
|
|
|
case 'text':
|
|
|
|
case 'color':
|
|
|
|
case 'date':
|
|
|
|
case 'datetime':
|
|
|
|
case 'datetime-local':
|
|
|
|
case 'email':
|
|
|
|
case 'file':
|
|
|
|
case 'image':
|
|
|
|
case 'month':
|
|
|
|
case 'number':
|
|
|
|
case 'password':
|
|
|
|
case 'range':
|
|
|
|
case 'tel':
|
|
|
|
case 'time':
|
|
|
|
case 'url':
|
|
|
|
case 'week':
|
|
|
|
formElement = html`<input
|
|
|
|
type=${propertySchema.inputType}
|
|
|
|
name=${inputName}
|
|
|
|
class="form-control"
|
|
|
|
id=${inputId}
|
2024-05-13 14:40:36 +00:00
|
|
|
min=${ifDefined(propertySchema?.min)}
|
|
|
|
max=${ifDefined(propertySchema?.max)}
|
|
|
|
minlength=${ifDefined(propertySchema?.minlength)}
|
|
|
|
maxlength=${ifDefined(propertySchema?.maxlength)}
|
|
|
|
@input=${(event: InputEvent) => this._updatePropertyFromValue(event, propertyName, rowId)}
|
|
|
|
.value=${propertyValue as string}
|
2024-05-13 00:14:22 +00:00
|
|
|
/>`
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'textarea':
|
|
|
|
formElement = html`<textarea
|
|
|
|
name=${inputName}
|
|
|
|
class="form-control"
|
|
|
|
id=${inputId}
|
2024-05-13 14:40:36 +00:00
|
|
|
min=${ifDefined(propertySchema?.min)}
|
|
|
|
max=${ifDefined(propertySchema?.max)}
|
|
|
|
minlength=${ifDefined(propertySchema?.minlength)}
|
|
|
|
maxlength=${ifDefined(propertySchema?.maxlength)}
|
|
|
|
@input=${(event: InputEvent) => this._updatePropertyFromValue(event, propertyName, rowId)}
|
|
|
|
.value=${propertyValue as string}
|
2024-05-13 00:14:22 +00:00
|
|
|
></textarea>`
|
|
|
|
break
|
|
|
|
|
|
|
|
case 'select':
|
2024-05-13 14:40:36 +00:00
|
|
|
formElement = html`<select
|
|
|
|
class="form-select"
|
|
|
|
aria-label="Default select example"
|
|
|
|
@change=${(event: InputEvent) => this._updatePropertyFromValue(event, propertyName, rowId)}
|
|
|
|
>
|
2024-05-13 00:14:22 +00:00
|
|
|
<option ?selected=${!propertyValue}>${propertySchema?.label ?? 'Choose your option'}</option>
|
2024-05-13 14:40:36 +00:00
|
|
|
${Object.entries(propertySchema?.options ?? {})
|
|
|
|
?.map(([value,name]) =>
|
|
|
|
html`<option ?selected=${propertyValue === value} value=${value}>${name}</option>`
|
|
|
|
)}
|
2024-05-13 00:14:22 +00:00
|
|
|
</select>`
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case Date:
|
|
|
|
switch (propertySchema.inputType) {
|
|
|
|
case undefined:
|
|
|
|
propertySchema.inputType = 'datetime'
|
|
|
|
|
|
|
|
case 'date':
|
|
|
|
case 'datetime':
|
|
|
|
case 'datetime-local':
|
|
|
|
case 'time':
|
|
|
|
formElement = html`<input
|
|
|
|
type=${propertySchema.inputType}
|
|
|
|
name=${inputName}
|
|
|
|
class="form-control"
|
|
|
|
id=${inputId}
|
2024-05-13 14:40:36 +00:00
|
|
|
min=${ifDefined(propertySchema?.min)}
|
|
|
|
max=${ifDefined(propertySchema?.max)}
|
|
|
|
minlength=${ifDefined(propertySchema?.minlength)}
|
|
|
|
maxlength=${ifDefined(propertySchema?.maxlength)}
|
|
|
|
@input=${(event: InputEvent) => this._updatePropertyFromValue(event, propertyName, rowId)}
|
|
|
|
.value=${(propertyValue as Date).toISOString()}
|
2024-05-13 00:14:22 +00:00
|
|
|
/>`
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case Number:
|
|
|
|
switch (propertySchema.inputType) {
|
|
|
|
case undefined:
|
|
|
|
propertySchema.inputType = 'number'
|
|
|
|
|
|
|
|
case 'number':
|
|
|
|
case 'range':
|
|
|
|
formElement = html`<input
|
|
|
|
type=${propertySchema.inputType}
|
|
|
|
name=${inputName}
|
|
|
|
class="form-control"
|
|
|
|
id=${inputId}
|
2024-05-13 14:40:36 +00:00
|
|
|
min=${ifDefined(propertySchema?.min)}
|
|
|
|
max=${ifDefined(propertySchema?.max)}
|
|
|
|
minlength=${ifDefined(propertySchema?.minlength)}
|
|
|
|
maxlength=${ifDefined(propertySchema?.maxlength)}
|
|
|
|
@input=${(event: InputEvent) => this._updatePropertyFromValue(event, propertyName, rowId)}
|
|
|
|
.value=${propertyValue as String}
|
2024-05-13 00:14:22 +00:00
|
|
|
/>`
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case Boolean:
|
|
|
|
switch (propertySchema.inputType) {
|
|
|
|
case undefined:
|
|
|
|
propertySchema.inputType = 'checkbox'
|
|
|
|
|
|
|
|
case 'checkbox':
|
|
|
|
formElement = html`<input
|
|
|
|
type="checkbox"
|
|
|
|
name=${inputName}
|
|
|
|
class="form-check-input"
|
|
|
|
id=${inputId}
|
2024-05-13 14:40:36 +00:00
|
|
|
@input=${(event: InputEvent) => this._updatePropertyFromValue(event, propertyName, rowId)}
|
|
|
|
.value=${propertyValue as String}
|
|
|
|
?checked=${propertyValue as Boolean}
|
2024-05-13 00:14:22 +00:00
|
|
|
/>`
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!formElement) {
|
2024-05-13 14:40:36 +00:00
|
|
|
console.warn(`value type '${propertyValue.constructor}' is incompatible`
|
|
|
|
+ `with field type '${propertySchema.inputType}' for form entry '${propertyName.toString()}'.`)
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return html`<td class="form-group">${formElement}</td>`
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-05-13 14:40:36 +00:00
|
|
|
_updatePropertyFromValue(event: Event, propertyName: string, rowId : number) {
|
2024-05-13 00:14:22 +00:00
|
|
|
let target = event?.target as (HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement)
|
2024-05-13 14:40:36 +00:00
|
|
|
let value = (target && target instanceof HTMLInputElement && target.type == "checkbox") ? !!(target?.checked) : target?.value
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-13 14:40:36 +00:00
|
|
|
if(value !== undefined) {
|
2024-05-13 00:14:22 +00:00
|
|
|
for(let row of this.rows) {
|
|
|
|
if(row._id === rowId) {
|
2024-05-13 14:40:36 +00:00
|
|
|
row[propertyName] = value
|
|
|
|
|
|
|
|
this.requestUpdate('rows')
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
console.warn(`Could not update property : Did not find a property named '${propertyName}' in row '${rowId}'`)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
console.warn(`Could not update property : Target or value was undefined`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|