@ditojs/admin 2.2.11 → 2.2.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.
@@ -4,6 +4,7 @@ import TypeMixin from '../mixins/TypeMixin.js'
4
4
  import { getUid } from './uid.js'
5
5
  import { SchemaGraph } from './SchemaGraph.js'
6
6
  import { appendDataPath, isTemporaryId } from './data.js'
7
+ import { isMatchingType, convertType } from './type.js'
7
8
  import {
8
9
  isObject,
9
10
  isString,
@@ -11,10 +12,12 @@ import {
11
12
  isFunction,
12
13
  isPromise,
13
14
  isModule,
15
+ asArray,
14
16
  clone,
15
17
  merge,
16
18
  camelize,
17
- mapConcurrently
19
+ mapConcurrently,
20
+ getValueAtDataPath
18
21
  } from '@ditojs/utils'
19
22
  import { markRaw } from 'vue'
20
23
 
@@ -42,7 +45,7 @@ export function iterateSchemaComponents(schemas, callback) {
42
45
  if (res !== undefined) {
43
46
  return res
44
47
  }
45
- } else {
48
+ } else if (isSchema(schema)) {
46
49
  for (const [name, component] of Object.entries(schema.components || {})) {
47
50
  const res = callback(component, name, 1)
48
51
  if (res !== undefined) {
@@ -59,33 +62,37 @@ export function iterateNestedSchemaComponents(schema, callback) {
59
62
  : undefined
60
63
  }
61
64
 
62
- export function findSchemaComponent(schema, callback) {
65
+ export function findNestedSchemaComponent(schema, callback) {
63
66
  return (
64
67
  iterateNestedSchemaComponents(
65
68
  schema,
66
69
  component => (callback(component) ? component : undefined)
67
- ) || null
70
+ ) ?? null
68
71
  )
69
72
  }
70
73
 
71
- export function someSchemaComponent(schema, callback) {
74
+ export function someNestedSchemaComponent(schema, callback) {
72
75
  return (
73
76
  iterateNestedSchemaComponents(
74
77
  schema,
75
78
  component => (callback(component) ? true : undefined)
76
- ) === true
79
+ ) ?? false
77
80
  )
78
81
  }
79
82
 
80
- export function everySchemaComponent(schema, callback) {
83
+ export function everyNestedSchemaComponent(schema, callback) {
81
84
  return (
82
85
  iterateNestedSchemaComponents(
83
86
  schema,
84
87
  component => (callback(component) ? undefined : false)
85
- ) !== false
88
+ ) ?? true
86
89
  )
87
90
  }
88
91
 
92
+ export function hasNestedSchemaComponents(schema) {
93
+ return someNestedSchemaComponent(schema, () => true) ?? false
94
+ }
95
+
89
96
  export function isSchema(schema) {
90
97
  return isObject(schema) && isString(schema.type)
91
98
  }
@@ -107,7 +114,7 @@ export function isPanel(schema) {
107
114
  }
108
115
 
109
116
  export function getSchemaIdentifier(schema) {
110
- return schema.name || schema.label || schema.type
117
+ return JSON.stringify(schema)
111
118
  }
112
119
 
113
120
  export async function resolveSchema(schema, unwrapModule = false) {
@@ -309,11 +316,12 @@ export async function processForms(api, schema, level) {
309
316
  )
310
317
  } else if (form) {
311
318
  form = schema.form = await processForm(api, form)
312
- } else if (components) {
319
+ } else if (isObject(components)) {
313
320
  // NOTE: Processing forms in computed components is not supported, since it
314
321
  // only can be computed in conjunction with actual data.
315
- if (isObject(components)) {
316
- form = { components }
322
+ form = {
323
+ type: 'form',
324
+ components
317
325
  }
318
326
  }
319
327
 
@@ -398,7 +406,7 @@ export function getViewFormSchema(schema, context) {
398
406
  return viewSchema
399
407
  ? // NOTE: Views can have tabs, in which case the view component is nested
400
408
  // in one of the tabs, go find it.
401
- findSchemaComponent(viewSchema, hasFormSchema) || null
409
+ findNestedSchemaComponent(viewSchema, hasFormSchema) || null
402
410
  : null
403
411
  }
404
412
 
@@ -478,6 +486,63 @@ export function keepAligned(schema) {
478
486
  return !!getTypeOptions(schema)?.keepAligned
479
487
  }
480
488
 
489
+ export function getSchemaValue(
490
+ keyOrDataPath,
491
+ { type, schema, callback = true, default: def, context } = {}
492
+ ) {
493
+ const types = type && asArray(type)
494
+ // For performance reasons, data-paths in `keyOrDataPath` can only be
495
+ // provided in in array format here:
496
+ let value = schema
497
+ ? isArray(keyOrDataPath)
498
+ ? getValueAtDataPath(schema, keyOrDataPath, () => undefined)
499
+ : schema[keyOrDataPath]
500
+ : undefined
501
+
502
+ if (value === undefined && def !== undefined) {
503
+ if (callback && isFunction(def) && !isMatchingType(types, def)) {
504
+ // Support `default()` functions for any type except `Function`:
505
+ def = def(context)
506
+ }
507
+ return def
508
+ }
509
+
510
+ if (isMatchingType(types, value)) {
511
+ return value
512
+ }
513
+ // Any schema value handled through `getSchemaValue()` can provide
514
+ // a function that's resolved when the value is evaluated:
515
+ if (callback && isFunction(value)) {
516
+ value = value(context)
517
+ }
518
+ // Now finally see if we can convert to the expect types.
519
+ if (types && value != null && !isMatchingType(types, value)) {
520
+ for (const type of types) {
521
+ const converted = convertType(type, value)
522
+ if (converted !== value) {
523
+ return converted
524
+ }
525
+ }
526
+ }
527
+ return value
528
+ }
529
+
530
+ export function shouldRenderSchema(schema, context) {
531
+ return (
532
+ getSchemaValue('if', {
533
+ type: Boolean,
534
+ schema,
535
+ context,
536
+ default: true
537
+ }) && (
538
+ !hasNestedSchemaComponents(schema) ||
539
+ someNestedSchemaComponent(schema, component =>
540
+ shouldRenderSchema(component, context)
541
+ )
542
+ )
543
+ )
544
+ }
545
+
481
546
  export function getDefaultValue(schema) {
482
547
  // Support default values both on schema and on component level.
483
548
  // NOTE: At the time of creation, components may not be instantiated, (e.g. if
@@ -542,8 +607,8 @@ export function computeValue(schema, data, name, dataPath, {
542
607
  })
543
608
  )
544
609
  if (value !== undefined) {
545
- // Access `data[name]` directly instead of `this.value = …` to update the
546
- // value without calling parse():
610
+ // Access `data[name]` directly to update the value without calling
611
+ // parse():
547
612
  data[name] = value
548
613
  }
549
614
  }