-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathview-config-utils.ts
More file actions
369 lines (330 loc) · 14.1 KB
/
Copy pathview-config-utils.ts
File metadata and controls
369 lines (330 loc) · 14.1 KB
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
/**
* ViewConfigPanel — Shared Utilities
*
* Extracted from ViewConfigPanel to enable reuse across the
* schema-driven config panel framework.
*/
import type { FilterGroup, SortItem } from '@object-ui/components';
// ---------------------------------------------------------------------------
// Operator mapping: @objectstack/spec ↔ FilterBuilder
// ---------------------------------------------------------------------------
export const SPEC_TO_BUILDER_OP: Record<string, string> = {
'=': 'equals',
'==': 'equals',
'!=': 'notEquals',
'<>': 'notEquals',
'>': 'greaterThan',
'<': 'lessThan',
'>=': 'greaterOrEqual',
'<=': 'lessOrEqual',
'contains': 'contains',
'not_contains': 'notContains',
'is_empty': 'isEmpty',
'is_not_empty': 'isNotEmpty',
'in': 'in',
'not_in': 'notIn',
'not in': 'notIn',
'before': 'before',
'after': 'after',
'between': 'between',
// Pass-through for already-normalized IDs
'equals': 'equals',
'notEquals': 'notEquals',
'greaterThan': 'greaterThan',
'lessThan': 'lessThan',
'greaterOrEqual': 'greaterOrEqual',
'lessOrEqual': 'lessOrEqual',
'notContains': 'notContains',
'isEmpty': 'isEmpty',
'isNotEmpty': 'isNotEmpty',
'notIn': 'notIn',
};
export const BUILDER_TO_SPEC_OP: Record<string, string> = {
'equals': '=',
'notEquals': '!=',
'greaterThan': '>',
'lessThan': '<',
'greaterOrEqual': '>=',
'lessOrEqual': '<=',
'contains': 'contains',
'notContains': 'not_contains',
'isEmpty': 'is_empty',
'isNotEmpty': 'is_not_empty',
'in': 'in',
'notIn': 'not in',
'before': 'before',
'after': 'after',
'between': 'between',
};
// ---------------------------------------------------------------------------
// Field type normalization: ObjectUI → FilterBuilder
// ---------------------------------------------------------------------------
/**
* Normalize raw field types to the 5 categories supported by FilterBuilder/SortBuilder.
* Lookup-like types (lookup, master_detail, user, owner) map to 'select' because
* FilterBuilder handles them identically when options are provided — the distinction
* between select and lookup operators is handled within FilterBuilder itself via the
* original field.type passed through ListView's filterFields.
*/
export function normalizeFieldType(rawType?: string): 'text' | 'number' | 'boolean' | 'date' | 'select' {
const t = (rawType || '').toLowerCase();
if (['integer', 'int', 'float', 'double', 'number', 'currency', 'money', 'percent', 'rating'].includes(t)) return 'number';
if (['date', 'datetime', 'datetime_tz', 'timestamp', 'time'].includes(t)) return 'date';
if (['boolean', 'bool', 'checkbox', 'switch'].includes(t)) return 'boolean';
if (['select', 'picklist', 'single_select', 'multi_select', 'enum', 'status', 'lookup', 'master_detail', 'user', 'owner'].includes(t)) return 'select';
return 'text';
}
// ---------------------------------------------------------------------------
// Spec-style filter bridge
// ---------------------------------------------------------------------------
function parseTriplet(arr: any[]): { id: string; field: string; operator: string; value: any } | null {
if (!Array.isArray(arr) || arr.length < 2) return null;
const [field, op, value] = arr;
if (typeof field !== 'string' || typeof op !== 'string') return null;
return {
id: crypto.randomUUID(),
field,
operator: SPEC_TO_BUILDER_OP[op] || op,
value: value ?? '',
};
}
function parseSingleOrNested(item: any): Array<{ id: string; field: string; operator: string; value: any }> {
if (Array.isArray(item)) {
const triplet = parseTriplet(item);
return triplet ? [triplet] : [];
}
if (typeof item === 'object' && item !== null && item.field) {
return [{
id: item.id || crypto.randomUUID(),
field: item.field,
operator: SPEC_TO_BUILDER_OP[item.operator] || item.operator || 'equals',
value: item.value ?? '',
}];
}
return [];
}
export function parseSpecFilter(raw: any): { logic: 'and' | 'or'; conditions: Array<{ id: string; field: string; operator: string; value: any }> } {
if (!Array.isArray(raw) || raw.length === 0) {
return { logic: 'and', conditions: [] };
}
// Detect ['and', ...conditions] or ['or', ...conditions]
if (typeof raw[0] === 'string' && (raw[0] === 'and' || raw[0] === 'or')) {
const logic = raw[0] as 'and' | 'or';
const rest = raw.slice(1);
const conditions = rest.flatMap((item: any) => parseSingleOrNested(item));
return { logic, conditions };
}
// Detect single triplet: ['field', '=', value] (all primitives at top level)
if (raw.length >= 2 && raw.length <= 3 && typeof raw[0] === 'string' && typeof raw[1] === 'string' && !Array.isArray(raw[0])) {
// Check it's not an array of arrays
if (!Array.isArray(raw[2])) {
const cond = parseTriplet(raw);
return { logic: 'and', conditions: cond ? [cond] : [] };
}
}
// Detect array of conditions: [[...], [...]] or [{...}, {...}]
if (Array.isArray(raw[0]) || (typeof raw[0] === 'object' && raw[0] !== null && !Array.isArray(raw[0]))) {
const conditions = raw.flatMap((item: any) => parseSingleOrNested(item));
return { logic: 'and', conditions };
}
// Fallback: try as single triplet
const cond = parseTriplet(raw);
return { logic: 'and', conditions: cond ? [cond] : [] };
}
/**
* Convert FilterGroup conditions back to spec-style filter array.
*/
export function toSpecFilter(logic: 'and' | 'or', conditions: Array<{ field: string; operator: string; value: any }>): any[] {
const triplets = conditions
.filter(c => c.field) // skip empty
.map(c => [c.field, BUILDER_TO_SPEC_OP[c.operator] || c.operator, c.value]);
if (triplets.length === 0) return [];
if (triplets.length === 1 && logic === 'and') return triplets[0];
if (logic === 'or') return ['or', ...triplets];
return triplets;
}
// ---------------------------------------------------------------------------
// Parse helpers
// ---------------------------------------------------------------------------
/** Parse comma-separated string to trimmed non-empty string array */
export function parseCommaSeparated(input: string): string[] {
return input.split(',').map(s => s.trim()).filter(Boolean);
}
/** Parse comma-separated string to positive number array */
export function parseNumberList(input: string): number[] {
return input.split(',').map(s => Number(s.trim())).filter(n => !isNaN(n) && n > 0);
}
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
/** View type labels for display */
export const VIEW_TYPE_LABELS: Record<string, string> = {
grid: 'Grid',
kanban: 'Kanban',
calendar: 'Calendar',
gallery: 'Gallery',
timeline: 'Timeline',
gantt: 'Gantt',
map: 'Map',
chart: 'Chart',
};
/** All available view type keys */
export const VIEW_TYPE_OPTIONS = Object.keys(VIEW_TYPE_LABELS);
/**
* Row height options with Tailwind gap classes for visual icons.
* Aligned with @objectstack/spec RowHeight enum — all 5 values.
*/
export const ROW_HEIGHT_OPTIONS: Array<{ value: string; gapClass: string }> = [
{ value: 'compact', gapClass: 'gap-0' },
{ value: 'short', gapClass: 'gap-px' },
{ value: 'medium', gapClass: 'gap-0.5' },
{ value: 'tall', gapClass: 'gap-1' },
{ value: 'extra_tall', gapClass: 'gap-1.5' },
];
// ---------------------------------------------------------------------------
// Field options derivation
// ---------------------------------------------------------------------------
export interface FieldOption {
value: string;
label: string;
type: 'text' | 'number' | 'boolean' | 'date' | 'select';
options?: any[];
/** Raw (unnormalized) field type from objectDef. Used by view-type
* predicates (isImageLikeField, isGeoLikeField) that need finer signals
* than the 5-bucket `type`. Optional for backward-compat. */
rawType?: string;
/** Raw field name (same as `value` in current callers but kept distinct
* so future callers can carry through display-name heuristics without
* affecting persistence keys). */
rawName?: string;
}
/** Derive field options from an objectDef for FilterBuilder/SortBuilder/Selects */
export function deriveFieldOptions(objectDef: { fields?: Record<string, any> }): FieldOption[] {
if (!objectDef.fields) return [];
return Object.entries(objectDef.fields)
.filter(([key, field]: [string, any]) => {
if (key.startsWith('_')) return false;
if (field?.hidden) return false;
return true;
})
.map(([key, field]: [string, any]) => ({
value: key,
label: field.label || key,
type: normalizeFieldType(field.type),
options: field.options,
rawType: field.type,
rawName: key,
}));
}
/** Convert draft filter → FilterGroup for FilterBuilder */
export function toFilterGroup(draftFilter: any): FilterGroup {
const parsed = parseSpecFilter(draftFilter);
return { id: 'root', logic: parsed.logic, conditions: parsed.conditions };
}
/** Convert draft sort → SortItem[] for SortBuilder */
export function toSortItems(draftSort: any): SortItem[] {
return (Array.isArray(draftSort) ? draftSort : []).map((s: any) => ({
id: s.id || crypto.randomUUID(),
field: s.field || '',
order: (s.order || s.direction || 'asc') as 'asc' | 'desc',
}));
}
// ---------------------------------------------------------------------------
// Field-role detection
// ---------------------------------------------------------------------------
//
// These predicates classify object fields by their suitability for specific
// view roles (kanban group-by, gallery image, map latitude, etc.). They are
// used by CreateViewDialog to filter eligible fields and to compute whether a
// view type is at all available for the current object.
//
// Each predicate accepts the *raw* objectDef field (unnormalized) so that we
// can match on richer signals (semantic field type, name conventions) than
// the 5-bucket normalized type.
const IMAGE_FIELD_TYPES = new Set([
'image', 'image_url', 'photo', 'picture', 'avatar',
'file', 'attachment', 'attachments', 'media',
'url', 'link', // URL fields commonly hold image links in CRM datasets
]);
const IMAGE_FIELD_NAME_HINTS = [
'image', 'photo', 'picture', 'avatar', 'thumbnail', 'thumb',
'logo', 'cover', 'banner', 'icon', 'attachment',
];
/**
* True when the field is plausibly an image source for a gallery view.
* Matches on field type (image/file/url-like) OR field name (avatar/photo/…).
*
* Accepts either a raw object-definition field (`{ type, name }`) or a derived
* `FieldOption` (`{ rawType, rawName, value }`).
*/
export function isImageLikeField(
field: { type?: string; name?: string; key?: string; rawType?: string; rawName?: string; value?: string } | undefined | null,
): boolean {
if (!field) return false;
const type = (field.rawType || field.type || '').toLowerCase();
if (IMAGE_FIELD_TYPES.has(type)) return true;
const name = (field.rawName || field.name || field.key || field.value || '').toLowerCase();
if (!name) return false;
return IMAGE_FIELD_NAME_HINTS.some((hint) => name.includes(hint));
}
const GEO_FIELD_TYPES = new Set([
'geolocation', 'geo', 'geo_point', 'geopoint', 'location', 'latlng', 'lnglat',
]);
/**
* True when the field is plausibly the latitude OR longitude component of a
* geo coordinate. `axis` selects which one.
*
* Matches on:
* - dedicated geo types (always considered both axes)
* - field name conventions (`lat`, `latitude`, `lng`, `lon`, `longitude`)
*
* Accepts either a raw object-definition field or a derived `FieldOption`.
*/
export function isGeoLikeField(
field: { type?: string; name?: string; key?: string; rawType?: string; rawName?: string; value?: string } | undefined | null,
axis: 'latitude' | 'longitude',
): boolean {
if (!field) return false;
const type = (field.rawType || field.type || '').toLowerCase();
if (GEO_FIELD_TYPES.has(type)) return true;
const name = (field.rawName || field.name || field.key || field.value || '').toLowerCase();
if (!name) return false;
if (axis === 'latitude') {
return /(^|[_-])lat([^a-z]|itude)?($|[_-])/.test(name) || name === 'lat';
}
return /(^|[_-])(lng|lon|long)([^a-z]|gitude)?($|[_-])/.test(name)
|| name === 'lng' || name === 'lon';
}
/**
* Pick the first option whose field name matches one of the preferred
* lowercase substrings. Falls back to the first option, or undefined when
* the list is empty. Used for "smart default" auto-pick in CreateViewDialog.
*/
export function pickPreferredField(
options: Array<{ value: string; label?: string }>,
preferredNames: readonly string[],
): string | undefined {
if (options.length === 0) return undefined;
for (const pref of preferredNames) {
const found = options.find((o) => o.value.toLowerCase().includes(pref));
if (found) return found.value;
}
return options[0]?.value;
}
/** Common preferred names for kanban grouping (status-like fields). */
export const KANBAN_GROUP_PREFERRED: readonly string[] = [
'status', 'stage', 'state', 'priority', 'category', 'type',
];
/** Common preferred names for primary date fields. */
export const PRIMARY_DATE_PREFERRED: readonly string[] = [
'start_date', 'startdate', 'due_date', 'duedate',
'event_date', 'date', 'created_at', 'createdat', 'created',
];
/** Common preferred names for end date fields (gantt). */
export const END_DATE_PREFERRED: readonly string[] = [
'end_date', 'enddate', 'finish_date', 'completion_date', 'closed_at',
];
/** Common preferred names for human-readable title fields. */
export const TITLE_PREFERRED: readonly string[] = [
'name', 'title', 'subject', 'label', 'full_name', 'display_name',
];