@museumwnf/viewer-core 2.20.0 → 2.21.0
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.
- package/package.json +1 -1
- package/src/catalogue/catalogueResults.js +123 -0
- package/src/createStandardViewer.js +32 -4
- package/src/index.js +2 -1
- package/src/testing/chromeSources.js +35 -0
- package/src/testing/index.js +1 -0
package/package.json
CHANGED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { computed, toValue } from 'vue'
|
|
2
|
+
import { useDataPackage } from '../composables/useDataPackage.js'
|
|
3
|
+
import { entityRef } from '../composables/useEntities.js'
|
|
4
|
+
import { useI18n } from '../i18n/index.js'
|
|
5
|
+
import { renderInline, renderPlain } from '../i18n/markdown.js'
|
|
6
|
+
import { dateRange } from './dates.js'
|
|
7
|
+
import { useFacets } from './facets.js'
|
|
8
|
+
import { useListQuery } from './listQuery.js'
|
|
9
|
+
import { sortChronological, usePagination } from './pagination.js'
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A results page's engine: the records of one entity, narrowed by the filters
|
|
13
|
+
* and the page the URL carries, in order, a page at a time, each drawn as a
|
|
14
|
+
* row. viewer-layout's `CatalogueResultsView` draws it; a page of another
|
|
15
|
+
* shape can call it directly and draw the result its own way.
|
|
16
|
+
*
|
|
17
|
+
* `spec` (an object, a ref or a getter) is the view's spec — its `entity`,
|
|
18
|
+
* `keys`, `facets`, `facetScope`, `scope`, `match`, `narrow`, `dates`,
|
|
19
|
+
* `sort`, `pageSize`, `record`, `recordRoute`, `summary` and `summaryLabel`;
|
|
20
|
+
* see CatalogueResultsView's header for each. `entity` is the route's own,
|
|
21
|
+
* for a `features.entities` page, used when the spec names none.
|
|
22
|
+
*
|
|
23
|
+
* Returns the query state (`filters`, `page`, `active`, `apply`, `reset`,
|
|
24
|
+
* `goToPage`), the records (`records`, `matching`), the facets' `options`,
|
|
25
|
+
* `pageInfo`, the page's `rows`, the `summary` parts, and `state`: all of it
|
|
26
|
+
* in one object, which is what the view hands each of its slots.
|
|
27
|
+
*/
|
|
28
|
+
export function useCatalogueResults(spec, { entity: routeEntity = '' } = {}) {
|
|
29
|
+
const { t, locale } = useI18n()
|
|
30
|
+
const pkg = useDataPackage()
|
|
31
|
+
const current = computed(() => toValue(spec))
|
|
32
|
+
|
|
33
|
+
// Read once, as the URL keys are: a page does not change entity.
|
|
34
|
+
const entity = current.value.entity ?? routeEntity
|
|
35
|
+
const records = entityRef(entity)
|
|
36
|
+
if (entity) pkg.loadTranslations(entity, 'en')
|
|
37
|
+
|
|
38
|
+
const dates = current.value.dates
|
|
39
|
+
const keys = current.value.keys ?? [
|
|
40
|
+
...Object.keys(current.value.facets ?? {}),
|
|
41
|
+
...(dates ? [dates.begin ?? 'begin', dates.end ?? 'end'] : []),
|
|
42
|
+
]
|
|
43
|
+
const { filters, page, active, apply, reset, goToPage } = useListQuery({ keys })
|
|
44
|
+
|
|
45
|
+
const helpers = {
|
|
46
|
+
t,
|
|
47
|
+
locale,
|
|
48
|
+
tr: (name, id) => pkg.tr(name, id, locale.value, 'en'),
|
|
49
|
+
renderInline,
|
|
50
|
+
renderPlain,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const matching = computed(() => {
|
|
54
|
+
const s = current.value
|
|
55
|
+
let list = records.value ?? []
|
|
56
|
+
if (s.scope) list = list.filter((record) => s.scope(record, filters, helpers))
|
|
57
|
+
for (const [key, facet] of Object.entries(s.facets ?? {})) {
|
|
58
|
+
const wanted = filters[key]
|
|
59
|
+
if (!wanted) continue
|
|
60
|
+
list = list.filter((record) => facetValues(record, facet).map(String).includes(String(wanted)))
|
|
61
|
+
}
|
|
62
|
+
if (s.match) list = list.filter((record) => s.match(record, filters, helpers))
|
|
63
|
+
if (s.narrow) list = s.narrow(list, filters, helpers)
|
|
64
|
+
if (s.dates) {
|
|
65
|
+
list = dateRange(list, {
|
|
66
|
+
begin: filters[s.dates.begin ?? 'begin'],
|
|
67
|
+
end: filters[s.dates.end ?? 'end'],
|
|
68
|
+
mode: s.dates.mode ?? 'overlap',
|
|
69
|
+
fields: s.dates.fields,
|
|
70
|
+
})
|
|
71
|
+
}
|
|
72
|
+
const sort = s.sort ?? 'chronological'
|
|
73
|
+
if (typeof sort === 'function') return sort(list)
|
|
74
|
+
if (sort === 'chronological') return sortChronological(list)
|
|
75
|
+
if (sort && typeof sort === 'object') return sortChronological(list, sort)
|
|
76
|
+
return list
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
const facetSource = computed(() => (current.value.facetScope === 'matching' ? matching.value : records.value ?? []))
|
|
80
|
+
const options = useFacets(facetSource, () => current.value.facets ?? {})
|
|
81
|
+
const pageInfo = usePagination(matching, { page, size: () => current.value.pageSize ?? 20 })
|
|
82
|
+
|
|
83
|
+
const recordRoute = computed(() => current.value.recordRoute ?? (routeEntity ? `${routeEntity}-detail` : 'item'))
|
|
84
|
+
function defaultRecord(record) {
|
|
85
|
+
const text = helpers.tr(entity, record.id)
|
|
86
|
+
const name = text.name ?? record.internal_name ?? record.id
|
|
87
|
+
return {
|
|
88
|
+
id: record.id,
|
|
89
|
+
image: record.images?.[0]?.url ?? '',
|
|
90
|
+
imageAlt: renderPlain(String(name)),
|
|
91
|
+
name: renderInline(String(name)),
|
|
92
|
+
meta: [],
|
|
93
|
+
badge: record.type ?? '',
|
|
94
|
+
to: { name: recordRoute.value, params: { id: record.id } },
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
const rows = computed(() => pageInfo.value.rows.map((record) => (current.value.record ?? defaultRecord)(record, helpers)))
|
|
98
|
+
|
|
99
|
+
const state = computed(() => ({
|
|
100
|
+
filters,
|
|
101
|
+
active: active.value,
|
|
102
|
+
apply,
|
|
103
|
+
reset,
|
|
104
|
+
goToPage,
|
|
105
|
+
matching: matching.value,
|
|
106
|
+
pageInfo: pageInfo.value,
|
|
107
|
+
options: options.value,
|
|
108
|
+
}))
|
|
109
|
+
|
|
110
|
+
const summary = computed(() => {
|
|
111
|
+
const s = current.value
|
|
112
|
+
if (s.summary) return s.summary({ ...state.value, t, total: (records.value ?? []).length })
|
|
113
|
+
return [{ label: t(s.summaryLabel ?? 'catalogue.results.itemsFound'), count: pageInfo.value.total }]
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
return { entity, records, filters, page, active, apply, reset, goToPage, matching, options, pageInfo, rows, summary, state, helpers }
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function facetValues(record, facet) {
|
|
120
|
+
if (typeof facet.values === 'function') return [facet.values(record)].flat()
|
|
121
|
+
const raw = record?.[facet.field]
|
|
122
|
+
return Array.isArray(raw) ? raw : [raw]
|
|
123
|
+
}
|
|
@@ -29,12 +29,15 @@ import { mergeMessages } from './i18n/index.js'
|
|
|
29
29
|
* website still imports its own family's `catalogues` and passes it as
|
|
30
30
|
* `dictionary`.
|
|
31
31
|
*
|
|
32
|
+
* Every website calls it as `createStandardViewer(config, { dictionary,
|
|
33
|
+
* ownMessages })`. The older three-argument form, `(config, siteClass,
|
|
34
|
+
* options)`, still works: a second argument that carries none of the option
|
|
35
|
+
* keys below is taken for the shell component.
|
|
36
|
+
*
|
|
32
37
|
* @param {object} config - the site's `dataset.config.js` object, exactly
|
|
33
38
|
* as passed to {@link createViewer} today
|
|
34
|
-
* @param {object} [siteClass] - the site's
|
|
35
|
-
* `config.shell` when `config` does not
|
|
36
|
-
* still declares its own `shell` keeps taking precedence, so existing
|
|
37
|
-
* `dataset.config.js` files need no change)
|
|
39
|
+
* @param {object} [siteClass] - (older form) the site's shell component, used
|
|
40
|
+
* as `config.shell` when `config` does not set one
|
|
38
41
|
* @param {object} [options]
|
|
39
42
|
* @param {object} [options.dictionary] - the family's shared catalogue,
|
|
40
43
|
* i.e. `catalogues` imported from
|
|
@@ -49,6 +52,10 @@ import { mergeMessages } from './i18n/index.js'
|
|
|
49
52
|
* `.unmount()` on it, e.g. in a test)
|
|
50
53
|
*/
|
|
51
54
|
export function createStandardViewer(config, siteClass, options = {}) {
|
|
55
|
+
if (isOptions(siteClass) && arguments.length < 3) {
|
|
56
|
+
options = siteClass
|
|
57
|
+
siteClass = undefined
|
|
58
|
+
}
|
|
52
59
|
const { dictionary, ownMessages, el = '#app' } = options
|
|
53
60
|
|
|
54
61
|
const messages = dictionary
|
|
@@ -59,3 +66,24 @@ export function createStandardViewer(config, siteClass, options = {}) {
|
|
|
59
66
|
app.mount(el)
|
|
60
67
|
return app
|
|
61
68
|
}
|
|
69
|
+
|
|
70
|
+
const OPTION_KEYS = ['dictionary', 'ownMessages', 'el']
|
|
71
|
+
|
|
72
|
+
function isOptions(value) {
|
|
73
|
+
return Boolean(value) && typeof value === 'object' && OPTION_KEYS.some((key) => key in value)
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* A website's own texts, by language, from its `locales/<lang>.json` files as
|
|
78
|
+
* `import.meta.glob('../locales/*.json', { eager: true })` returns them — the
|
|
79
|
+
* glob itself stays in the site's `main.js`, where Vite resolves it:
|
|
80
|
+
*
|
|
81
|
+
* ownMessages: textsByLanguage(import.meta.glob('../locales/*.json', { eager: true }))
|
|
82
|
+
*/
|
|
83
|
+
export function textsByLanguage(modules) {
|
|
84
|
+
const texts = {}
|
|
85
|
+
for (const [path, module] of Object.entries(modules ?? {})) {
|
|
86
|
+
texts[path.split('/').pop().replace(/\.json$/, '')] = module.default ?? module
|
|
87
|
+
}
|
|
88
|
+
return texts
|
|
89
|
+
}
|
package/src/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export { createViewer } from './createViewer.js'
|
|
2
|
-
export { createStandardViewer } from './createStandardViewer.js'
|
|
2
|
+
export { createStandardViewer, textsByLanguage } from './createStandardViewer.js'
|
|
3
3
|
export { useCatalogueData } from './composables/useCatalogueData.js'
|
|
4
4
|
export {
|
|
5
5
|
CATALOGUE_DATE_MODE, CATALOGUE_PAGE_SIZE, objectsAndMonumentsSummary, useCatalogue,
|
|
@@ -24,6 +24,7 @@ export { LEGACY_SITES, toNewSite, watchNewSiteLinks, withScheme } from './legacy
|
|
|
24
24
|
|
|
25
25
|
// The list pages: query state, pagination, facets, dates, keyword search.
|
|
26
26
|
export { useListQuery } from './catalogue/listQuery.js'
|
|
27
|
+
export { useCatalogueResults } from './catalogue/catalogueResults.js'
|
|
27
28
|
export { paginate, sortChronological, usePagination } from './catalogue/pagination.js'
|
|
28
29
|
export { facetOptions, useFacets } from './catalogue/facets.js'
|
|
29
30
|
export {
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto'
|
|
2
|
+
import { existsSync, readdirSync, readFileSync } from 'node:fs'
|
|
3
|
+
import { relative, resolve, sep } from 'node:path'
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What is wrong with a site's chrome taken from legacy (decision D7,
|
|
7
|
+
* inventory-app#2233): `sources` is the folder's SOURCES.json, whose `files`
|
|
8
|
+
* name each file by its path from the site's root with the sha256 of the
|
|
9
|
+
* bytes legacy serves. Every listed file must be there, byte for byte, and the
|
|
10
|
+
* folder must hold nothing the list does not name.
|
|
11
|
+
*
|
|
12
|
+
* Returns one line per problem, so a site's test reads
|
|
13
|
+
* `expect(checkChromeSources(sources)).toEqual([])` and a failure names every
|
|
14
|
+
* file at once. Paths resolve from `root`, the site's own directory (where
|
|
15
|
+
* Vitest runs).
|
|
16
|
+
*/
|
|
17
|
+
export function checkChromeSources(sources, { folder = 'theme/assets/legacy', root = process.cwd() } = {}) {
|
|
18
|
+
const problems = []
|
|
19
|
+
const listed = new Set(Object.keys(sources.files ?? {}))
|
|
20
|
+
for (const [path, { sha256 }] of Object.entries(sources.files ?? {})) {
|
|
21
|
+
const file = resolve(root, path)
|
|
22
|
+
if (!existsSync(file)) {
|
|
23
|
+
problems.push(`${path}: missing`)
|
|
24
|
+
continue
|
|
25
|
+
}
|
|
26
|
+
if (createHash('sha256').update(readFileSync(file)).digest('hex') !== sha256) {
|
|
27
|
+
problems.push(`${path}: not the file legacy serves`)
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
const present = readdirSync(resolve(root, folder), { recursive: true, withFileTypes: true })
|
|
31
|
+
.filter((entry) => entry.isFile() && entry.name !== 'SOURCES.json')
|
|
32
|
+
.map((entry) => `${relative(root, entry.parentPath).split(sep).join('/')}/${entry.name}`)
|
|
33
|
+
for (const path of present.filter((p) => !listed.has(p))) problems.push(`${path}: not listed in SOURCES.json`)
|
|
34
|
+
return problems
|
|
35
|
+
}
|
package/src/testing/index.js
CHANGED