2024-05-23 15:17:28 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 Mehdi Benadel <https://mehdibenadel.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
/* eslint no-fallthrough: "off" */
|
|
|
|
|
2024-05-23 16:38:07 +00:00
|
|
|
import { html, nothing, TemplateResult } from 'lit'
|
2024-05-13 00:14:22 +00:00
|
|
|
import { repeat } from 'lit/directives/repeat.js'
|
|
|
|
import { customElement, property, state } from 'lit/decorators.js'
|
2024-05-13 14:40:36 +00:00
|
|
|
import { ifDefined } from 'lit/directives/if-defined.js'
|
2024-05-23 00:26:38 +00:00
|
|
|
import { unsafeHTML } from 'lit/directives/unsafe-html.js'
|
2024-05-23 16:38:07 +00:00
|
|
|
import { LivechatElement } from './livechat'
|
2024-05-23 00:26:38 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
// This content comes from the file assets/images/plus-square.svg, from the Feather icons set https://feathericons.com/
|
2024-05-23 00:26:38 +00:00
|
|
|
const AddSVG: string =
|
2024-05-23 20:52:39 +00:00
|
|
|
`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
|
|
|
|
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
|
|
|
|
stroke-linejoin="round" class="feather feather-plus-square">
|
2024-05-23 00:26:38 +00:00
|
|
|
<rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
|
|
|
|
<line x1="12" y1="8" x2="12" y2="16"></line><line x1="8" y1="12" x2="16" y2="12"></line>
|
|
|
|
</svg>`
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
// This content comes from the file assets/images/x-square.svg, from the Feather icons set https://feathericons.com/
|
2024-05-23 00:26:38 +00:00
|
|
|
const RemoveSVG: string =
|
2024-05-23 20:52:39 +00:00
|
|
|
`<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24"
|
|
|
|
fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"
|
|
|
|
stroke-linejoin="round" class="feather feather-x-square">
|
2024-05-23 00:26:38 +00:00
|
|
|
<rect x="3" y="3" width="18" height="18" rx="2" ry="2"></rect>
|
|
|
|
<line x1="9" y1="9" x2="15" y2="15"></line><line x1="15" y1="9" x2="9" y2="15"></line>
|
|
|
|
</svg>`
|
|
|
|
|
|
|
|
type DynamicTableAcceptedTypes = number | string | boolean | Date | Array<number | string>
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
type DynamicTableAcceptedInputTypes = 'textarea'
|
2024-05-23 20:52:39 +00:00
|
|
|
| 'select'
|
|
|
|
| 'checkbox'
|
|
|
|
| 'range'
|
|
|
|
| 'color'
|
|
|
|
| 'date'
|
|
|
|
| 'datetime'
|
|
|
|
| 'datetime-local'
|
|
|
|
| 'email'
|
|
|
|
| 'file'
|
|
|
|
| 'image'
|
|
|
|
| 'month'
|
|
|
|
| 'number'
|
|
|
|
| 'password'
|
|
|
|
| 'tel'
|
|
|
|
| 'text'
|
|
|
|
| 'time'
|
|
|
|
| 'url'
|
|
|
|
| 'week'
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
interface CellDataSchema {
|
|
|
|
min?: number
|
|
|
|
max?: number
|
|
|
|
minlength?: number
|
|
|
|
maxlength?: number
|
|
|
|
size?: number
|
|
|
|
label?: string
|
|
|
|
options?: { [key: string]: string }
|
2024-05-23 00:26:38 +00:00
|
|
|
datalist?: DynamicTableAcceptedTypes[]
|
|
|
|
separator?: string
|
2024-05-13 00:14:22 +00:00
|
|
|
inputType?: DynamicTableAcceptedInputTypes
|
|
|
|
default?: DynamicTableAcceptedTypes
|
|
|
|
}
|
|
|
|
|
2024-05-23 13:52:12 +00:00
|
|
|
@customElement('livechat-dynamic-table-form')
|
2024-05-23 16:38:07 +00:00
|
|
|
export class DynamicTableFormElement extends LivechatElement {
|
2024-05-13 00:14:22 +00:00
|
|
|
@property({ attribute: false })
|
2024-05-23 00:26:38 +00:00
|
|
|
public header: { [key: string]: { colName: TemplateResult, description: TemplateResult } } = {}
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
@property({ attribute: false })
|
2024-05-23 00:26:38 +00:00
|
|
|
public schema: { [key: string]: CellDataSchema } = {}
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
@property({ attribute: false })
|
2024-05-23 20:52:39 +00:00
|
|
|
public rows: Array<{ [key: string]: DynamicTableAcceptedTypes }> = []
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
@state()
|
2024-05-23 20:52:39 +00:00
|
|
|
public _rowsById: Array<{ _id: number, row: { [key: string]: DynamicTableAcceptedTypes } }> = []
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
@property({ attribute: false })
|
|
|
|
public formName: string = ''
|
|
|
|
|
|
|
|
@state()
|
|
|
|
private _lastRowId = 1
|
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
@property({ attribute: false })
|
2024-05-23 19:51:25 +00:00
|
|
|
private columnOrder: string[] = []
|
2024-05-23 00:26:38 +00:00
|
|
|
|
|
|
|
// fixes situations when list has been reinitialized or changed outside of CustomElement
|
2024-05-23 20:52:39 +00:00
|
|
|
private readonly _updateLastRowId = (): void => {
|
|
|
|
for (const rowById of this._rowsById) {
|
|
|
|
this._lastRowId = Math.max(this._lastRowId, rowById._id + 1)
|
2024-05-23 00:26:38 +00:00
|
|
|
}
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
private readonly _getDefaultRow = (): { [key: string]: DynamicTableAcceptedTypes } => {
|
2024-05-23 00:26:38 +00:00
|
|
|
this._updateLastRowId()
|
|
|
|
return Object.fromEntries([...Object.entries(this.schema).map((entry) => [entry[0], entry[1].default ?? ''])])
|
|
|
|
}
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
private readonly _addRow = (): void => {
|
|
|
|
const newRow = this._getDefaultRow()
|
|
|
|
this._rowsById.push({ _id: this._lastRowId++, row: newRow })
|
2024-05-23 00:26:38 +00:00
|
|
|
this.rows.push(newRow)
|
|
|
|
this.requestUpdate('rows')
|
2024-05-23 19:51:25 +00:00
|
|
|
this.requestUpdate('_rowsById')
|
2024-05-23 00:26:38 +00:00
|
|
|
this.dispatchEvent(new CustomEvent('update', { detail: this.rows }))
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
private readonly _removeRow = (rowId: number): void => {
|
|
|
|
const rowToRemove = this._rowsById.filter(rowById => rowById._id === rowId).map(rowById => rowById.row)[0]
|
2024-05-23 19:51:25 +00:00
|
|
|
this._rowsById = this._rowsById.filter(rowById => rowById._id !== rowId)
|
2024-05-23 00:26:38 +00:00
|
|
|
this.rows = this.rows.filter((row) => row !== rowToRemove)
|
|
|
|
this.requestUpdate('rows')
|
2024-05-23 19:51:25 +00:00
|
|
|
this.requestUpdate('_rowsById')
|
2024-05-23 00:26:38 +00:00
|
|
|
this.dispatchEvent(new CustomEvent('update', { detail: this.rows }))
|
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
protected override render = (): unknown => {
|
2024-05-23 14:56:11 +00:00
|
|
|
const inputId = `peertube-livechat-${this.formName.replace(/_/g, '-')}-table`
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
this._updateLastRowId()
|
|
|
|
|
|
|
|
this._rowsById.filter(rowById => this.rows.includes(rowById.row))
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
for (const row of this.rows) {
|
|
|
|
if (this._rowsById.filter(rowById => rowById.row === row).length === 0) {
|
|
|
|
this._rowsById.push({ _id: this._lastRowId++, row })
|
2024-05-13 14:40:36 +00:00
|
|
|
}
|
|
|
|
}
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-23 19:51:25 +00:00
|
|
|
if (this.columnOrder.length !== Object.keys(this.header).length) {
|
|
|
|
this.columnOrder = this.columnOrder.filter(key => Object.keys(this.header).includes(key))
|
|
|
|
this.columnOrder.push(...Object.keys(this.header).filter(key => !this.columnOrder.includes(key)))
|
|
|
|
}
|
|
|
|
|
2024-05-13 00:14:22 +00:00
|
|
|
return html`
|
2024-05-23 19:51:25 +00:00
|
|
|
<div class="table-responsive">
|
|
|
|
<table class="table" id=${inputId}>
|
|
|
|
${this._renderHeader()}
|
|
|
|
<tbody>
|
|
|
|
${repeat(this._rowsById, (rowById) => rowById._id, this._renderDataRow)}
|
|
|
|
</tbody>
|
|
|
|
${this._renderFooter()}
|
|
|
|
</table>
|
|
|
|
</div>
|
2024-05-13 00:14:22 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
private readonly _renderHeader = (): TemplateResult => {
|
2024-05-13 14:40:36 +00:00
|
|
|
return html`<thead>
|
|
|
|
<tr>
|
2024-05-23 20:52:39 +00:00
|
|
|
${Object.entries(this.header)
|
|
|
|
.sort(([k1, _1], [k2, _2]) => this.columnOrder.indexOf(k1) - this.columnOrder.indexOf(k2))
|
|
|
|
.map(([_, v]) => this._renderHeaderCell(v))}
|
2024-05-23 00:26:38 +00:00
|
|
|
<th scope="col"></th>
|
2024-05-13 14:40:36 +00:00
|
|
|
</tr>
|
|
|
|
</thead>`
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
private readonly _renderHeaderCell = (headerCellData: { colName: TemplateResult
|
|
|
|
description: TemplateResult }): TemplateResult => {
|
2024-05-13 14:40:36 +00:00
|
|
|
return html`<th scope="col">
|
2024-05-23 20:52:39 +00:00
|
|
|
<div data-toggle="tooltip" data-placement="bottom" data-html="true" title=${headerCellData.description}>
|
|
|
|
${headerCellData.colName}
|
|
|
|
</div>
|
2024-05-13 14:40:36 +00:00
|
|
|
</th>`
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
private readonly _renderDataRow = (rowData: { _id: number
|
|
|
|
row: {[key: string]: DynamicTableAcceptedTypes} }): TemplateResult => {
|
2024-05-23 14:56:11 +00:00
|
|
|
const inputId = `peertube-livechat-${this.formName.replace(/_/g, '-')}-row-${rowData._id}`
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-13 14:40:36 +00:00
|
|
|
return html`<tr id=${inputId}>
|
2024-05-23 20:52:39 +00:00
|
|
|
${Object.keys(this.header)
|
|
|
|
.sort((k1, k2) => this.columnOrder.indexOf(k1) - this.columnOrder.indexOf(k2))
|
|
|
|
.map(k => this.renderDataCell([k, rowData.row[k] ?? this.schema[k].default], rowData._id))}
|
|
|
|
<td class="form-group">
|
|
|
|
<button type="button"
|
|
|
|
class="peertube-button-link orange-button dynamic-table-remove-row"
|
|
|
|
@click=${() => this._removeRow(rowData._id)}>
|
|
|
|
${unsafeHTML(RemoveSVG)}
|
|
|
|
</button>
|
|
|
|
</td>
|
2024-05-13 14:40:36 +00:00
|
|
|
</tr>`
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
private readonly _renderFooter = (): TemplateResult => {
|
2024-05-23 00:26:38 +00:00
|
|
|
return html`<tfoot>
|
|
|
|
<tr>
|
|
|
|
${Object.values(this.header).map(() => html`<td></td>`)}
|
2024-05-23 20:52:39 +00:00
|
|
|
<td>
|
|
|
|
<button type="button"
|
|
|
|
class="peertube-button-link orange-button dynamic-table-add-row"
|
|
|
|
@click=${this._addRow}>
|
|
|
|
${unsafeHTML(AddSVG)}
|
|
|
|
</button>
|
|
|
|
</td>
|
2024-05-23 00:26:38 +00:00
|
|
|
</tr>
|
|
|
|
</tfoot>`
|
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
renderDataCell = (property: [string, DynamicTableAcceptedTypes], rowId: number): TemplateResult => {
|
2024-05-23 19:51:25 +00:00
|
|
|
let [propertyName, propertyValue] = property
|
2024-05-13 00:14:22 +00:00
|
|
|
const propertySchema = this.schema[propertyName] ?? {}
|
|
|
|
|
|
|
|
let formElement
|
|
|
|
|
2024-05-23 14:56:11 +00:00
|
|
|
const inputName = `${this.formName.replace(/-/g, '_')}_${propertyName.toString().replace(/-/g, '_')}_${rowId}`
|
2024-05-23 20:52:39 +00:00
|
|
|
const inputId =
|
|
|
|
`peertube-livechat-${this.formName.replace(/_/g, '-')}-${propertyName.toString().replace(/_/g, '-')}-${rowId}`
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-23 19:51:25 +00:00
|
|
|
switch (propertySchema.default?.constructor) {
|
2024-05-13 00:14:22 +00:00
|
|
|
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':
|
2024-05-23 20:52:39 +00:00
|
|
|
formElement = this._renderInput(rowId,
|
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
|
|
|
propertyValue as string)
|
2024-05-13 00:14:22 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
case 'textarea':
|
2024-05-23 20:52:39 +00:00
|
|
|
formElement = this._renderTextarea(rowId,
|
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
|
|
|
propertyValue as string)
|
2024-05-13 00:14:22 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
case 'select':
|
2024-05-23 20:52:39 +00:00
|
|
|
formElement = this._renderSelect(rowId,
|
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
|
|
|
propertyValue as string)
|
2024-05-13 00:14:22 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
case Date:
|
|
|
|
switch (propertySchema.inputType) {
|
|
|
|
case undefined:
|
|
|
|
propertySchema.inputType = 'datetime'
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
case 'date':
|
|
|
|
case 'datetime':
|
|
|
|
case 'datetime-local':
|
|
|
|
case 'time':
|
2024-05-23 20:52:39 +00:00
|
|
|
formElement = this._renderInput(rowId,
|
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
|
|
|
(propertyValue as Date).toISOString())
|
2024-05-23 00:26:38 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
break
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
case Number:
|
|
|
|
switch (propertySchema.inputType) {
|
|
|
|
case undefined:
|
|
|
|
propertySchema.inputType = 'number'
|
|
|
|
|
|
|
|
case 'number':
|
|
|
|
case 'range':
|
2024-05-23 20:52:39 +00:00
|
|
|
formElement = this._renderInput(rowId,
|
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
|
|
|
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':
|
2024-05-23 20:52:39 +00:00
|
|
|
formElement = this._renderCheckbox(rowId,
|
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
|
|
|
propertyValue as boolean)
|
2024-05-13 00:14:22 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
case Array:
|
|
|
|
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':
|
2024-05-23 19:51:25 +00:00
|
|
|
if (propertyValue.constructor !== Array) {
|
|
|
|
propertyValue = (propertyValue) ? [propertyValue as (number | string)] : []
|
|
|
|
}
|
2024-05-23 00:26:38 +00:00
|
|
|
formElement = this._renderInput(rowId, inputId, inputName, propertyName, propertySchema,
|
2024-05-23 20:52:39 +00:00
|
|
|
(propertyValue)?.join(propertySchema.separator ?? ',') ?? propertyValue ?? propertySchema.default ?? '')
|
2024-05-23 00:26:38 +00:00
|
|
|
break
|
|
|
|
case 'textarea':
|
2024-05-23 19:51:25 +00:00
|
|
|
if (propertyValue.constructor !== Array) {
|
|
|
|
propertyValue = (propertyValue) ? [propertyValue as (number | string)] : []
|
|
|
|
}
|
2024-05-23 00:26:38 +00:00
|
|
|
formElement = this._renderTextarea(rowId, inputId, inputName, propertyName, propertySchema,
|
2024-05-23 20:52:39 +00:00
|
|
|
(propertyValue)?.join(propertySchema.separator ?? ',') ?? propertyValue ?? propertySchema.default ?? '')
|
2024-05-23 00:26:38 +00:00
|
|
|
break
|
|
|
|
}
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!formElement) {
|
2024-05-23 20:52:39 +00:00
|
|
|
console.warn(`value type '${(propertyValue.constructor.toString())}' is incompatible` +
|
|
|
|
`with field type '${propertySchema.inputType as string}' for form entry '${propertyName.toString()}'.`)
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return html`<td class="form-group">${formElement}</td>`
|
2024-05-23 00:26:38 +00:00
|
|
|
}
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
_renderInput = (rowId: number,
|
|
|
|
inputId: string,
|
|
|
|
inputName: string,
|
|
|
|
propertyName: string,
|
|
|
|
propertySchema: CellDataSchema,
|
|
|
|
propertyValue: string): TemplateResult => {
|
2024-05-23 00:26:38 +00:00
|
|
|
return html`<input
|
|
|
|
type=${propertySchema.inputType}
|
|
|
|
name=${inputName}
|
|
|
|
class="form-control"
|
|
|
|
id=${inputId}
|
2024-05-23 19:51:25 +00:00
|
|
|
list=${(propertySchema.datalist) ? inputId + '-datalist' : nothing}
|
|
|
|
min=${ifDefined(propertySchema.min)}
|
|
|
|
max=${ifDefined(propertySchema.max)}
|
|
|
|
minlength=${ifDefined(propertySchema.minlength)}
|
|
|
|
maxlength=${ifDefined(propertySchema.maxlength)}
|
|
|
|
@change=${(event: Event) => this._updatePropertyFromValue(event, propertyName, propertySchema, rowId)}
|
2024-05-23 00:26:38 +00:00
|
|
|
.value=${propertyValue}
|
|
|
|
/>
|
2024-05-23 20:52:39 +00:00
|
|
|
${(propertySchema.datalist)
|
|
|
|
? html`<datalist id=${inputId + '-datalist'}>
|
2024-05-24 12:08:48 +00:00
|
|
|
${(propertySchema.datalist ?? []).map((value) => html`<option value=${value}>`)}
|
2024-05-23 20:52:39 +00:00
|
|
|
</datalist>`
|
|
|
|
: nothing}
|
2024-05-23 00:26:38 +00:00
|
|
|
`
|
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
_renderTextarea = (rowId: number,
|
|
|
|
inputId: string,
|
|
|
|
inputName: string,
|
|
|
|
propertyName: string,
|
|
|
|
propertySchema: CellDataSchema,
|
|
|
|
propertyValue: string): TemplateResult => {
|
2024-05-23 00:26:38 +00:00
|
|
|
return html`<textarea
|
|
|
|
name=${inputName}
|
|
|
|
class="form-control"
|
|
|
|
id=${inputId}
|
2024-05-23 19:51:25 +00:00
|
|
|
min=${ifDefined(propertySchema.min)}
|
|
|
|
max=${ifDefined(propertySchema.max)}
|
|
|
|
minlength=${ifDefined(propertySchema.minlength)}
|
|
|
|
maxlength=${ifDefined(propertySchema.maxlength)}
|
|
|
|
@change=${(event: Event) => this._updatePropertyFromValue(event, propertyName, propertySchema, rowId)}
|
2024-05-23 00:26:38 +00:00
|
|
|
.value=${propertyValue}
|
|
|
|
></textarea>`
|
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
_renderCheckbox = (rowId: number,
|
|
|
|
inputId: string,
|
|
|
|
inputName: string,
|
|
|
|
propertyName: string,
|
|
|
|
propertySchema: CellDataSchema,
|
|
|
|
propertyValue: boolean): TemplateResult => {
|
2024-05-23 00:26:38 +00:00
|
|
|
return html`<input
|
|
|
|
type="checkbox"
|
|
|
|
name=${inputName}
|
|
|
|
class="form-check-input"
|
|
|
|
id=${inputId}
|
2024-05-23 19:51:25 +00:00
|
|
|
@change=${(event: Event) => this._updatePropertyFromValue(event, propertyName, propertySchema, rowId)}
|
2024-05-23 00:26:38 +00:00
|
|
|
.value=${propertyValue}
|
|
|
|
?checked=${propertyValue}
|
|
|
|
/>`
|
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
_renderSelect = (rowId: number,
|
|
|
|
inputId: string,
|
|
|
|
inputName: string,
|
|
|
|
propertyName: string,
|
|
|
|
propertySchema: CellDataSchema,
|
|
|
|
propertyValue: string): TemplateResult => {
|
2024-05-23 00:26:38 +00:00
|
|
|
return html`<select
|
|
|
|
class="form-select"
|
|
|
|
aria-label="Default select example"
|
2024-05-23 19:51:25 +00:00
|
|
|
@change=${(event: Event) => this._updatePropertyFromValue(event, propertyName, propertySchema, rowId)}
|
2024-05-23 00:26:38 +00:00
|
|
|
>
|
2024-05-23 19:51:25 +00:00
|
|
|
<option ?selected=${!propertyValue}>${propertySchema.label ?? 'Choose your option'}</option>
|
|
|
|
${Object.entries(propertySchema.options ?? {})
|
2024-05-23 00:26:38 +00:00
|
|
|
?.map(([value, name]) =>
|
|
|
|
html`<option ?selected=${propertyValue === value} value=${value}>${name}</option>`
|
|
|
|
)}
|
|
|
|
</select>`
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
_updatePropertyFromValue = (event: Event,
|
|
|
|
propertyName: string,
|
|
|
|
propertySchema: CellDataSchema,
|
2024-05-23 20:55:58 +00:00
|
|
|
rowId: number): void => {
|
2024-05-23 20:52:39 +00:00
|
|
|
const target = event.target as (HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement)
|
|
|
|
const value = (target)
|
|
|
|
? (target instanceof HTMLInputElement && target.type === 'checkbox')
|
|
|
|
? !!(target.checked)
|
|
|
|
: target.value
|
|
|
|
: undefined
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
if (value !== undefined) {
|
2024-05-23 20:52:39 +00:00
|
|
|
for (const rowById of this._rowsById) {
|
2024-05-23 00:26:38 +00:00
|
|
|
if (rowById._id === rowId) {
|
2024-05-23 19:51:25 +00:00
|
|
|
switch (propertySchema.default?.constructor) {
|
2024-05-23 00:26:38 +00:00
|
|
|
case Array:
|
|
|
|
rowById.row[propertyName] = (value as string).split(propertySchema.separator ?? ',')
|
2024-05-23 19:51:25 +00:00
|
|
|
break
|
2024-05-23 00:26:38 +00:00
|
|
|
default:
|
|
|
|
rowById.row[propertyName] = value
|
2024-05-23 19:51:25 +00:00
|
|
|
break
|
2024-05-23 00:26:38 +00:00
|
|
|
}
|
2024-05-13 14:40:36 +00:00
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
this.rows = this._rowsById.map(rowById => rowById.row)
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
this.requestUpdate('rows')
|
2024-05-23 19:51:25 +00:00
|
|
|
this.requestUpdate('_rowsById')
|
2024-05-23 00:26:38 +00:00
|
|
|
this.dispatchEvent(new CustomEvent('update', { detail: this.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}'`)
|
2024-05-23 20:52:39 +00:00
|
|
|
} else {
|
|
|
|
console.warn('Could not update property : Target or value was undefined')
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|