cardinal

Desk plugins

TypeScript and Vue that change forms, lists and pages in the desk, on sites that have the app installed.

A YAML-only RecType already works in the desk. Write a plugin when a form needs buttons, a field needs a filtered Link, or the app needs its own page.

cardinal app new NAME --with-frontend writes frontend/, a Vite library build of src/index.ts. The desk imports the bundle only on sites that have the app installed. cardinal build and cardinal start build it.

A form plugin

The scaffold's plugin adds a button to ToDo:

frontend/src/index.ts
import { registerForm } from '@cardinal/desk/plugins'

registerForm('ToDo', (frm) => {
  frm.on('refresh', () => frm.addButton('Library', () => frm.setIndicator('Hello from Library', 'info')))
})

CRM's Holiday List wires two Button fields to RPCs:

crm/frontend/src/holiday-list.ts
export function holidayListForm({ call, failed }: Deps) {
  return (frm: Frm) => {
    const ask = async (method: string) => {
      try {
        const doc = (await call(method, { doc: frm.doc })) as Record<string, unknown>
        frm.setValue('holidays', doc.holidays)
      } catch (e) {
        failed((e as Error).message)
      }
    }
    frm.on('add_to_holidays', () => ask('crm.crm_holiday_list.get_weekly_off_dates'))
    frm.on('clear_table', () => ask('crm.crm_holiday_list.clear_table'))
  }
}

frm.on(event, handler) runs on refresh, when the named field changes, or when a Button field is clicked. <table>.<field> names a field in a child row.

frm methodDoes
setValue(field, value)Sets a field as its control would
setQuery(field, filters)Narrows what a Link lists
setField(field, look)Changes a field's type, options or description
addButton(label, action)Adds a button beside the menu
setPrimaryAction(label, action)Replaces Save, Submit or Update
setIndicator(label, color)Replaces the status indicator
setReadOnly(), disableSave()Lock the form until the next refresh
dialog(component, props)Shows a Vue component over the form
show(component, props, { after })Shows a Vue component on the form
save(), reload()Save, or read the record again

Other registrations

All come from @cardinal/desk/plugins.

FunctionRegisters
registerList(rectype, plugin)A list's indicators and cell text
registerPage(name, component)A desk page listed in the manifest's pages
registerRecordPage(rectype, component)A page that opens saved records in place of the form. ?form=desk still reaches the form
registerWidget(name, component)A workspace widget from the manifest's widgets
registerSetupSlide(slide)A slide in the first-run setup wizard. Its values reach the app's setup_wizard/2 as args["app"]
registerTimelineEntry(rectype, kind)An entry kind on a form's timeline, with its own filter tab
registerPanel(name, component, opts)A side panel on every desk page

List plugins

registerList takes any of these keys. CRM adds a status colour, a Kanban guard and a bulk action on leads:

crm/frontend/src/index.ts
registerList('CRM Lead', {
  indicator: statusIndicator(rows),
  beforeMove: lostMove(load, t),
  actions: [{ label: t('Convert to Deal'), run: (names: string[]) => convertSelected(names, leadDeps) }],
})
KeyDoes
fieldsExtra fields each row loads for indicator and formatters
indicator(row)The row's indicator. Falls back to the RecType's states
formattersPlain text for a column, by fieldname
actionsItems in the Actions menu. run gets the selected names
beforeMove(move)Runs before a Kanban card moves. false cancels; an object saves those values too
empty{text, action} shown when the list has no records and no filter

Panels and timeline entries

CRM docks a Calls panel, shown only on sites with a calling provider:

crm/frontend/src/calls.ts
registerPanel('crm-calls', CallPanel, {
  roles: ['Sales User', 'Sales Manager', 'System Manager'],
  label: 'Calls',
  icon: PhoneIcon,
  shortcut: SHORTCUT,
  when: () => calls.state.medium !== null,
})

when(boot, path) runs again on each boot refetch and navigation, so a flag in the app's boot/1 turns the panel on or off without a reload. floating: true opens it as a card from a corner pill. shortcut is a letter for Ctrl or Cmd.

A timeline kind loads its entries through the sdk, so the viewer's rights apply:

crm/frontend/src/timeline.ts
registerTimelineEntry(rectype, { name: 'calls', label: t('Calls'), icon: PhoneOutgoingIcon, load: loadCalls, component: CallEntry })

load({rectype, name}) returns entries with name, creation and an optional owner, newest first.

Desk components for plugins

@cardinal/desk/plugins also exports PageHeader (an app page's top bar), ForbiddenView (the Not permitted page), ReportChart (a code report's chart) and FlowCanvas (the automation and workflow graph canvas).

Vue, @cardinal/sdk, @cardinal/ui and @cardinal/desk/plugins are not bundled. The desk's import map gives the plugin the desk's own copies. Call your RPCs with request from @cardinal/sdk. See The browser SDK.

The app's own frontend

An app can also serve separate single-page apps. HRMS serves its employee app at /hrms/ and its roster at /hr/:

hrms/app.exs
frontend: [
  %{mount: "hrms", entry: "app/index.html"},
  %{mount: "hr", entry: "roster/index.html"}
]

entry is the index.html of a folder in frontend/. One map without the list works for a single frontend. cardinal app build APP builds the plugin and this frontend.

Test it

The scaffold ships src/index.test.ts, which mocks @cardinal/desk/plugins and runs with bun test in frontend/.

On this page