Web UI Design: Entity Masters - fieldenms/tg GitHub Wiki

Entity Masters

Everything in TG is modelled by means of fractal objects -- an entity and its companion. This includes entities that model both nouns and verbs in the target domain. So, objects like Work Activity and Purchase Order, but also processes like Purchase Order Authorisation and Issuing of Part to a Work Order are modelled as entities. Each entity may have its own Entity Master through which it gets created and modified. Basically, entity masters represent the main way to organise a UI for users to modify the state of an information system. Therefore, it is very important to understand all of the building blocks for constructing Entity Masters and the ways to interact between them at the client side, and also them and corresponding web resources.

  1. Kinds of Entity Masters
  2. Simple Entity Master
  3. Compound Entity Master
  4. Multistep Entity Master
  5. Master Components
  6. Declaring components
  7. Blocking while the entity is unsaved
  8. Refreshing
  9. Layout and focus
  10. Platform components
  11. Application components
  12. Entity Master Actions
  13. Property action affordance
  14. Entity action affordance
  15. When actions should not be executed?

Kinds of Entity Masters

There are two kinds of Entity Master, each operating in their well-defined and very specific way.

Simple Entity Master

Simple Entity Masters are used to represent an entity without dependent entities or a dependent entity itself. For example, a majority of table code entities have no dependent entities and can be conveniently represented using a Simple Entity Master (e.g. entity Address or Rego).

At the same time, simple dependent entities can also be represented using Simple Entity Masters. For example, WaDirectCharge is an entity that attains its meaning strictly in the context of some WorkActivity, has no dependent entities, and can be conveniently represented as a Simple Entity Master. However, in this case the master carries information about a corresponding master entity (Work Activity), which is critical for both validation and persistence of dependent entities.

Besides editors of properties, a Simple Entity Master can contain read-only components, such as a chart, a map or an embedded entity centre, which are described in Master Components.

Simple Entity Masters could be displayed either in a modal or normal way. The modal way usually has a very specific purpose of either providing a quick preview of some entity or create a simple entity ad-hoc, or provide data for some action (and thus execute that action). A Simple Entity Master in a dialog form should only have two buttons -- CANCEL and OK -- and should always be displayed in the EDIT state. The OK button should correspond to action Save with appropriately defined POST-SAVE action. The CANCEL button, should simply close the dialog and fire the POST-CANCEL event.

Compound Entity Master

Compound Entity Masters are used to represent complex entities -- those that have a master role in one-2-one and one-2-many associations. Such masters are equipped with a menu to navigate between their compound parts. The depth of the menu should be limited to one, consisting of the top level and the level below at the maximum. This constraint ensures the simplicity of the navigation system from the user perspective, assuming that it should always be possible to arrange Entity Master menu in this way.

The way to represent one-2-one association should be as per Swing UI, where a separate menu entry is associated with panel with located on it editors for properties of the dependent entity. It can be thought of as an embedded simple master, provisioning for standard and custom actions.

A one-2-many association should be represented by a Custom Entity Centre with the ability to edit and add new dependent entities by invoking a corresponding Simple Entity Master in a dialog mode.

Multistep Entity Master

The Multistep Entity Masters should represent a wizard like approach to data entry, where information gets entered page by page (step by step). There are two possible ways to implement Multistep Entity Masters:

  1. Multi entity approach where each page has a separate entity behind (persistent or most likely functional) with its own web-resource and a companion object. Pressing button Next should result in a POST request to a corresponding web-resource to "execute" (save) a presented entity instance. If such execution is successful, the client navigates to the next page, which gets associated with a returned entity instance of a different type. Otherwise an error is presented to the user and the current page does not change. The process continues until the last page results in a successful POST request.

  2. Single entity approach where a single entity instance get its properties populated at different steps of the Multistep Entity Master (i.e. on different pages). Actuation of the Next button on every page should result in a POST request to the same entity specific web-resource. But, each time the entity instance would have more of its properties populated. Basically, is page in the Multistep Entity Master represents a set of logically grouped properties of the same entity instance that should be entered at the same time.

