Skip to content

Latest commit

 

History

History
149 lines (130 loc) · 6.23 KB

extension-points.md

File metadata and controls

149 lines (130 loc) · 6.23 KB
type Guide
title Extension points
description The registries plugins extend with layer formats, theme, entry, filter and icon types, panels and dataviews, and how to replace or wrap a MAPP method; use when a plugin adds a type or changes core behaviour.
resource ../../apps/mapp/lib/mapp.mjs
tags
plugins
mapp
ui
guide
status draft
sources
id resource
mapp
../../apps/mapp/lib/mapp.mjs
id resource
ui
../../apps/ui/lib/ui.mjs
id resource
decorate
../../apps/mapp/lib/layer/decorate.mjs
id resource
vector
../../apps/mapp/lib/layer/format/vector.mjs
id resource
featurestyle
../../apps/mapp/lib/layer/featureStyle.mjs
id resource
fields
../../apps/mapp/lib/layer/featureFields.mjs
id resource
olstyle
../../apps/mapp/lib/utils/olStyle.mjs
id resource
infoj
../../apps/ui/lib/locations/infoj.mjs
id resource
filters
../../apps/ui/lib/layers/filters.mjs
id resource
view
../../apps/ui/lib/layers/view.mjs
id resource
legend
../../apps/ui/lib/layers/drawLegend.mjs
id resource
dataview
../../apps/ui/lib/Dataview.mjs
id resource
compose
../../apps/mapp/lib/utils/compose.mjs
id resource
mapview
../../apps/mapp/lib/mapview/_mapview.mjs

Extension points

mapp.mjs assigns the MAPP library to globalThis.mapp, and ui.mjs adds mapp.ui.1 Core modules call each other through this object, eg. mapp.layer.formats[layer.format](layer), and look the method up when they call it. A plugin changes MAPP by assigning to it: a new key in a registry adds a type, a new value for an existing key replaces a method. ui.mjs does the same: it replaces mapp.utils.alert, mapp.utils.confirm and mapp.layer.drawLegend with ui versions.2

Registries

A workspace value selects a method from a registry. A plugin adds a value by adding the method; the workspace key then accepts it.

Registry Called as Selected by
mapp.layer.formats (layer) layer.format3
mapp.layer.featureFormats (layer, features) layer.featureFormat4
mapp.layer.themes (theme, feature), optional .retain(theme, featuresObject) theme.type5
mapp.ui.layers.legends (layer) theme.type6
mapp.layer.featureFields.distribution (layer) theme.distribution7
mapp.utils.svgSymbols (icon, feature), returns an SVG style.icon.type8
mapp.ui.locations.entries (entry), returns an element entry.type9
mapp.ui.layers.filters (layer, filter) entry.filter.type10
mapp.ui.layers.panels (layer), returns a panel a layer key11
mapp.ui.utils[name].create (dataview) dataview.dataview, or chart and table12
mapp.layer, mapp.plugins (layer) a layer key3
mapp.plugins (config, mapview) a locale key; see plugins

A theme type needs a method in mapp.layer.themes to style features and one in mapp.ui.layers.legends to draw its legend. A layer with an unknown layer.format is skipped and a theme with an unknown type is ignored, each with a warning; an unknown entry type renders nothing.

mapp.layer.featureFormats and mapp.utils.svgSymbols are module namespaces, which cannot be extended. Replace them with a copy:

mapp.utils.svgSymbols = {
  ...mapp.utils.svgSymbols,
  star: (icon) => `<svg …>`,
};

The workspace schemas list the core values of these keys as examples, not as an enum, so a workspace with a plugin's value passes the strict schema.

Replacing and wrapping methods

Assign a function to replace a method. To keep the core behaviour, keep a reference to the original and call it:

const decorate = mapp.layer.decorate;

mapp.layer.decorate = async (layer) => {
  layer.attribution ??= { 'My data': 'https://example.org' };
  return decorate(layer);
};

mapp.utils.compose(f, g) returns a function which passes its argument through f and then g; each function takes and returns one value. Bind the original to compose it into itself: mapp.layer.decorate = mapp.utils.compose(mapp.layer.decorate.bind(), logLayer); logLayer then receives the promise of the decorated layer.13

When an assignment takes effect

Plugin modules assign on import. mapp.Mapview imports the URLs in locale.plugins and layer.plugins before it runs the plugins and before a view adds layers, so registrations are in place when layers are decorated.14 A replacement applies to calls made after it:

  • A layer keeps the style function mapp.layer.featureStyle(layer) returned when its format was decorated. Replace featureStyle before the layers are added, or redecorate the layer.
  • Methods already called, eg. mapp.Mapview itself, stay as they were.
  • A plugin loads after mapp.mjs and ui.mjs and wins over their methods.

Pitfalls

  • A registry key is a workspace value: a plugin which adds mapp.layer.themes.heat makes "type": "heat" valid in every layer of the view. Choose names which core MAPP does not use and is unlikely to add.
  • Assigning a key which exists replaces the core method for every layer, location and view on the page.
  • A module namespace throws on assignment in a module: replace the object instead of adding a key.
  • A method added to mapp.layer runs for every layer with a key of its name, like a layer plugin.
  • The workspace keys a plugin reads are not in the knowledge bundle. The strict schema rejects them in documented objects such as a layer; validate a workspace with plugin keys against the open schema, or document the keys where the schema is generated.

Footnotes

  1. mapp.mjs ↩

  2. ui.mjs ↩

  3. layer/decorate.mjs: format check and the layer key loop ↩ ↩2

  4. layer/format/vector.mjs load ↩

  5. layer/featureStyle.mjs; format/mvt.mjs calls retain ↩

  6. ui layers/drawLegend.mjs ↩

  7. layer/featureFields.mjs process ↩

  8. utils/olStyle.mjs ↩

  9. ui locations/infoj.mjs ↩

  10. ui layers/filters.mjs ↩

  11. ui layers/view.mjs ↩

  12. ui Dataview.mjs ↩

  13. utils/compose.mjs ↩

  14. mapview/_mapview.mjs mapviewPromise ↩