Menu Plugin
Shows groups of radios and checkboxes that control which datasets are shown, and keeps the choices in the URL.
The groups are shown in a Layers panel. Datasets are shown or hidden by the choices through their own visibleWhen. Keeping the choices in the URL means they survive a reload or a shared link.
NOTE
This plugin is in early development. Its options and behaviour may change.
Usage
import createDatasetsPlugin from '@defra/interactive-map/plugins/datasets'import createMenuPlugin from '@defra/interactive-map/plugins/menu'
const menuPlugin = createMenuPlugin({ menu: [ { id: 'period', label: 'Period', type: 'radio', value: 'roman', items: [ { id: 'roman', label: 'Roman' }, { id: 'medieval', label: 'Medieval' } ] } ]})
const datasetsPlugin = createDatasetsPlugin({ datasets: [ { id: 'roman-sites', geojson: romanSites, visibleWhen: { menu: { period: ['roman'] } } } ]})
const interactiveMap = new InteractiveMap('map', { plugins: [menuPlugin, datasetsPlugin]})For UMD, add plugins/menu/dist/umd/index.js and call defra.menuPlugin().
Options
Options are passed to the factory function when creating the plugin.
menu
- Type:
MenuGroup[](required)
The menu's groups of options. Datasets can be shown or hidden by the selected options with their own visibleWhen.
MenuGroup properties
| Property | Type | Description |
|---|---|---|
id | string | Required. Unique identifier for the group, also used as its URL parameter |
label | string | The group's legend |
type | 'radio' | 'checkbox' | Required. Whether one option (radio) or any number of options (checkbox) can be selected |
value | string | Radio groups only. The selected option's id, when the URL doesn't set it |
items | MenuItem[] | Required. The group's options |
visibleWhen | boolean | { menu?: Record<string, string[]> } | When the group is shown. A hidden group's URL parameter is removed |
MenuItem properties
| Property | Type | Description |
|---|---|---|
id | string | Required. The option's value: a radio group's value, or a checkbox's ID |
label | string | Required. Text shown beside the option |
checked | boolean | Checkbox groups only. Whether the checkbox starts checked, when the URL doesn't set the group |
visibleWhen | boolean | { menu?: Record<string, string[]> } | When the option is shown |
MenuVisibleWhen
- Type:
boolean | { menu?: Record<string, string[]> }
true or false, or { menu: { groupId: values } } to show it only while every listed group's selected value is one of its values.
manifest
- Type:
Partial<PluginManifest>
Overrides for the plugin's buttons, panels and controls, matched by id. See PluginDescriptor.manifest.
Menu state and the URL
The menu's state maps:
- each radio group's
idto the selected option'sid - each checkbox's
idtotrueorfalse
It's read from the URL when the map loads, falling back to each group's value or each checkbox's checked. Each change updates the URL without reloading the page: a radio group's parameter holds its selected id, and a checkbox group's holds the checked ids, comma separated.
?period=medieval&features=walls,ditchesA dataset's visibleWhen menu conditions are checked against this state.
Methods
This plugin does not expose any public methods.
Events
Subscribe to events using interactiveMap.on().
menu:changed
Emitted whenever the menu state changes: when it's first read from the URL as the map loads, and when the user changes a choice.
- Payload:
- the menu state, as described in Menu state and the URL
interactiveMap.on('menu:changed', (menuState) => { console.log('Period:', menuState.period)})