Master Components

A Simple Entity Master can contain read-only web components next to the editors of its properties, such as a chart, a map, or an entity centre whose data depends on the master entity. Components display data and invoke actions, but do not change property values. A component is bound to the fully-fledged entity of the master, and whatever else it needs is declared through attributes. Components were introduced in issue #2846, whose change overview has more details.

Declaring components

There are two forms of a component in the Entity Master DSL:

  • addProp(prop).asComponent(importPath) -- a component in place of an editor of a property, which may be dot-notated; it has the title and description of the property, and property actions as editors do.
  • addComponent(importPath) -- a component bound to the entity rather than to a property, placed next to the editors; it has no title unless declared, and its action is shown where the component places it, as property actions are.

The import path is resolved as /resources/<importPath>.js, and the element name is the last segment of the path, unless declared with withElementName.

final IMaster<Location> masterConfig = new SimpleMasterBuilder<Location>()
    .forEntity(Location.class)
    .addProp(Location_.key()).asSinglelineText().also()
    .addProp(Location_.desc()).asMultilineText().also()
    // a map in place of an editor of the GIS information of the location
    .addProp(Location_.gisInfo()).asComponent("gis/tg-map-component")
        .withAttr("gis-component-uri", "gis/location/tg-location-gis-master-component")
        .withAttr("gis-component-name", "LocationGisMasterComponent")
        .skipBlockingWhenUnsaved()
        .withAction(editGisInfoAction)
        .also()
    // an entity centre of the work orders of the location
    .addComponent("centre/tg-centre-component")
        .withTitle("Active Work Orders by Status")
        .withHeight(300)
        .withAttr("mi-type", MiLocationMaster_ActiveWorkOrders.class.getName())
        // the centre follows the status of the location as the user changes it, before the location is saved
        .skipBlockingWhenUnsaved()
        .refreshWhen(LocationStatusChanged.class)
        .also()
    .addAction(MasterActions.REFRESH)
    .addAction(MasterActions.SAVE)
    .setLayoutFor(Device.DESKTOP, Optional.empty(), layout)
    .done();

Both forms have the same options, each of them optional, declared in this order:

Method Purpose
withTitle / withDesc The title and its tooltip.
withElementName The element name, in place of the last segment of the import path.
withHeight Makes the component resizable by the user, keeping the declared height, or the height set by the user, at least.
withMinHeight The least height of the component.
withAttr / withPropAttr An attribute with a static value or a binding expression over the master, such as [[centreUuid]], or bound to the value of a property of the master entity.
skipBlockingWhenUnsaved Keeps the component interactive while the entity is unsaved.
refreshWhen The condition for refreshing the component after each validation of a change made by the user.
withAction / withMultiAction The action of the component.

The data that a component displays must be fetched with the master entity, including the property that it represents and the properties bound with withPropAttr. The configuration is validated when the master is constructed, and errors are reported with EntityMasterConfigurationException.

Blocking while the entity is unsaved

A component is visible as soon as the master is loaded, so, unlike a section of a compound master, nothing would prevent interaction with it while the entity is unsaved. Therefore, by default, a component is blocked while the master entity is an instance of a persistent type that is new or has changes that are not yet saved, including edits in progress. A pane covers the component with a message to save or cancel the changes, and neither the component nor its actions can be used, including through shortcuts. skipBlockingWhenUnsaved() opts out, for components that display values of the master entity itself, or data that depends on them, such as an embedded centre restricted by a property of the entity, which remain consistent with the entity while it is unsaved.

Refreshing

The master asks its components to refresh whenever it retrieves the entity, such as when the master is opened or its changes are cancelled, and whenever it saves the entity. Between these, a component in place of an editor is refreshed whenever the value of its property changes, whether by the user or by a definer, while a component bound to the entity is not refreshed. refreshWhen replaces this default with a condition, which the server evaluates after each validation of a change made by the user:

public class LocationStatusChanged implements IComponentRefreshCondition<Location> {
    @Override
    public boolean shouldRefresh(final ComponentRefreshContext<Location> context) {
        return context.isChanged(Location_.status());
    }
}

What refreshing means is up to the component, such as running an embedded centre again. A refresh requested while the component is blocked takes place once it gets unblocked.

A component that is blocked while the entity is unsaved is blocked whenever the user has changed the entity, and gets unblocked when the changes are saved or cancelled, which refreshes all components anyway. So refreshWhen, and the refresh of a component in place of an editor whenever the value of its property changes, take effect for components declared with skipBlockingWhenUnsaved(), such as the embedded centre in the example above, which follows the status of the location as the user changes it, and in masters of entities that are not persistent.

Layout and focus

A component takes a cell in the master layout in the order of declaration, as an editor does, and fills the height of its cell. In a flexible row, such as FLEXIBLE_ROW of LayoutComposer, a component grows and shrinks with the master, down to the height that it keeps at least. The height set by the user by resizing a component is remembered, and a double tap on the resizer restores the declared height. For a component declared with addComponent, the height is remembered by the position of the component among such components, so once they are added, removed or reordered in the master, a remembered height applies to the component that takes that position, until the user restores it with a double tap.

A component takes part in the focus traversal of the master in its place among the editors, and is focused as a whole, as a rich text editor is. Enter moves the focus into the content of the component, where Tab and Shift+Tab cycle, and Esc moves it back; an Esc that closes a list opened from the content, such as the result list of an autocompleter, closes only the list. The master focuses a component when it shows the entity, as it focuses an editor, unless the component is blocked.

Platform components

  • centre/tg-centre-component embeds the entity centre of menu item type mi-type. It is the most universal of the components, as besides the grid, the centre can present its data in alternative views, declared with addAlternativeView, such as a chart, a map or any other representation, and an alternative view made preferred with makePreferred() is shown in place of the grid. Therefore, it should be considered first, before developing an application component. The context of the centre includes the master entity with its unsaved changes, so a query enhancer with context().withMasterEntity() can restrict the query by it. The centre runs whenever the component is refreshed, and has its own uuid, so that its actions do not refresh the master. Its grid fills the component if the centre declares fitToHeight(), and otherwise has the height of its rows, which leaves room for insertion points of the centre.
  • gis/tg-map-component draws the value of its property, an entity or a collection of entities, or the master entity itself when bound to the entity. The entities are drawn by a GIS component of the application, a subclass of MasterGisComponent that is exported as gis-component-name from module gis-component-uri. Attribute labels-toggle adds a toggle of the labels of map features, and property actions reach the map as action.mapComponent.

Application components

An application introduces its own component where its data needs a presentation that neither an embedded centre nor a map provides, such as a gauge of a value of the master entity. An application component is a Polymer element that mixes in TgMasterComponentBehavior (master/tg-master-component-behavior.js), which implements the contract of a component, so that the element itself only presents its data. Introducing a component takes three steps.

1. Create the element

The module of the element is placed among the web resources of the application, in its web UI module, such as src/main/web/fielden/components/tg-gauge-component.js, from where it is served as /resources/components/tg-gauge-component.js. The element name is the name of the module, unless declared otherwise with withElementName.

import { html, PolymerElement } from '/resources/polymer/@polymer/polymer/polymer-element.js';
import { mixinBehaviors } from '/resources/polymer/@polymer/polymer/lib/legacy/class.js';
import { TgMasterComponentBehavior } from '/resources/master/tg-master-component-behavior.js';

const template = html`
    <style>
        :host {
            display: flex;
            flex-direction: column;
        }
        #content {
            flex: 1;
        }
        .gauge {
            height: 24px;
            background-color: #03a9f4;
        }
    </style>
    <div class="tg-component-title" hidden$="[[!propTitle]]" tooltip-text$="[[propDesc]]">[[propTitle]]</div>
    <div id="content" tabindex="0">
        <div class="gauge" style$="[[_gaugeStyle(_value, max)]]"></div>
    </div>
    <slot name="property-action"></slot>
`;

class TgGaugeComponent extends mixinBehaviors([TgMasterComponentBehavior], PolymerElement) {

    static get template() {
        return template;
    }

    static get properties() {
        return {
            // declared with withAttr("max", "100") in the Entity Master DSL
            max: {
                type: Number,
                value: 100
            },
            // the default least height, unless declared with withMinHeight
            minHeight: {
                type: String,
                value: '120px'
            },
            // the value that the gauge shows, taken from the entity at each refresh
            _value: {
                type: Number,
                value: 0
            }
        };
    }

    // the element at which the component is focused as a whole
    component() {
        return this.$.content;
    }

    // called with the entity in place whenever the master retrieves or saves the entity, and whenever the value of the property changes
    refresh() {
        this._value = this.entity.get(this.propertyName) || 0;
    }

    _gaugeStyle(value, max) {
        return `width: ${Math.min(100, 100 * value / max)}%;`;
    }
}

customElements.define('tg-gauge-component', TgGaugeComponent);

The behaviour provides the following, so the element implements only what is specific to it:

  • entity, the fully-fledged entity of the master, propertyName for a component in place of an editor, and propTitle and propDesc, the title and description of the property or those declared for the component; the element with class tg-component-title is the title, which looks as the title of a focused editor while the focus is within the component.
  • Property actions, which are the children of the element in slot property-action; the element places them with <slot name="property-action"></slot>, such as next to its title or in its toolbar, and the behaviour shows only the action for the entity, which the master selects as for editors.
  • Blocking while the entity is unsaved, resizing with the remembered height, and the vertical margins of editors, 8px above and below, which the element may change in its own styles; attribute resizable is set while the component is resizable, so that the element can keep its content clear of the resizer.
  • Focus: the component is focused as a whole at component(), the element itself by default, which needs tabindex="0"; without tabindex, the focusable elements of the component take part in the focus traversal of the master in their place among the editors.

2. Implement refresh()

refresh() is where a component loads or presents its data for the master entity. The behaviour implements it as doing nothing, and a component overrides it, as the gauge above does.

A refresh takes place:

  • whenever the master retrieves the entity, such as when the master is opened or its changes are cancelled, and whenever it saves the entity;
  • after each validation of a change made by the user for which the condition declared with refreshWhen holds;
  • for a component in place of an editor without refreshWhen, whenever the value of its property changes, whether by the user or by a definer, as compared with the value for which the component was last refreshed, with values compared as values of properties are, such as entities by their keys.

The master asks for the first two by calling requestRefresh() of the component, as described in Refreshing, and the behaviour follows the value of the property for the third.

The behaviour calls refresh() with entity in place, so refresh() reads what it needs from this.entity. It is never called while the component is blocked: a refresh asked for while the component is blocked takes place once it gets unblocked. As a component that is blocked while the entity is unsaved gets unblocked when the changes are saved or cancelled, which refreshes all components anyway, the last two kinds of refresh take effect for a component declared with skipBlockingWhenUnsaved(), as the gauge is, and in masters of entities that are not persistent. It is called at microtask timing, so that refreshes asked for at once, such as when a save also unblocks the component, result in a single call. A component may also call requestRefresh() itself, such as from a button of its own, to refresh on the same terms.

A component that runs something for the master entity on the server does so in refresh(), with the master entity from masterEntityContext(), including its unsaved changes, as the context, in the form in which the contexts of the master carry it, as for property and entity actions. For example, tg-centre-component gives its embedded centre a new function that provides the master entity at each refresh, which loads the centre the first time and runs it again afterwards:

