740 lines
31 KiB
Vue
740 lines
31 KiB
Vue
<template>
|
||
<div class="space-y-2.5 rounded-xl border border-gray-200 p-2.5 dark:border-dark-600" data-test="option-source">
|
||
<div class="flex items-center justify-between gap-2">
|
||
<p class="input-label">{{ t('form.optionSource') }}</p>
|
||
<span class="badge badge-primary flex-shrink-0">{{ kindLabel }}</span>
|
||
</div>
|
||
|
||
<!-- 先说清「候选从哪儿来」:这一格能点的东西是这儿定的 -->
|
||
<div>
|
||
<Select
|
||
data-test="option-source-kind"
|
||
:model-value="kind"
|
||
:options="kindOptions"
|
||
@update:model-value="pickKind(String($event) as OptionSourceKind)"
|
||
/>
|
||
<p class="input-hint">{{ t(OPTION_SOURCE_HINT_KEYS[kind]) }}</p>
|
||
<!-- 两格下拉组件的分工写在这儿:配歪的人不必猜「为什么这一格里挑不到数据源」 -->
|
||
<p v-if="positioningHint" class="input-hint text-gray-400 dark:text-dark-400">{{ positioningHint }}</p>
|
||
</div>
|
||
|
||
<!-- 手填选项:一条两格——给人看的那句与往数据里存的那个值 -->
|
||
<div v-if="kind === 'manual'">
|
||
<div class="mb-1 flex items-center justify-between">
|
||
<label class="input-label">{{ t('form.options') }}</label>
|
||
<button type="button" class="btn btn-ghost btn-sm" @click="addOption">
|
||
<Icon name="plus" class="mr-1 h-3.5 w-3.5" />
|
||
{{ t('common.add') }}
|
||
</button>
|
||
</div>
|
||
<div v-for="(option, index) in field.options ?? []" :key="index" class="mb-2">
|
||
<div class="flex items-center gap-1.5">
|
||
<input
|
||
:value="option.label"
|
||
class="input py-1.5 text-sm"
|
||
:placeholder="t('form.optionPlaceholder')"
|
||
:aria-label="t('form.optionPlaceholder')"
|
||
data-test="option-label"
|
||
@input="patchOptionLabel(index, String(($event.target as HTMLInputElement).value))"
|
||
/>
|
||
<button type="button" class="btn btn-ghost btn-icon" @click="removeOption(index)">
|
||
<Icon name="x" class="h-4 w-4" />
|
||
</button>
|
||
</div>
|
||
<input
|
||
:value="String(option.value ?? '')"
|
||
class="input mt-1 w-full py-1 text-xs"
|
||
:placeholder="t('form.optionValuePlaceholder')"
|
||
:aria-label="t('form.optionValueStored')"
|
||
data-test="option-value"
|
||
@input="patchOptionValue(index, String(($event.target as HTMLInputElement).value))"
|
||
/>
|
||
</div>
|
||
<p v-if="!(field.options ?? []).length" class="input-hint text-amber-600 dark:text-amber-400">
|
||
{{ t('form.optionsEmpty') }}
|
||
</p>
|
||
<p v-else class="input-hint">{{ t('form.optionValueHint') }}</p>
|
||
</div>
|
||
|
||
<!-- 数据字典:编码选一本,级联挑上级 -->
|
||
<template v-if="kind === 'dict'">
|
||
<div>
|
||
<label class="input-label">{{ t('form.dictSource') }}</label>
|
||
<Select
|
||
data-test="option-source-dict"
|
||
:model-value="field.dictCode ?? ''"
|
||
:options="dictOptions"
|
||
:placeholder="t('form.optionSourceDictNone')"
|
||
searchable
|
||
@update:model-value="patch({ dictCode: String($event) || undefined })"
|
||
/>
|
||
</div>
|
||
<OptionMapEditor
|
||
v-if="field.dictCode"
|
||
kind="dict"
|
||
:model-value="field.optionMap"
|
||
:picker="field.picker"
|
||
:style="resolvedStyle"
|
||
:label="field.label"
|
||
:extras="extraKeys"
|
||
:sample="sample"
|
||
@update:model-value="patch({ optionMap: $event })"
|
||
@update:picker="patch({ picker: $event })"
|
||
/>
|
||
<div>
|
||
<label class="input-label">{{ t('form.cascadeFrom') }}</label>
|
||
<Select
|
||
:model-value="field.cascadeFrom ?? ''"
|
||
:options="siblingOptions"
|
||
:placeholder="t('form.cascadeNone')"
|
||
@update:model-value="patch({ cascadeFrom: String($event) || undefined })"
|
||
/>
|
||
<p class="input-hint">{{ cascadeHint }}</p>
|
||
</div>
|
||
</template>
|
||
|
||
<!-- 流程单据:先按流程类型筛一遍再挑流程,同名流程多时不会选错 -->
|
||
<template v-if="kind === 'process'">
|
||
<ProcessPicker
|
||
:model-value="field.relationProcess ?? ''"
|
||
:label="t('form.relationLimit')"
|
||
:placeholder="t('form.relationLimitAll')"
|
||
@update:model-value="patch({ relationProcess: String($event) || undefined })"
|
||
/>
|
||
<!--
|
||
能挂哪几种状态的单据。这一格以前不问这一句,在办与已终止的单子跟办结归档的混在一列里,
|
||
于是「关联已审批流程」挑得到一张走到一半被终止的——默认只给归档通过的,要放宽在这儿放宽。
|
||
-->
|
||
<div class="mt-2">
|
||
<span class="input-label">{{ t('form.relationStatuses') }}</span>
|
||
<div class="mt-1 flex flex-wrap gap-x-3 gap-y-1">
|
||
<label
|
||
v-for="item in RELATION_STATUSES"
|
||
:key="item"
|
||
class="flex cursor-pointer items-center gap-1.5 text-xs text-gray-600 dark:text-dark-300"
|
||
:data-test="`relation-status-${item}`"
|
||
>
|
||
<input
|
||
type="checkbox"
|
||
class="h-3.5 w-3.5 flex-shrink-0 rounded"
|
||
:checked="relationStatuses.includes(item)"
|
||
@change="toggleRelationStatus(item, ($event.target as HTMLInputElement).checked)"
|
||
/>
|
||
{{ t(RELATION_STATUS_LABEL_KEYS[item]) }}
|
||
</label>
|
||
</div>
|
||
<p class="input-hint">{{ t('form.relationStatusesHint') }}</p>
|
||
</div>
|
||
<!--
|
||
候选按「这是谁发起的」收。这一档出现之前只有一条路:我发起 / 我办过 / 我被抄送的并集,
|
||
于是「关联流程」的下拉里一大半是别人发起、我只是顺手批过的单子(原话:
|
||
**「选择流程范围不应该是已办,而是只有我发起的流程」**)。默认收成只给自已发起的,
|
||
要那片大的就换回「与我相关」——与上面那三档各管一件事,不揉进同一排复选框。
|
||
-->
|
||
<div class="mt-2">
|
||
<span class="input-label">{{ t('form.relationScope') }}</span>
|
||
<Select
|
||
data-test="relation-scope"
|
||
:model-value="relationScope"
|
||
:options="relationScopeOptions"
|
||
@update:model-value="pickRelationScope(String($event))"
|
||
/>
|
||
<p class="input-hint">{{ t(RELATION_SCOPE_HINT_KEYS[relationScope]) }}</p>
|
||
</div>
|
||
<OptionMapEditor
|
||
kind="process"
|
||
:model-value="field.optionMap"
|
||
:picker="field.picker"
|
||
:style="resolvedStyle"
|
||
:label="field.label"
|
||
:extras="extraKeys"
|
||
:form-fields="formFields"
|
||
:sample="sample"
|
||
@update:model-value="patch({ optionMap: $event })"
|
||
@update:picker="patch({ picker: $event })"
|
||
/>
|
||
<!-- 关联带出:这一格挂上那几张单之后,把里面的值写进这一张单的主表字段 / 明细表的列。
|
||
只有主表字段摆这一栏——明细列里那一格挂的也是单据,但「一行生一批行」没人能预期。 -->
|
||
<LinkFillPanel :field="field" :siblings="siblings" :reveal="revealLinkFill" @update="patch($event)" />
|
||
</template>
|
||
|
||
<!-- 外部数据源:列表型源,候选按需搜,走后端代理;入参让它按这张单的上下文筛 -->
|
||
<template v-if="kind === 'dataSource'">
|
||
<SourceBindingEditor
|
||
:model-value="sourceEntry"
|
||
:sources="searchableSources"
|
||
:keys="siblingKeys"
|
||
:person-keys="siblingPersonKeys"
|
||
:self-key="field.key"
|
||
:self-arg-key="field.key"
|
||
:show-picks="false"
|
||
:show-pick="false"
|
||
:label="t('form.optionSourceSourceLabel')"
|
||
:placeholder="t('form.optionSourceSourceNone')"
|
||
:args-hint="t('form.optionSourceArgsHint')"
|
||
:missing-option="missingSourceOption"
|
||
data-test="option-source-datasource"
|
||
@update:model-value="pickSource($event)"
|
||
/>
|
||
<!-- 配完当场搜一页:上游通不通、显示名取得对不对,在这儿看一眼比保存后再试便宜 -->
|
||
<div v-if="field.dataSourceCode" class="flex items-center gap-1.5">
|
||
<button type="button" class="btn btn-secondary btn-sm" :disabled="probing" data-test="option-source-probe" @click="probe">
|
||
<Icon name="search" class="mr-1 h-3.5 w-3.5" />
|
||
{{ probing ? t('common.loading') : t('form.optionSourceProbe') }}
|
||
</button>
|
||
<span v-if="probeText" class="min-w-0 truncate text-[11px]" :class="probeOk ? 'text-gray-500 dark:text-dark-400' : 'text-amber-600 dark:text-amber-400'">
|
||
{{ probeText }}
|
||
</span>
|
||
</div>
|
||
<OptionMapEditor
|
||
v-if="field.dataSourceCode"
|
||
kind="dataSource"
|
||
:model-value="field.optionMap"
|
||
:picker="field.picker"
|
||
:style="resolvedStyle"
|
||
:label="field.label"
|
||
:extras="extraKeys"
|
||
:sample="sample"
|
||
@update:model-value="patch({ optionMap: $event })"
|
||
@update:picker="patch({ picker: $event })"
|
||
/>
|
||
</template>
|
||
|
||
<!--
|
||
同一格换一家候选:按这张单上的取值挑一条来源(报销类型 = 差旅问差标字典,
|
||
= 招待问 ERP 科目)。都不成立就用上面那一套,所以老表单一个字都不用改。
|
||
-->
|
||
<OptionRoutesEditor
|
||
:field="field"
|
||
:siblings="siblings"
|
||
:dict-choices="dictOptions"
|
||
:searchable-sources="searchableSources"
|
||
:fetch-sources="fetchSources"
|
||
@update="patch({ optionRoutes: $event.length ? $event : undefined })"
|
||
@reveal-auto-fill="emit('revealAutoFill')"
|
||
/>
|
||
|
||
<!--
|
||
再定「用什么姿势挑」:三种姿势都画成同一只输入框,区别只在点开之后
|
||
(一列选项 / 一个能搜的框 / 摊成一排)。
|
||
-->
|
||
<div class="border-t border-gray-100 pt-2 dark:border-dark-700">
|
||
<!--
|
||
「可以选好几个」只给这两格下拉组件:单选框 / 多选框本来就是点选的一排,
|
||
那一档自带多选;关联审批单一直是一串值,没有这个开关可言。
|
||
-->
|
||
<label v-if="canPickMultiple" class="mb-2 flex items-start gap-2">
|
||
<input
|
||
type="checkbox"
|
||
class="mt-0.5 h-4 w-4 flex-shrink-0 rounded border-gray-300 text-primary-600 dark:border-dark-600"
|
||
:checked="multiple"
|
||
data-test="option-source-multiple"
|
||
@change="pickMultiple(($event.target as HTMLInputElement).checked)"
|
||
/>
|
||
<span class="min-w-0">
|
||
<span class="block text-xs text-gray-700 dark:text-dark-200">{{ t('form.multiplePick') }}</span>
|
||
<span class="block input-hint">{{ multipleHint }}</span>
|
||
</span>
|
||
</label>
|
||
<label class="input-label">{{ t('form.optionSourceStyle') }}</label>
|
||
<Select
|
||
data-test="option-source-style"
|
||
:model-value="style"
|
||
:options="styleOptions"
|
||
@update:model-value="patch({ pickerStyle: String($event) === 'auto' ? undefined : (String($event) as PickerStyle) })"
|
||
/>
|
||
<p class="input-hint">{{ t(PICKER_STYLE_HINT_KEYS[resolvedStyle]) }}</p>
|
||
</div>
|
||
|
||
<!--
|
||
带回来的值撞不进候选清单那一事。下拉框的候选以前是硬边界:外部取数 / 关联单据带出 /
|
||
字典带出写进来的值一旦不在清单里,页面上会被清成空、提交时被判「取值已不在可选范围内」,
|
||
人看见的是「明明带回来了却是空的」。这一格的值既然归系统写,就别拿本地清单判对面给的值。
|
||
-->
|
||
<label class="flex items-start gap-2 border-t border-gray-100 pt-2 dark:border-dark-700">
|
||
<input
|
||
type="checkbox"
|
||
class="mt-0.5 h-4 w-4 flex-shrink-0 rounded border-gray-300 text-primary-600 dark:border-dark-600"
|
||
:checked="acceptsOutsideValue(field)"
|
||
data-test="option-accept-outside"
|
||
@change="patch({ allowOutsideValue: ($event.target as HTMLInputElement).checked || undefined })"
|
||
/>
|
||
<span class="min-w-0">
|
||
<span class="block text-xs text-gray-700 dark:text-dark-200">{{ t('form.optionAcceptOutside') }}</span>
|
||
<span class="block input-hint">{{ t('form.optionAcceptOutsideHint') }}</span>
|
||
</span>
|
||
</label>
|
||
|
||
<p v-for="problem in problems" :key="problem" class="input-hint text-amber-600 dark:text-amber-400">
|
||
{{ problem }}
|
||
</p>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
/**
|
||
* 「这一格的候选从哪儿来、看哪一列、存哪一列、用什么姿势挑」在设计器里的那一栏。
|
||
*
|
||
* 这几件事以前散在三个地方:选项编辑器在中间,选项来源(字典)藏在联动面板里,
|
||
* 关联审批单的流程范围又是一块单独的——配一格下拉要在三块面板之间来回找。这里合成一栏:
|
||
* 先挑来源,来源下面就地配它自己的东西(手填的选项 / 哪本字典 / 哪个流程 / 哪张数据源),
|
||
* 再定「显示哪一列、存哪一列」,最后定挑选方式。口径全在 utils/optionSource.ts 与
|
||
* utils/optionMap.ts,这一栏只负责把它摆成人能配的样子。
|
||
*
|
||
* 底下那行「例:显示「螺栓 M8」,保存 M1」用的是一条**真候选**:字典取这本的第一项,
|
||
* 数据源默默搜一页拿第一条,单据取一张真单据。取不到就只说列名,不编一条假的骗人。
|
||
*/
|
||
import { computed, onMounted, ref, watch } from 'vue'
|
||
import { useI18n } from 'vue-i18n'
|
||
import Icon from '@/components/icons/Icon.vue'
|
||
import Select from '@/components/common/Select.vue'
|
||
import OptionMapEditor from './OptionMapEditor.vue'
|
||
import ProcessPicker from './ProcessPicker.vue'
|
||
import LinkFillPanel from './LinkFillPanel.vue'
|
||
import SourceBindingEditor from './SourceBindingEditor.vue'
|
||
import OptionRoutesEditor from './OptionRoutesEditor.vue'
|
||
import { workAPI } from '@/api'
|
||
import { listDataSourcesCached, processFields, searchOptions } from '@/api/dataSources'
|
||
import { OPTION_ARGS_KEY, resolveBindingArgs } from '@/utils/binding'
|
||
import { dictValuesCached, listDictsCached } from '@/api/dicts'
|
||
import { extraKeysOf } from '@/utils/optionMap'
|
||
import { displayFormPathsOf } from '@/utils/optionMap'
|
||
import {
|
||
OPTION_SOURCE_HINT_KEYS,
|
||
OPTION_SOURCE_LABEL_KEYS,
|
||
PICKER_STYLE_HINT_KEYS,
|
||
PICKER_STYLE_LABEL_KEYS,
|
||
RELATION_STATUSES,
|
||
RELATION_STATUS_DEFAULT,
|
||
RELATION_STATUS_LABEL_KEYS,
|
||
RELATION_SCOPES,
|
||
RELATION_SCOPE_DEFAULT,
|
||
RELATION_SCOPE_HINT_KEYS,
|
||
RELATION_SCOPE_LABEL_KEYS,
|
||
acceptsOutsideValue,
|
||
holdsListValue,
|
||
optionRoutesOf,
|
||
optionSourcePatch,
|
||
optionSourceProblems,
|
||
pickerStyleOf,
|
||
relationScopeOf,
|
||
relationStatusesOf,
|
||
sourceKindChoicesOf,
|
||
sourceKindOf,
|
||
styleChoicesOf,
|
||
withMissingOption
|
||
} from '@/utils/optionSource'
|
||
import type { RelationScope, RelationStatus } from '@/utils/optionSource'
|
||
import type {
|
||
DataSource,
|
||
DictItem,
|
||
DictType,
|
||
FetchSourceEntry,
|
||
FormFieldDef,
|
||
OptionSourceKind,
|
||
PickerStyle,
|
||
ProcessFieldOption
|
||
} from '@/types'
|
||
|
||
const props = withDefaults(
|
||
defineProps<{
|
||
field: FormFieldDef
|
||
/** 同一张表单里的其它字段:级联上级从这里面挑 */
|
||
siblings?: FormFieldDef[]
|
||
/** 从明细表那一侧或总览弹窗跳过来时递增:带出那一栏滚到眼前并亮一下 */
|
||
revealLinkFill?: number
|
||
}>(),
|
||
{ siblings: () => [], revealLinkFill: 0 }
|
||
)
|
||
|
||
const emit = defineEmits<{
|
||
(e: 'patch', values: Partial<FormFieldDef>): void
|
||
/** 候选路由里点了「字典那一家的带出在哪儿配」:外层把「联动」那一栏打开并闪一下那一块 */
|
||
(e: 'revealAutoFill'): void
|
||
}>()
|
||
|
||
const { t } = useI18n()
|
||
|
||
const dictRows = ref<DictType[]>([])
|
||
const dataSources = ref<DataSource[]>([])
|
||
/** 数据源清单到手了吗:没到手之前不判「这张源还在不在」。 */
|
||
const loaded = ref(false)
|
||
|
||
onMounted(async () => {
|
||
dictRows.value = await listDictsCached()
|
||
dataSources.value = await listDataSourcesCached()
|
||
loaded.value = true
|
||
})
|
||
|
||
const kind = computed<OptionSourceKind>(() => sourceKindOf(props.field))
|
||
const resolvedStyle = computed(() => pickerStyleOf(props.field, kind.value))
|
||
const style = computed<PickerStyle>(() => props.field.pickerStyle ?? 'auto')
|
||
|
||
const kindLabel = computed(() => t(OPTION_SOURCE_LABEL_KEYS[kind.value]))
|
||
|
||
const kindOptions = computed(() =>
|
||
sourceKindChoicesOf(props.field).map((item) => ({ value: item, label: t(OPTION_SOURCE_LABEL_KEYS[item]) }))
|
||
)
|
||
|
||
const styleOptions = computed(() =>
|
||
styleChoicesOf(props.field.type, kind.value, multiple.value).map((item) => ({
|
||
value: item,
|
||
label: item === 'auto' ? t('form.optionSourceStyleAuto') : t(PICKER_STYLE_LABEL_KEYS[item])
|
||
}))
|
||
)
|
||
|
||
/** 只有这两格下拉组件能勾「选好几个」;其余类型的值是几个的,类型本身就说了。 */
|
||
const canPickMultiple = computed(() => props.field.type === 'select' || props.field.type === 'picker')
|
||
const multiple = computed(() => holdsListValue(props.field))
|
||
/** 三种姿势都挑得了多个,只有平铺那一档要另说一句:它本来就是摊开来给人点的一排。 */
|
||
const multipleHint = computed(() =>
|
||
resolvedStyle.value === 'inline' ? t('form.multiplePickInlineHint') : t('form.multiplePickHint')
|
||
)
|
||
|
||
/** 两格下拉组件的分工:这一格挑不了的来源,指个路比只给三条可选项好。 */
|
||
const positioningHint = computed(() => {
|
||
if (props.field.type === 'select') return t('form.optionSourcePositionSelect')
|
||
if (props.field.type === 'picker') return t('form.optionSourcePositionPicker')
|
||
return ''
|
||
})
|
||
|
||
const dictOptions = computed(() =>
|
||
withMissingOption(
|
||
[
|
||
{ value: '', label: t('form.optionSourceDictNone') },
|
||
...dictRows.value.map((item) => ({ value: item.code, label: `${item.name} · ${item.code}` }))
|
||
],
|
||
props.field.dictCode,
|
||
(code) => ({ value: code, label: `${code} · ${t('form.optionSourceDictMissing')}` })
|
||
)
|
||
)
|
||
|
||
|
||
const siblingOptions = computed(() =>
|
||
(props.siblings ?? [])
|
||
.filter((item) => item.key && item.key !== props.field.key)
|
||
.map((item) => ({ value: item.key, label: `${item.label || item.key}` }))
|
||
)
|
||
|
||
/** 级联把上级存的值当字典项的上级归属比:上级那一格若存的是别的列,这一格筛不出东西。 */
|
||
const cascadeHint = computed(() => {
|
||
const parent = (props.siblings ?? []).find((item) => item.key === (props.field.cascadeFrom ?? '').trim())
|
||
if (parent && (parent.optionMap?.valueKey ?? '') !== '' && parent.optionMap?.valueKey !== 'value') {
|
||
return t('form.cascadeHintMapped', { field: parent.label || parent.key, key: parent.optionMap?.valueKey })
|
||
}
|
||
return t('form.cascadeHint')
|
||
})
|
||
|
||
const problems = computed(() =>
|
||
optionSourceProblems(props.field, {
|
||
keys: new Set(siblingKeys.value),
|
||
source: sourceProbe.value,
|
||
rows: routeSourceRows.value,
|
||
peers: props.siblings
|
||
})
|
||
)
|
||
|
||
/**
|
||
* 条件路由里那几条各引用一张源,逐条判「这张源还在不在、能不能当候选」。
|
||
*
|
||
* 清单还没回来时给 ``undefined``(这一项不判,免得刚打开面板就冒一句「源不存在」);
|
||
* 回来了却找不到就给 ``null``,那一句是真的该说。
|
||
*/
|
||
const routeSourceRows = computed<Record<string, DataSource | null> | undefined>(() => {
|
||
if (!loaded.value) return undefined
|
||
const out: Record<string, DataSource | null> = {}
|
||
optionRoutesOf(props.field).forEach((route) => {
|
||
const code = (route.dataSourceCode ?? '').trim()
|
||
if (!code || code in out) return
|
||
out[code] = dataSources.value.find((item) => item.code === code) ?? null
|
||
})
|
||
return out
|
||
})
|
||
|
||
/** 同一张表单里已有的字段名:候选入参的 `{form.x}` 引用要在这里面判。 */
|
||
const siblingKeys = computed(() => (props.siblings ?? []).map((item) => item.key).filter(Boolean))
|
||
/** 选人字段单独一份:候选按 SQL 筛时,那一格的数组要包一层 account 才绑得进去 */
|
||
const siblingPersonKeys = computed(() =>
|
||
(props.siblings ?? [])
|
||
.filter((item) => String(item.type ?? '') === 'user')
|
||
.map((item) => item.key)
|
||
.filter(Boolean)
|
||
)
|
||
|
||
/** 只有列表型源能当候选:取值型源回的是一个值,不是能挑的一批。 */
|
||
const searchableSources = computed(() => dataSources.value.filter((item) => item.kind === 'search'))
|
||
|
||
/** 取值型源:条件路由那一条自带的「带出」要问的就是它们(候选问 A 家清单、那几列在 B 家) */
|
||
const fetchSources = computed(() => dataSources.value.filter((item) => item.kind === 'fetch'))
|
||
|
||
/** 编辑器要的那三份:候选来源这一路只有一张源,带出去向用不上。 */
|
||
/** 用上的源被删了也要看得见那一行(下拉按值找不着就会显示成「还没选」)。 */
|
||
const missingSourceOption = computed(() => {
|
||
const code = (props.field.dataSourceCode ?? '').trim()
|
||
if (!code || searchableSources.value.some((item) => item.code === code)) return undefined
|
||
// 源还在、只是不能当候选(是取一个值的):那一行要说的是这个,不是「已删除」
|
||
const row = dataSources.value.find((item) => item.code === code)
|
||
const why = row ? t('form.optionSourceSourceNotList') : t('form.optionSourceSourceMissing')
|
||
return { value: code, label: `${code} · ${why}` }
|
||
})
|
||
|
||
const sourceEntry = computed<FetchSourceEntry>(() => ({
|
||
source: props.field.dataSourceCode ?? '',
|
||
args: { ...(props.field[OPTION_ARGS_KEY] ?? {}) },
|
||
picks: {}
|
||
}))
|
||
|
||
/**
|
||
* 交给检查函数的那一行源。
|
||
*
|
||
* 数据源清单还没回来时给 `undefined`(= 这一项不判,免得刚打开面板就冒一句「源不存在」);
|
||
* 清单回来了却找不到,就给 `null`,那一句是真的该说。
|
||
*/
|
||
const sourceProbe = computed<DataSource | null | undefined>(() => {
|
||
const code = (props.field.dataSourceCode ?? '').trim()
|
||
if (!loaded.value || !code) return undefined
|
||
// 在**全量**清单里找:只在可搜的清单里找会把「取一个值的那种源」报成「源不存在」
|
||
return dataSources.value.find((item) => item.code === code) ?? null
|
||
})
|
||
|
||
function patch(values: Partial<FormFieldDef>) {
|
||
emit('patch', values)
|
||
}
|
||
|
||
/** 换源或改入参:入参清空时整个键撤掉,别在表单里留一份空对象。 */
|
||
function pickSource(entry: FetchSourceEntry) {
|
||
const args = Object.fromEntries(
|
||
Object.entries(entry.args || {}).filter(([, value]) => String(value ?? '').trim())
|
||
)
|
||
patch({
|
||
dataSourceCode: entry.source || undefined,
|
||
[OPTION_ARGS_KEY]: Object.keys(args).length ? args : undefined
|
||
} as Partial<FormFieldDef>)
|
||
}
|
||
|
||
/** 换来源:新来源那格留着,别的来源那几格一并清掉(口径见 optionSourcePatch)。 */
|
||
function pickKind(next: OptionSourceKind) {
|
||
if (next === kind.value) return
|
||
patch(optionSourcePatch(props.field, next))
|
||
}
|
||
|
||
/**
|
||
* 勾「可以选好几个」:只改这一格存的值是什么形状。
|
||
*
|
||
* 三种姿势都装得下一串值(下拉里连着勾、弹出框里勾完点确定、平铺就是那一排多选框),
|
||
* 所以不替人改挑选方式——配的人挑的是什么姿势,勾完还是那个姿势。
|
||
*/
|
||
function pickMultiple(next: boolean) {
|
||
if (next === multiple.value) return
|
||
patch({ multiple: next || undefined })
|
||
}
|
||
|
||
// ------------------------------------------------------------------ 手填选项
|
||
|
||
function addOption() {
|
||
const options = [...(props.field.options ?? [])]
|
||
const label = `${t('form.optionOne')}${options.length + 1}`
|
||
options.push({ value: label, label })
|
||
patch({ options })
|
||
}
|
||
|
||
/** 改显示文字:存的值还跟它一样时一起走,一旦单独改过就别再被顶掉。 */
|
||
function patchOptionLabel(index: number, label: string) {
|
||
const options = [...(props.field.options ?? [])]
|
||
const current = options[index]
|
||
const keepOwnValue = String(current?.value ?? '') !== String(current?.label ?? '')
|
||
options[index] = { value: keepOwnValue ? (current?.value ?? label) : label, label }
|
||
patch({ options })
|
||
}
|
||
|
||
/** 改存的值:清空当「跟显示文字一样」,那一格不留一个存不出值的选项。 */
|
||
function patchOptionValue(index: number, value: string) {
|
||
const options = [...(props.field.options ?? [])]
|
||
const label = options[index]?.label ?? ''
|
||
options[index] = { value: value.trim() || label, label }
|
||
patch({ options })
|
||
}
|
||
|
||
function removeOption(index: number) {
|
||
patch({ options: (props.field.options ?? []).filter((_, at) => at !== index) })
|
||
}
|
||
|
||
// -------------------------------------------------- 演「显示什么、存什么」的那条真候选
|
||
|
||
/** 这一来源多带出来的那几个键(字典项 extra / 数据源 mapExtra + 实际搜回来的那几个)。 */
|
||
const extraKeys = computed(() => {
|
||
const fromItems = extraKeysOf(probeRows.value.length ? probeRows.value : dictRowsItems.value)
|
||
if (kind.value !== 'dataSource') return fromItems
|
||
const declared = Object.keys(source.value?.mapExtra ?? {})
|
||
return Array.from(new Set([...declared, ...fromItems]))
|
||
})
|
||
|
||
/** 拿来演样子的那一条候选。 */
|
||
const sample = computed<Record<string, unknown> | null>(() => {
|
||
if (kind.value === 'dict') return (dictRowsItems.value[0] as unknown as Record<string, unknown>) ?? null
|
||
return (probeRows.value[0] as unknown as Record<string, unknown>) ?? processSample.value
|
||
})
|
||
|
||
// ------------------------------------------------------------------ 字典取一项
|
||
|
||
const dictRowsItems = ref<DictItem[]>([])
|
||
|
||
watch(
|
||
() => [kind.value, props.field.dictCode ?? ''] as const,
|
||
async ([active, code]) => {
|
||
dictRowsItems.value = []
|
||
if (active !== 'dict' || !code) return
|
||
try {
|
||
dictRowsItems.value = (await dictValuesCached([code]))[code] ?? []
|
||
} catch {
|
||
/* 取不到就不演样例,那句提示自己会说不 */
|
||
}
|
||
},
|
||
{ immediate: true }
|
||
)
|
||
|
||
// -------------------------------------------------------------- 数据源试搜
|
||
|
||
const probing = ref(false)
|
||
const probeOk = ref(true)
|
||
const probeText = ref('')
|
||
/** 试搜回来的那几条:既给「显示/存值」演样子,也顺手把上游多带出来的键摸清楚。 */
|
||
const probeRows = ref<{ value: string; label: string; extra?: Record<string, unknown> }[]>([])
|
||
|
||
const source = computed(() => dataSources.value.find((item) => item.code === (props.field.dataSourceCode ?? '').trim()))
|
||
|
||
/** 按当前这份配置搜一页:上游通不通、显示名取得对不对,配的人当场就能看见。 */
|
||
async function probe() {
|
||
const code = (props.field.dataSourceCode ?? '').trim()
|
||
if (!code) return
|
||
probing.value = true
|
||
try {
|
||
// 配的人此刻手上没有一张真单,能算出来的入参就带上(算不出来的一格不带)
|
||
const args = resolveBindingArgs({ args: props.field[OPTION_ARGS_KEY] ?? {} }, {}) ?? {}
|
||
const result = await searchOptions(code, '', args)
|
||
const items = result?.items ?? []
|
||
probeRows.value = items
|
||
probeOk.value = Boolean(result?.ok) && items.length > 0
|
||
probeText.value = probeOk.value
|
||
? t('form.optionSourceProbeOk', {
|
||
count: result?.itemCount ?? items.length,
|
||
sample: items.slice(0, 3).map((item) => item.label).join(' / ')
|
||
})
|
||
: t('form.optionSourceProbeEmpty', { error: result?.error || t('form.optionSourceProbeNone') })
|
||
} catch {
|
||
probeOk.value = false
|
||
probeText.value = t('form.optionSourceProbeFailed')
|
||
} finally {
|
||
probing.value = false
|
||
}
|
||
}
|
||
|
||
// 挑完源就默默搜一页:那行「显示「螺栓」,保存 M1」得有条真候选才说得准,等手按太晚
|
||
watch(
|
||
() => [kind.value, props.field.dataSourceCode ?? ''] as const,
|
||
([active, code]) => {
|
||
probeRows.value = []
|
||
probeText.value = ''
|
||
if (active === 'dataSource' && code) void probe()
|
||
},
|
||
{ immediate: true }
|
||
)
|
||
|
||
// ------------------------------------------------------------ 单据取一张真单
|
||
|
||
/** 这一格勾中的那几档(没配就是只给归档通过的)。 */
|
||
const relationStatuses = computed(() => relationStatusesOf(props.field))
|
||
|
||
/**
|
||
* 勾一档就存一份清单,并按界面那三档的顺序存。
|
||
*
|
||
* 勾回「只给归档通过」时把键整个抹掉:那正是没配过这一格读出来的结果,
|
||
* 存一份跟不存一样的配置,只会让下一个人以为有人特意配过什么。
|
||
*/
|
||
function toggleRelationStatus(item: RelationStatus, checked: boolean) {
|
||
const picked = new Set(relationStatuses.value)
|
||
if (checked) picked.add(item)
|
||
else picked.delete(item)
|
||
const next = RELATION_STATUSES.filter((one) => picked.has(one))
|
||
const isDefault = next.length === 1 && next[0] === RELATION_STATUS_DEFAULT[0]
|
||
patch({ relationStatuses: isDefault ? undefined : [...next] })
|
||
}
|
||
|
||
const processSample = ref<Record<string, unknown> | null>(null)
|
||
|
||
/** 这一格挑候选的那一片人:默认只给自已发起的。摆在那一趟 watch 之前——它要跟着一起问接口。 */
|
||
const relationScope = computed<RelationScope>(() => relationScopeOf(props.field))
|
||
const relationScopeOptions = computed(() =>
|
||
RELATION_SCOPES.map((item) => ({ value: item, label: t(RELATION_SCOPE_LABEL_KEYS[item]) }))
|
||
)
|
||
|
||
/**
|
||
* 换范围:回到默认那一档就把键整个抹掉。
|
||
*
|
||
* 与状态那三档同一分寸——「只给我发起的」正是没配过这一格读出来的结果,
|
||
* 存一份跟不存一模一样的配置,只会让下一任配的人以为有人特意配过什么。
|
||
*/
|
||
function pickRelationScope(next: string) {
|
||
const picked = (RELATION_SCOPES as string[]).includes(next) ? (next as RelationScope) : RELATION_SCOPE_DEFAULT
|
||
if (picked === relationScope.value) return
|
||
patch({ relationScope: picked === RELATION_SCOPE_DEFAULT ? undefined : picked })
|
||
}
|
||
|
||
watch(
|
||
() =>
|
||
[
|
||
kind.value,
|
||
props.field.relationProcess ?? '',
|
||
relationStatuses.value.join(','),
|
||
relationScope.value,
|
||
displayFormPathsOf(props.field).join(',')
|
||
] as const,
|
||
async ([active, process, statuses, scope, fields]) => {
|
||
processSample.value = null
|
||
if (active !== 'process') return
|
||
try {
|
||
// 这一条只是演样子,取不到就少一行提示,不该弹一句错误打扰配的人。
|
||
// 按这一格勾的那几档取:拿一张已终止的单子当样例,配的人看着就是另一回事了
|
||
// 范围与点名的列也照这一格此刻的配法问:配「只给我发起的」却演出一张别人发起的单,
|
||
// 那句「例:这一格显示…」就是在骗人
|
||
const page = await workAPI.listRelatedWorks({
|
||
keyword: '',
|
||
process,
|
||
status: statuses,
|
||
scope,
|
||
...(fields ? { fields } : {}),
|
||
page: 1,
|
||
page_size: 1
|
||
})
|
||
processSample.value = (page?.items?.[0] as unknown as Record<string, unknown>) ?? null
|
||
} catch {
|
||
/* 一张单子都取不到(新环境、没权限)就算了:只说列名,不编一张假单 */
|
||
}
|
||
},
|
||
{ immediate: true }
|
||
)
|
||
|
||
/**
|
||
* 限定过哪个流程:那条流程的表单里有哪些格,一并进「显示哪一列 / 那一行小字」那几份清单。
|
||
*
|
||
* 拿的是「从另一个流程带数据」那一份字段清单(`LinkFillPanel` 用的同一句)——同一张单据,
|
||
* 「带出哪一列」与「显示哪一列」问的是同一样东西,清单也该是同一份。没限定流程时挑不出:
|
||
* 那时挂的可能是任何一条流程的单,配的人手敲 `data.字段名` 就行(那几格本来就允许自己敲)。
|
||
*/
|
||
const formFields = ref<ProcessFieldOption[]>([])
|
||
|
||
watch(
|
||
() => [kind.value, props.field.relationProcess ?? ''] as const,
|
||
async ([active, process]) => {
|
||
formFields.value = []
|
||
if (active !== 'process' || !process) return
|
||
try {
|
||
formFields.value = (await processFields(process))?.fields ?? []
|
||
} catch {
|
||
formFields.value = []
|
||
}
|
||
},
|
||
{ immediate: true }
|
||
)
|
||
</script>
|