Skip to content

Declaring a tool

A tool plug-in adds a tool to Initiative, beside projects, files and calendars. It runs no code: it declares the tool's rows, its items and their fields, and Initiative creates the tables in each community that installs it. From then on the tool works as a built-in tool does and is offered in every initiative. It is shared, trashed, archived, commented on, tagged, linked, searched and exported like every other tool, and has a switch and a create permission in each initiative, none of which it declares. What it does declare is what each of its tables draws: its lists and an item page for each, in the layout format Initiative draws its own tools in. They are the tool's starting layouts, which each community then changes in the layout editor.

This is Counters, declared as a tool:

import { definePlugin, type LayoutCardField } from "initiative-plugin-sdk/manifest";

const field = (key: string): LayoutCardField => ({ type: "field", props: { field: key } });

export default definePlugin({
  publicId: "beyonders.counters",
  uid: "TSJVTCM0S7GV2X",
  name: "Counters",
  tool: {
    key: "counter_group",
    name: { en: "Counters" },
    instance_name: { en: "Counter group" },
    icon: "gauge",
    fields: [{ key: "description", name: { en: "Description" }, kind: "text" }],
    layouts: {
      lists: [
        {
          kind: "grid",
          card: {
            type: "card",
            children: [
              { type: "items", props: { item: "counter", fields: ["name", "count"], limit: 4, tint: "color" } },
              field("name"),
              { type: "stack", props: { tone: "muted" }, children: [field("description")] },
            ],
          },
        },
        { kind: "table", columns: ["name", "description", "updated_at"] },
      ],
      detail: {
        header: [{ type: "stack", props: { direction: "row", gap: "sm", align: "start" }, children: [field("name"), { type: "actions" }] }],
        main: [field("description"), { type: "relations" }],
        side: [{ type: "section", children: [{ type: "properties" }] }, { type: "byline" }],
      },
    },
    items: [
      {
        key: "counter",
        name: { en: "Counter" },
        fields: [
          {
            key: "count",
            name: { en: "Count" },
            kind: "counter",
            counter: { min: "min", max: "max", step: "step", initial: "initial_count" },
          },
          { key: "min", name: { en: "Minimum" }, kind: "number" },
          { key: "max", name: { en: "Maximum" }, kind: "number" },
          { key: "step", name: { en: "Step" }, kind: "number", default: 1, required: true },
          { key: "initial_count", name: { en: "Starting count" }, kind: "number", default: 0, required: true },
          { key: "color", name: { en: "Colour" }, kind: "color", default: "random" },
          {
            key: "view_mode",
            name: { en: "Look" },
            kind: "select",
            default: "number",
            required: true,
            options: [
              { value: "number", label: { en: "Number" } },
              { value: "bar", label: { en: "Progress bar" } },
              { value: "clock", label: { en: "Segmented clock" } },
            ],
          },
        ],
        checks: [
          { field: "min", at_most: { field: "max" } },
          { field: "step", above: 0 },
        ],
        layouts: {
          lists: [
            {
              kind: "grid",
              card: { type: "card", props: { tint: "color" }, children: [field("name"), field("count")] },
              displays: { count: { from: "view_mode" } },
              presets: [
                { slug: "by-name", name: { en: "By name" }, sort: [{ field: "name" }] },
                { slug: "by-count", name: { en: "By count" }, sort: [{ field: "count", dir: "desc" }, { field: "name" }] },
              ],
            },
            { kind: "table", columns: ["name", "count", "min", "max"], displays: { count: { from: "view_mode" } } },
          ],
          detail: {
            header: [
              { type: "stack", props: { direction: "row", gap: "sm", align: "start" }, children: [field("name"), { type: "actions" }] },
            ],
            main: [field("count"), { type: "relations" }],
            side: [
              {
                type: "section",
                children: ["color", "min", "max", "step", "initial_count", "view_mode"].map(field),
              },
              { type: "section", children: [{ type: "properties" }] },
            ],
          },
        },
        open_while: { field: "count", below: { field: "max" } },
      },
    ],
    actions: [
      {
        id: "reset_all",
        name: { en: "Reset all" },
        on: "counter_group",
        confirm: { en: "Put every counter in this group back to its starting count?" },
        icon: "rotate-ccw",
        write: '{"items": {"counter": [items.counter.{"id": id, "set": {"count": initial_count}}]}}',
      },
    ],
  },
});

