URL hooks
mapp.hooks.current holds the URL params of the page. The MAPP library parses them once, when it loads.[1] A view reads its options from them, and a mapview with hooks: true writes its state back into the URL.
Behaviour
trueandfalsebecome booleans.layersandlocationsare always arrays; any other value with a comma becomes an array.- A
tokenparam is removed fromhooks.currentand from the URL. set,push,filter,removeandremoveAllchangehooks.currentand push the new URL to the browser history.
These hooks have a meaning in MAPP and the default view:
| Hook | Effect |
|---|---|
locale | The locale to load; nested locales are comma separated |
lat, lng, z | The start view; they win over locale.view[2] |
layers | Only these layer keys are displayed when added[3] |
locations | layerKey!id of the selected locations[4] |
language | The language of the dictionary and the view HTML |
userlocale | The name of a locale stored for the user |
id, layer | The location of a report view |
With hooks: true the mapview sets lat, lng and z after each move, pushes a layer key to layers when the layer is shown and filters it out when hidden, and pushes and filters locations as locations are selected and removed. The default view sets locale.
The location hook is ${layer.key}!${location.id}. A view selects the hooked locations after adding the layers:
for (const hook of mapp.hooks.current.locations) {
const [layerKey, id] = hook.split('!');
mapp.location.get({ layer: mapview.layers[decodeURIComponent(layerKey)], id });
}Flow
mapp.mjs calls hooks.parse() → the view reads mapp.hooks.current → mapp.Mapview({ hooks: true }) → updateView, addLayer, layer.show/hide, location.get/remove call mapp.hooks.set, push or filter → history.pushState.
Where to change
- Parsing and URL updates:
apps/mapp/lib/hooks.mjs. - Which state is written:
mapview/_mapview.mjs(view),layer/decorate.mjs(layers),location/get.mjsandlocation/decorate.mjs(locations).
Tests
apps/mapp/tests/hooks.test.mjs covers parsing, token removal, set, push, filter, remove and removeAll.
Pitfalls
- Only one mapview per page should have
hooks: true. Two mapviews would write the samelat,lng,zandlayers. - A non empty
layershook overridesdisplayof every added layer, including layers added later by a script. mapp.location.gettoggles: for a location which is already selected it removes the location and returnsundefined. It also returnsundefinedfor a layer withoutinfoj.- The URL params also reach the view API, but only simple strings are substituted into the HTML. Read numbers and lists from the hooks.
- The library assigns the global
mapp; there is noMAPPalias.