UNPKG

@itwin/appui-abstract

Version:
167 lines • 7.1 kB
/*--------------------------------------------------------------------------------------------- * Copyright (c) Bentley Systems, Incorporated. All rights reserved. * See LICENSE.md in the project root for license terms and full copyright notice. *--------------------------------------------------------------------------------------------*/ /** @packageDocumentation * @module Dialog */ import { BeUiEvent } from "@itwin/core-bentley"; import { PropertyEditorParamTypes } from "../properties/EditorParams"; import { PropertyRecord } from "../properties/Record"; import { PropertyValueFormat } from "../properties/Value"; import { PropertyChangeStatus, UiDataProvider } from "./UiDataProvider"; /** Enum for button types. Determines button label, and default button style. * @public */ export var DialogButtonType; (function (DialogButtonType) { DialogButtonType["None"] = ""; DialogButtonType["Close"] = "close"; DialogButtonType["OK"] = "ok"; DialogButtonType["Cancel"] = "cancel"; DialogButtonType["Yes"] = "yes"; DialogButtonType["No"] = "no"; DialogButtonType["Retry"] = "retry"; DialogButtonType["Next"] = "next"; DialogButtonType["Previous"] = "previous"; })(DialogButtonType || (DialogButtonType = {})); /** Enum for button style. * @public */ export var DialogButtonStyle; (function (DialogButtonStyle) { DialogButtonStyle["None"] = ""; DialogButtonStyle["Primary"] = "iui-cta"; DialogButtonStyle["Hollow"] = "iui-default"; DialogButtonStyle["Blue"] = "iui-high-visibility"; })(DialogButtonStyle || (DialogButtonStyle = {})); /** * @public */ export class UiLayoutDataProvider extends UiDataProvider { _items; /** Applies changes from one or more properties - some dialogs will use this to send a bulk set of changes back to the provider */ processChangesInUi(properties) { // Default implementation is to just pass each property to applyUiPropertyChange properties.forEach((property) => this.applyUiPropertyChange(property)); return { status: PropertyChangeStatus.Success }; } /** Applies change of a single property - this is the default method used when property editors are dynamically generated. */ // istanbul ignore next applyUiPropertyChange = (_updatedValue) => { throw (new Error("Derived UiDataProvider should implement this to apply change to a single property.")); }; _rows; /** Array of dialog rows */ get rows() { if (!this._rows) { this._rows = this.layoutDialogRows(); } return this._rows; } loadItemsInternal(items) { this._items = items ? items : []; this._rows = this.layoutDialogRows(); } /** Called by UI to request available properties that can be bound to user supplied UI components (See Tool1UiProvider for example). */ // istanbul ignore next supplyDialogItems() { throw (new Error("Derived UiDataProvider must implement this method to supply set of properties.")); } get items() { if (undefined === this._items) { this.loadItemsInternal(this.supplyDialogItems()); } return this._items; } /** Called to inform listeners that new properties are ready for display in UI. */ reloadDialogItems(emitEvent = true) { this.loadItemsInternal(this.supplyDialogItems()); // istanbul ignore else if (emitEvent) this.fireItemsReloadedEvent(); } /** * @internal */ layoutDialogRows() { const rows = []; this.items.forEach((item) => { const row = rows.find((value) => value.priority === item.editorPosition.rowPriority); if (row) { row.items.push(item); } else { rows.push({ priority: item.editorPosition.rowPriority, items: [item] }); } }); // sort rows rows.sort((a, b) => a.priority - b.priority); // sort records rows.forEach((row) => row.items.sort((a, b) => a.editorPosition.columnIndex - b.editorPosition.columnIndex)); return rows; } /** Determines if a dialog item editor wants a label */ static editorWantsLabel(item) { if (item.property.editor && item.property.editor.params) { const params = item.property.editor.params.find((param) => param.type === PropertyEditorParamTypes.SuppressEditorLabel); // istanbul ignore else if (params) return false; } return true; } /** Determines if a dialog items has an associated lock property */ static hasAssociatedLockProperty(item) { return !!item.lockProperty; } /** Gets the disabled state for a given dialog item */ static getItemDisabledState(baseDialogItem) { const dialogItem = baseDialogItem; // istanbul ignore else if (dialogItem === undefined || dialogItem.lockProperty === undefined) return !!baseDialogItem.isDisabled; const value = dialogItem.lockProperty.value; // istanbul ignore next if (value === undefined) return !!baseDialogItem.isDisabled; return !value.value; } /** Gets a property record for a given dialog item */ static getPropertyRecord = (dialogItem) => { const propertyValue = { valueFormat: PropertyValueFormat.Primitive, value: dialogItem.value.value, displayValue: dialogItem.value.displayValue }; const record = new PropertyRecord(propertyValue, dialogItem.property); record.isDisabled = UiLayoutDataProvider.getItemDisabledState(dialogItem); return record; }; /** Determines if a dialog row only contains button group editors */ static onlyContainButtonGroupEditors(row) { for (const item of row.items) { // istanbul ignore else if (UiLayoutDataProvider.hasAssociatedLockProperty(item) || undefined === item.property.editor || "enum-buttongroup" !== item.property.editor.name || UiLayoutDataProvider.editorWantsLabel(item)) return false; } return true; } } /** [[DialogLayoutDataProvider]] Abstract class that allows property values to be passed between hosting API and Dialog that generates and arranges components dynamically * including the buttons at the bottom of the dialog. * @public */ export class DialogLayoutDataProvider extends UiLayoutDataProvider { onButtonsReloadedEvent = new BeUiEvent(); /** Called to inform listeners that modal dialog button data needs to be refreshed. */ fireDialogButtonsReloadEvent() { this.onButtonsReloadedEvent.emit(); } supplyButtonData() { // Derived class should override const buttons = []; // istanbul ignore next buttons.push({ type: DialogButtonType.OK, onClick: () => { } }); // istanbul ignore next buttons.push({ type: DialogButtonType.Cancel, onClick: () => { } }); return buttons; } } //# sourceMappingURL=UiLayoutDataProvider.js.map