@museumwnf/viewer-core 2.22.0 → 2.23.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/README.md +5 -0
- package/package.json +1 -1
- package/src/globalSearch.js +74 -0
- package/src/index.js +1 -0
package/README.md
CHANGED
|
@@ -157,6 +157,8 @@ What a website's `main.js` still writes, and why it cannot move here:
|
|
|
157
157
|
| `media` | `{ legacyHost }` | no | the host of the legacy media server, for [`mediaUrl()`](#the-declaration-outside-a-component) |
|
|
158
158
|
| `links` | object | no | the addresses a website links out to: `portal`, `galleries`, `myCollection`, `about`, `contact`, `legalNotice`, `credits`, `cookies` |
|
|
159
159
|
| `site.origin` | string | no | the absolute origin this build is deployed at, base path included when there is one (`https://metanull.github.io/islamicart`, no trailing slash) — declared once here rather than baked into the package, since the package cannot know where it will be served; read by [`sourceUrl()`](#the-declaration-outside-a-component) |
|
|
160
|
+
| `site.slug` | string | no | the site's slug, as in the exporters' instance files; defaults to the last segment of `site.origin`. The global search's link sends it as `from` |
|
|
161
|
+
| `globalSearch` | `true` or `{ url }` | no | **off by default.** Turns on the link to the global search (inventory-app M15, ADR 005): `true` for its default address (`GLOBAL_SEARCH_URL`), `{ url }` for another https address. viewer-layout's header then shows its search box. The parity check turns it on for the sites whose legacy had a similar feature |
|
|
160
162
|
|
|
161
163
|
`dataset.config.js` is the whole declaration of a website. Before it mounts,
|
|
162
164
|
a website reads nothing from its data package but `manifest.json`; it reads
|
|
@@ -579,6 +581,9 @@ below for whichever future site needs its own).
|
|
|
579
581
|
| `mediaUrl(path, size = 'hi_res')` | `${config.media.legacyHost}/${size}/${path}` for a legacy media path the package carries; an address is returned as it is |
|
|
580
582
|
| `useSiteRights()` | `{ holder, termsUrl, attribution }` from the data package's `manifest.rights`; a package with no such block offers no holder or attribution, and `termsUrl` falls back to `mwnfLinks.legalNotice` |
|
|
581
583
|
| `sourceUrl(route)` | `${config.site.origin}/#${path}` for `route` — a route already resolved (its `fullPath` read as it is) or a raw location the installed router resolves; null when the website declares no `site.origin` |
|
|
584
|
+
| `globalSearchUrl({ q, lang })` | the global search's address for `q` in `lang`, with this site's slug as `from`; null unless the site sets `globalSearch`. It never takes an address from content: the search site accepts only slugs from its own list of sites |
|
|
585
|
+
| `useGlobalSearch()` | `{ enabled, url(q, lang) }` for a component, such as viewer-layout's search box |
|
|
586
|
+
| `siteSlug()` | this site's slug: `site.slug`, else the last segment of `site.origin`; null when neither is a valid slug |
|
|
582
587
|
|
|
583
588
|
### `useDataPackage()` → data access (the only allowed way to read the data package)
|
|
584
589
|
|
package/package.json
CHANGED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { useSiteConfig } from './siteConfig.js'
|
|
2
|
+
|
|
3
|
+
// The global search (inventory-app M15, ADR 005): one search over every site's
|
|
4
|
+
// items, on a site of its own. A site opts in with `globalSearch` in its
|
|
5
|
+
// dataset.config.js, off by default:
|
|
6
|
+
//
|
|
7
|
+
// globalSearch: true // the default address
|
|
8
|
+
// globalSearch: { url: 'https://example.org/search/' } // another address
|
|
9
|
+
//
|
|
10
|
+
// The link carries the text, the language and this site's own slug as `from`,
|
|
11
|
+
// and nothing else: the search site accepts a slug only if it is in its list
|
|
12
|
+
// of sites, never an address, so no parameter can send a visitor elsewhere.
|
|
13
|
+
|
|
14
|
+
/** Where the search site is served, unless a site's config names another. */
|
|
15
|
+
export const GLOBAL_SEARCH_URL = 'https://museumwithnofrontiers.github.io/search/'
|
|
16
|
+
|
|
17
|
+
/** A site's slug: lowercase words joined by single hyphens, as the exporters' instance files. */
|
|
18
|
+
const SLUG = /^[a-z0-9]+(-[a-z0-9]+)*$/
|
|
19
|
+
const LANGUAGE = /^[a-z]{2}$/
|
|
20
|
+
|
|
21
|
+
function searchBase(setting) {
|
|
22
|
+
if (setting === true) return GLOBAL_SEARCH_URL
|
|
23
|
+
if (setting && typeof setting === 'object' && typeof setting.url === 'string') {
|
|
24
|
+
let url
|
|
25
|
+
try {
|
|
26
|
+
url = new URL(setting.url)
|
|
27
|
+
} catch {
|
|
28
|
+
return null
|
|
29
|
+
}
|
|
30
|
+
if (url.protocol !== 'https:' || url.search || url.hash || url.username || url.password) return null
|
|
31
|
+
return url.href.endsWith('/') ? url.href : `${url.href}/`
|
|
32
|
+
}
|
|
33
|
+
return null
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* This site's slug: `site.slug` when declared, else the last segment of
|
|
38
|
+
* `site.origin` (every site is served one path segment under a common
|
|
39
|
+
* parent). Null when neither gives a valid slug.
|
|
40
|
+
*/
|
|
41
|
+
export function siteSlug(config = useSiteConfig()) {
|
|
42
|
+
const declared = config?.site?.slug
|
|
43
|
+
if (typeof declared === 'string') return SLUG.test(declared) ? declared : null
|
|
44
|
+
const origin = String(config?.site?.origin ?? '').replace(/\/+$/, '')
|
|
45
|
+
const last = origin.slice(origin.lastIndexOf('/') + 1)
|
|
46
|
+
return SLUG.test(last) ? last : null
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* The address of the global search for `q` in `lang`, or null when the site
|
|
51
|
+
* has not opted in. Built only from the configured address and the escaped
|
|
52
|
+
* parameters.
|
|
53
|
+
*/
|
|
54
|
+
export function globalSearchUrl({ q = '', lang = '' } = {}, config = useSiteConfig()) {
|
|
55
|
+
const base = searchBase(config?.globalSearch)
|
|
56
|
+
if (!base) return null
|
|
57
|
+
const params = new URLSearchParams()
|
|
58
|
+
const text = String(q ?? '').trim()
|
|
59
|
+
if (text) params.set('q', text)
|
|
60
|
+
if (LANGUAGE.test(String(lang))) params.set('lang', String(lang))
|
|
61
|
+
const from = siteSlug(config)
|
|
62
|
+
if (from) params.set('from', from)
|
|
63
|
+
const query = params.toString()
|
|
64
|
+
return `${base}#/${query ? `?${query}` : ''}`
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** For a component: whether the site has opted in, and the link builder. */
|
|
68
|
+
export function useGlobalSearch() {
|
|
69
|
+
const config = useSiteConfig()
|
|
70
|
+
return {
|
|
71
|
+
enabled: searchBase(config?.globalSearch) !== null,
|
|
72
|
+
url: (q, lang) => globalSearchUrl({ q, lang }, config),
|
|
73
|
+
}
|
|
74
|
+
}
|
package/src/index.js
CHANGED
|
@@ -21,6 +21,7 @@ export { mediaUrl, useSiteConfig } from './siteConfig.js'
|
|
|
21
21
|
export { bindDisplayLanguage, displayLanguage } from './displayLanguage.js'
|
|
22
22
|
export { siblingSiteUrl, sourceUrl, useSiteRights } from './rights.js'
|
|
23
23
|
export { LEGACY_SITES, toNewSite, watchNewSiteLinks, withScheme } from './legacySites.js'
|
|
24
|
+
export { GLOBAL_SEARCH_URL, globalSearchUrl, siteSlug, useGlobalSearch } from './globalSearch.js'
|
|
24
25
|
export { legacyRecordId } from './legacyIds.js'
|
|
25
26
|
|
|
26
27
|
// The list pages: query state, pagination, facets, dates, keyword search.
|