.. include:: /Includes.rst.txt .. _extending-screens: ======= Screens ======= A screen is a page of the web interface described as data in TypeScript: a list, a record form, dialogs, commands and custom elements. The core renders it with the element ```` and reads and writes through the HTTP API. A screen has two parts: a definition registered in TypeScript and a page controller in PHP that names the screen and the actions each actor may perform. Register a screen ================= Register screens with ``registerScreen()`` in the entry module of the package (see :ref:`extending-frontend`). The list of working-time models, shortened: .. code-block:: typescript import { registerScreen } from '@adsum/core/screens/registry.js'; registerScreen('workingTimeModels', { list: { path: '/working-time-models', link: '/working-time/models/{id}', columns: [ { key: 'code', label: 'screen.model.code' }, { key: 'name', label: 'screen.common.name' }, { key: 'scheduleType', label: 'screen.model.scheduleType', type: 'enum', enumPrefix: 'screen.model.scheduleType.' }, { key: 'weeklyTargetSeconds', label: 'screen.model.weeklyTarget', type: 'duration' }, ], filters: [ { name: 'archived', label: 'screen.model.archivedOnly', type: 'boolean' }, ], actions: [ { key: 'archive', label: 'screen.common.archive', icon: 'archive', method: 'POST', path: '/working-time-models/{id}/archive', confirm: 'screen.common.archiveQuestion', when: '!archived' }, ], }, create: { collection: '/working-time-models', label: 'screen.model.create', form: { fields: modelFields(false) } }, }); All ``label`` values are translation keys (see :ref:`extending-translations-icons`). Paths are API paths without the ``/api/v1`` prefix. ``{parameter}`` placeholders are filled from the page parameters first, then from the row. Parts of a screen ================= A screen definition has these optional parts; a section may use them too. ``list`` A collection of rows from one API operation. ``create`` A dialog that creates a row of the list. It has ``collection`` (the path to post to), ``label`` and ``form``. ``method: 'PUT'`` with ``wrap`` sends several records in a list. ``reveal`` names a property of the created record that is shown once, such as the secret of a token. ``edit`` A dialog that edits a row of the list: ``resource`` (the path of the row), ``method`` and ``form``. ``record`` One record that is edited in place: ``resource``, ``method`` (``PATCH`` by default, or ``PUT``) and ``form``. ``component`` A custom element, given by its tag name. ``sections`` Tabs. Each section has a ``key``, a ``label`` and any of the parts above. ``subject`` and ``commands`` The record the screen is about and the buttons that act on it (see below). Lists ===== ``list`` has the following properties: ``path`` The collection operation. ``itemsKey`` names the property that holds the rows; the default is ``items``. ``columns`` Each column has ``key``, ``label`` and a ``type``: ``text`` (default), ``localisedText``, ``duration``, ``date``, ``dateTime``, ``boolean``, ``enum``, ``enumList``, ``reference``, ``json`` or ``lookup``. An ``enum`` column needs ``enumPrefix``, the prefix of the translation keys of the values. A ``lookup`` column needs ``lookup``, the collection that names the IDs. ``sort`` makes a column sortable by that value of the ``sort`` parameter. ``filters`` Each filter has ``name`` (the query parameter), ``label`` and a ``type``: ``choice``, ``boolean`` or ``date``. A ``choice`` has ``options`` or ``optionsFrom``. ``facet: true`` offers only values that leave rows. ``search`` The name of the search query parameter of the operation. ``link`` The page a row links to. ``actions`` Row actions: ``key``, ``label``, ``method`` (``POST``, ``DELETE`` or ``EVENT``), ``path``, and optionally ``icon``, ``confirm``, ``when``, ``versionOf``, ``body`` and ``display`` (``text`` shows the label instead of an icon). ``versionOf`` is the resource whose version is sent as ``If-Match``. ``defaults`` Values for parameters the operation requires, such as ``{ from: 'monthStart', to: 'monthEnd' }``. Besides dates, ``today``, ``monthStart``, ``monthEnd``, ``yearStart`` and ``yearEnd`` are allowed. ``perPage``, ``tree``, ``download`` The page size, a tree view of the same rows, and a download link for the same rows (``label`` and ``path``). A ``when`` condition is checked against the row. ``archived`` holds when the property is truthy, ``state:active`` when it equals or contains the value, and a leading ``!`` negates the condition. Forms ===== A form is a list of fields: .. code-block:: typescript const fields: FieldDefinition[] = [ { name: 'code', type: 'text', label: 'screen.model.code', required: true }, { name: 'name', source: 'names', type: 'localisedText', label: 'screen.common.name', required: true }, { name: 'scheduleType', type: 'choice', label: 'screen.model.scheduleType', required: true, options: [ { value: 'fixed', label: 'screen.model.scheduleType.fixed' }, { value: 'flextime', label: 'screen.model.scheduleType.flextime' }, ] }, { name: 'workingTimeModelId', type: 'choice', label: 'screen.rotation.model', optionsFrom: { path: '/working-time-models?perPage=200', label: 'name' } }, ]; The field types are ``text``, ``textarea``, ``email``, ``password``, ``number``, ``duration``, ``date``, ``time``, ``boolean``, ``choice``, ``multiChoice``, ``tags``, ``localisedText`` (one value per language) and ``objectList`` (rows of nested ``fields``). Fields have these further properties: - ``name`` is the property the API writes, ``source`` the property it reads if that is another one. - ``required``, ``readOnly`` and ``nullable`` control input. ``help`` is the translation key of a hint. - ``options`` are fixed choices, ``optionsFrom`` reads choices from a collection (``path``, ``label``, optional ``value``), and ``optionsOf: 'timeZones'`` offers the IANA time zones. - ``section`` is the translation key of a heading shown before the field. - ``default`` is the start value of a new record. - ``objectList`` fields take ``fields``, ``fixedRows`` and ``defaultRows``. Commands ======== Commands are buttons on a screen that act on its ``subject``, the record named by the screen: .. code-block:: typescript registerScreen('employee', { subject: '/employees/{employee}', commands: [ { key: 'cancelExit', label: 'screen.employee.cancelExit', path: '/employees/{employee}/exit/cancel', when: 'exitDate', versioned: true, confirm: 'screen.employee.cancelExitQuestion' }, ], }); A command has ``key``, ``label``, ``path`` and ``method`` (``POST`` by default, or ``DELETE``). Further properties: ``form`` Opens a dialog with the fields and sends them as the request body. ``confirm`` The translation key of a question asked first. ``when`` A condition on the subject. The command is offered only if it holds. ``versioned`` Sends the version of the subject as ``If-Match``. ``then`` The page to open after the command succeeded. ``answer`` A custom element that shows the answer of the operation below the form. The dialog stays open. Use it for a preview. Sections ======== A screen with ``sections`` shows them as tabs. This is the page of one working-time model: .. code-block:: typescript registerScreen('workingTimeModel', { sections: [ { key: 'data', label: 'screen.model.data', record: { resource: '/working-time-models/{model}', method: 'PUT', form: { fields: modelFields(true) } } }, { key: 'versions', label: 'screen.model.versions', list: { path: '/working-time-models/{model}/versions', columns: [ { key: 'effectiveFrom', label: 'screen.model.effectiveFrom', type: 'date' }, ] } }, ], }); A section with ``restricted: true`` is shown only if the action ``
.view`` is allowed. Custom elements =============== ``component`` names a custom element, defined in the entry module of the package. It receives the page parameters as the property ``parameters`` and the attribute ``editable`` if the action named like its tag is allowed. With ``componentRestricted: true`` it is rendered only if that action is allowed. Page controller =============== A page controller connects a route to a screen. It calls ``ScreenPage::render()`` with the screen name, the translation key of the title, the actions and optional parameters: .. code-block:: php #[AsController] final readonly class WorkingTimePageController { public function __construct( private ScreenPage $screens, ) {} #[Route('/working-time/models', name: 'app_working_time_models', methods: ['GET'])] #[Access('hr', 'admin')] public function models(): Response { return $this->screens->render('workingTimeModels', 'screen.model.title', ['create' => ['hr', 'admin'], 'archive' => ['hr', 'admin']]); } #[Route('/working-time/models/{id}', name: 'app_working_time_model', requirements: ['id' => '[0-9a-f-]{36}'], methods: ['GET'])] #[Access('hr', 'admin')] public function model(string $id): Response { return $this->screens->render('workingTimeModel', 'screen.model.record', ['data.edit' => ['hr', 'admin']], ['model' => $id]); } } The third argument maps each action to the actors that may perform it, in the vocabulary of ``#[Access]`` (see :ref:`extending-navigation-access`). The screen offers buttons, row actions and edit forms only for the allowed actions and shows everything else read-only. The fourth argument gives the path parameters of the screen, such as ``{model}``. Action names: - ``create`` and ``edit`` for the dialogs and the record form of the screen - the ``key`` of a row action or a command, such as ``archive`` - ``
.`` inside a section, such as ``data.edit`` or ``versions.view`` The API operation behind a screen checks the access again.