/* * Copyright (c) 2010, 2026 BSI Business Systems Integration AG * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 * which is available at https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 */ import { AdapterData, App, Device, GroupBox, Locale, locales, LogicalGridLayout, ModelAdapterLike, ObjectCreator, ObjectFactory, ObjectFactoryOptions, ObjectIdProvider, objects, ObjectType, Session, strings, TileGrid, UuidPathOptions, ValueField, Widget, widgets } from './index'; import $ from 'jquery'; let $activeElements = null; /** * The minimal model declaration (usually extends {@link ObjectModel}) as it would be used in a nested declaration (e.g. a {@link FormField} within a {@link GroupBox}). * The {@link objectType} is optional as sometimes it might be already given by the context (e.g. when passing a {@link MenuModel} to a method {@link insertMenu()} where the method sets a default {@link objectType} if missing). */ export type ModelOf = TObject extends { model?: infer TModel } ? TModel : object; /** * Model used to initialize an object instance. Usually the same as {@link ModelOf} but with some minimal required properties (mandatory properties). * The definition of the required properties can be done by the object itself by declaring a property called `initModel`. * A typical object with an `initModel` adds an e.g. {@link parent} or {@link session} property which needs to be present when initializing an already created instance. */ export type InitModelOf = TObject extends { initModel?: infer TInitModel } ? TInitModel : ModelOf; /** * Model required to create a new object as child of an existing. To identify the object an {@link objectType} is mandatory. * But as the properties required to initialize the instance are derived from the parent, no other mandatory properties are required. */ export type ChildModelOf = ModelOf & { objectType: ObjectType }; /** * A full object model declaring all mandatory properties. Such models contain all information to create ({@link objectType}) and initialize (e.g. {@link parent}) a new object. */ export type FullModelOf = InitModelOf & ChildModelOf; /** * Represents an instance of an object or its minimal model ({@link ModelOf}). */ export type ObjectOrModel = T | ModelOf; /** * Represents an instance of an object or its child model ({@link ChildModelOf}). */ export type ObjectOrChildModel = T | ChildModelOf; /** * Represents an instance of an object or its type ({@link ObjectType}). */ export type ObjectOrType = T | ObjectType; export type Constructor = new(...args: any[]) => T; export type AbstractConstructor = abstract new(...args: any[]) => T; export type DeepPartial = T extends object ? { [P in keyof T]?: DeepPartial; } : T; export interface ObjectWithType { objectType: string; } /** * Represents an object having an uuid. */ export interface ObjectWithUuid { /** * The unique identifier property of the object. * * This property alone may not be unique within a widget tree (e.g. if template widgets are used). * To get a more unique id use {@link buildUuidPath} instead. */ uuid: string; /** * Parent to be used instead of the default parent when computing the uuid path for the object. * * @see buildUuidPath */ uuidParent?: ObjectWithUuid; /** * Computes a unique identifier for the object. * * Compared to {@link uuid} it also considers the {@link classId} property and may use a fallback logic if none of these two properties are available, see {@link ObjectIdProvider.uuid}. * * This property alone may not be unique within a widget tree (e.g. if template widgets are used). * To get a more unique id use {@link buildUuidPath} instead. * * @param useFallback Optional boolean specifying if a fallback identifier may be created in case an object has no specific identifier set. The fallback may be less stable. Default is true. * @returns the uuid for the object or null. */ buildUuid(useFallback?: boolean): string; /** * Computes a unique identifier for the object considering parent objects (if existing). * * Note: The returned id may not be unique within the application! E.g. if the same form is opened twice, its children will share the same ids. * @param options Optional {@link UuidPathOptions} controlling the computation of the path. * @see ObjectIdProvider.uuidPath. */ buildUuidPath(options?: UuidPathOptions): string; /** * Sets the {@link uuid} property. */ setUuid(uuid: string); } export interface ObjectWithId { id: TId; } export interface ObjectModel { objectType?: ObjectType; } export interface ObjectModelWithId { id?: TId; } export interface ObjectModelWithUuid extends ObjectModel { /** * A unique identifier for the object. Typically, a new random UUID can be used. */ uuid?: string; /** * Parent to be used instead of the default parent when computing the uuid path for the object. * * @see ObjectWithUuid.buildUuidPath */ uuidParent?: ObjectWithUuid; } export interface ReloadPageOptions { /** * If true, the page reload is not executed in the current thread but scheduled using setTimeout(). * This is useful if the caller wants to execute some other code before reload. The default is false. */ schedule?: boolean; /** * If true, the body is cleared first before reload. This is useful to prevent * showing "old" content in the browser until the new content arrives. The default is true. */ clearBody?: boolean; /** * The new URL to load. If not specified, the current location is used (window.location). */ redirectUrl?: string; } function create(objectType: Constructor, model?: InitModelOf, options?: ObjectFactoryOptions): T; function create(model: FullModelOf, options?: ObjectFactoryOptions): T; function create(objectType: string, model?: object, options?: ObjectFactoryOptions): any; function create(model: { objectType: string; [key: string]: any }, options?: ObjectFactoryOptions): any; function create(objectType: ObjectType | FullModelOf, model?: InitModelOf, options?: ObjectFactoryOptions): T; /** * Creates a new object instance. * * Delegates the create call to {@link ObjectFactory.create}. */ function create(objectType: ObjectType | FullModelOf, model?: InitModelOf, options?: ObjectFactoryOptions): T { return ObjectFactory.get().create(objectType, model, options); } function widget(widgetIdOrElement: string | number | HTMLElement | JQuery, partIdOrType?: string | Constructor): T; function widget(widgetIdOrElement: string | number | HTMLElement | JQuery, partId?: string): Widget; /** * Resolves the widget using the given widget id or HTML element. * * If the argument is a string or a number, it will search the widget hierarchy for the given id using {@link Widget.widget}. * If the argument is a {@link HTMLElement} or {@link JQuery} element, it will use {@link widgets.get(elem)} to get the widget which belongs to the given element. * * @param widgetIdOrElement * a widget ID or an HTML or jQuery element * @param partId * partId of the session the widget belongs to (optional, only relevant if the * argument is a widget ID). If omitted, the first session is used. * @returns the widget for the given element or id */ function widget(widgetIdOrElement: string | number | HTMLElement | JQuery, partIdOrType?: string | Constructor): T { if (objects.isNullOrUndefined(widgetIdOrElement)) { return null; } let $elem = widgetIdOrElement; if (typeof widgetIdOrElement === 'string' || typeof widgetIdOrElement === 'number') { // Find widget for ID let partId = typeof partIdOrType === 'string' ? partIdOrType : null; let session = scout.getSession(partId); if (session) { let id = strings.asString(widgetIdOrElement); return session.root.widget(id) as T; } } return widgets.get($elem as (HTMLElement | JQuery)) as T; } export const scout = { objectFactories: new Map(), /** * Returns the first of the given arguments that is not null or undefined. If no such element * is present, the last argument is returned. If no arguments are given, undefined is returned. */ nvl(...args: any[]): any { let result; for (let i = 0; i < args.length; i++) { result = args[i]; if (result !== undefined && result !== null) { break; } } return result; }, /** * Use this method in your functions to assert that a mandatory parameter is passed * to the function. Throws an error when value is not set. * * @param type if this optional parameter is set, the given value must be of this type (instanceof check) * @returns the value (for direct assignment) */ assertParameter(parameterName: string, value?: T, type?: Constructor | AbstractConstructor): T { if (objects.isNullOrUndefined(value)) { throw new Error('Missing required parameter \'' + parameterName + '\''); } if (type && !(value instanceof type)) { throw new Error('Parameter \'' + parameterName + '\' has wrong type'); } return value; }, /** * Use this method to assert that a mandatory property is set. Throws an error when value is not set. * * @param type if this parameter is set, the value must be of this type (instanceof check) * @returns the value (for direct assignment) */ assertProperty(object: object, propertyName: string, type?: Constructor | AbstractConstructor) { let value = object[propertyName]; if (objects.isNullOrUndefined(value)) { throw new Error('Missing required property \'' + propertyName + '\''); } if (type && !(value instanceof type)) { throw new Error('Property \'' + propertyName + '\' has wrong type'); } return value; }, /** * Throws an error if the given value is null or undefined. Otherwise, the value is returned. * * @param value value to check * @param msg optional error message when the assertion fails */ assertValue(value: T, msg?: string): T { if (objects.isNullOrUndefined(value)) { throw new Error(msg || 'Missing value'); } return value; }, /** * Throws an error if the given value is not an instance of the given type. Otherwise, the value is returned. * * @param value value to check * @param type type to check against with "instanceof" * @param msg optional error message when the assertion fails */ assertInstance(value: any, type: Constructor | AbstractConstructor, msg?: string): T { if (!(value instanceof type)) { throw new Error(msg || 'Value has wrong type'); } return value; }, /** * Checks if one of the arguments from 1-n is equal to the first argument. * @param args to check against the value, may be an array or a variable argument list. */ isOneOf(value: any, ...args /* explicit any produces warning at calling js code */): boolean { if (args.length === 0) { return false; } let argsToCheck = args; if (args.length === 1 && Array.isArray(args[0])) { argsToCheck = args[0]; } return argsToCheck.indexOf(value) !== -1; }, create, /** * Ensures the given parameter is an object. * * If it is an object, it will be returned as it is. * If it is an {@link ObjectType}, {@link scout.create} is used to create the object. * * @param model will be passed to {@link scout.create} if an object needs to be created and ignored otherwise. */ ensure(objectOrType: ObjectOrType, model?: InitModelOf): T { if (typeof objectOrType === 'string' || typeof objectOrType === 'function') { return scout.create(objectOrType, model); } return objectOrType; }, /** * Prepares the DOM for scout in the given document. This should be called once while initializing scout. * If the target document is not specified, the global "document" variable is used instead. * * This is used by apps (App, LoginApp, LogoutApp) * * Currently, it does the following: * - Remove the