Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Boards

A board is a dashboard a screen hosts: widgets on a twenty-column grid, each fetched after the page opens, arranged by the people who may.

Host a board

use laterite_admin::board::{Board, BoardCx, Layout, Placement};
use laterite_admin::routes::{RouteCtx, Screen};
use laterite_admin::{AuthenticatedUser, PageHead, Shell};

struct Sales;

impl Screen for Sales {
    fn mount(&self, ctx: &RouteCtx) -> Router {
        let board = Arc::new(
            Board::new(
                "acme.sales",
                Layout::rows(vec![
                    vec![Placement::new("orders", "acme.orders_today", 7).locked()],
                    vec![Placement::new("trend", "acme.revenue", 20)
                        .properties(json!({ "days": "30" }))],
                ]),
            )
            .edit_permission("acme.view_sales")
            .share_permission("acme.manage_sales")
            .offer(["acme.*"]),
        );
        let (page, ctx, base) = (board.clone(), ctx.clone(), ctx.base_path().to_string());
        Router::new()
            .route("/", get(move |Extension(shell): Extension<Shell>,
                                  Extension(user): Extension<AuthenticatedUser>,
                                  Query(query): Query<HashMap<String, String>>| {
                let (board, ctx, base) = (page.clone(), ctx.clone(), base.clone());
                async move {
                    let cx = BoardCx { ctx: &ctx, shell: &shell, user: &user, query: &query, width: 20 };
                    let body = board.body(&cx, &base).await;
                    shell.page_with(PageHead::new(t!("Sales")).assets(&board.assets(&ctx)), body)
                }
            }))
            .merge(board.routes(ctx))
    }
}
Board methodSetsDefault
new(key, layout)the key its layouts are kept under, and the widgets it starts withrequired
editing(Editing)Off, Personal, Shared or BothBoth
view_permission(p)who may open it; its routes refuse anyone elsewhoever reaches the screen
edit_permission(p)who arranges their ownwhoever sees the board
share_permission(p)who changes the layout everyone seesnobody
allow(Allow)which of moving, resizing, adding, removing and configuring edit mode offersall
offer(codes)the kinds Add widget lists; acme.* is every kind under a prefixevery registered kind
with_kind(kind)a kind this board alone offersnone
rows()each row on its own linea widget sits under the one above it in its columns
carry(names)query parameters the board’s requests keep, such as rangenone
Placement methodSets
new(id, kind, width)the widget’s id on this board, its kind’s code, its columns of 20
properties(json)its settings
locked()not moved, resized or removed
keep()no Remove
fixed_settings()no Configure

Write a kind of widget

use laterite_admin::board::{BoardCx, Kind, KindReg};
use laterite_admin::form::FormField;

struct Revenue;

#[laterite_core::strata::async_trait]
impl Kind for Revenue {
    fn code(&self) -> &str {
        "acme.revenue"
    }
    fn label(&self) -> Text {
        t!("Revenue")
    }
    fn group(&self) -> Text {
        t!("Sales")
    }
    fn default_width(&self) -> u8 {
        10
    }
    fn properties(&self) -> Vec<FormField> {
        vec![FormField::of("days", t!("Days"), "select")
            .options(json!({ "options": [{ "value": "7", "label": "7" }, { "value": "30", "label": "30" }] }))]
    }
    fn title(&self, shell: &Shell, properties: &Value) -> Option<String> {
        Some(shell.t("Revenue"))
    }
    async fn render(&self, cx: &BoardCx<'_>, properties: &Value) -> Result<Markup, String> {
        let days = properties["days"].as_str().unwrap_or("30");
        acme::revenue::chart(cx.ctx.db(), days).await.map_err(|e| e.to_string())
    }
}

fn register(&self, registry: &mut laterite_core::Registry) {
    registry.add(KindReg::new(Arc::new(Revenue)));
}
Kind methodReturnsDefault
codevendor.name, unique across modulesrequired
label, group, descriptionits entry in Add widgetgroup Widgets, no description
permissionthe permission needed to see or add itnone
default_width, min_width, max_widthcolumns of 207, 3, 20
resizablewhether its width changestrue
onceat most one on a boardfalse
baredrawn with no card around itfalse
propertiesits settings as form fields; with none it has no Configurenone
properties_for(cx, current)the fields for a widget whose settings are current, where one setting decides another’s choicesproperties()
reload_onthe settings whose change draws Configure again through properties_fornone
check(properties)Err with what to tell the person; nothing is savedOk
titlethe heading above itnone
refreshseconds between fetchesfetched once
renderthe content as Markup; an Err is shown in the widget’s placerequired

BoardCx gives ctx, shell, user, query (the page’s query string) and width.

Let a caller read a widget

use laterite_admin::board::ReadCx;
use laterite_core::serde_json::{json, Value};

fn reads(&self) -> bool {
    true
}

async fn data(&self, cx: &ReadCx<'_>, properties: &Value) -> Result<Value, String> {
    let from = cx.query.get("from").map(String::as_str);
    let points = acme::revenue::by_day(cx.ctx.db(), properties["currency"].as_str(), from).await?;
    Ok(json!({ "points": points }))
}
let board = Arc::new(board);
ctx.add_surface(board.surface("/acme/sales", t!("Sales")));
Kind methodReturnsDefault
readswhether data answersfalse
data(cx, properties)what the widget shows, as JSONErr

ReadCx gives ctx, user and query. Board::surface(path, label) is the board as a surface:

PartControlsActs
the boardeach name it carriesshow
each widget, by its placement idits properties, and what the board carries where it readsread where it reads