Skip to content

Embedding the builder ​

<datum-form-builder> is the whole builder as one element: the palette, the canvas, the inspector and the workspaces beside them. An application places it in its own shell, hands it a form and stores what comes back.

The whole page ​

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Form builder</title>
    <link rel="stylesheet" href="/datum/datum-builder.css">
    <script type="module" src="/datum/datum-builder.js"></script>
  </head>
  <body>
    <datum-form-builder
      id="builder"
      layout="contained"
      style="height: 640px"
      src="/forms/customer-intake.json"
      save="/api/forms/customer-intake"
    >
    </datum-form-builder>

    <script type="module">
      const builder = document.getElementById("builder");

      // The data sources the rendering page will supply: the canvas previews their options and
      // the inspector's Data source picker lists their ids.
      builder.dataSources = {
        countries: [
          { value: "CA", label: "Canada" },
          { value: "US", label: "United States" },
          { value: "MX", label: "Mexico" }
        ]
      };

      // Every edit reports the whole form as authored so far.
      builder.addEventListener("datum-builder-change", ({ detail }) => {
        console.log(detail.reason, detail.definition.id);
      });

      // New, Open, Download and Save are reported once they are done.
      builder.addEventListener("datum-builder-file-action", ({ detail }) => {
        console.log(detail.action, detail.name);
      });
    </script>
  </body>
</html>
ResultThis page, running. Addresses under /forms//api/ are answered by the example.
What the page did

Nothing yet.

The builder needs room. Measured on the builder's own width:

  • Under 900 pixels the side panels become drawers.
  • Under 768 it takes a phone layout with a bar along its bottom holding the palette, the outline and the rest.

So on a wide window the result runs past the text column, into the place of the page outline, which steps aside while the builder is on screen. Press the builder's Full window button (the arrows on its bar) for the builder at the full size of the window.

Sizing it ​

  • layout="contained" keeps the builder's bar and rails inside the box the page gives it, so the page sets a height.
  • layout="page" takes the viewport instead, for a page that is only the builder.

Under a header of the application's own, set --db-shell-top to the header's height so the builder starts below it.

Handing it a form ​

src fetches a definition, and the definition property takes the object. With neither, the builder opens on a new form, which holds a Submit button and nothing else.

dataSources takes the same sources the rendering page will supply. The builder never stores them: the canvas previews their options and the inspector offers their ids.

New, Open and Download ​

The bar leads with the builder's own file actions, over the whole definition as a JSON file.

  • Each asks before it replaces unsaved work and is reported as datum-builder-file-action once it is done.
  • file-actions picks which show: a list such as "open download", or "off" for none.
  • The fileActions property takes the page's own function for any of them.

The page's controls on the bar ​

A control with slot="toolbar" rides the builder's bar, drawn in the builder's look. Mark the main one with data-builder-primary for the accent. The other places on the bar are listed in Customising the builder.

Hearing every edit ​

datum-builder-change fires on every edit with the whole form in detail.definition, ready to store or to hand straight to a <datum-form> on the same page for a live preview. The builder emits what the form takes, so one line sits between them.

index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Form builder with a live preview</title>
    <link rel="stylesheet" href="/datum/datum-builder.css">
    <script type="module" src="/datum/datum-builder.js"></script>
  </head>
  <body>
    <datum-form-builder
      id="builder"
      layout="contained"
      style="height: 640px"
      src="/forms/customer-intake.json"
    >
    </datum-form-builder>
    <datum-form id="preview"></datum-form>

    <script type="module">
      const builder = document.getElementById("builder");
      const preview = document.getElementById("preview");

      // The same data sources on both: the canvas previews them, and the form renders them.
      const dataSources = {
        countries: [
          { value: "CA", label: "Canada" },
          { value: "US", label: "United States" },
          { value: "MX", label: "Mexico" }
        ]
      };
      builder.dataSources = dataSources;
      preview.dataSources = dataSources;

      // What the builder emits is what the form takes: no conversion in between.
      builder.addEventListener("datum-builder-change", ({ detail }) => {
        preview.definition = detail.definition;
      });
    </script>
  </body>
</html>
ResultThis page, running. Addresses under /forms//api/ are answered by the example.
What the page did

Nothing yet.

Edit a component in the builder and the form under it follows. In the column the builder takes its small-screen layout, so press the builder's Full window button (the arrows on its bar) to work in it at the full width of the window, then press it again to see the form.

Saving ​

The first example saves with one attribute, save="/api/forms/customer-intake".

  1. Make an edit and the builder shows "Changes not saved" on its bar.
  2. Choose Save from the ⋮ menu, or press Ctrl/Cmd+S in the builder, and the log shows the PUT the builder sent.
  3. A 2xx answer clears the cue.

Use Server reply to Save to see a refusal or an unreachable server, which show beside the menu as the reason the form was not saved.

That is the simplest of several ways. Saving forms chooses between them, and covers what the builder does when the person leaves with unsaved work.

A server that checks the revision ​

A page that keeps its forms on a server usually needs a request the attributes cannot make, such as one that carries the revision the form was loaded at. For that, hand the builder a function: builder.fileActions = { save, saveLabel } puts Save in the ⋮ menu and on Ctrl/Cmd+S. The builder then:

  • Asks about an open edit.
  • Shows "Saving…" while the promise is pending.
  • Shows the message of a rejection beside the menu.
index.html
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Form builder</title>
    <link rel="stylesheet" href="/datum/datum-builder.css">
    <script type="module" src="/datum/datum-builder.js"></script>
  </head>
  <body>
    <datum-form-builder id="builder" layout="contained" style="height: 640px"></datum-form-builder>

    <script type="module">
      const builder = document.getElementById("builder");
      const FORM_URL = "/api/forms/customer-intake";

      builder.dataSources = {
        countries: [
          { value: "CA", label: "Canada" },
          { value: "US", label: "United States" },
          { value: "MX", label: "Mexico" }
        ]
      };

      // The revision the form was loaded at. Every save sends it back as If-Match, so a save from
      // a page that fell behind another author's is refused with 409, not laid over theirs.
      const loaded = await fetch(FORM_URL).then((response) => response.json());
      let revision = loaded.revision;

      // The definition the builder is handed is its saved one: builder.dirty stays false, and the
      // builder shows no "Changes not saved" cue, until the author changes something.
      builder.definition = loaded.definition;

      async function save() {
        // What this save sends: an edit made while it runs stays unsaved.
        const definition = builder.definition;
        let response;
        try {
          response = await fetch(FORM_URL, {
            method: "PUT",
            headers: { "content-type": "application/json", "if-match": String(revision) },
            body: JSON.stringify(definition)
          });
        } catch {
          throw new Error("the server could not be reached");
        }
        // A thrown error is the reason the builder shows beside its menu.
        if (response.status === 409) throw new Error("someone else saved this form first");
        if (!response.ok) throw new Error("the server answered " + response.status);
        revision = (await response.json()).revision;
        // The form as sent is the saved one now, so the cue clears. An edit made meanwhile stays unsaved.
        builder.savedDefinition = definition;
      }

      // Save in the builder's ⋮ menu, and Ctrl/Cmd+S inside the builder. The builder asks about
      // an edit still open in the inspector first, and shows "Saving…" while the promise is pending.
      builder.fileActions = { save, saveLabel: "Save" };
    </script>
  </body>
</html>
ResultThis page, running. Addresses under /forms//api/ are answered by the example.
What the page did

Nothing yet.

  1. Make an edit and the builder shows "Changes not saved" on its bar.
  2. Choose Save from the ⋮ menu, or press Ctrl/Cmd+S in the builder, and the log shows the PUT with its If-Match revision and the cue clears.
  3. Turn on Someone else saves first and save again: the server has moved on, the PUT is refused with 409 and the page throws, which is what shows beside the menu.

The function sets savedDefinition to the form it sent, not to whatever the builder holds when the answer arrives, so an edit made during the save stays unsaved.

Trying the server's checks in the browser ​

A server judges a submission with the validation sandbox, a WebAssembly program. The builder can run that same program in the page, so an author sees the verdict a submission would get before there is a server to send one to. In Try it, the author turns on WebAssembly validation, and the checks then run on both sides: the browser's as the person meets them, and the sandbox's as the server would answer.

It is on by default and costs nothing until it is used:

  • The builder looks for the sandbox module, datum-wasm.js, beside its own bundle.
  • It fetches the module only when an author first turns the validator on.
  • If the file is not there, Try it says so and checks in the browser alone.
wasmDoes
absent or autoUses datum-wasm.js beside the builder's bundle
a URLUses the module at that address. A URL on another origin is refused unless allow-origins lists it
offNo sandbox. Try it checks in the browser alone
html
<datum-form-builder src="/forms/intake.json" wasm="/assets/datum-wasm.js"></datum-form-builder>

What the run is given, so that it answers as a server would:

  • The form as it stands, unsaved edits included, with its translations.
  • The answers typed in Try it.
  • The rule modules of the page's plugins. A plugin's wasmPlugin is loaded into the sandbox, so its validators and formula functions judge here as they do on the server. See Plugins.
  • The host values the author set in the Settings panel, for rules that read host..

The sandbox cannot reach the network, the file system or the clock, here as on a server. It runs the checks the sandbox owns. Two things are not part of it:

  • A host validator that needs your own database.
  • The limits and hooks a real host adds.

See Validation and the sandbox for which check runs where.

Checks of your own ​

To have Try it ask something else, such as your real server, give the builder a serverChecks object. It wins over wasm.

js
builder.serverChecks = {
  label: "Intake service",
  async run({ root, answers, locale, kinds, hostContext }) {
    const response = await fetch("/api/try", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ root, answers, locale, kinds, hostContext }) });
    return { errors: (await response.json()).errors };
  }
};
  • label names the checks in the Try it panel.
  • run receives the form's root component, the answers, the locale, the form's kinds map when it uses a plugin's or a binding's component type, and the host values when the author gave any.
  • run resolves with { errors }, each error shaped as the sandbox's own.

Next ​

MIT licence. Your forms, your data, your keys.