.. include:: /Includes.rst.txt .. _extending-frontend: ======== Frontend ======== Every package brings its own frontend, written in TypeScript. The core collects the frontends of all installed packages into one import map. A package ships its built files, so an installation needs no Node.js. Declare the frontend ==================== A package with a ``public/`` directory and a ``config/frontend.php`` contributes a frontend. The file returns an array: .. code-block:: php return [ 'imports' => [ '@adsum/working-time/' => 'js/', ], 'module' => '@adsum/working-time/main.js', ]; ``imports`` Maps an import prefix to a directory below ``public/``. Every ``.js`` file in it is importable as ``@adsum//``. ``module`` The entry module of the package. The core imports it on every page, after the translations are loaded. ``styles`` Optional. Style sheets below ``public/`` added to every page, for example ``css/main.css``. ``importMaps`` Optional. Files below ``public/`` that contain further import map entries for built vendor modules. ``favicon`` Optional. A file below ``public/`` used as the favicon. The name of the package is the directory name without the ``adsum-`` prefix: the package in ``adsum-working-time`` is ``working-time``. Its ``public/`` directory is published as ``/bundles//``. Files found by convention ========================= The core also reads two directories in ``public/``: - ``public/translations/.json``: the frontend catalogues, built from the XLIFF files of the package (see :ref:`extending-translations-icons`). - ``public/icons/.svg``: the icons, usable by name in navigation entries and screens. Source layout in this repository ================================ In this repository the sources live outside the package; Vite builds them into ``public/`` of the package. .. code-block:: text Build/Sources/TypeScript// TypeScript, one JavaScript file per file Build/Sources/Icons// icons.json and own SVG icons Build/Sources/Scss// main.scss, built into css/main.css packages/web-vision/adsum-/public/ js/, icons/, translations/, css/ A package developed elsewhere ships its built ``public/`` directory in the same shape, built with any tool. Entry module ============ The entry module registers the screens and defines the web components of the package. The entry module of the attendance package: .. code-block:: typescript import './screens.js'; import { Clock } from './Clock.js'; if (customElements.get('tt-clock') === undefined) { customElements.define('tt-clock', Clock); } Importing ``./screens.js`` runs the ``registerScreen()`` calls (see :ref:`extending-screens`). Web components ============== Components are Lit elements. ``lit`` and Web Awesome come from the import map; do not bundle them. Components use the translations and the API client of the core. The clock of the attendance package, ````, shortened: .. code-block:: typescript import { LitElement, html, nothing, type TemplateResult } from 'lit'; import { ApiClient } from '@adsum/core/api/ApiClient.js'; import { translate } from '@adsum/core/i18n/translations.js'; export class Clock extends LitElement { public static override properties = { status: { state: true }, }; public declare status: { state: string } | undefined; private readonly api = new ApiClient(); public override connectedCallback(): void { super.connectedCallback(); void this.reload(); } protected override render(): TemplateResult | typeof nothing { return this.status === undefined ? nothing : html`${translate(`clock.state.${this.status.state}`)}`; } private async reload(): Promise { this.status = await this.api.get<{ state: string }>('/me/clock'); } } ``ApiClient`` has ``get()``, ``read()``, ``post()``, ``patch()``, ``put()`` and ``delete()``. It prefixes ``/api/v1``, sends the session cookie and throws on error responses. ``patch()``, ``put()`` and ``delete()`` take the ETag of the resource as the option ``ifMatch``. Icons in components =================== Show an icon with Web Awesome's ``wa-icon`` and the icon library ``adsum``: .. code-block:: html