refresh() {
    this._getMasterEntity = () => this.masterEntityContext();
}

An element can also bind to entity directly, as tg-map-component does, as entity follows every change of the master entity, including each validation of a change made by the user. A binding follows every change of the entity, while refresh() takes place only at the moments above, which suits work such as a request to the server, or presenting the value of the property of the component, as the gauge does.

The values that a component reads from the entity must be fetched with the master entity. A component that only presents values of the entity is usually declared with skipBlockingWhenUnsaved(), as its values remain consistent with the unsaved entity.

3. Declare it in the master

The component is declared in the Entity Master DSL with its import path, as the platform components are, with its attributes declared with withAttr or withPropAttr:

.addProp(Location_.capacityUsage()).asComponent("components/tg-gauge-component")
    .withHeight(160)
    .withAttr("max", "100")
    .skipBlockingWhenUnsaved()
    .also()

Declared in place of an editor of capacityUsage without refreshWhen, the gauge is refreshed whenever the value of the property changes, as well as whenever the master retrieves or saves the entity.

Entity Master Actions

All actions on Entity Masters regardless of whether this is a Simple or a Compound Master belong to two categories:

  • Entity related -- the semantics of such actions pertains to an entity as a whole; for example, action Authorise semantically is related to an entity, even though the underlying change really just updates its single property status; actions in this category should only be enabled when Entity Master is in state VIEW. This is required to make sure that all entity changes are persisted. Often, the post-action of such actions should lead to invocation of a standard action Refresh to make sure that the latest data is displayed to the user.

  • Property related -- the semantics of such actions pertains to a single property modification; such actions should only be enabled when Entity Master is in EDIT state; as good example of such action is ad-hoc creation of new value to be assigned to a specific property; there should always be a visual cue that associates action with a specific property such as a button that is located next to a property editor.

Property action affordance

It is proposed that a single property related action should be represented by using icon buttons, located immediately to the right of a property editor. In case of Polymer this should be a paper-icon-button.

There could be a situation where more than one property action is required. In such cases a paper-menu-button should be used, which basically looks like a paper-icon-button, but invokes a dropdown menu with a selection of actions to perform.

While designing the user interaction, the use of a single action should be preferred over the use of menu-button. This ensures simpler and more functional (usable) UX.

Action API should provide a way to specify upon what Entity Master state a property action becomes enabled. From the layout perspective, the action icon button should considered as an extension of the widget (e.g. an input) that represents a corresponding property. This basically means that when calculating the width for placing a property editor, the icon button width should also be taken into account as depicted on the image below.

Icon buttons are a good approach as they are based on SVG with transparent background. This makes them very light weight, almost translucent, and thus not "jumping into the face".

Entity action affordance

Entity actions should be located at the bottom of the page, beneath all property editors and other property related widgets. It is proposed that entity actions be represented by a raised button such as a paper-button. Although, the use of icons on raised buttons is permitted, a simple text with a nice ink effect should be preferred.

If at all possible the buttons should be aligned vertically on a single line. The width of all buttons should be the same. If not all buttons can be fit to a single horizontal line, for example, due to a narrow window or screen, then those action buttons that do not fit on the right should wrap to a new line.

Similar as for property actions, Action API should provide a way to specify upon what Entity Master state an entity action should become enabled. The default actions such as Save and Edit have this information embedded and there should be no way to change it.

When actions should not be executed?

Under normal circumstances an Entity Master should be in one of two states -- EDIT or VIEW. Actions become enabled or disabled according to their definition based on these states.

However, there could be a situation where transition from one state to another might take longer than few milliseconds, and during this period the buttons that would become disabled in the destination state could still be pressed by the user.

Therefore, it is important to prevent action execution until the state is undetermined. The client side implementation of actions should take this into account and provide a generic behaviour whereby their execution is prevented for an undetermined master state.

⚠️ **GitHub.com Fallback** ⚠️