Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F22407071
collection-field.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Flag For Later
Award Token
Authored By
Unknown
Size
4 KB
Referenced Files
None
Subscribers
None
collection-field.ts
View Options
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-explicit-any */
import
{
predicates
}
from
"@sealcode/ts-predicates"
;
import
{
Context
}
from
"koa"
;
import
{
CollectionItem
,
Depromisify
,
ExtractFieldDecoded
,
ExtractFieldInput
,
Field
as
SealiousField
,
FieldTypes
as
SealiousFieldTypes
,
}
from
"sealious"
;
import
{
FormControl
}
from
"../controls/controls.js"
;
import
{
FormDataValue
}
from
"../form-types.js"
;
import
{
FieldParseResult
,
FormField
,
FormFieldValidationResponse
,
}
from
"./field.js"
;
import
{
ExtractedFieldInfo
}
from
"../../utils/extract-fields-from-collection.js"
;
import
{
TextBasedSimpleField
}
from
"./simple-form-field.js"
;
import
{
Image
as
ImageField
}
from
"./image.js"
;
import
{
wrapAttribute
,
wrapKeyName
}
from
"../../utils/wrap-attributes.js"
;
import
{
StructuredSealiousField
}
from
"./structured.js"
;
export
function
sealiousFieldToFormField
(
sealiousField
:
SealiousField
<
any
>
,
field_name
:
string
,
prefix
:
string
)
:
FormField
{
const
name
=
prefix
?
`
${
prefix
}
[
${
field_name
}
]`
:
field_name
;
let
formfield
:
FormField
=
new
TextBasedSimpleField
(
false
);
if
(
sealiousField
instanceof
SealiousFieldTypes
.
Image
)
{
formfield
=
new
ImageField
(
false
);
}
if
(
sealiousField
instanceof
SealiousFieldTypes
.
Structured
)
{
formfield
=
new
StructuredSealiousField
(
sealiousField
);
}
formfield
.
setName
(
name
);
formfield
.
setLabel
(
field_name
);
if
(
prefix
)
{
formfield
.
setValuePath
(
prefix
+
"."
+
field_name
);
}
return
formfield
;
}
export
class
CollectionField
<
F
extends
SealiousField
<
any
>
,
Required
extends
boolean
,
>
extends
FormField
<
Required
,
ExtractFieldDecoded
<
F
>
,
FormControl
,
ExtractFieldInput
<
F
>
>
{
constructor
(
required
:
Required
,
public
sealiousField
:
F
)
{
super
(
required
);
}
predicate
=
predicates
.
unknown
;
async
parse
(
ctx
:
Context
,
raw_value
:
FormDataValue
)
:
Promise
<
FieldParseResult
<
ExtractFieldDecoded
<
F
>>>
{
if
(
raw_value
===
undefined
)
{
return
{
parsable
:
true
,
parsed_value
:
{}
as
any
,
error
:
null
};
}
const
encoded_value
=
(
await
this
.
sealiousField
.
encode
(
ctx
.
$context
,
raw_value
))
as
Depromisify
<
ReturnType
<
F
[
"encode"
]
>>
;
const
parsed_value
=
(
await
this
.
sealiousField
.
decode
(
ctx
.
$context
,
encoded_value
,
null
))
as
ExtractFieldDecoded
<
F
>
;
return
{
parsable
:
true
,
error
:
null
,
parsed_value
,
};
}
public
async
isValueValid
(
ctx
:
Context
,
value
:
ExtractFieldDecoded
<
F
>
)
:
Promise
<
FormFieldValidationResponse
>
{
const
{
valid
,
reason
}
=
await
this
.
sealiousField
.
checkValue
(
ctx
.
$context
,
await
this
.
parsedToSealious
(
value
),
undefined
,
null
,
undefined
);
return
{
valid
,
message
:
reason
||
(
!
valid
?
"Wrong value"
:
""
)
};
}
async
parsedToSealious
(
parsed
:
unknown
)
:
Promise
<
ExtractFieldInput
<
F
>>
{
return
sealiousFieldToFormField
(
this
.
sealiousField
,
this
.
name
,
""
).
parsedToSealious
(
parsed
)
as
ExtractFieldInput
<
F
>
;
}
public
getEmptyValue
()
:
ExtractFieldDecoded
<
F
>
{
return
""
as
unknown
as
ExtractFieldDecoded
<
F
>
;
}
async
sealiousValueToForm
(
ctx
:
Context
,
value
:
any
,
item
:
CollectionItem
<
any
>
)
:
Promise
<
FormDataValue
>
{
return
sealiousFieldToFormField
(
this
.
sealiousField
,
this
.
name
,
""
).
sealiousValueToForm
(
ctx
,
value
,
item
);
}
getControl
()
:
FormControl
{
return
sealiousFieldToFormField
(
this
.
sealiousField
,
this
.
name
,
""
).
getControl
();
}
generateFieldDeclaration
(
field_info
:
ExtractedFieldInfo
,
vars
:
{
form_field_types
:
string
;
sealious_field
:
string
}
)
:
string
{
return
`new
${
vars
.
form_field_types
}
.
${
this
.
constructor
.
name
}
(
${
String
(
field_info
.
is_required
)
}
,
${
vars
.
sealious_field
}
)`
;
}
generateInitialValue
(
field_info
:
ExtractedFieldInfo
,
vars
:
{
form_field_types
:
string
;
form_fields
:
string
;
}
)
:
string
{
// compared to the version in native field, we add the third argument here
return
`
${
wrapKeyName
(
field_info
.
name
)
}
: await
${
vars
.
form_fields
}${
wrapAttribute
(
field_info
.
name
)
}
.sealiousValueToForm(ctx, item.get("
${
field_info
.
name
}
"), item)`
;
}
}
File Metadata
Details
Attached
Mime Type
application/javascript
Expires
Fri, Nov 27, 20:26 (2 h, 41 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1201079
Default Alt Text
collection-field.ts (4 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline