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

JavaScript API

The window.lat helpers a contributed screen or widget calls, the events every island announces, and the attributes that join a form of your own in.

Raise a message from script

window.lat.flash('Import finished.', 'success');   // or 'error', which stays until dismissed

Confirm before acting

<button type="submit" data-lat-confirm="Delete the selected records? This cannot be undone.">
  Delete
</button>

Ask from script

lat.confirm('Archive 12 articles?', { go: 'Archive', cancel: 'Keep', focus: 'cancel' }, function (yes) {
  if (yes) archive();
});
LabelDoes
goNames the confirming button. Default OK.
cancelNames the other. Default Cancel, localized.
focus'cancel' opens with the cursor on it. Default: the confirming button.

Join a form in

<form method="post" action="/admin/acme/import" data-lat-widget="form" data-lat-focus="first">
AttributeDoes
data-lat-widget="form"Asks before leaving with changes unsaved.
data-lat-refusedThe form holds a submission that was not saved: it opens changed, with the cursor in the first field carrying an error.
data-lat-focusfirst opens with the cursor in the first field; off leaves it alone.
data-lat-confirm-leave="off"Never asks.
data-lat-counterOn a control with maxlength: auto or on.
data-lat-grow="off"On a textarea: keeps the height it was given.
data-lat-preset, data-lat-preset-typeOn a field’s wrapper: the field followed and the shape (slug, url, file, exact).
data-lat-trigger-action, data-lat-trigger-field, data-lat-trigger-conditionOn a field’s wrapper: the dependency, as Field Dependencies spells it.

Keep Enter inside a widget

<div data-lat-widget="acme-lookup" data-lat-enter-scope data-lat-enter-target=".acme-lookup__go">
  <input type="search" name="q">
  <button type="button" class="acme-lookup__go">Look up</button>
</div>
AttributeDoes
data-lat-enter-scopeOn the root of a widget holding an input: Enter acts there and never submits the form around it.
data-lat-enter-target="<selector>"The button Enter presses inside that scope. Without a target, Enter does nothing.

Listen and drive

Every island announces what it does as a DOM event named lat:<component>:<event>, bubbling from its root. Server-side facts are on the event bus.

var stop = lat.on('repeater:added', function (detail, event) {
  console.log(detail.count, 'rows; the new one is', detail.row);
});

// A `before-` event is cancelable: this caps a repeater at five rows.
lat.on('repeater:before-add', function (detail, event) {
  if (detail.count >= 5) event.preventDefault();
});

// Drive an island through its controller.
lat.get(document.querySelector('[data-lat-widget="repeater"]')).add();

// A rich editor: the same three, plus the engine for anything else.
var rich = lat.get(document.querySelector('[data-lat-widget="richeditor"]'));
rich.editor.commands.insertContent('<p>Hello</p>');

// A markdown editor: the same, plus the Preview tab.
lat.get(document.querySelector('[data-lat-widget="markdown"]')).preview();

// A code editor: read, replace, focus.
var code = lat.get(document.querySelector('[data-lat-widget="codeeditor"]'));
code.set(code.get().trim());
code.focus();
EventDetailCancelable
repeater:before-addcountYes
repeater:addedrow, index, countNo
repeater:before-removerow, index, countYes
repeater:removedindex, countNo
repeater:movedrow, from, to, countNo
repeater:duplicatedrow, index, countNo
ref-picker:changedid, label, previousNo
ref-picker:clearedpreviousNo
selection:changedids, countNo
checklist:changedvalues, count, totalNo
flash:showntext, levelNo
flash:dismissedtextNo
confirm:openedtextNo
confirm:confirmed, confirm:cancelledNoneNo
copy:copiedvalueNo
reveal:shown, reveal:hiddenNoneNo
preset:filledfrom, valueNo
trigger:changedfield, metNo
ControllerMethods
Repeateradd(), remove(index), move(from, to), duplicate(index), count()
Record picker, searched selectvalue(), label(), choose({ id, label }), clear()
Checklistvalues(), count(), set(values), all(), none(), search(text)
Formchanged(), settle(): takes the form as it stands for saved
Password revealshow(), hide()
HelperDoes
lat.on(name, handler)Listens page-wide, swapped content included. Returns the function that stops listening.
lat.emit(el, name, detail, cancelable)Announces lat:<name> from el. Returns false when a listener cancelled it.
lat.get(el)The controller of the island at or around el.
lat.scan(root)Starts the islands in markup a script added. Swapped content starts without it.
lat.confirm(text, labels, done)Asks in the admin’s dialog. done hears true or false.
lat.flash(text, level)Raises a message: success, or error, which stays until dismissed.
lat.assets.ensure(url)Loads a stylesheet or script once.

Put a code editor on a contributed screen

<div data-lat-widget="codeeditor" data-language="yaml" style="--rows:16">
  <textarea class="lat-input lat-editor" id="spec" name="spec" rows="16" data-lat-grow="off">{{ spec }}</textarea>
</div>
shell.page_with(PageHead::new("Specification").assets(laterite_admin::CODE_EDITOR_ASSETS), body)
AttributeValuesDefault
data-languagetext, html, css, javascript, json, yaml, markdown, jinjatext
data-line-numbersoffon
data-wraponoff
--rowslines12
readonly on the textareaA read-only editor

Without the script the textarea is the control.

Choose from the library

var picker = lat.mediaPicker({
  multiple: false,
  words: { title: 'Choose from the library', use: 'Use selected', cancel: 'Cancel' },
  onDone: function (items) { console.log(items[0].id, items[0].url); }
});
picker.open('/admin/media/pick?mode=image');
OptionValues
multipletrue, false
wordstitle, use, cancel
onDone(items)each item: id, name, kind, thumb, url, is_image
open(url)the picker fragment, with ?mode=image for images alone

Write an island

window.lat.widget('char-count', function (el) {
  var input = el.querySelector('input');
  function count() { return input.value.length; }
  input.addEventListener('input', function () {
    el.querySelector('.count').textContent = count();
    lat.emit(el, 'acme.char-count:changed', { count: count() });
  });
  // Returned: `lat.get(el).count()` reads it from outside.
  return { count: count };
});
<div data-lat-widget="char-count">
  <input type="text" name="title">
  <span class="count">0</span>
</div>
ReturnDoes
The controller objectWhat lat.get(el) hands out.
rootThe markup an island built around its element; lat.get then finds the controller from inside it too.

Bind by structure, not by id: the same markup may appear more than once on a page and inside swapped content.