Authoring
What MAPP plugins are, how locale and layer keys run them, how the mapview loads plugin modules, and how to author and version one; use when adding a plugin or a tool that the core libraries do not have.
Resource and sources
- ../../apps/mapp/lib/utils/loadPlugins.mjs
wikihttps://github.com/GEOLYTIX/xyz/wiki/Plugins/3fc379b7ada42dff57f19e9f28694868e5a17cac (revision3fc379b7ada42dff57f19e9f28694868e5a17cac)load../../apps/mapp/lib/utils/loadPlugins.mjsmapview../../apps/mapp/lib/mapview/_mapview.mjslayer../../apps/mapp/lib/layer/decorate.mjscore../../apps/mapp/lib/plugins/_plugins.mjsui../../apps/ui/lib/plugins/_plugins.mjsversion../../apps/mapp/lib/utils/versionCheck.mjsesm../../apps/mapp/lib/utils/esmImport.mjs- ../../apps/ui/lib/ui.mjs
- ../../apps/ui/lib/layers/view.mjs
- ../../apps/ui/lib/locations/infoj.mjs
- ../../apps/mapp/tests/utils/loadPlugins.test.mjs
- ../../apps/mapp/tests/plugins
- ../../apps/ui/tests/plugins
publichttps://github.com/GEOLYTIX/mapp/tree/main/plugins- ../../apps/ui/tests/locations/infoj.test.mjs
A plugin is a function in mapp.plugins. A workspace turns it on with a key of the same name; the key's value is the plugin's configuration. The configuration keys are documented in locale plugins.
Kinds of plugin
- Locale plugins run once per mapview, as
plugin(locale[key], mapview), for every locale key which names a plugin.[1] They add buttons tomapview.mapButton, panels, or behaviour. - Layer plugins run as
plugin(layer)inmapp.layer.decoratefor every layer key which names a plugin.[2] The layer view does not exist yet. - Extensions are not in
mapp.plugins. A module adds a method to a registry, eg. a layer format tomapp.layer.formatsor an entry type tomapp.ui.locations.entries, or replaces a MAPP method; the tabulator and chartjs plugins add dataview renderers tomapp.ui.utils. See extension points for every registry.
Built in are consent, layer_order and svg_templates from apps/mapp/lib/plugins,[3] and the ui plugins, eg. zoomBtn, login, fullscreen, which ui.js adds to mapp.plugins.[4]
Loading and running
mapp.Mapview loads and runs plugins only when locale.syncPlugins is not empty or the locale has svgTemplates.[1:1] Then:
layer.pluginsURLs are merged intolocale.plugins, without duplicates.- With the mapview option
loadPlugins: true(or an array of endings),mapp.utils.loadPlugins(locale.plugins)imports each URL.[5] - The
syncPluginskeys run one after another, in order, even without a locale key; their configuration is thenundefined. - All other locale keys which name a plugin run in parallel.
loadPlugins imports only URLs ending in .js or .mjs. A URL without http is resolved against the page origin, not mapp.host. A module which fails to load is logged and skipped; the promise always resolves. A plugin which throws is logged and does not stop the mapview. The XYZ API replaces ${NAME} in locale.plugins with the SRC_NAME variable.
Authoring a plugin
A plugin module assigns its method on import; loadPlugins does not register exports. A minimal locale plugin which adds a button:
// home_button.js
mapp.utils.versionCheck('5.1') || console.warn('home_button needs MAPP v5.1');
mapp.plugins.home_button = (config = {}, mapview) => {
if (!mapview.mapButton) return;
mapview.mapButton.append(mapp.utils.html.node`
<button title=${config.title || mapp.dictionary.home || 'Home'}
onclick=${() => mapview.Map.getView().setZoom(config.z || 2)}>
<span class="notranslate material-symbols-outlined">home`);
};"plugins": ["${PLUGINS}/home_button.js"],
"home_button": { "title": "Back to start", "z": 6 }mapp.utils.versionCheck(v)is true whenmapp.versionisvor newer.[6]- Import third party modules with
mapp.utils.esmImport('name@version')from esm.sh, not at the top of the plugin.[7] - Read texts from
mapp.dictionary. Store per user state withmapp.utils.userIndexedDB. - A plugin which needs another one first, eg.
posthogafterconsent, is listed after it insyncPlugins.
Plugin repositories
GEOLYTIX/mapp/plugins is the public home of shared plugins, eg. tabulator, chartjs, the isoline and routing plugins, and measure_distance.[8] Many were written for earlier MAPP versions; check a plugin against the MAPP version of the deployment before using it.
Serve plugins from a path which names the MAPP version they were written for, eg. v5.0.0/, so a workspace pins the version its deployment runs.
Pitfalls
- A key which names a plugin runs it, whether meant or not. A layer key and a locale key with the same name call the plugin with different arguments.
mapp.layermethods with the layer key's name run too, before the plugin.- A URL with a query string, eg.
plugin.js?v=2, is not loaded. - A plugin URL runs in every user's browser; list only sources the deployment controls.