@94ai/nf-search-form 3.1.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,1153 @@
1
+ <template>
2
+ <div class="nf-search-form" ref="topDom">
3
+ <nf-form ref="nfSearchFormRef"
4
+ :model="nfSearchFormModel"
5
+ :inline="inline"
6
+ :label-position="labelPosition"
7
+ :rules="rules"
8
+ :label-suffix="labelSuffix"
9
+ :hide-required-asterisk="hideRequiredAsterisk"
10
+ :show-message="showMessage"
11
+ :inline-message="inlineMessage"
12
+ :status-icon="statusIcon"
13
+ :validate-on-rule-change="validateOnRuleChange"
14
+ :size="size"
15
+ :disabled="disabled"
16
+
17
+ :label-width="labelWidth+'px'"
18
+
19
+ :expand-btn-type="expandBtnType"
20
+ :expand-btn-icon="expandBtnIcon"
21
+ :expand-btn-plain="expandBtnPlain"
22
+ :expand-btn-size="expandBtnSize"
23
+ :expand-btn-round="expandBtnRound"
24
+ :expand-btn-circle="expandBtnCircle"
25
+ :expand-btn-loading="expandBtnLoading"
26
+ :expand-btn-disabled="expandBtnDisabled"
27
+
28
+ :columns="inline ? newColumns : columns"
29
+ :up-icon="upIcon"
30
+ :down-icon="downIcon"
31
+ :show-btn="showBtn"
32
+ :is-collapse="isCollapse"
33
+ :expand-btn-tooltip-desc="expandBtnTooltipDesc"
34
+ :shrink-btn-tooltip-desc="shrinkBtnTooltipDesc"
35
+
36
+ @isCollapseChange="isCollapseChange"
37
+
38
+ >
39
+ <template
40
+ v-for="item in renderSearchFormItemOptions"
41
+ >
42
+ <nf-form-item
43
+ v-if="itemIsShow(item)"
44
+ :class="(item.class || '') + ' nf-search-form-nf-form-item'"
45
+ :hidden="!!item.hidden"
46
+ :key="item.key"
47
+ :prop="item.prop"
48
+ :rules="item.rules"
49
+ :show-message="item.showMessage"
50
+ :size="item.size"
51
+ :inline-message="item.inlineMessage"
52
+ :required="item.required"
53
+ :label-width="item.labelWidth"
54
+ >
55
+ <slot :name="item.labelSlot" slot="label">
56
+ {{ item.label ? (item.isShowColon?item.label + ':':item.label) : '' }}
57
+ </slot>
58
+ <search-form-item
59
+ :key="item.key"
60
+ :item-width="itemWidth"
61
+ v-model="nfSearchFormModel[item.prop]"
62
+ :item-options="item">
63
+ <!-- 定制化SearchItem?提示-->
64
+ <slot :name="item.tooltip.slot" v-if="item.tooltip" :slot="item.tooltip.slot" />
65
+ <!-- 定制化SearchItem插槽-->
66
+ <slot :name="item.slot" v-if="item.slot" :slot="item.slot" />
67
+ </search-form-item>
68
+ <!-- 控制显示该表单项-->
69
+ <div v-if="closable" class="nf-search-form-nf-form-item-close-icon">
70
+ <i class="el-icon-error" @click="hiddenNfSearchFormItemOptionField(item.prop)" />
71
+ </div>
72
+ </nf-form-item>
73
+ </template>
74
+ <nf-form-item :label="showBtnBoxLabel"
75
+ v-if="btnModel==='inline'"
76
+ class="nf-search-form-nf-form-item-btn-box-label"
77
+ ref="defaultButtonSlot"
78
+ >
79
+ <div class="nf-flex nf-flex-align-items-start">
80
+ <slot v-if="isShowBtnBoxSlot && !reverseBtnSlot" name="btnBox" />
81
+ <nf-button
82
+ v-if="btnItems.includes('search')"
83
+ :type="queryBtnType"
84
+ :icon="queryBtnIcon"
85
+ :plain="queryBtnPlain"
86
+ :size="queryBtnSize"
87
+ :round="queryBtnRound"
88
+ :circle="queryBtnCircle"
89
+ :loading="queryBtnLoading"
90
+ :disabled="queryBtnDisabled"
91
+ :class="(queryBtnClass || '') + ' nf-btn-search'"
92
+ @click="onNfSearchFormSearch"
93
+ >{{ queryBtnText }}
94
+ </nf-button>
95
+ <nf-button
96
+ v-if="btnItems.includes('export')"
97
+ :type="exportBtnType"
98
+ :icon="exportBtnIcon"
99
+ :plain="exportBtnPlain"
100
+ :size="exportBtnSize"
101
+ :round="exportBtnRound"
102
+ :circle="exportBtnCircle"
103
+ :loading="exportBtnLoading"
104
+ :disabled="exportBtnDisabled"
105
+ :class="(exportBtnClass || '') + ' nf-btn-export'"
106
+ @click="onNfSearchFormExport"
107
+ >{{ exportBtnText }}
108
+ </nf-button>
109
+ <nf-button
110
+ v-if="btnItems.includes('reset')"
111
+ :type="resetBtnType"
112
+ :icon="resetBtnIcon"
113
+ :plain="resetBtnPlain"
114
+ :size="resetBtnSize"
115
+ :round="resetBtnRound"
116
+ :circle="resetBtnCircle"
117
+ :loading="resetBtnLoading"
118
+ :disabled="resetBtnDisabled"
119
+ :class="(resetBtnClass || '') + ' nf-btn-reset'"
120
+ @click="resetNfSearchFormModel"
121
+ >{{ resetBtnText }}
122
+ </nf-button>
123
+ <nf-button :type="checkBtnType"
124
+ :plain="checkBtnPlain"
125
+ :size="checkBtnSize"
126
+ :round="checkBtnRound"
127
+ :circle="checkBtnCircle"
128
+ :loading="checkBtnLoading"
129
+ :disabled="checkBtnDisabled"
130
+ :class="(checkBtnClass || '') + ' nf-btn-check'"
131
+ v-if="columnEdit"
132
+ @click="configNfSearchItemVisibleDialog = true"
133
+ >
134
+ <el-tooltip :content="columnDisplayBtnText" placement="top">
135
+ <span class="icon-wrap"><i class="iconfont icon-shaixuan1 nf-fs12"></i></span>
136
+ </el-tooltip>
137
+ </nf-button>
138
+ <slot v-if="isShowBtnBoxSlot && reverseBtnSlot" name="btnBox" />
139
+ </div>
140
+ </nf-form-item>
141
+ </nf-form>
142
+ <div class="nf-flex nf-flex-align-items nf-mt10" v-if="btnModel==='block'">
143
+ <slot v-if="isShowBtnBoxSlot && !reverseBtnSlot" name="btnBox" />
144
+ <nf-button
145
+ v-if="btnItems.includes('search')"
146
+ :type="queryBtnType"
147
+ :icon="queryBtnIcon"
148
+ :plain="queryBtnPlain"
149
+ :size="queryBtnSize || size"
150
+ :round="queryBtnRound"
151
+ :circle="queryBtnCircle"
152
+ :loading="queryBtnLoading"
153
+ :disabled="queryBtnDisabled"
154
+ :class="(queryBtnClass || '') + ' nf-btn-search'"
155
+ @click="onNfSearchFormSearch"
156
+ >{{ queryBtnText }}
157
+ </nf-button>
158
+ <nf-button
159
+ v-if="btnItems.includes('export')"
160
+ :type="exportBtnType"
161
+ :icon="exportBtnIcon"
162
+ :plain="exportBtnPlain"
163
+ :size="exportBtnSize || size"
164
+ :round="exportBtnRound"
165
+ :circle="exportBtnCircle"
166
+ :loading="exportBtnLoading"
167
+ :disabled="exportBtnDisabled"
168
+ :class="(exportBtnClass || '') + ' nf-btn-export'"
169
+ @click="onNfSearchFormExport"
170
+ >{{ exportBtnText }}
171
+ </nf-button>
172
+ <nf-button
173
+ v-if="btnItems.includes('reset')"
174
+ :type="resetBtnType"
175
+ :icon="resetBtnIcon"
176
+ :plain="resetBtnPlain"
177
+ :size="resetBtnSize || size"
178
+ :round="resetBtnRound"
179
+ :circle="resetBtnCircle"
180
+ :loading="resetBtnLoading"
181
+ :disabled="resetBtnDisabled"
182
+ :class="(resetBtnClass || '') + ' nf-btn-reset'"
183
+ @click="resetNfSearchFormModel"
184
+ >{{ resetBtnText }}
185
+ </nf-button>
186
+ <nf-button :type="checkBtnType"
187
+ v-if="columnEdit"
188
+ :plain="checkBtnPlain"
189
+ :size="checkBtnSize || size"
190
+ :round="checkBtnRound"
191
+ :circle="checkBtnCircle"
192
+ :loading="checkBtnLoading"
193
+ :disabled="checkBtnDisabled"
194
+ @click="configNfSearchItemVisibleDialog = true"
195
+ :class="(checkBtnClass || '') + ' nf-btn-check'"
196
+ >
197
+ <el-tooltip :content="columnDisplayBtnText" placement="top" v-if="columnEdit">
198
+ <span class="icon-wrap"><i class="iconfont icon-shaixuan1 nf-fs12"></i></span>
199
+ </el-tooltip>
200
+ </nf-button>
201
+ <slot v-if="isShowBtnBoxSlot && reverseBtnSlot" name="btnBox" />
202
+ </div>
203
+ <column-check-dialog
204
+ :title="moreDialogTitle"
205
+ :cancel-text="columnCheckCancelButtonText"
206
+ :confirm-text="columnCheckConfirmButtonText"
207
+ :basic-field-desc="columnCheckBasicFieldDesc"
208
+
209
+ ref="columnCheckDialog"
210
+ v-if="columnEdit && configNfSearchItemVisibleDialog && checkMode === 'dialog'"
211
+ :show.sync="configNfSearchItemVisibleDialog"
212
+ :modal-append-to-body="modalAppendToBody"
213
+ :storage-key="storageKey"
214
+ :table-columns="formOptions"
215
+ :width="moreDialogWidth"
216
+ :modal="modal"
217
+ :fullscreen="fullscreen"
218
+ :append-to-body="appendToBody"
219
+ :top="top"
220
+ :lock-scroll="lockScroll"
221
+ :custom-class="customClass"
222
+ :close-on-click-modal="closeOnlickModal"
223
+ :close-on-press-escape="closeOnPressEscape"
224
+ :show-close="showClose"
225
+ :before-close="beforeClose"
226
+ :center="center"
227
+ :destroy-on-close="destroyOnClose"
228
+ @save="nfSearchFormItemOptionFieldsSave"
229
+ @open="$emit('open', $event)"
230
+ @opened="$emit('opened', $event)"
231
+ @close="$emit('close', $event)"
232
+ @closed="$emit('closed', $event)"
233
+ >
234
+ <slot slot="title" name="dialogTitle"></slot>
235
+ <slot slot="footer" name="dialogFooter"></slot>
236
+ </column-check-dialog>
237
+ <nf-drawer
238
+ :title="moreDialogTitle"
239
+ :cancel-button-text="columnCheckCancelButtonText"
240
+ :confirm-button-text="columnCheckConfirmButtonText"
241
+
242
+ ref="columnCheckNfDrawer"
243
+ size="680px"
244
+ v-if="columnEdit && checkMode === 'drawer'"
245
+ :destroy-on-close="true"
246
+ :visible.sync="configNfSearchItemVisibleDialog"
247
+ :tabs="columnCheckTabs"
248
+ :current-tab="columnCheckCurrentTab"
249
+ @confirm="columnCheckConfirm"
250
+ @changeTab="(done) => $emit('changeTab', done)"
251
+ @cancel="(event) => $emit('cancel', event)"
252
+ >
253
+ <slot slot="title" name="columCheckTitle" />
254
+ <slot slot="footer" name="columCheckFooter" />
255
+ <column-check-drawer
256
+ :basic-field-desc="columnCheckBasicFieldDesc"
257
+ :search-placeholder="columnCheckSearchPlaceholder"
258
+ :fixed-left-desc="columnCheckFixedLeftDesc"
259
+ :clear-desc="columnCheckClearDesc"
260
+ :no-fixed-desc="columnCheckNoFixedDesc"
261
+ :select-empty-warning-tips="selectEmptyWarningTips"
262
+ :get-checked-length-desc="columnCheckGetCheckedLengthDesc"
263
+ :get-all-check-desc="columnCheckGetAllCheckDesc"
264
+
265
+ ref="columnCheckDrawer"
266
+ :storage-key="storageKey"
267
+ :table-columns="formOptions"
268
+ :column-edit="columnEdit"
269
+ />
270
+ </nf-drawer>
271
+ </div>
272
+ </template>
273
+
274
+ <script lang="ts">
275
+ import { defineComponent } from 'vue-demi'
276
+
277
+ export default defineComponent({
278
+ name: 'nf-search-form'
279
+ })
280
+ </script>
281
+
282
+
283
+ <script lang="ts" setup>
284
+ import SearchFormItem from './nf-search-form-item.vue'
285
+ import ColumnCheckDialog from './nf-column-check-dialog.vue'
286
+ import ColumnCheckDrawer from './nf-column-check-drawer.vue'
287
+ import ResizeObserver from 'resize-observer-polyfill'
288
+ import { createUniqueString } from '@94ai/common-utils'
289
+ import { NfSearchFormItemOption } from './types'
290
+ import {
291
+ ref,
292
+ computed,
293
+ getCurrentInstance,
294
+ watch,
295
+ onBeforeMount,
296
+ onMounted,
297
+ onBeforeUnmount,
298
+ nextTick
299
+ } from 'vue-demi'
300
+ import { ITabItem } from '@94ai/nf-drawer/lib/types'
301
+ import {
302
+ NfTableColumnAttributes
303
+ } from '@94ai/nf-table/lib/types'
304
+ import { set } from 'vue'
305
+ import {
306
+ CachedManager,
307
+ } from '@94ai/common-utils'
308
+ import {
309
+ throttle
310
+ } from 'lodash-es'
311
+
312
+
313
+ const props = withDefaults(defineProps<{
314
+ /**
315
+ * nf-form扩展配置项
316
+ */
317
+ upIcon?: string,
318
+ downIcon?: string,
319
+ columns?: number,
320
+ showBtn?: boolean,
321
+ isCollapse?: boolean,
322
+ expandBtnType?: string,
323
+ expandBtnIcon?: string,
324
+ expandBtnPlain?: boolean,
325
+ expandBtnSize?: string,
326
+ expandBtnRound?: boolean,
327
+ expandBtnCircle?: boolean,
328
+ expandBtnLoading?: boolean,
329
+ expandBtnDisabled?: boolean,
330
+
331
+ expandBtnTooltipDesc?: string,
332
+ shrinkBtnTooltipDesc?: string,
333
+ /**
334
+ * 以下同el-form配置项
335
+ */
336
+ rules?: Record<string, any>
337
+ labelSuffix?: string
338
+ hideRequiredAsterisk?: boolean
339
+ showMessage?: boolean
340
+ inlineMessage?: boolean
341
+ statusIcon?: boolean
342
+ validateOnRuleChange?: boolean
343
+ size?: 'medium' | 'small' | 'mini'
344
+ disabled?: boolean
345
+ /**
346
+ * 控制label和content和按钮的组合位置
347
+ */
348
+ labelPosition?: 'top' | 'right' | 'left'
349
+ inline?: boolean
350
+ btnBoxLabel?: string
351
+ btnModel?: 'inline' | 'block'
352
+ /**
353
+ * 兼容ai-search-form控制item父dom宽度
354
+ */
355
+ itemWidth?: number
356
+ /**
357
+ * label宽度
358
+ */
359
+ labelWidth?: number,
360
+ /**
361
+ * 所有的表单项
362
+ */
363
+ formOptions?: NfSearchFormItemOption[] | any,
364
+ /**
365
+ * 列编辑,是否显示关闭选项功能
366
+ */
367
+ closable?: boolean,
368
+ /**
369
+ * 列编辑,是否显示按钮配置表单项显示
370
+ */
371
+ columnEdit?: boolean,
372
+ /**
373
+ * 列编辑,显示的表单项,prop数组
374
+ */
375
+ showItems?: string[],
376
+ priority?: 'cached' | 'value',
377
+ storageKey?: string
378
+ checkMode?: 'dialog' | 'drawer',
379
+ selectEmptyWarningTips?: string,
380
+ columnDisplayBtnText?: string
381
+ /**
382
+ * 列编辑按钮修饰
383
+ */
384
+ checkBtnIcon?: string,
385
+ checkBtnType?: string,
386
+ checkBtnPlain?: boolean,
387
+ checkBtnSize?: string,
388
+ checkBtnRound?: boolean,
389
+ checkBtnCircle?: boolean,
390
+ checkBtnLoading?: boolean,
391
+ checkBtnDisabled?: boolean,
392
+ checkBtnClass?: string,
393
+ /**
394
+ * 列编辑drawer/dialog
395
+ */
396
+ columnCheckCancelButtonText?: string,
397
+ columnCheckConfirmButtonText?: string,
398
+ moreDialogTitle?: string,
399
+ /**
400
+ * 列编辑dialog设置
401
+ */
402
+ moreDialogWidth?: string,
403
+ modalAppendToBody?: boolean,
404
+ modal?: boolean,
405
+ fullscreen?: boolean,
406
+ top?: string,
407
+ appendToBody?: boolean,
408
+ lockScroll?: boolean,
409
+ customClass?: string,
410
+ closeOnlickModal?: boolean,
411
+ closeOnPressEscape?: boolean,
412
+ showClose?: boolean,
413
+ beforeClose?: Function,
414
+ center?: boolean,
415
+ destroyOnClose?: boolean,
416
+ /**
417
+ * 列编辑drawer
418
+ */
419
+ columnCheckSearchPlaceholder?: string
420
+ columnCheckFixedLeftDesc?: string
421
+ columnCheckClearDesc?: string
422
+ columnCheckNoFixedDesc?: string
423
+ columnCheckBasicFieldDesc?: string
424
+ columnCheckGetCheckedLengthDesc?: any
425
+ columnCheckGetAllCheckDesc?: any
426
+ columnCheckTabs?: ITabItem[],
427
+ columnCheckCurrentTab?: string,
428
+ /**
429
+ * 兼容ai-search-form,控制额外的默认功能按钮
430
+ */
431
+ btnItems?: string[] | 'search' | 'export' | 'reset' | '',
432
+ reverseBtnSlot?: boolean,
433
+ /**
434
+ * 导出按钮
435
+ */
436
+ exportBtnIcon?: string,
437
+ exportBtnType?: string,
438
+ exportBtnPlain?: boolean,
439
+ exportBtnSize?: string,
440
+ exportBtnRound?: boolean,
441
+ exportBtnCircle?: boolean,
442
+ exportBtnLoading?: boolean,
443
+ exportBtnDisabled?: boolean,
444
+ exportBtnText?: string,
445
+ exportBtnClass?: string,
446
+ /**
447
+ * 查询按钮
448
+ */
449
+ queryBtnIcon?: string,
450
+ queryBtnType?: string,
451
+ queryBtnPlain?: boolean,
452
+ queryBtnSize?: string,
453
+ queryBtnRound?: boolean,
454
+ queryBtnCircle?: boolean,
455
+ queryBtnLoading?: boolean,
456
+ queryBtnDisabled?: boolean,
457
+ queryBtnText?: string,
458
+ queryBtnClass?: string,
459
+ /**
460
+ * 重置按钮
461
+ */
462
+ resetBtnIcon?: string,
463
+ resetBtnType?: string,
464
+ resetBtnPlain?: boolean,
465
+ resetBtnSize?: string,
466
+ resetBtnRound?: boolean,
467
+ resetBtnCircle?: boolean,
468
+ resetBtnLoading?: boolean,
469
+ resetBtnDisabled?: boolean,
470
+ resetBtnText?: string,
471
+ resetBtnClass?: string,
472
+ }>(), {
473
+ priority: 'cached',
474
+ storageKey: 'nf-search-form-columns-cached-key',
475
+ checkMode: 'drawer',
476
+ upIcon: 'icon-xiangshang2',
477
+ downIcon: 'icon-xiangxia2',
478
+ showBtn: true,
479
+ isCollapse: false,
480
+ expandBtnType: '',
481
+ expandBtnPlain: true,
482
+ expandBtnRound: false,
483
+ expandBtnCircle: false,
484
+ expandBtnLoading: false,
485
+ expandBtnDisabled: false,
486
+ columns: undefined,
487
+ labelPosition: 'top',
488
+ inline: true,
489
+ rules: undefined,
490
+ labelSuffix: undefined,
491
+ hideRequiredAsterisk: false,
492
+ showMessage: true,
493
+ inlineMessage: false,
494
+ statusIcon: false,
495
+ validateOnRuleChange: true,
496
+ size: 'mini',
497
+ disabled: false,
498
+ labelWidth: 150,
499
+ formOptions: () => [],
500
+ showItems: () => [],
501
+ closable: false,
502
+ itemWidth: 227,
503
+ btnItems: () => '',
504
+ columnEdit: false,
505
+ btnBoxLabel: '',
506
+ btnModel: 'block',
507
+ checkBtnIcon: '',
508
+ checkBtnPlain: true,
509
+ checkBtnType: '',
510
+ checkBtnRound: false,
511
+ checkBtnCircle: false,
512
+ checkBtnLoading: false,
513
+ checkBtnDisabled: false,
514
+ checkBtnClass: '',
515
+ queryBtnIcon: '',
516
+ queryBtnPlain: false,
517
+ queryBtnType: 'primary',
518
+ queryBtnRound: false,
519
+ queryBtnCircle: false,
520
+ queryBtnLoading: false,
521
+ queryBtnDisabled: false,
522
+ queryBtnClass: '',
523
+ resetBtnIcon: '',
524
+ resetBtnPlain: true,
525
+ resetBtnType: 'default',
526
+ resetBtnRound: false,
527
+ resetBtnCircle: false,
528
+ resetBtnLoading: false,
529
+ resetBtnDisabled: false,
530
+ resetBtnClass: '',
531
+ exportBtnIcon: '',
532
+ exportBtnPlain: true,
533
+ exportBtnType: 'primary',
534
+ exportBtnRound: false,
535
+ exportBtnCircle: false,
536
+ exportBtnLoading: false,
537
+ exportBtnDisabled: false,
538
+ exportBtnClass: '',
539
+ reverseBtnSlot: false,
540
+ moreDialogWidth: '600',
541
+ modalAppendToBody: true,
542
+ modal: true,
543
+ fullscreen: false,
544
+ top: '15vh',
545
+ appendToBody: false,
546
+ lockScroll: true,
547
+ customClass: '',
548
+ closeOnlickModal: true,
549
+ closeOnPressEscape: true,
550
+ showClose: true,
551
+ beforeClose: undefined,
552
+ center: false,
553
+ destroyOnClose: false,
554
+ expandBtnTooltipDesc: '展开',
555
+ shrinkBtnTooltipDesc: '收起',
556
+ exportBtnText: '导出',
557
+ resetBtnText: '重置',
558
+ queryBtnText: '查询',
559
+ selectEmptyWarningTips: '请选择至少一个选项',
560
+ columnCheckCancelButtonText: '取消',
561
+ columnCheckConfirmButtonText: '确定',
562
+ moreDialogTitle: '设置显示查询框',
563
+ columnDisplayBtnText: '添加筛选条件',
564
+ columnCheckSearchPlaceholder: '搜索',
565
+ columnCheckFixedLeftDesc: '固定在左侧',
566
+ columnCheckClearDesc: '清除',
567
+ columnCheckNoFixedDesc: '不固定',
568
+ columnCheckBasicFieldDesc: '基础字段',
569
+ columnCheckGetCheckedLengthDesc: (length) => '已选(' + length + ')',
570
+ columnCheckGetAllCheckDesc: (length) => '全选(' + length + ')'
571
+ })
572
+
573
+ const emit = defineEmits<{
574
+ /**
575
+ * 通知FormData更改
576
+ * @param e
577
+ * @param event
578
+ */
579
+ (e: 'onChange', event: Record<string, any>): void,
580
+ /**
581
+ * 通知显示Items更改
582
+ * @param e
583
+ * @param event
584
+ */
585
+ (e: 'itemsChange', event: string[]): void
586
+ /**
587
+ * 搜索
588
+ * @param e
589
+ * @param event
590
+ */
591
+ (e: 'onSearch', event: Record<string, any>): void
592
+ /**
593
+ * 导出
594
+ * @param e
595
+ * @param event
596
+ */
597
+ (e: 'onExport', event: Record<string, any>): void
598
+ /**
599
+ * 重置字段
600
+ * @param e
601
+ */
602
+ (e: 'onResetFields'): void
603
+ /**
604
+ * 表单折叠展开
605
+ * @param e
606
+ */
607
+ (e: 'isCollapseChange', event: boolean): void
608
+ /**
609
+ * 同步showItems
610
+ * @param e
611
+ * @param event
612
+ */
613
+ (e: 'update:showItems', event: string[] | undefined): void
614
+ }>()
615
+
616
+ const instance = getCurrentInstance()?.proxy
617
+ const defaultButtonSlot = ref()
618
+ const nfSearchFormRef = ref()
619
+ const columnCheckNfDrawer = ref()
620
+ const columnCheckDrawer = ref()
621
+ const columnCheckDialog = ref()
622
+
623
+ /**
624
+ * 表单填充的数据
625
+ */
626
+ const nfSearchFormModel = ref<Record<string, any>>({})
627
+
628
+ /**
629
+ * 同步用户参数
630
+ */
631
+ const initLocalShow = (init = false) => {
632
+ if (!init) {
633
+ props.formOptions._skip = true
634
+ }
635
+ const result = props.formOptions.filter(item => {
636
+ if (props.showItems?.includes(item.prop!)) {
637
+ item.initHidden = false
638
+ }
639
+ return !item.initHidden
640
+ }).map(item => item.prop!)
641
+ if (!init) {
642
+ nextTick(() => {
643
+ props.formOptions._skip = false
644
+ })
645
+ }
646
+ if (result.length) {
647
+ return result
648
+ }
649
+ return props.formOptions.map(item => item.prop!)
650
+ }
651
+ /**
652
+ * 表单显示的item集合
653
+ */
654
+ const localShowItems = ref<string[]>(initLocalShow(true))
655
+ watch(() => localShowItems.value?.length, async () => {
656
+ emit('update:showItems', localShowItems.value)
657
+ if (Object.keys(nfSearchFormModel.value).length) { // 有可能用computed或异步引起数据更改实时同步model
658
+ refreshNfSearchFormModel(false)
659
+ }
660
+ }, {
661
+ immediate: true
662
+ })
663
+ const syncLocalShowColumnToCookies = () => {
664
+ if (props.columnEdit) {
665
+ const columnState = {}
666
+ props.formOptions.map((i, index) => {
667
+ const prop = i.prop
668
+ columnState[prop!] = {
669
+ initHidden: i.initHidden || false,
670
+ prop,
671
+ index
672
+ }
673
+ })
674
+ CachedManager.syncCachedSet(props.storageKey, columnState)
675
+ }
676
+ }
677
+ const syncShowItemToInitHidden = () => {
678
+ props.formOptions._skip = true
679
+ props.formOptions.forEach(item => {
680
+ if (localShowItems.value?.includes(item.prop!)) {
681
+ item.initHidden = false
682
+ } else {
683
+ item.initHidden = true
684
+ }
685
+ })
686
+ nextTick(() => {
687
+ props.formOptions._skip = false
688
+ })
689
+ }
690
+
691
+ const setLocalShowItems = (list, syncToInitHidden = false) => {
692
+ if (JSON.stringify(list.sort()) !== JSON.stringify(localShowItems.value.sort())) {
693
+ localShowItems.value = list
694
+ if (syncToInitHidden) {
695
+ syncShowItemToInitHidden()
696
+ }
697
+ }
698
+ }
699
+
700
+
701
+ const syncCookiesToLocalShowColumn = async (refreshModel = false) => {
702
+ if (props.columnEdit) {
703
+ try {
704
+ let columnState: string | NfTableColumnAttributes | undefined = CachedManager.syncCachedGet(props.storageKey)
705
+ if (!columnState) {
706
+ columnState = await CachedManager.asyncCachedGet(props.storageKey)
707
+ }
708
+ if (columnState) {
709
+ if (typeof columnState === 'string') {
710
+ columnState = JSON.parse(columnState)
711
+ }
712
+ const showColumnItems: string[] = []
713
+ for (const key in columnState as NfTableColumnAttributes) {
714
+ if (!columnState![key].initHidden) {
715
+ showColumnItems.push(columnState![key].prop)
716
+ }
717
+ }
718
+ setLocalShowItems(showColumnItems, true)
719
+ }
720
+ } catch (e) {
721
+ console.log(e)
722
+ }
723
+ }
724
+ if (refreshModel) {
725
+ refreshNfSearchFormModel()
726
+ }
727
+ }
728
+
729
+
730
+ const initCachedData = async () => {
731
+ if (props.columnEdit) {
732
+ // 同步缓存参数
733
+ let cached: string | NfTableColumnAttributes | undefined = CachedManager.syncCachedGet(props.storageKey)
734
+ if (!cached) {
735
+ cached = await CachedManager.asyncCachedGet(props.storageKey)
736
+ }
737
+ const cachedPriority = props.priority === 'cached'
738
+ if (cachedPriority ? cached : localShowItems.value?.length) {
739
+ cachedPriority ? await syncCookiesToLocalShowColumn() : syncLocalShowColumnToCookies()
740
+ } else if (cachedPriority ? localShowItems.value?.length : cached) {
741
+ cachedPriority ? syncLocalShowColumnToCookies() : await syncCookiesToLocalShowColumn()
742
+ }
743
+ }
744
+ }
745
+
746
+ initCachedData()
747
+
748
+ /**
749
+ * 超过数量显示更多
750
+ */
751
+ const configNfSearchItemVisibleDialog = ref(false)
752
+ /**
753
+ * 计算一行展示的个数
754
+ */
755
+ const newColumns = ref(3)
756
+
757
+ /**
758
+ * 定制按钮
759
+ */
760
+ const isShowBtnBoxSlot = computed(() => {
761
+ return instance?.$slots.btnBox
762
+ })
763
+
764
+ /**
765
+ * 转换成最终的表单项渲染数据
766
+ */
767
+ const renderSearchFormItemOptions = ref<NfSearchFormItemOption[]>([])
768
+ let initSearchForm = true
769
+ const syncCookiesToSearchFormItemOptions = async (readCached = true) => {
770
+ props.formOptions._skip = true
771
+ const columns = props.formOptions!.map((item, index) => {
772
+ item.key = item.key || createUniqueString()
773
+ item._index = index // 混合缓存后的数据
774
+ item.__index = index // 原始顺序
775
+ set(item, 'initHidden', !!item.initHidden)
776
+ set(item, 'destory', !!item.destory)
777
+ return item
778
+ })
779
+ if (readCached && props.columnEdit) {
780
+ // 读取缓存 Column 配置
781
+ try {
782
+ // 同步缓存参数
783
+ let columnState: string | NfTableColumnAttributes | undefined = CachedManager.syncCachedGet(props.storageKey)
784
+ if (!columnState) {
785
+ columnState = await CachedManager.asyncCachedGet(props.storageKey)
786
+ }
787
+ props.formOptions._skip = true
788
+ if (columnState) {
789
+ if (typeof columnState === 'string') {
790
+ columnState = JSON.parse(columnState)
791
+ }
792
+ for (const key in columnState as NfTableColumnAttributes) {
793
+ const state = columnState![key]
794
+ columns.every(i => {
795
+ if (i.prop === key) {
796
+ i.initHidden = state.initHidden
797
+ i._index = state.index
798
+ return false
799
+ }
800
+ return true
801
+ })
802
+ }
803
+ }
804
+ } catch (e) {
805
+ console.log(e)
806
+ }
807
+ }
808
+ renderSearchFormItemOptions.value = columns.sort((a, b) => a._index - b._index < 0 ? -1 : a._index - b._index > 0 ? 1 : 0);
809
+ nextTick(() => {
810
+ if (initSearchForm) {
811
+ initSearchForm = false
812
+ calcColumns()
813
+ }
814
+ props.formOptions._skip = false;
815
+ })
816
+ }
817
+ syncCookiesToSearchFormItemOptions()
818
+
819
+ /**
820
+ * 判断SearchItem是否显示
821
+ * @param prop
822
+ */
823
+ const itemIsShow = (item: NfSearchFormItemOption) => {
824
+ return !item.initHidden && !item.destory
825
+ }
826
+
827
+
828
+ /**
829
+ * 实时触发onChange
830
+ */
831
+ watch(
832
+ () => nfSearchFormModel.value,
833
+ () => {
834
+ emit('onChange', nfSearchFormModel.value)
835
+ }, {
836
+ deep: true
837
+ })
838
+
839
+ const showBtnBoxLabel = ref('')
840
+
841
+ watch(() => {
842
+ return props.btnBoxLabel
843
+ }, () => {
844
+ showBtnBoxLabel.value = props.btnBoxLabel
845
+ }, {
846
+ immediate: true
847
+ })
848
+
849
+
850
+ /**
851
+ * 初始化formData表单数据
852
+ */
853
+ onBeforeMount(() => {
854
+ /**
855
+ * 有部分人喜欢用computed当作formOptions,
856
+ * 由于computed的数据子孙属性改变会引起整个formOptions引用的改动,而且旧对象引用的子属性并未变更
857
+ * 这里需要watch同步到renderSearchFormItemOptions(子组件借助renderSearchFormItemOptions引用watch去监听,如果是data当作formOptions可以不用这个watch)
858
+ *
859
+ * 用computed去改里面的副作用,这样会整个computed改变引起整个表单重新渲染,为此需要每个item传个key,不然就不会有局部更新的效果
860
+ * 用data就是data引用不动,局部动,就会直接局部更新了(不需要这里的watch,考虑到用户用computed写起来更简单,这里就做了兼容,算是优化)
861
+ */
862
+ watch(() => {
863
+ return props.formOptions
864
+ }, async () => {
865
+ if (props.formOptions._skip) {
866
+ return
867
+ }
868
+ setLocalShowItems(initLocalShow())
869
+ await syncCookiesToSearchFormItemOptions()
870
+ }, {
871
+ deep: true
872
+ })
873
+ refreshNfSearchFormModel(false, true)
874
+ })
875
+
876
+
877
+ const refreshNfSearchFormModel = (outEmit = true, init = false) => {
878
+ const obj: Record<string, any> = {}
879
+ props.formOptions.filter(item => !item.destory).forEach(v => {
880
+ if (localShowItems.value.includes(v.prop)) {
881
+ if (init) {
882
+ if (v.initValue !== undefined) {
883
+ obj[v.prop!] = v.initValue
884
+ } else {
885
+ obj[v.prop!] = null
886
+ }
887
+ } else {
888
+ const value = nfSearchFormModel.value[v.prop]
889
+ obj[v.prop!] = typeof value === 'undefined' ? null : value
890
+ }
891
+ }
892
+ })
893
+ if (JSON.stringify(nfSearchFormModel.value) !== JSON.stringify(obj)) {
894
+ nfSearchFormModel.value = obj
895
+ if (outEmit) {
896
+ emit('itemsChange', localShowItems.value)
897
+ }
898
+ }
899
+ }
900
+
901
+ const isCollapseChange = async (val: boolean) => {
902
+ emit('isCollapseChange', val)
903
+ await nextTick()
904
+ refreshLabel()
905
+ }
906
+
907
+ const domWidth = ref()
908
+ const topDom = ref()
909
+ const refreshLabel = () => {
910
+ if (props.btnModel === 'inline' && !props.btnBoxLabel && props.labelPosition === 'top') {
911
+ if (nfSearchFormRef.value.$el.getBoundingClientRect().left === defaultButtonSlot.value.$el.getBoundingClientRect().left) {
912
+ showBtnBoxLabel.value = ''
913
+ } else {
914
+ showBtnBoxLabel.value = '.'
915
+ }
916
+ }
917
+ }
918
+ const calcColumns = () => {
919
+ if (props.columns) {
920
+ newColumns.value = props.columns
921
+ return
922
+ }
923
+ if (!nfSearchFormRef.value) return
924
+ if (!renderSearchFormItemOptions.value.length) {
925
+ return
926
+ }
927
+ let count = 0
928
+ const formWidth = nfSearchFormRef.value.$el.offsetWidth
929
+ nfSearchFormRef.value.formItems.reduce((prev, cur) => {
930
+ // 每项的宽度加上marginRight:16
931
+ const ret = prev + cur.$el.offsetWidth + 16
932
+ if (ret > formWidth) return prev
933
+ count += 1
934
+ return ret
935
+ }, 0)
936
+ newColumns.value = count
937
+ }
938
+
939
+ const throttleCalcColumns = throttle(calcColumns, 100)
940
+
941
+ const onResize = async (params: Record<string, any> = {}) => {
942
+ const {
943
+ width
944
+ } = (params?.[0]?.contentRect) || {}
945
+ if (width && domWidth.value && width === domWidth.value) {
946
+ return
947
+ }
948
+ if (domWidth.value) {
949
+ await nextTick()
950
+ domWidth.value = topDom.value.offsetWidth
951
+ }
952
+ refreshLabel()
953
+ }
954
+
955
+ const observer = ref()
956
+ onMounted(async () => {
957
+ await nextTick()
958
+ observer.value = new ResizeObserver(onResize)
959
+ observer.value.observe(topDom.value, { box: 'border-box' })
960
+ })
961
+
962
+ onBeforeUnmount(() => {
963
+ observer.value?.unobserve(topDom.value)
964
+ })
965
+
966
+ /**
967
+ * 重置校验
968
+ */
969
+ const resetNfSearchFormModel = () => {
970
+ nfSearchFormRef.value?.resetFields()
971
+ emit('onResetFields')
972
+ }
973
+
974
+ /**
975
+ * 校验
976
+ * @param callback
977
+ */
978
+ const onNfSearchFormValidate = (callback) => {
979
+ nfSearchFormRef.value.validate(valid => {
980
+ if (valid) {
981
+ callback?.()
982
+ }
983
+ })
984
+ }
985
+ /**
986
+ * 搜索
987
+ */
988
+ const onNfSearchFormSearch = () => {
989
+ onNfSearchFormValidate(() => {
990
+ emit('onSearch', nfSearchFormModel.value)
991
+ })
992
+ }
993
+ /**
994
+ * 导出
995
+ */
996
+ const onNfSearchFormExport = () => {
997
+ onNfSearchFormValidate(() => {
998
+ emit('onExport', nfSearchFormModel.value)
999
+ })
1000
+ }
1001
+
1002
+ /**
1003
+ * 把该item暂时隐藏
1004
+ * @param prop
1005
+ */
1006
+ const hiddenNfSearchFormItemOptionField = async (prop) => {
1007
+ prop.formOptions._skip = true
1008
+ prop.formOptions.every(item => {
1009
+ if (item.prop === prop) {
1010
+ item.initHidden = true
1011
+ return false
1012
+ }
1013
+ return true
1014
+ })
1015
+ nextTick(() => {
1016
+ props.formOptions._skip = false
1017
+ })
1018
+ localShowItems.value = prop.formOptions.filter(item => !item.initHidden).map(item => item.prop)
1019
+ refreshNfSearchFormModel()
1020
+ await syncLocalShowColumnToCookies()
1021
+ await syncCookiesToSearchFormItemOptions()
1022
+ await nextTick()
1023
+ refreshLabel()
1024
+ }
1025
+
1026
+ const columnCheckConfirmFeedback = (value: Record<string, any>) => {
1027
+ if (!props.columnEdit) {
1028
+ const itemsList: string[] = []
1029
+ for (const key in value) {
1030
+ if (value[key]) {
1031
+ itemsList.push(key)
1032
+ }
1033
+ }
1034
+ setLocalShowItems(itemsList, true)
1035
+ }
1036
+ }
1037
+
1038
+ // 抽屉确定
1039
+ const columnCheckConfirm = async (cb, error) => {
1040
+ try {
1041
+ const value = columnCheckDrawer.value.syncTableColumnsToCookies()
1042
+ columnCheckConfirmFeedback(value)
1043
+ await syncCookiesToLocalShowColumn(true)
1044
+ await syncCookiesToSearchFormItemOptions()
1045
+ await nextTick()
1046
+ refreshLabel()
1047
+ nfSearchFormRef.value?.getFormItems()
1048
+ cb()
1049
+ } catch (e) {
1050
+ error()
1051
+ console.log(e)
1052
+ }
1053
+ }
1054
+
1055
+ async function clearDragCached() {
1056
+ await CachedManager.asyncCachedSet(`${props.storageKey}$drag-column-width`, null)
1057
+ }
1058
+
1059
+ async function clearColumnEditCached() {
1060
+ await CachedManager.asyncCachedSet(props.storageKey, null)
1061
+ }
1062
+
1063
+ /**
1064
+ * 保存表单显示的字段
1065
+ */
1066
+ const nfSearchFormItemOptionFieldsSave = async (value) => {
1067
+ columnCheckConfirmFeedback(value)
1068
+ await syncCookiesToLocalShowColumn(true)
1069
+ await syncCookiesToSearchFormItemOptions()
1070
+ await nextTick()
1071
+ refreshLabel()
1072
+ }
1073
+
1074
+ defineExpose({
1075
+ /**
1076
+ * 列编辑显示的列prop
1077
+ */
1078
+ localShowItems,
1079
+ /**
1080
+ * 列编辑同步组件到cookies
1081
+ */
1082
+ syncLocalShowColumnToCookies,
1083
+ /**
1084
+ * 列编辑同步cookies到组件localShowItems
1085
+ */
1086
+ syncCookiesToLocalShowColumn,
1087
+ /**
1088
+ * showItems同步props.formOptions[i].initHidden
1089
+ */
1090
+ syncShowItemToInitHidden,
1091
+ /**
1092
+ * 列编辑隐藏某个表单列目
1093
+ */
1094
+ hiddenNfSearchFormItemOptionField,
1095
+ /**
1096
+ * 列编辑操作后刷新表单数据模式
1097
+ */
1098
+ refreshNfSearchFormModel,
1099
+ /**
1100
+ * 渲染表单项数据
1101
+ */
1102
+ renderSearchFormItemOptions,
1103
+ /**
1104
+ * 重置
1105
+ */
1106
+ resetNfSearchFormModel,
1107
+ /**
1108
+ * 校验
1109
+ */
1110
+ onNfSearchFormValidate,
1111
+ /**
1112
+ * 搜索
1113
+ */
1114
+ onNfSearchFormSearch,
1115
+ /**
1116
+ * 导出
1117
+ */
1118
+ onNfSearchFormExport,
1119
+ /**
1120
+ * 表单数据
1121
+ */
1122
+ nfSearchFormModel,
1123
+ /**
1124
+ * 表单 ref
1125
+ */
1126
+ nfSearchFormRef,
1127
+ /**
1128
+ * 列编辑nf-drawer ref
1129
+ */
1130
+ columnCheckNfDrawer,
1131
+ /**
1132
+ * 列编辑column-check-dialog ref
1133
+ */
1134
+ columnCheckDialog,
1135
+ /**
1136
+ * 列编辑column-check-drawer ref
1137
+ */
1138
+ columnCheckDrawer,
1139
+ /**
1140
+ * 列编辑dialog/drawer显示/隐藏
1141
+ */
1142
+ configNfSearchItemVisibleDialog,
1143
+ /**
1144
+ * 列编辑同步cookies到组件渲染数据
1145
+ */
1146
+ syncCookiesToSearchFormItemOptions,
1147
+ onResize,
1148
+ visibleChange: refreshLabel,
1149
+ clearDragCached,
1150
+ clearColumnEditCached
1151
+ })
1152
+
1153
+ </script>