form-group-repeater.js 3.08 KB
import { IMG_URL } from '../../../utils/imageBase.js'
const { buildRowValuesFromFields } = require('../../utils/order-list-params.js')
const { resolveSubformMaxRows } = require('../../form-engine/subform-limits.js')
const {
	buildUpgradeIconUrl,
	FORM_NAV_ICON_LEFT,
	FORM_NAV_ICON_RIGHT
} = require('../../utils/upgrade-icon-url.js')

Component({
	options: {
		styleIsolation: 'apply-shared'
	},

	properties: {
		group: {
			type: Object,
			value: {}
		},
		visibleRowFields: {
			type: Array,
			value: []
		},
		rowTotal: {
			type: Number,
			value: 0
		},
		activeIndex: {
			type: Number,
			value: 0
		},
		disabled: {
			type: Boolean,
			value: false
		},
		errors: {
			type: Object,
			value: {}
		},
		requestContext: {
			type: Object,
			value: {}
		}
	},

	data: {
    IMG_URL,
		rowValues: {},
		canAdd: true,
		navIconLeft: '',
		navIconRight: ''
	},

	lifetimes: {
		attached() {
			this.setData({
				navIconLeft: buildUpgradeIconUrl(FORM_NAV_ICON_LEFT),
				navIconRight: buildUpgradeIconUrl(FORM_NAV_ICON_RIGHT)
			})
		}
	},

	observers: {
		visibleRowFields(visibleRowFields = []) {
			this.setData({
				rowValues: buildRowValuesFromFields(visibleRowFields)
			})
		},
		'group, rowTotal, disabled'() {
			const group = this.properties.group || {}
			const rowTotal = this.properties.rowTotal || 0
			const disabled = this.properties.disabled
			const maxRows = resolveSubformMaxRows(group)
			this.setData({
				canAdd: !disabled && rowTotal < maxRows
			})
		}
	},

	methods: {
		getActiveRowIndex() {
			const total = this.properties.rowTotal || 0
			let index = this.properties.activeIndex || 0
			if (total > 0 && index >= total) index = total - 1
			if (index < 0) index = 0
			return index
		},

		onAdd() {
			if (this.properties.disabled || !this.data.canAdd) return
			this.triggerEvent('add', { groupKey: this.properties.group?.key })
		},

		onRemove() {
			if (this.properties.disabled) return
			this.triggerEvent('remove', {
				groupKey: this.properties.group?.key,
				rowIndex: this.getActiveRowIndex()
			})
		},

		onNav(e) {
			const direction = Number(e.currentTarget.dataset.direction) || 0
			this.triggerEvent('nav', {
				groupKey: this.properties.group?.key,
				direction
			})
		},

		resolveRowIndexFromDetail(detail = {}) {
			const raw = detail.rowIndex
			if (raw !== undefined && raw !== null && raw !== '') {
				const index = Number(raw)
				if (!Number.isNaN(index) && index >= 0) {
					return index
				}
			}
			return this.getActiveRowIndex()
		},

		onFieldChange(e) {
			const childKey = e.detail?.key
			const value = e.detail?.value
			const displayLabel = e.detail?.displayLabel
			const metaValue = e.detail?.metaValue
			const orderRaw = e.detail?.orderRaw
			if (!childKey) return
			this.triggerEvent('change', {
				groupKey: this.properties.group?.key,
				rowIndex: this.resolveRowIndexFromDetail(e.detail),
				childKey,
				value,
				displayLabel,
				metaValue,
				orderRaw
			})
		},

		onFieldBlur(e) {
			this.triggerEvent('blur', {
				...e.detail,
				groupKey: this.properties.group?.key,
				rowIndex: this.resolveRowIndexFromDetail(e.detail)
			})
		}
	}
})