Page Menu
Home
Sealhub
Search
Configure Global Search
Log In
Files
F1262985
sealious-list.ts
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
2 KB
Referenced Files
None
Subscribers
None
sealious-list.ts
View Options
import
{
Context
}
from
"koa"
;
import
{
BaseListPageFields
,
DEFAULT_ITEMS_PER_PAGE
,
ListPage
,
}
from
"./list.js"
;
import
{
Collection
,
CollectionItem
,
ItemList
,
Context
as
SealiousContext
,
CollectionInput
,
}
from
"sealious"
;
import
{
FlatTemplatable
,
Templatable
,
tempstream
}
from
"tempstream"
;
import
{
peopleWhoCan
}
from
"./access-control.js"
;
import
qs
from
"qs"
;
import
{
is
,
predicates
}
from
"@sealcode/ts-predicates"
;
export
abstract
class
SealiousItemListPage
<
C
extends
Collection
,
F
extends
typeof
BaseListPageFields
>
extends
ListPage
<
CollectionItem
<
C
>
,
F
>
{
constructor
(
public
collection
:
C
)
{
super
();
}
async
getContextForList
(
ctx
:
Context
)
:
Promise
<
SealiousContext
>
{
return
ctx
.
$context
;
}
async
getTotalPages
(
ctx
:
Context
,
itemsPerPage
:
number
)
:
Promise
<
number
>
{
const
query
=
this
.
collection
.
list
(
await
this
.
getContextForList
(
ctx
));
await
this
.
addFilter
(
ctx
,
query
);
const
{
items
}
=
await
query
.
fetch
();
return
Math
.
ceil
(
items
.
length
/
itemsPerPage
);
}
protected
async
addFilter
(
ctx
:
Context
,
list
:
ItemList
<
C
>
)
:
Promise
<
void
>
{
const
filters
=
await
this
.
getFilterValues
(
ctx
);
for
(
const
key
in
filters
)
{
if
(
filters
[
key
]
===
undefined
||
filters
[
key
]
==
""
)
{
delete
filters
[
key
];
}
}
if
(
Object
.
keys
(
filters
).
length
)
{
list
.
filter
(
filters
as
Partial
<
CollectionInput
<
C
>>
);
}
}
async
makeQuery
(
ctx
:
Context
)
:
Promise
<
ItemList
<
C
>>
{
const
query
=
this
.
collection
.
list
(
await
this
.
getContextForList
(
ctx
));
const
sort
=
await
this
.
getSort
(
ctx
);
if
(
sort
)
{
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-explicit-any
query
.
sort
({
[
sort
.
field
]
:
sort
.
order
}
as
any
);
}
return
query
;
}
async
getItems
(
ctx
:
Context
,
page
:
number
,
itemsPerPage
:
number
|
null
)
:
Promise
<
CollectionItem
<
C
>
[]
>
{
const
query
=
await
this
.
makeQuery
(
ctx
);
await
this
.
addFilter
(
ctx
,
query
);
const
{
items
}
=
await
query
.
paginate
({
items
:
itemsPerPage
||
DEFAULT_ITEMS_PER_PAGE
,
page
})
.
fetch
();
return
items
;
}
async
getFilterValues
(
ctx
:
Context
)
{
const
query_params
=
qs
.
parse
(
ctx
.
search
.
slice
(
1
));
const
_filter_values
=
query_params
?
.
filter
||
{};
const
filter_values
:
Record
<
string
,
string
|
undefined
>
=
is
(
_filter_values
,
predicates
.
record
(
predicates
.
string
,
predicates
.
string
)
)
?
_filter_values
:
{};
return
filter_values
;
}
renderListContainer
(
_
:
Context
,
content
:
Templatable
)
:
FlatTemplatable
{
return
tempstream
`<div class="sealgen-sealious-list-container">
${
content
}
</div>`
;
}
async
renderItem
(
_
:
Context
,
item
:
CollectionItem
<
C
>
,
_index
:
number
)
:
Promise
<
FlatTemplatable
>
{
return
`<div>
${
item
.
id
}
</div>`
;
}
canAccess
=
peopleWhoCan
(
"list"
,
this
.
collection
);
}
File Metadata
Details
Attached
Mime Type
text/x-java
Expires
Fri, Jan 24, 15:16 (17 h, 20 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
601009
Default Alt Text
sealious-list.ts (2 KB)
Attached To
Mode
rSGEN sealgen
Attached
Detach File
Event Timeline
Log In to Comment