Its manifest's plugin_kind is tool and its features are ["tool"], or ["dashboards", "tool"] when it brings dashboards. Beside the tool it carries only dashboards, default_name, min_plugin_api, minimum_age, availability and help: no service, hosts, scopes, connections, endpoints, widgets, pages, fields, parts, actions, settings or permissions. Only a tool plug-in carries a tool.

  • Keys. The tool's key, each item's and each field's are a lower-case letter, then lower-case letters, digits and _, at most 17 characters, so a kind (the key, ., the plug-in's uid) fits Initiative's kind columns. Initiative names the tables and columns after them. Two plug-ins may declare the same tool key: Initiative tells them apart by the plug-in.
  • Columns every row has. id, initiative_id, name, position, created_at, updated_at, created_by, created_by_kind, deleted_at, deleted_by, deleted_by_kind, purge_at, archived_at, held_at, hold_id, listing_uid, listing_version, comments_enabled, comments_locked_at and actions (TOOL_COLUMNS). A field never takes one of those names, nor tags, nor one ending in _id, which is how Initiative names an item's key to the row it sits under.
  • The icon is one name from TOOL_ICONS.
  • Fields. At most 32 on the tool's rows and on each item table. A kind is one of text, number, date, datetime, checkbox, url, currency, select, multi_select, user_reference, scale, counter and color. A select or multi_select field has options, each value once; a currency field names its currency; only a counter field has a counter. A default is a value of the field's kind: an option's value for a select, a list of them for a multi_select, #rrggbb for a colour, or random for a colour Initiative picks from its palette for each new row. A counter starts at its initial, and a person is never a default.
  • required: true makes a field's column NOT NULL. A required field has a default, which a row added without the field takes, and which every row already there takes when a version adds the field or makes it required, so the rule never depends on what an earlier version declared. A counter's default is its initial, so it needs none; a person is never a default, so a user_reference field is never required. Counters requires its step, its starting count and its look.
  • A counter's min, max, step and initial are each a number, or the key of a number field of the same table, so each row holds its own bounds. A number step is above 0.
  • checks on the tool or an item: at most 8 rules every row of the table holds to, in open_while's comparisons. Each names a number or counter field of the table and one of below, at_most, above, at_least, equals or not_equals, against a number or { field }, another such field of the table. Initiative makes each a CHECK, so a row where either side is empty passes and a change that breaks one is refused. Where both sides have a default, the defaults a new row starts with hold to it. Counters checks { field: "min", at_most: { field: "max" } } and { field: "step", above: 0 }.
  • Items. At most 4 item tables. An item sits under the tool's rows, or, with parent, under another item that sits under them: two levels at most. An item is deleted with the row it sits under.
  • Layouts. The tool and each item declare layouts: lists, the ways a group of their rows is listed, and a detail, how one is shown on its own page. Counters lists its counter groups as cards, each previewing its first four counters in their colours above its name and description, or as a table; it lists a group's counters in rows or as a grid, each card in the counter's colour with its name centered over its count, drawn in the counter's own look; and a counter's page puts its name and its menu across the top, its count and what it links to in the middle, and how it counts beside them. Each is a tree of parts naming the table's fields:
  • A field is one the table declares, by its key, or one every row has (TOOL_LAYOUT_FIELDS): name, created_by, created_at, updated_at and tags. A layout never names another table's field.
  • lists holds one to four lists, each of a different kind. Each person picks the one they see, and the first is the one they start on. A grid is a card per row, a masonry wall the same cards at their own heights and rows the same cards one under another, each as wide as the list, each drawn as its card says; a table is a column per field in columns. A card holds field, stack (with direction, gap, wrap, align (start, or center for its parts and their text centered) and tone), properties (the initiative's own properties for the row), items and further card parts. Initiative's other lists (a board, a Gantt chart, a calendar, a timeline) are drawn by a status or dates a declaration does not name, so a tool does not list itself in them.
  • A card's props.tint names a color field of the table, which the card is drawn in. A row with no colour draws plain.
  • An items part shows the row's first items of an item table directly under it, in their own order, each a small tile: item names the table, fields at most 4 of its fields, each once, the first the tile's caption and the rest large beneath it, each its value alone, limit how many, from 1 to 8, and tint one of its color fields, which each tile is drawn in.
  • displays draws a scale or counter field in a look, by its key: a scale as a number, stars or a bar, a counter as a number, a bar or a clock. { from: "<select field>" } takes the look from a select field of the row, so each row keeps its own, and every option of that field is a look the drawn field has: Counters' view_mode offers number, bar and clock. A bar and a clock need the counter's bounds, so a row missing one draws a number.
  • presets are at most 20 named sorts a person picks a list in. Each has a slug (lower-case letters and digits, words joined by single -, at most 64 characters, unique in its list), a name in each language, and sort, at most 4 { field, dir }, first to last, each field once. A preset sorts by a number, counter, currency, scale, date, datetime or checkbox field the table declares, or by name, created_at or updated_at (TOOL_SORT_FIELDS); dir is asc (the default) or desc. Picking one sorts what the person sees; the rows' own order, which people drag, stays as it is.
  • A detail has three regions, header, main and side, each a list of parts: field, stack, section (with a title drawn as written, and collapsed), properties, and the parts every tool's page has (TOOL_DETAIL_PARTS): byline, notice, actions, relations and comments. A field there can be required, a person filling it in to add a row, unless it is every row's, a counter or a checkbox.
  • A layout holds at most 300 parts, a table's columns counting as parts, nested at most 6 deep.

On install they become the community's layouts for each table, and from then on they are the community's to change. - open_while says when an item is still open. An open item blocks what it is linked to until it finishes, as a task does. It names one field of the item and one condition on it: - a number or counter field below, at_most, above, at_least, equals or not_equals a number or { field }, another such field; - a checkbox that is true or false; - a select whose value is in a list of its options; - a date or datetime that is not_passed. - Actions. At most 16, each run on the tool's rows or an item's. Initiative first asks the reader for the fields params names, then evaluates write and makes the change it answers (toolChange), under the reader's own standing, so an action can do nothing the reader couldn't do by hand. write reads item, the row it runs on (its id, name and fields); items, the items directly under it, by item key; params; and now. It answers set, values for the row's own fields, items, values for the items under it by their id, or both. A null clears a field. An action's icon, one name from TOOL_ICONS, is drawn before its name.

validateManifest checks all of this.

A tool plug-in's dashboards

A tool plug-in may bring dashboards over its own data, at most 8. Each is published as a dashboard listing of its own, offered to communities that installed the plug-in, and an initiative adds it like any other dashboard. Each tile is one of Initiative's built-in widgets (stat, progress, chart and the rest), and its binding is a query, { source: "query", sql }, never an endpoint: a tool plug-in has none. Counters brings its Counter board:

dashboards: [
  {
    uid: "8V57MMR212TEY5",
    public_id: "beyonders-studio.counter-board",
    name: "Counter board",
    description: "Your counters, front and center.",
    layout: { columns: 12 },
    widgets: [
      {
        id: "w1",
        type: "stat",
        title: "The headline number",
        grid: { x: 0, y: 0, w: 4, h: 3 },
        binding: { source: "query", sql: "SELECT name, count FROM p_tsjvtcm0s7gv2x_counter ORDER BY name" },
      },
      {
        id: "w3",
        type: "chart",
        title: "The whole group",
        grid: { x: 0, y: 3, w: 12, h: 4 },
        options: { mark: "bar" },
        binding: { source: "query", sql: "SELECT name, count FROM p_tsjvtcm0s7gv2x_counter ORDER BY count DESC" },
      },
    ],
  },
],
  • A query reads only the plug-in's own tables, named p_, the plug-in's uid in lower case, _ and the table's key (p_tsjvtcm0s7gv2x_counter). Initiative refuses a version whose query names anything else: a built-in tool's table, another plug-in's or a shared one. A query is at most 20,000 characters.
  • type is a built-in widget and options its options (a chart's mark), which Initiative checks against the widget.
  • It runs as whoever views the dashboard, read-only and with Initiative's limits on time and rows, so a tile shows only rows its viewer may already read.