-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathviewSpec.ts
1195 lines (1158 loc) · 37.9 KB
/
viewSpec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import type { LocalizedString } from 'typesafe-i18n';
import { f } from '../../utils/functools';
import type { IR, RA, RR } from '../../utils/types';
import { filterArray, localized } from '../../utils/types';
import { formatDisjunction } from '../Atoms/Internationalization';
import type { LiteralField, Relationship } from '../DataModel/specifyField';
import type { SpecifyTable } from '../DataModel/specifyTable';
import { genericTables } from '../DataModel/tables';
import type { Tables } from '../DataModel/types';
import type { FormCondition } from '../FormParse';
import { paleoPluginTables } from '../FormPlugins/PaleoLocation';
import { toLargeSortConfig, toSmallSortConfig } from '../Molecules/Sorting';
import type { SpecToJson, Syncer } from '../Syncer';
import { pipe, syncer } from '../Syncer';
import { syncers } from '../Syncer/syncers';
import type { SimpleXmlNode } from '../Syncer/xmlToJson';
import { createSimpleXmlNode } from '../Syncer/xmlToJson';
import { createXmlSpec } from '../Syncer/xmlUtils';
import { relationshipIsToMany } from '../WbPlanView/mappingHelpers';
/* eslint-disable @typescript-eslint/no-magic-numbers */
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
export const formDefinitionSpec = (table: SpecifyTable | undefined) =>
createXmlSpec({
columnDefinitions: pipe(
syncers.xmlChildren('columnDef'),
syncers.map(syncers.object(columnDefinitionSpec()))
),
rowDefinition: pipe(
syncers.xmlChild('rowDef', 'optional'),
syncers.maybe(syncers.object(rowSizeDefinitionSpec()))
),
legacyBusinessRules: pipe(
/*
* This element is often present, but empty. Looking at sp6 code, there
* is no difference between it being emmpty and not being there at all
*/
syncers.xmlChild('enableRules', 'optional'),
syncers.maybe(syncers.object(legacyBusinessRulesSpec()))
),
definitions: definitions(table),
});
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const definitions = (table: SpecifyTable | undefined) =>
pipe(
syncers.xmlChildren('rows'),
syncers.fallback<RA<SimpleXmlNode>>(() => [createSimpleXmlNode()]),
syncers.map(syncers.object(rowsSpec(table)))
);
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const rowsSpec = (table: SpecifyTable | undefined) =>
createXmlSpec({
rows: pipe(
syncers.xmlChildren('row'),
syncers.map(
pipe(
syncers.xmlChildren('cell'),
syncers.map(
pipe(
preProcessProps,
syncers.object(cellSpec()),
syncers.switch(
'rest',
'definition',
pipe(
syncers.xmlAttribute('type', 'required'),
syncers.fallback(localized('field'))
),
{
label: 'Label',
field: 'Field',
separator: 'Separator',
subview: 'SubView',
panel: 'Panel',
command: 'Command',
iconview: 'IconView',
blank: 'Blank',
} as const,
{
Label: labelSpec,
Field: fieldSpec,
Separator: separatorSpec,
SubView: subViewSpec,
Panel: panelSpec,
Command: commandSpec,
IconView: iconViewSpec,
Blank: emptySpec,
Unknown: emptySpec,
} as const,
table
),
// Make sure command is on a correct table
syncers.change(
'definition',
(cell) => {
if (cell.definition.type !== 'Command')
return cell.definition;
const name =
cell.definition.name ??
(f.includes(
Object.keys(commandTables),
cell.definition.label
)
? cell.definition.label
: undefined);
if (name === undefined) return cell.definition;
const allowedTable = commandTables[name];
if (
allowedTable !== undefined &&
table !== undefined &&
allowedTable !== table.name
) {
console.error(
`Can't display ${
cell.definition.label ?? name ?? 'plugin'
} on the ${table.name} form. Instead, try ` +
`displaying it on the ${genericTables[allowedTable].label} form`
);
return { ...cell.definition, name: undefined };
}
return {
...cell.definition,
name,
};
},
({ definition }) => definition
)
)
)
)
)
),
condition: pipe(
syncers.xmlAttribute('condition', 'skip', false),
syncer<LocalizedString | undefined, FormCondition>(
(rawCondition) => {
if (rawCondition === undefined) return undefined;
const isAlways = rawCondition.trim() === 'always';
if (isAlways) return { type: 'Always' } as const;
const [rawField, ...condition] = rawCondition.split('=');
const field = syncers.field(table?.name).serializer(rawField);
return field === undefined
? undefined
: ({
type: 'Value',
field,
value: condition.join('='),
} as const);
},
(props) => {
if (props === undefined) return undefined;
if (props.type === 'Always') return localized('always');
const { field, value } = props;
const joined = syncers.field(table?.name).deserializer(field);
return joined === undefined || joined.length === 0
? undefined
: localized(`${joined}=${value}`);
}
)
),
columnDefinitions: pipe(
syncers.xmlAttribute('colDef', 'skip'),
syncers.default(localized('')),
syncers.split(',')
),
});
/**
* Split initialize="abc=def;ghi=jkl" into initialize abc="def" and initialize ghi="jkl"
* Using space in the prefix intentionally as space is not allowed in XML
* attributes, thus this won't cause a collision
*/
const attributeName = 'initialize';
const prefix = `${attributeName} `;
const preProcessProps = syncer<SimpleXmlNode, SimpleXmlNode>(
(node) => ({
...node,
attributes: {
...node.attributes,
...parseSpecifyProperties(node.attributes[attributeName], prefix),
},
}),
(node) => {
const entries = Object.entries(node.attributes);
const props = Object.fromEntries(
filterArray(
entries.map(([key, value]) =>
typeof value === 'string' &&
value.length > 0 &&
key.startsWith(prefix)
? [key.slice(prefix.length), value]
: undefined
)
)
);
const rest = Object.fromEntries(
entries.filter(([key]) => !key.startsWith(prefix))
);
return {
...node,
attributes: {
...rest,
[attributeName]: buildSpecifyProperties(props),
},
};
}
);
const columnDefinitionSpec = f.store(() =>
createXmlSpec({
// Commonly one of 'lnx', 'mac', 'exp'
os: syncers.xmlAttribute('os', 'skip'),
definition: syncers.xmlContent,
})
);
const rowSizeDefinitionSpec = f.store(() =>
createXmlSpec({
auto: pipe(
syncers.xmlAttribute('auto', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
cell: syncers.xmlAttribute('cell', 'skip'),
sep: syncers.xmlAttribute('sep', 'skip'),
definition: pipe(
syncers.xmlContent,
syncers.default(''),
syncers.split(',')
),
})
);
const legacyBusinessRulesSpec = f.store(() =>
createXmlSpec({
id: syncers.xmlAttribute('id', 'skip'),
rule: syncers.xmlContent,
})
);
const cellSpec = f.store(() =>
createXmlSpec({
id: syncers.xmlAttribute('id', 'skip'),
// Make cell occupy more than one column
colSpan: pipe(
syncers.xmlAttribute('colSpan', 'skip'),
syncers.maybe(syncers.toDecimal),
syncers.default<number>(1)
),
description: syncers.xmlAttribute('desc', 'skip'),
/*
* Specify 7 only
* When invisible, field is rendered with visibility="hidden"
*/
visible: pipe(
syncers.xmlAttribute('invisible', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false),
syncers.flip
),
title: syncers.xmlAttribute('initialize title', 'skip'),
// Default: right for labels, left for the rest
align: pipe(
syncers.xmlAttribute('initialize align', 'skip'),
syncers.maybe(syncers.enum(['left', 'right', 'center']))
),
// Specify 7 only
foregroundColor: syncers.xmlAttribute('initialize foregroundColor', 'skip'),
// Specify 7 only
foregroundColorDark: syncers.xmlAttribute(
'initialize foregroundColorDark',
'skip'
),
legacyForegroundColor: syncers.xmlAttribute('initialize fg', 'skip'),
legacyVisible: pipe(
syncers.xmlAttribute('initialize visible', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(true)
),
// In sp6, if true, disconnects the field from the database
legacyIgnore: pipe(
syncers.xmlAttribute('ignore', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
rest: syncers.captureLogContext(),
})
);
export const parseSpecifyProperties = (value = '', prefix = ''): IR<string> =>
Object.fromEntries(
filterArray(
value.split(';').map((part) => {
const [rawName, ...values] = part.split('=');
const name = rawName.toLowerCase().trim();
return name.length === 0
? undefined
: ([
`${prefix}${name}`,
values.join('=').trim().replaceAll('%3B', ';'),
] as const);
})
)
);
const buildSpecifyProperties = (properties: IR<string>): string =>
Object.entries(properties)
.filter(([key, value]) => key.length > 0 && value.length > 0)
.map(
([key, value]) => `${key.toLowerCase()}=${value.replaceAll(';', '%3B')}`
)
.join(';');
const labelSpec = f.store(() =>
createXmlSpec({
label: syncers.xmlAttribute('label', 'skip'),
labelForCellId: syncers.xmlAttribute('labelFor', 'skip'),
icon: syncers.xmlAttribute('icon', 'skip'),
legacyName: syncers.xmlAttribute('name', 'skip'),
})
);
const separatorSpec = f.store(() =>
createXmlSpec({
label: syncers.xmlAttribute('label', 'skip'),
icon: syncers.xmlAttribute('icon', 'skip'),
forClass: pipe(
syncers.xmlAttribute('forClass', 'skip'),
syncers.maybe(syncers.tableName)
),
canCollapse: pipe(
syncers.xmlAttribute('collapse', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
})
);
const borderSpec = f.store(() =>
createXmlSpec({
legacyBorderStyle: pipe(
syncers.xmlAttribute('initialize border', 'skip'),
syncers.maybe(
syncers.enum(['etched', 'lowered', 'raised', 'empty', 'line'])
)
),
// Used only if border style is line
legacyBorderColor: syncers.xmlAttribute('initialize borderColor', 'skip'),
legacyBackgroundColor: syncers.xmlAttribute('initialize bgColor', 'skip'),
})
);
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const subViewSpec = (
cell: SpecToJson<ReturnType<typeof cellSpec>>,
table: SpecifyTable | undefined
) =>
createXmlSpec({
name: pipe(
syncers.xmlAttribute('name', 'required'),
syncers.maybe(syncers.field(table?.name)),
syncer(
(fields: RA<LiteralField | Relationship> | undefined) => {
const field = fields?.at(-1);
if (field?.isRelationship === false) {
console.error('SubView can only be used to display a relationship');
return undefined;
}
if (field !== undefined && field.getReverse() === undefined) {
console.error(
`No reverse relationship exists${
relationshipIsToMany(field) ? '' : '. Use a querycbx instead'
}`
);
return undefined;
}
if (field?.type === 'many-to-many') {
// ResourceApi does not support .rget() on a many-to-many
console.warn('Many-to-many relationships are not supported');
}
return fields;
},
(field) => field
)
),
defaultType: pipe(
syncers.xmlAttribute('defaultType', 'skip'),
syncers.maybe(syncers.enum(['form', 'table', 'icon'] as const))
),
buttonLabel: syncers.xmlAttribute('label', 'skip'),
viewSetName: syncers.xmlAttribute('viewSetName', 'skip'),
viewName: syncers.xmlAttribute('viewName', 'skip'),
isReadOnly: pipe(
syncers.xmlAttribute('readOnly', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
legacyRows: pipe(
syncers.xmlAttribute('rows', 'skip'),
syncers.maybe(syncers.toDecimal),
syncers.default(5)
),
legacyValidationType: pipe(
syncers.xmlAttribute('valType', 'skip'),
syncers.maybe(syncers.enum(['Changed', 'Focus', 'None', 'OK'] as const)),
syncers.default('Changed')
),
displayAsButton: pipe(
syncers.xmlAttribute('initialize btn', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
icon: syncers.xmlAttribute('initialize icon', 'skip'),
legacyHelpContext: syncers.xmlAttribute('initialize hc', 'skip'),
// Specify 7 only
sortField: pipe(
syncers.xmlAttribute('initialize sortField', 'skip'),
syncers.maybe(
syncer(
(raw: string) => {
const cellName = cell.rest.node.attributes.name;
const cellRelationship =
typeof cellName === 'string'
? syncers.field(table?.name).serializer(cellName)?.at(-1)
: undefined;
const cellRelatedTableName =
cellRelationship?.isRelationship === true
? cellRelationship.relatedTable.name
: undefined;
const parsed = toLargeSortConfig(raw);
const fieldNames = syncers
.field(cellRelatedTableName)
.serializer(parsed.fieldNames.join('.'));
return fieldNames === undefined
? undefined
: {
...parsed,
fieldNames,
};
},
(parsed) =>
parsed === undefined
? ''
: toSmallSortConfig({
...parsed,
fieldNames: parsed.fieldNames.map(({ name }) => name),
})
)
)
),
legacyNoScrollBars: pipe(
syncers.xmlAttribute('initialize noScrollBars', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
legacyNoSeparator: pipe(
syncers.xmlAttribute('initialize noSep', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
legacyNoSeparatorMoreButton: pipe(
syncers.xmlAttribute('initialize noSepMoreBtn', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
legacyDisplayAsToMany: pipe(
syncers.xmlAttribute('initialize many', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
legacyCollapse: pipe(
syncers.xmlAttribute('initialize collapse', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
legacyAddSearch: pipe(
syncers.xmlAttribute('initialize addSearch', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
legacyAddAdd: pipe(
syncers.xmlAttribute('initialize addAdd', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
...borderSpec(),
});
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const panelSpec = (
_cell: SpecToJson<ReturnType<typeof cellSpec>>,
table: SpecifyTable | undefined
) =>
createXmlSpec({
columnDefinitions: pipe(
syncers.xmlAttribute('colDef', 'skip'),
syncers.default(localized('')),
syncers.split(',')
),
rowDefinitions: pipe(
syncers.xmlAttribute('rowDef', 'skip'),
syncers.default(localized('')),
syncers.split(',')
),
/*
* This is recognized by sp6 code, but never actually used to influence
* anything.
* The original sp6 docs mention that if set to "buttonbar", the elements
* would be centered.
*/
panelType: syncers.xmlAttribute('initialize panelType', 'skip'),
// Used by sp6 to control panels (using hardcoded java logic)
legacyName: syncers.xmlAttribute('name', 'skip'),
...borderSpec(),
rows: veryUnsafeRows(table),
});
/**
* This is not a correct TypeScript type, but it is necessary as TypeScript
* is not yet able to infer the type of complex circular structures like
* this.
* See https://github.com/microsoft/TypeScript/issues/45213
*/
const veryUnsafeRows = (
table: SpecifyTable | undefined
): Syncer<SimpleXmlNode, SimpleXmlNode> =>
definitions(table) as unknown as Syncer<SimpleXmlNode, SimpleXmlNode>;
const commandTables = {
generateLabelBtn: undefined,
ShowLoansBtn: 'Preparation',
ReturnLoan: 'Loan',
} as const;
const commandSpec = f.store(() =>
createXmlSpec({
name: pipe(
syncers.xmlAttribute('name', 'required'),
syncers.maybe(syncers.enum(Object.keys(commandTables)))
),
legacyCommandType: pipe(
syncers.xmlAttribute('commandType', 'skip'),
syncers.maybe(
syncers.enum(['Interactions', 'App', 'ClearCache'] as const)
)
),
legacyAction: pipe(
syncers.xmlAttribute('action', 'skip'),
syncers.maybe(syncers.enum(['ReturnLoan'] as const))
),
label: pipe(
syncers.xmlAttribute('label', 'skip'),
syncers.maybe(
// A migration for https://github.com/specify/specify6/issues/203
syncer(
(label) =>
label === 'SHOW_LOANS' ? localized('SHOW_INTERACTIONS') : label,
f.id
)
)
),
legacyIsDefault: pipe(
syncers.xmlAttribute('default', 'skip'),
syncers.maybe(syncers.toBoolean)
),
})
);
const iconViewSpec = f.store(() =>
createXmlSpec({
name: syncers.xmlAttribute('name', 'skip'),
viewSetName: syncers.xmlAttribute('viewSetName', 'skip'),
viewName: syncers.xmlAttribute('viewName', 'skip'),
legacyNoSeparator: pipe(
syncers.xmlAttribute('initialize noSep', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
legacyNoMoreButton: pipe(
syncers.xmlAttribute('initialize noSepMoreBtn', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
})
);
// FIXME: when changing cell type, remove attributes
const emptySpec = f.store(() => createXmlSpec({}));
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const fieldSpec = (
cell: SpecToJson<ReturnType<typeof cellSpec>>,
table: SpecifyTable | undefined
) =>
createXmlSpec({
field: pipe(
syncers.object(rawFieldSpec(table)),
syncers.switch(
'rest',
'definition',
pipe(
syncers.xmlAttribute('uiType', 'required'),
syncers.fallback(localized('text'))
),
{
combobox: 'ComboBox',
text: 'Text',
formattedtext: 'Text',
dsptextfield: 'Text',
label: 'Text',
textfieldinfo: 'Text',
textarea: 'TextArea',
// FEATURE: allow switching between these text and textarea types in the UI
textareabrief: 'TextArea',
plugin: 'Plugin',
querycbx: 'QueryComboBox',
checkbox: 'CheckBox',
tristate: 'TriState',
spinner: 'Spinner',
list: 'List',
url: 'Url',
image: 'Image',
browse: 'Browse',
colorchooser: 'ColorChooser',
} as const,
{
ComboBox: comboBoxSpec,
Text: textSpec,
TextArea: textAreaSpec,
Plugin: pluginWrapperSpec,
QueryComboBox: queryComboBoxSpec,
CheckBox: checkBoxSpec,
// TEST: figure out how this works in sp6
TriState: checkBoxSpec,
Spinner: spinnerSpec,
List: listSpec,
Image: imageSpec,
Url: emptySpec,
Browse: browseSpec,
ColorChooser: emptySpec,
Unknown: emptySpec,
} as const,
{ cell, table }
),
syncers.change(
'isReadOnly',
(cell) =>
cell.isReadOnly ||
cell.definition.rawType === 'dsptextfield' ||
cell.definition.rawType === 'label',
({ isReadOnly }) => isReadOnly
)
),
});
const specialFieldNames = new Set([
// Occurs in uiType="plugin"
'this',
// Occurs in uiType="checkbox"
'generateLabelChk',
// Occurs in uiType="checkbox"
'generateInvoice',
// Occurs in uiType="checkbox"
'sendEMail',
]);
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const rawFieldSpec = (table: SpecifyTable | undefined) =>
createXmlSpec({
field: pipe(
syncers.xmlAttribute('name', 'skip'),
syncers.preserveInvalid(
syncer((name) => {
const parsed = syncers.field(table?.name, 'silent').serializer(name);
if (
parsed === undefined &&
name !== undefined &&
!specialFieldNames.has(name)
)
console.error(`Unknown field name: ${name}`);
return parsed;
}, syncers.field(table?.name, 'strict').deserializer)
)
),
legacyEditOnCreate: pipe(
syncers.xmlAttribute('initialize editOnCreate', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
isRequired: pipe(
syncers.xmlAttribute('required', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
isReadOnly: pipe(
syncers.xmlAttribute('readonly', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
legacyValidationType: pipe(
syncers.xmlAttribute('valType', 'skip'),
syncers.maybe(syncers.enum(['Changed', 'Focus'] as const)),
syncers.default('Changed')
),
defaultValue: syncers.xmlAttribute('default', 'skip'),
// Example: '%s'
legacyFormat: syncers.xmlAttribute('format', 'skip', false),
// Example: CollectingEventDetail
dataObjectFormatter: syncers.xmlAttribute('formatName', 'skip'),
// Example: CatalogNumberNumeric
uiFieldFormatter: syncers.xmlAttribute('uiFieldFormatter', 'skip'),
rest: syncers.captureLogContext(),
});
const comboBoxSpec = f.store(() =>
createXmlSpec({
pickListName: syncers.xmlAttribute('pickList', 'skip'),
// FEATURE: go over all attributes to see what sp7 should start supporting
legacyData: pipe(
syncers.xmlAttribute('initialize data', 'skip', false),
syncers.maybe(syncers.fancySplit(','))
),
})
);
const textSpec = f.store(() =>
createXmlSpec({
minLength: pipe(
syncers.xmlAttribute('initialize minLength', 'skip'),
syncers.maybe(syncers.toDecimal)
),
maxLength: pipe(
syncers.xmlAttribute('initialize maxLength', 'skip'),
syncers.maybe(syncers.toDecimal)
),
isPassword: pipe(
syncers.xmlAttribute('initialize isPassword', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
legacyColumns: pipe(
syncers.xmlAttribute('cols', 'skip'),
syncers.maybe(syncers.toDecimal),
syncers.default(10)
),
// Allow editing even the auto numbered part of the formatted field
legacyAllEdit: pipe(
syncers.xmlAttribute('initialize allEdit', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
// Allow entering only a part of the formatted field be typed in (used for search forms)
legacyIsPartial: pipe(
syncers.xmlAttribute('initialize isPartial', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
// Only displayed in sp6 for uiType="textfieldinfo"
legacyDisplayDialog: syncers.xmlAttribute('initialize displayDlg', 'skip'),
// Only used in sp6 for uiType="dsptextfield" and uiType="formattedtext"
legacyTransparent: pipe(
syncers.xmlAttribute('initialize transparent', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
/*
* Used only if uiType="formattedtext". For Catalog Number field.
* This is either for series data entry, or for displaying catalog number
* field as separate inputs (one for each part of the formatter)
*/
legacyIsSeries: pipe(
syncers.xmlAttribute('initialize series', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
/*
* Assume that the value received from the user is already formatted and
* UI formatter does not need to be called. True by default only for numeric
* catalog number formatter, for others, can be overwritten using this prop
*/
legacyAssumeFormatted: pipe(
syncers.xmlAttribute('initialize fromUiFmt', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
})
);
// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
const textAreaSpec = (
_field: SpecToJson<ReturnType<typeof rawFieldSpec>>,
_: unknown,
rawType: string
) =>
createXmlSpec({
rows: pipe(
syncers.xmlAttribute('rows', 'skip'),
syncers.maybe(syncers.toDecimal),
syncers.default(rawType === 'textareabrief' ? 1 : 4)
),
legacyColumns: pipe(
syncers.xmlAttribute('cols', 'skip'),
syncers.maybe(syncers.toDecimal),
syncers.default(10)
),
});
const queryComboBoxSpec = (
_spec: SpecToJson<ReturnType<typeof rawFieldSpec>>,
{
table,
}: {
readonly table: SpecifyTable | undefined;
}
) =>
createXmlSpec({
field: pipe(
rawFieldSpec(table).field,
syncer(
({ parsed, ...rest }) => {
if (
parsed?.some(
(field) => field.isRelationship && relationshipIsToMany(field)
)
)
console.error(
'Unable to render a to-many relationship as a querycbx. Use a Subview instead'
);
return { parsed, ...rest };
},
(value) => value
)
),
// Customize view name
dialogViewName: syncers.xmlAttribute('initialize displayDlg', 'skip'),
searchDialogViewName: syncers.xmlAttribute('initialize searchDlg', 'skip'),
showSearchButton: pipe(
syncers.xmlAttribute('initialize searchBtn', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(true)
),
showCloneButton: pipe(
syncers.xmlAttribute('initialize cloneBtn', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
showViewButton: pipe(
syncers.xmlAttribute('initialize viewBtn', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
showEditButton: pipe(
syncers.xmlAttribute('initialize editBtn', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(true)
),
showNewButton: pipe(
syncers.xmlAttribute('initialize newBtn', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(true)
),
legacyHelpContext: syncers.xmlAttribute('initialize hc', 'skip'),
// Make query compatible with multiple ORMs
legacyAdjustQuery: pipe(
syncers.xmlAttribute('initialize adjustQuery', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(true)
),
});
const checkBoxSpec = f.store(() =>
createXmlSpec({
label: syncers.xmlAttribute('label', 'skip'),
// TEST: figure out how this works in sp6
legacyIsEditable: pipe(
syncers.xmlAttribute('initialize editable', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
})
);
const spinnerSpec = f.store(() =>
createXmlSpec({
min: pipe(
syncers.xmlAttribute('initialize min', 'skip'),
syncers.maybe(syncers.toFloat)
),
max: pipe(
syncers.xmlAttribute('initialize max', 'skip'),
syncers.maybe(syncers.toFloat)
),
// Specify 7 only
step: pipe(
syncers.xmlAttribute('initialize step', 'skip'),
syncers.maybe(syncers.toFloat)
),
})
);
const listSpec = f.store(() =>
createXmlSpec({
legacyDisplayType: pipe(
syncers.xmlAttribute('dsptype', 'skip'),
syncers.default(localized('list'))
),
legacyRows: pipe(
syncers.xmlAttribute('rows', 'skip'),
syncers.maybe(syncers.toDecimal),
syncers.default(15)
),
legacyData: pipe(
syncers.xmlAttribute('data', 'skip', false),
syncers.maybe(syncers.fancySplit(','))
),
})
);
const imageSpec = f.store(() =>
createXmlSpec({
size: pipe(
syncers.xmlAttribute('initialize size', 'skip'),
// Format: width,height in px
syncers.default(localized('150,150')),
syncers.split(','),
syncers.map(syncers.toDecimal)
),
/*
* Whether to display the image. By default the image is only
* displayed when in edit mode
*/
legacyIsVisible: pipe(
syncers.xmlAttribute('initialize edit', 'skip'),
syncers.maybe(syncers.toBoolean)
),
legacyHasBorder: pipe(
syncers.xmlAttribute('initialize border', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(true)
),
legacyUrl: syncers.xmlAttribute('initialize url', 'skip'),
legacyIcon: syncers.xmlAttribute('initialize icon', 'skip'),
legacyIconSize: pipe(
syncers.xmlAttribute('initialize iconSize', 'skip'),
syncers.maybe(syncers.toDecimal),
syncers.maybe(syncers.numericEnum([16, 24, 32] as const))
),
})
);
const browseSpec = f.store(() =>
createXmlSpec({
legacyDirectoriesOnly: pipe(
syncers.xmlAttribute('initialize dirsonly', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(false)
),
legacyUseAlternateFilePicker: pipe(
syncers.xmlAttribute('initialize forInput', 'skip'),
syncers.maybe(syncers.toBoolean),
syncers.default<boolean>(true)
),
/*
* Extension to filter files by. Example "jpg". Only one is supported
* Fed into:
* https://docs.oracle.com/javase/7/docs/api/javax/swing/filechooser/FileNameExtensionFilter.html
*/
legacyFilter: syncers.xmlAttribute('initialize fileFilter', 'skip'),
/*
* Human friendly description of what files are allowed
*/
legacyFilterDescription: syncers.xmlAttribute(
'initialize fileFilterDesc',
'skip'
),
/*
* Example "jpeg". Adds that extension to picked file name if not already
* present
*/
legacyDefaultExtension: syncers.xmlAttribute(
'initialize defaultExtension',
'skip'
),
})
);
const pluginWrapperSpec = (
field: SpecToJson<ReturnType<typeof rawFieldSpec>>,
{
cell,
table,
}: {
readonly cell: SpecToJson<ReturnType<typeof cellSpec>>;
readonly table: SpecifyTable | undefined;
}
) =>
createXmlSpec({