View API
A view is an HTML page that the XYZ API serves from a template in workspace.templates. The browser loads the MAPP libraries and runs the view's script. The API does not render the map; it only fills in a few .
Behaviour
- Routes.
${DIR}/view/:template,${DIR}/view?template=…,${DIR}/and/all call the view module.[1] Without atemplateparam the view isdefault_view. With aDIR, a request for/is redirected toDIR. - Core view templates.
default_viewis _default.html;login_view,register_viewandpassword_reset_vieware_login.html;user_admin_viewis_user.html.[2] Workspace templates with the same key replace them, and so doCUSTOM_TEMPLATES. See templates. - Template lookup. The template is a string
template, asrcloaded on first use (eg.file:/public/views/_default.html, resolved fromXYZ_CWD), or a language template with one HTML string or src per language code. An unknown template key returns400with the error message.[3] - Access. The view routes pass through the authentication middleware. A
PRIVATEinstance redirects users without a login to the login view orAUTH_PATH. The view module does not check roles on the template. - Substitution.
is replaced with the param of that name, or with an empty string when there is no such param.[4] The params are the URL path and query params plus:
| Param | Value |
|---|---|
| The DIR environment variable, the path prefix of the instance |
| The TITLE environment variable |
| The AUTH_PATH environment variable of a custom login provider |
| true if PRIVATE or PUBLIC is set, ie. the instance has an ACL |
| The language URL param if it is two lower case letters, otherwise en |
| The template key |
| The language template of the msg param, used by the login view |
Only param values which are strings of A-Z a-z 0-9 _ - are substituted. The values true, false and null are parsed into non-strings, and [a,b] into arrays, so these are removed too.[5] The user is never a param; the browser requests it from /api/user/cookie.
Flow
router.js → validateRequestParams.js (query into params, language defaults to en) → validateRequestAuth.js (user, PRIVATE redirect) → view.js → languageTemplates.js → getTemplate.js (cache, src) → substitution → HTML response.
Where to change
- Params and substitution:
apps/xyz/mod/view.js. - Core view keys:
apps/xyz/mod/workspace/templates/_views.js. - The core views themselves:
public/views/; see core views.
Tests
apps/xyz/tests/mod/view.test.mjs loads tests/assets/view.json: the default view returns 200, defaults to en, and a param with a character outside the whitelist is removed. The default view in the browser is tested by apps/ui/tests/default.spec.mjs.
Pitfalls
- A placeholder name must be a letter followed by letters and digits.
orNaNdoes not match, so it stays in the HTML as written. - A param value with a dot, space or comma is removed, eg.
lat=51.5or a nestedlocale=UK,London. Read such values in the browser from URL hooks instead. does not fall back to the user's language:validateRequestParamssetslanguagetoenfirst, so only the URL param counts. The default view applies the user's language in the browser.- Views are cached with the workspace. A changed
srcfile is read again only afterWORKSPACE_AGEor a cache reset. - The public directory is served at both
${DIR}/publicand${DIR}. The core views use/public/…; do the same in custom views.