Dialog Addons
A feature often needs to add something to the dialogs of entities it doesn't own: a History tab, a link to a dashboard of the record, a button that opens a related page. Dialog addons let it do so without a change in the dialogs: the server says which addons a dialog has for the user, and the client adds them as tabs, toolbar buttons or menu items.

Turning Them On
In the script init of the application, e.g. Modules/Common/script-init/dialogaddons-init.ts of StartSharp:
import { DialogAddons } from "@serenity-is/corelib";
import { serverDialogAddonSource } from "@serenity-is/extensions";
import { AuditHistoryView } from "@serenity-is/pro.extensions";
// the addons come from the IDialogAddonProvider's of the server
DialogAddons.addSource(serverDialogAddonSource);
// the component of the History tab
AuditHistoryView.register();
DialogAddons.enableAutoRegister(args => {
// args.cancel = true leaves an addon out of a dialog, e.g. args.addon.Key === "History"
});
From then on, every entity dialog asks the sources for the addons of its entity type once per page: the key of its row,
e.g. Northwind.Order, else its own entity type.
Giving Addons on the Server
An IDialogAddonProvider gives the addons of an entity type that the user may see:
public class CustomerDashboardAddonProvider : IDialogAddonProvider
{
public IEnumerable<DialogAddon> GetAddons(string entityType, IRequestContext context)
{
if (entityType != "Northwind.Customer" || !context.Permissions.HasPermission("Northwind:General"))
yield break;
yield return new DialogAddon
{
Key = "SalesDashboard",
Placement = DialogAddonPlacements.Button,
Title = "Sales",
Icon = "fa fa-bar-chart",
Url = "~/Dashboards/customer-sales?f.Customer={CustomerID}",
Target = "_blank"
};
}
}
services.TryAddEnumerable(ServiceDescriptor.Singleton<IDialogAddonProvider, CustomerDashboardAddonProvider>());
The Services/Extensions/DialogAddon/List service of Serenity.Extensions collects them from all the providers, keeping
the first one of a key, and sorts them by their Order. Only the permitted ones should be given, as the client shows what it gets.
A DialogAddon has:
| Property | |
|---|---|
Key |
Unique in a dialog, e.g. History; the key of its tab. |
Placement |
tab (the default), button, or menu (DialogAddonPlacements). |
Title, Icon, Hint |
What it shows; the title may be a local text key. |
Order |
Its order among the others. |
Component |
The key of the client component that a tab shows, or that a button opens in a dialog. |
Url, Target |
The address that a button or menu item opens instead of a component. |
VisibleOnTab |
The tab of the form that a button or menu item is shown on only. |
ShowForNew |
Whether it is shown for a new record too; false by default. |
Options |
The options of the component. |
A tab is added after the tabs of the form; in a dialog without tabs, it is a button that opens it in a dialog. The menu items of a dialog share a menu button (⋮) in its toolbar.
Placeholders
The Url and the text Options may have placeholders of the fields of the record, e.g. {CustomerID}, which are
filled from the record as it is being edited, URL-encoded in an address. An addon whose placeholder has no value, e.g. as
no customer is chosen yet, is hidden until it has one.
Components
A tab, or a button without an address, shows a component that the client registers by the key the addon gives:
DialogAddons.registerComponent("MyApp.CustomerNotes", (host, context) => {
const view = new CustomerNotesView(host, context.entityId, context.options);
return {
update: context => view.load(context.entityId), // another record was loaded
destroy: () => view.destroy() // the dialog was closed
};
});
The context has the dialog, the addon, the entity type, the ID and the record of the dialog, and the options of the addon with their placeholders filled. A tab creates its component when it is shown first.
DialogAddons.addSource also takes a function of your own, e.g. for addons that don't need the server:
DialogAddons.addSource(entityType => entityType === "Northwind.Order" ?
[{ Key: "Print", Placement: "menu", Title: "Print", Url: "~/Northwind/OrderPrint/{OrderID}" }] : []);
The Building Blocks
The addons are built on hooks that other features can use too:
BaseDialog.onAfterInit, static and per dialog, is raised when a dialog is initialized, with its form, tabs and toolbar there, andEntityDialog.onAfterLoadEntitywhen it loads a record.addPropertyTab(container, { key, title, paneId })adds a tab to the tabs of a property grid and returns its pane, andactivePropertyTabKey(container)gives the key of the active one.
See Also
- Audit Log, whose History tab is an addon
- Entity Links
- EntityDialog and CRUD Workflows