2024-05-23 15:17:28 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 Mehdi Benadel <https://mehdibenadel.com>
|
2024-06-05 13:56:03 +00:00
|
|
|
// SPDX-FileCopyrightText: 2024 John Livingston <https://www.john-livingston.fr/>
|
2024-05-23 15:17:28 +00:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2024-05-26 03:06:28 +00:00
|
|
|
import type { TagsInputElement } from './tags-input'
|
2024-06-05 13:56:03 +00:00
|
|
|
import type { DirectiveResult } from 'lit/directive'
|
2024-05-26 03:06:28 +00:00
|
|
|
import { ValidationErrorType } from '../models/validation'
|
2024-06-06 09:36:07 +00:00
|
|
|
import { maxSize, inputFileAccept } from 'shared/lib/emojis'
|
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-26 03:06:28 +00:00
|
|
|
import { classMap } from 'lit/directives/class-map.js'
|
2024-05-23 16:38:07 +00:00
|
|
|
import { LivechatElement } from './livechat'
|
2024-05-26 03:06:28 +00:00
|
|
|
import { ptTr } from '../directives/translation'
|
2024-06-16 17:48:02 +00:00
|
|
|
import { AddSVG, RemoveSVG } from '../buttons'
|
2024-05-23 00:26:38 +00:00
|
|
|
|
|
|
|
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-26 03:06:28 +00:00
|
|
|
| 'tags'
|
2024-06-05 13:56:03 +00:00
|
|
|
| 'image-file'
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
interface CellDataSchema {
|
|
|
|
min?: number
|
|
|
|
max?: number
|
|
|
|
minlength?: number
|
|
|
|
maxlength?: number
|
|
|
|
size?: number
|
2024-05-26 03:06:28 +00:00
|
|
|
label?: TemplateResult | string
|
2024-05-13 00:14:22 +00:00
|
|
|
options?: { [key: string]: string }
|
2024-05-23 00:26:38 +00:00
|
|
|
datalist?: DynamicTableAcceptedTypes[]
|
2024-06-13 08:48:13 +00:00
|
|
|
separator?: string
|
2024-05-13 00:14:22 +00:00
|
|
|
inputType?: DynamicTableAcceptedInputTypes
|
|
|
|
default?: DynamicTableAcceptedTypes
|
2024-06-07 12:08:35 +00:00
|
|
|
colClassList?: string[] // CSS classes to add to the <td> element.
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-26 03:06:28 +00:00
|
|
|
interface DynamicTableRowData {
|
|
|
|
_id: number
|
|
|
|
_originalIndex: number
|
|
|
|
row: { [key: string]: DynamicTableAcceptedTypes }
|
|
|
|
}
|
|
|
|
|
2024-06-07 10:02:50 +00:00
|
|
|
interface DynamicFormHeaderCellData {
|
|
|
|
colName: TemplateResult | DirectiveResult
|
|
|
|
description: TemplateResult | DirectiveResult
|
|
|
|
headerClassList?: string[]
|
|
|
|
}
|
|
|
|
|
2024-06-05 13:56:03 +00:00
|
|
|
export interface DynamicFormHeader {
|
2024-06-07 10:02:50 +00:00
|
|
|
[key: string]: DynamicFormHeaderCellData
|
2024-06-05 13:56:03 +00:00
|
|
|
}
|
|
|
|
export interface DynamicFormSchema { [key: string]: CellDataSchema }
|
|
|
|
|
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-06-05 13:56:03 +00:00
|
|
|
public header: DynamicFormHeader = {}
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
@property({ attribute: false })
|
2024-06-05 13:56:03 +00:00
|
|
|
public schema: DynamicFormSchema = {}
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-06-06 09:36:07 +00:00
|
|
|
@property({ attribute: false })
|
|
|
|
public maxLines?: number = undefined
|
|
|
|
|
2024-05-26 03:06:28 +00:00
|
|
|
@property()
|
|
|
|
public validation?: {[key: string]: ValidationErrorType[] }
|
|
|
|
|
|
|
|
@property({ attribute: false })
|
|
|
|
public validationPrefix: string = ''
|
|
|
|
|
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-26 03:06:28 +00:00
|
|
|
public _rowsById: DynamicTableRowData[] = []
|
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-06-12 13:43:09 +00:00
|
|
|
private async _addRow (): Promise<void> {
|
2024-05-23 20:52:39 +00:00
|
|
|
const newRow = this._getDefaultRow()
|
2024-05-26 03:06:28 +00:00
|
|
|
// Create row and assign id and original index
|
|
|
|
this._rowsById.push({ _id: this._lastRowId++, _originalIndex: this.rows.length, 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-06-12 13:43:09 +00:00
|
|
|
|
|
|
|
// Once the update is completed, we give focus to the first input field of the new row.
|
|
|
|
await this.updateComplete
|
|
|
|
// Note: we make multiple querySelector, to be sure to not get a nested table.
|
|
|
|
// We want the top level table associated tr.
|
|
|
|
const input = this.querySelector('table')?.querySelector(
|
2024-06-13 08:48:13 +00:00
|
|
|
'&>tbody>tr:last-child>td input:not([type=hidden]),' +
|
|
|
|
'&>tbody>tr:last-child>td livechat-tags-input,' +
|
|
|
|
'&>tbody>tr:last-child>td textarea'
|
2024-06-12 13:43:09 +00:00
|
|
|
)
|
|
|
|
if (input) {
|
|
|
|
(input as HTMLElement).focus()
|
|
|
|
}
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 10:48:40 +00:00
|
|
|
private async _removeRow (rowId: number): Promise<void> {
|
2024-06-12 14:26:35 +00:00
|
|
|
const confirmMsg = await this.ptTranslate(LOC_ACTION_REMOVE_ENTRY_CONFIRM)
|
2024-06-11 10:48:40 +00:00
|
|
|
await new Promise<void>((resolve, reject) => {
|
2024-06-12 14:26:35 +00:00
|
|
|
this.ptOptions.peertubeHelpers.showModal({
|
2024-06-11 10:48:40 +00:00
|
|
|
title: confirmMsg,
|
|
|
|
content: '',
|
|
|
|
close: true,
|
|
|
|
cancel: {
|
|
|
|
value: 'cancel',
|
|
|
|
action: reject
|
|
|
|
},
|
|
|
|
confirm: {
|
|
|
|
value: 'confirm',
|
|
|
|
action: resolve
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
2024-05-23 20:52:39 +00:00
|
|
|
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()
|
|
|
|
|
2024-05-26 03:06:28 +00:00
|
|
|
// Filter removed rows
|
2024-06-13 10:31:23 +00:00
|
|
|
// FIXME: is this really necessary?
|
|
|
|
this._rowsById = this._rowsById.filter(rowById => this.rows.includes(rowById.row))
|
2024-05-23 00:26:38 +00:00
|
|
|
|
2024-05-26 03:06:28 +00:00
|
|
|
for (let i = 0; i < this.rows.length; i++) {
|
2024-06-13 10:31:23 +00:00
|
|
|
if (!this._rowsById.find(rowById => rowById.row === this.rows[i])) {
|
2024-05-26 03:06:28 +00:00
|
|
|
// Add row and assign id
|
|
|
|
this._rowsById.push({ _id: this._lastRowId++, _originalIndex: i, row: this.rows[i] })
|
|
|
|
} else {
|
|
|
|
// Update index in case it changed
|
|
|
|
this._rowsById.filter(rowById => rowById.row === this.rows[i])
|
|
|
|
.forEach((value) => { value._originalIndex = i })
|
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-06-11 15:49:18 +00:00
|
|
|
const columns = Object.entries(this.header)
|
|
|
|
.sort(([k1, _1], [k2, _2]) => this.columnOrder.indexOf(k1) - this.columnOrder.indexOf(k2))
|
2024-05-13 14:40:36 +00:00
|
|
|
return html`<thead>
|
|
|
|
<tr>
|
2024-06-11 15:49:18 +00:00
|
|
|
${columns.map(([_, v]) => this._renderHeaderCell(v))}
|
|
|
|
<th scope="col"></th>
|
|
|
|
</tr>
|
|
|
|
<tr>
|
|
|
|
${columns.map(([_, v]) => this._renderHeaderDescriptionCell(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-06-07 10:02:50 +00:00
|
|
|
private readonly _renderHeaderCell = (headerCellData: DynamicFormHeaderCellData): TemplateResult => {
|
2024-06-11 15:49:18 +00:00
|
|
|
return html`<th scope="col" class=${headerCellData.headerClassList?.join(' ') ?? ''}>
|
2024-06-07 10:02:50 +00:00
|
|
|
<div
|
|
|
|
data-toggle="tooltip"
|
|
|
|
data-placement="bottom"
|
|
|
|
data-html="true"
|
|
|
|
>
|
2024-05-23 20:52:39 +00:00
|
|
|
${headerCellData.colName}
|
|
|
|
</div>
|
2024-05-13 14:40:36 +00:00
|
|
|
</th>`
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 15:49:18 +00:00
|
|
|
private _renderHeaderDescriptionCell (headerCellData: DynamicFormHeaderCellData): TemplateResult {
|
|
|
|
const classList = ['livechat-dynamic-table-form-description-header']
|
|
|
|
if (headerCellData.headerClassList) {
|
|
|
|
classList.push(...headerCellData.headerClassList)
|
|
|
|
}
|
|
|
|
return html`<th scope="col" class=${classList.join(' ')}>
|
|
|
|
${headerCellData.description}
|
|
|
|
</th>`
|
|
|
|
}
|
|
|
|
|
2024-05-26 03:06:28 +00:00
|
|
|
private readonly _renderDataRow = (rowData: DynamicTableRowData): 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))
|
2024-05-26 03:06:28 +00:00
|
|
|
.map(key => this.renderDataCell(key,
|
|
|
|
rowData.row[key] ?? this.schema[key].default,
|
|
|
|
rowData._id,
|
|
|
|
rowData._originalIndex))}
|
2024-05-23 20:52:39 +00:00
|
|
|
<td class="form-group">
|
|
|
|
<button type="button"
|
2024-06-13 15:00:08 +00:00
|
|
|
class="dynamic-table-remove-row"
|
2024-06-12 17:13:35 +00:00
|
|
|
.title=${ptTr(LOC_ACTION_REMOVE_ENTRY) as any}
|
2024-06-12 08:27:28 +00:00
|
|
|
@click=${async () => this._removeRow(rowData._id)}
|
2024-06-11 10:32:36 +00:00
|
|
|
>
|
2024-05-23 20:52:39 +00:00
|
|
|
${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-06-06 09:36:07 +00:00
|
|
|
if (this.maxLines && this._rowsById.length >= this.maxLines) {
|
|
|
|
return html``
|
|
|
|
}
|
2024-06-13 13:21:31 +00:00
|
|
|
// Note: the addRow button is in first column, so it won't be hidden if screen not wide enough.
|
2024-05-23 00:26:38 +00:00
|
|
|
return html`<tfoot>
|
|
|
|
<tr>
|
2024-06-13 13:21:31 +00:00
|
|
|
<td class="dynamic-table-add-row-cell">
|
2024-05-23 20:52:39 +00:00
|
|
|
<button type="button"
|
2024-06-13 15:00:08 +00:00
|
|
|
class="dynamic-table-add-row"
|
2024-06-12 17:13:35 +00:00
|
|
|
.title=${ptTr(LOC_ACTION_ADD_ENTRY) as any}
|
2024-06-11 10:32:36 +00:00
|
|
|
@click=${this._addRow}
|
|
|
|
>
|
2024-05-23 20:52:39 +00:00
|
|
|
${unsafeHTML(AddSVG)}
|
|
|
|
</button>
|
|
|
|
</td>
|
2024-06-13 13:21:31 +00:00
|
|
|
${Object.values(this.header).map(() => html`<td></td>`)}
|
2024-05-23 00:26:38 +00:00
|
|
|
</tr>
|
|
|
|
</tfoot>`
|
|
|
|
}
|
|
|
|
|
2024-05-26 03:06:28 +00:00
|
|
|
renderDataCell = (propertyName: string,
|
|
|
|
propertyValue: DynamicTableAcceptedTypes,
|
|
|
|
rowId: number,
|
|
|
|
originalIndex: number): TemplateResult => {
|
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-26 03:06:28 +00:00
|
|
|
const feedback = this._renderFeedback(inputId, propertyName, originalIndex)
|
|
|
|
|
2024-05-23 19:51:25 +00:00
|
|
|
switch (propertySchema.default?.constructor) {
|
2024-05-13 00:14:22 +00:00
|
|
|
case String:
|
2024-06-05 13:56:03 +00:00
|
|
|
propertySchema.inputType ??= 'text'
|
2024-05-13 00:14:22 +00:00
|
|
|
switch (propertySchema.inputType) {
|
|
|
|
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-26 03:06:28 +00:00
|
|
|
formElement = html`${this._renderInput(rowId,
|
2024-05-23 20:52:39 +00:00
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
2024-05-26 03:06:28 +00:00
|
|
|
propertyValue as string,
|
|
|
|
originalIndex)}
|
|
|
|
${feedback}
|
|
|
|
`
|
2024-05-13 00:14:22 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
case 'textarea':
|
2024-05-26 03:06:28 +00:00
|
|
|
formElement = html`${this._renderTextarea(rowId,
|
2024-05-23 20:52:39 +00:00
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
2024-05-26 03:06:28 +00:00
|
|
|
propertyValue as string,
|
|
|
|
originalIndex)}
|
|
|
|
${feedback}
|
|
|
|
`
|
2024-05-13 00:14:22 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
case 'select':
|
2024-05-26 03:06:28 +00:00
|
|
|
formElement = html`${this._renderSelect(rowId,
|
2024-05-23 20:52:39 +00:00
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
2024-05-26 03:06:28 +00:00
|
|
|
propertyValue as string,
|
|
|
|
originalIndex)}
|
|
|
|
${feedback}
|
|
|
|
`
|
2024-05-13 00:14:22 +00:00
|
|
|
break
|
2024-06-05 13:56:03 +00:00
|
|
|
|
|
|
|
case 'image-file':
|
|
|
|
formElement = html`${this._renderImageFileInput(rowId,
|
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
|
|
|
propertyValue?.toString(),
|
|
|
|
originalIndex)}
|
|
|
|
${feedback}
|
|
|
|
`
|
|
|
|
break
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
break
|
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
case Date:
|
2024-06-05 13:56:03 +00:00
|
|
|
propertySchema.inputType ??= 'datetime'
|
2024-05-23 00:26:38 +00:00
|
|
|
switch (propertySchema.inputType) {
|
|
|
|
case 'date':
|
|
|
|
case 'datetime':
|
|
|
|
case 'datetime-local':
|
|
|
|
case 'time':
|
2024-05-26 03:06:28 +00:00
|
|
|
formElement = html`${this._renderInput(rowId,
|
2024-05-23 20:52:39 +00:00
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
2024-05-26 03:06:28 +00:00
|
|
|
(propertyValue as Date).toISOString(),
|
|
|
|
originalIndex)}
|
|
|
|
${feedback}
|
|
|
|
`
|
2024-05-23 00:26:38 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
break
|
2024-05-13 00:14:22 +00:00
|
|
|
|
|
|
|
case Number:
|
2024-06-05 13:56:03 +00:00
|
|
|
propertySchema.inputType ??= 'number'
|
2024-05-13 00:14:22 +00:00
|
|
|
switch (propertySchema.inputType) {
|
|
|
|
case 'number':
|
|
|
|
case 'range':
|
2024-05-26 03:06:28 +00:00
|
|
|
formElement = html`${this._renderInput(rowId,
|
2024-05-23 20:52:39 +00:00
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
2024-05-26 03:06:28 +00:00
|
|
|
propertyValue as string,
|
|
|
|
originalIndex)}
|
|
|
|
${feedback}
|
|
|
|
`
|
2024-05-13 00:14:22 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
|
|
|
case Boolean:
|
2024-06-05 13:56:03 +00:00
|
|
|
propertySchema.inputType ??= 'checkbox'
|
2024-05-13 00:14:22 +00:00
|
|
|
switch (propertySchema.inputType) {
|
|
|
|
case 'checkbox':
|
2024-05-26 03:06:28 +00:00
|
|
|
formElement = html`${this._renderCheckbox(rowId,
|
2024-05-23 20:52:39 +00:00
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
2024-05-26 03:06:28 +00:00
|
|
|
propertyValue as boolean,
|
|
|
|
originalIndex)}
|
|
|
|
${feedback}
|
|
|
|
`
|
2024-05-13 00:14:22 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
break
|
|
|
|
|
2024-05-23 00:26:38 +00:00
|
|
|
case Array:
|
2024-06-05 13:56:03 +00:00
|
|
|
propertySchema.inputType ??= 'text'
|
2024-05-23 00:26:38 +00:00
|
|
|
switch (propertySchema.inputType) {
|
|
|
|
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-26 03:06:28 +00:00
|
|
|
formElement = html`${this._renderInput(rowId,
|
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
2024-06-13 08:48:13 +00:00
|
|
|
(propertyValue)?.join(propertySchema.separator ?? ',') ??
|
2024-05-26 03:06:28 +00:00
|
|
|
propertyValue ?? propertySchema.default ?? '',
|
|
|
|
originalIndex)}
|
|
|
|
${feedback}
|
|
|
|
`
|
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-26 03:06:28 +00:00
|
|
|
formElement = html`${this._renderTextarea(rowId,
|
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
2024-06-13 08:48:13 +00:00
|
|
|
(propertyValue)?.join(propertySchema.separator ?? ',') ??
|
2024-05-26 03:06:28 +00:00
|
|
|
propertyValue ?? propertySchema.default ?? '',
|
|
|
|
originalIndex)}
|
|
|
|
${feedback}
|
|
|
|
`
|
|
|
|
break
|
|
|
|
case 'tags':
|
|
|
|
if (propertyValue.constructor !== Array) {
|
|
|
|
propertyValue = (propertyValue) ? [propertyValue as (number | string)] : []
|
|
|
|
}
|
|
|
|
formElement = html`${this._renderTagsInput(rowId,
|
|
|
|
inputId,
|
|
|
|
inputName,
|
|
|
|
propertyName,
|
|
|
|
propertySchema,
|
|
|
|
propertyValue,
|
|
|
|
originalIndex)}
|
|
|
|
${feedback}
|
|
|
|
`
|
2024-05-23 00:26:38 +00:00
|
|
|
break
|
|
|
|
}
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!formElement) {
|
2024-06-14 13:17:14 +00:00
|
|
|
this.logger.warn(`value type '${(propertyValue.constructor.toString())}' is incompatible` +
|
2024-05-23 20:52:39 +00:00
|
|
|
`with field type '${propertySchema.inputType as string}' for form entry '${propertyName.toString()}'.`)
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
2024-06-07 12:08:35 +00:00
|
|
|
const classList = ['form-group']
|
|
|
|
if (propertySchema.colClassList) {
|
|
|
|
classList.push(...propertySchema.colClassList)
|
|
|
|
}
|
|
|
|
return html`<td class=${classList.join(' ')}>${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,
|
2024-05-26 03:06:28 +00:00
|
|
|
propertyValue: string,
|
|
|
|
originalIndex: number): TemplateResult => {
|
2024-05-23 00:26:38 +00:00
|
|
|
return html`<input
|
2024-06-12 17:13:35 +00:00
|
|
|
type=${propertySchema.inputType as any}
|
2024-05-23 00:26:38 +00:00
|
|
|
name=${inputName}
|
2024-06-12 15:08:48 +00:00
|
|
|
class=${classMap(
|
|
|
|
Object.assign(
|
|
|
|
{ 'form-control': true },
|
|
|
|
this._getInputValidationClass(propertyName, originalIndex)
|
|
|
|
)
|
|
|
|
)}
|
2024-05-23 00:26:38 +00:00
|
|
|
id=${inputId}
|
2024-05-26 03:06:28 +00:00
|
|
|
aria-describedby="${inputId}-feedback"
|
2024-06-12 17:13:35 +00:00
|
|
|
list=${ifDefined(propertySchema.datalist ? inputId + '-datalist' : undefined)}
|
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}
|
|
|
|
/>
|
2024-05-23 20:52:39 +00:00
|
|
|
${(propertySchema.datalist)
|
|
|
|
? html`<datalist id=${inputId + '-datalist'}>
|
2024-06-12 17:13:35 +00:00
|
|
|
${(propertySchema.datalist ?? []).map((value) => html`<option value=${value.toString()}>`)}
|
2024-05-23 20:52:39 +00:00
|
|
|
</datalist>`
|
2024-05-26 03:06:28 +00:00
|
|
|
: nothing}`
|
|
|
|
}
|
|
|
|
|
|
|
|
_renderTagsInput = (rowId: number,
|
|
|
|
inputId: string,
|
|
|
|
inputName: string,
|
|
|
|
propertyName: string,
|
|
|
|
propertySchema: CellDataSchema,
|
|
|
|
propertyValue: Array<string | number>,
|
|
|
|
originalIndex: number): TemplateResult => {
|
|
|
|
return html`<livechat-tags-input
|
|
|
|
.name=${inputName}
|
2024-06-12 15:08:48 +00:00
|
|
|
class=${classMap(
|
|
|
|
Object.assign(
|
|
|
|
{ 'form-control': true },
|
|
|
|
this._getInputValidationClass(propertyName, originalIndex)
|
|
|
|
)
|
|
|
|
)}
|
2024-05-26 03:06:28 +00:00
|
|
|
id=${inputId}
|
2024-06-12 17:13:35 +00:00
|
|
|
.inputPlaceholder=${propertySchema.label as any}
|
2024-05-26 03:06:28 +00:00
|
|
|
aria-describedby="${inputId}-feedback"
|
2024-06-12 17:13:35 +00:00
|
|
|
.min=${propertySchema.min}
|
|
|
|
.max=${propertySchema.max}
|
|
|
|
.minlength=${propertySchema.minlength}
|
|
|
|
.maxlength=${propertySchema.maxlength}
|
|
|
|
.datalist=${propertySchema.datalist as any}
|
2024-06-13 14:42:23 +00:00
|
|
|
.separator=${propertySchema.separator ?? '\n'}
|
2024-05-26 03:06:28 +00:00
|
|
|
@change=${(event: Event) => this._updatePropertyFromValue(event, propertyName, propertySchema, rowId)}
|
2024-06-12 17:13:35 +00:00
|
|
|
.value=${propertyValue as any}
|
|
|
|
></livechat-tags-input>`
|
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,
|
2024-05-26 03:06:28 +00:00
|
|
|
propertyValue: string,
|
|
|
|
originalIndex: number): TemplateResult => {
|
2024-05-23 00:26:38 +00:00
|
|
|
return html`<textarea
|
|
|
|
name=${inputName}
|
2024-06-12 15:08:48 +00:00
|
|
|
class=${classMap(
|
|
|
|
Object.assign(
|
|
|
|
{ 'form-control': true },
|
|
|
|
this._getInputValidationClass(propertyName, originalIndex)
|
|
|
|
)
|
|
|
|
)}
|
2024-05-23 00:26:38 +00:00
|
|
|
id=${inputId}
|
2024-05-26 03:06:28 +00:00
|
|
|
aria-describedby="${inputId}-feedback"
|
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-26 03:06:28 +00:00
|
|
|
.value=${propertyValue}></textarea>`
|
2024-05-23 00:26:38 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
_renderCheckbox = (rowId: number,
|
|
|
|
inputId: string,
|
|
|
|
inputName: string,
|
|
|
|
propertyName: string,
|
|
|
|
propertySchema: CellDataSchema,
|
2024-05-26 03:06:28 +00:00
|
|
|
propertyValue: boolean,
|
|
|
|
originalIndex: number): TemplateResult => {
|
2024-05-23 00:26:38 +00:00
|
|
|
return html`<input
|
|
|
|
type="checkbox"
|
|
|
|
name=${inputName}
|
2024-06-12 15:08:48 +00:00
|
|
|
class=${classMap(
|
|
|
|
Object.assign(
|
|
|
|
{ 'form-check-input': true },
|
|
|
|
this._getInputValidationClass(propertyName, originalIndex)
|
|
|
|
)
|
|
|
|
)}
|
2024-05-23 00:26:38 +00:00
|
|
|
id=${inputId}
|
2024-05-26 03:06:28 +00:00
|
|
|
aria-describedby="${inputId}-feedback"
|
2024-05-23 19:51:25 +00:00
|
|
|
@change=${(event: Event) => this._updatePropertyFromValue(event, propertyName, propertySchema, rowId)}
|
2024-06-11 16:01:29 +00:00
|
|
|
value="1"
|
2024-05-26 03:06:28 +00:00
|
|
|
?checked=${propertyValue} />`
|
2024-05-23 00:26:38 +00:00
|
|
|
}
|
|
|
|
|
2024-05-23 20:52:39 +00:00
|
|
|
_renderSelect = (rowId: number,
|
|
|
|
inputId: string,
|
|
|
|
inputName: string,
|
|
|
|
propertyName: string,
|
|
|
|
propertySchema: CellDataSchema,
|
2024-05-26 03:06:28 +00:00
|
|
|
propertyValue: string,
|
|
|
|
originalIndex: number): TemplateResult => {
|
2024-05-23 00:26:38 +00:00
|
|
|
return html`<select
|
2024-06-12 15:08:48 +00:00
|
|
|
class=${classMap(
|
|
|
|
Object.assign(
|
|
|
|
{ 'form-select': true },
|
|
|
|
this._getInputValidationClass(propertyName, originalIndex)
|
|
|
|
)
|
|
|
|
)}
|
2024-05-26 03:06:28 +00:00
|
|
|
id=${inputId}
|
|
|
|
aria-describedby="${inputId}-feedback"
|
|
|
|
aria-label=${inputName}
|
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-06-05 13:56:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_renderImageFileInput = (rowId: number,
|
|
|
|
inputId: string,
|
|
|
|
inputName: string,
|
|
|
|
propertyName: string,
|
|
|
|
propertySchema: CellDataSchema,
|
|
|
|
propertyValue: string,
|
|
|
|
originalIndex: number
|
|
|
|
): TemplateResult => {
|
|
|
|
return html`<livechat-image-file-input
|
|
|
|
.name=${inputName}
|
2024-06-12 15:08:48 +00:00
|
|
|
class=${classMap(this._getInputValidationClass(propertyName, originalIndex))}
|
2024-06-05 13:56:03 +00:00
|
|
|
id=${inputId}
|
|
|
|
aria-describedby="${inputId}-feedback"
|
|
|
|
@change=${(event: Event) => this._updatePropertyFromValue(event, propertyName, propertySchema, rowId)}
|
2024-06-06 09:36:07 +00:00
|
|
|
.value=${propertyValue}
|
|
|
|
.maxSize=${maxSize}
|
|
|
|
.accept=${inputFileAccept}
|
|
|
|
></livechat-image-file-input>`
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
|
2024-05-26 03:06:28 +00:00
|
|
|
_getInputValidationClass = (propertyName: string,
|
|
|
|
originalIndex: number): { [key: string]: boolean } => {
|
|
|
|
const validationErrorTypes: ValidationErrorType[] | undefined =
|
|
|
|
this.validation?.[`${this.validationPrefix}.${originalIndex}.${propertyName}`]
|
|
|
|
|
|
|
|
return validationErrorTypes !== undefined
|
|
|
|
? (validationErrorTypes.length ? { 'is-invalid': true } : { 'is-valid': true })
|
|
|
|
: {}
|
|
|
|
}
|
|
|
|
|
|
|
|
_renderFeedback = (inputId: string,
|
|
|
|
propertyName: string,
|
|
|
|
originalIndex: number): TemplateResult | typeof nothing => {
|
|
|
|
const errorMessages: TemplateResult[] = []
|
|
|
|
const validationErrorTypes: ValidationErrorType[] | undefined =
|
|
|
|
this.validation?.[`${this.validationPrefix}.${originalIndex}.${propertyName}`]
|
|
|
|
|
|
|
|
if (validationErrorTypes !== undefined && validationErrorTypes.length !== 0) {
|
2024-06-06 09:36:07 +00:00
|
|
|
if (validationErrorTypes.includes(ValidationErrorType.Missing)) {
|
|
|
|
errorMessages.push(html`${ptTr(LOC_INVALID_VALUE_MISSING)}`)
|
|
|
|
}
|
2024-05-26 03:06:28 +00:00
|
|
|
if (validationErrorTypes.includes(ValidationErrorType.WrongType)) {
|
|
|
|
errorMessages.push(html`${ptTr(LOC_INVALID_VALUE_WRONG_TYPE)}`)
|
|
|
|
}
|
|
|
|
if (validationErrorTypes.includes(ValidationErrorType.WrongFormat)) {
|
|
|
|
errorMessages.push(html`${ptTr(LOC_INVALID_VALUE_WRONG_FORMAT)}`)
|
|
|
|
}
|
|
|
|
if (validationErrorTypes.includes(ValidationErrorType.NotInRange)) {
|
|
|
|
errorMessages.push(html`${ptTr(LOC_INVALID_VALUE_NOT_IN_RANGE)}`)
|
|
|
|
}
|
2024-06-06 13:03:12 +00:00
|
|
|
if (validationErrorTypes.includes(ValidationErrorType.Duplicate)) {
|
|
|
|
errorMessages.push(html`${ptTr(LOC_INVALID_VALUE_DUPLICATE)}`)
|
|
|
|
}
|
2024-05-26 03:06:28 +00:00
|
|
|
|
|
|
|
return html`<div id="${inputId}-feedback" class="invalid-feedback">${errorMessages}</div>`
|
|
|
|
} else {
|
|
|
|
return nothing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-26 03:06:28 +00:00
|
|
|
const target = event.target as (TagsInputElement | HTMLInputElement | HTMLTextAreaElement | HTMLSelectElement)
|
2024-05-23 20:52:39 +00:00
|
|
|
const value = (target)
|
|
|
|
? (target instanceof HTMLInputElement && target.type === 'checkbox')
|
|
|
|
? !!(target.checked)
|
|
|
|
: target.value
|
|
|
|
: undefined
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-06-13 10:31:23 +00:00
|
|
|
if (value === undefined) {
|
2024-06-14 13:17:14 +00:00
|
|
|
this.logger.warn('Could not update property : Target or value was undefined')
|
2024-06-13 10:31:23 +00:00
|
|
|
return
|
|
|
|
}
|
2024-05-13 00:14:22 +00:00
|
|
|
|
2024-06-13 10:31:23 +00:00
|
|
|
const rowById = this._rowsById.find(rowById => rowById._id === rowId)
|
|
|
|
if (!rowById) {
|
2024-06-14 13:17:14 +00:00
|
|
|
this.logger.warn(`Could not update property : Did not find a property named '${propertyName}' in row '${rowId}'`)
|
2024-06-13 10:31:23 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (propertySchema.default?.constructor) {
|
|
|
|
case Array:
|
|
|
|
if (value.constructor === Array || !propertySchema.separator) {
|
|
|
|
rowById.row[propertyName] = value
|
|
|
|
} else {
|
|
|
|
rowById.row[propertyName] = (value as string)
|
|
|
|
.split(propertySchema.separator)
|
|
|
|
}
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
rowById.row[propertyName] = value
|
|
|
|
break
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
2024-06-13 10:31:23 +00:00
|
|
|
|
|
|
|
this.rows = this._rowsById.map(rowById => rowById.row)
|
|
|
|
|
|
|
|
this.requestUpdate('rows')
|
|
|
|
this.requestUpdate('_rowsById')
|
|
|
|
this.dispatchEvent(new CustomEvent('update', { detail: this.rows }))
|
2024-05-13 00:14:22 +00:00
|
|
|
}
|
|
|
|
}
|