/* * Copyright (c) 2010, 2025 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 { aria, ButtonAdapterMenu, ButtonEventMap, ButtonKeyStroke, ButtonLayout, ButtonModel, ContextMenuPopup, Device, DoubleClickSupport, EnumObject, FormField, icons, InitModelOf, KeyStrokeContext, LoadingSupport, Menu, objects, scout, styles, tooltips, Widget } from '../../../index'; export class Button extends FormField implements ButtonModel { declare model: ButtonModel; declare eventMap: ButtonEventMap; declare self: Button; defaultButton: boolean; displayStyle: ButtonDisplayStyle; iconId: string; keyStroke: string; keyStrokeScope: Widget; processButton: boolean; selected: boolean; systemType: ButtonSystemType; preventDoubleClick: boolean; stackable: boolean; shrinkable: boolean; adaptedBy: ButtonAdapterMenu; buttonKeyStroke: ButtonKeyStroke; formKeyStrokeContext: KeyStrokeContext; popup: ContextMenuPopup; $buttonLabel: JQuery; $submenuIcon: JQuery; protected _doubleClickSupport: DoubleClickSupport; constructor() { super(); this.adaptedBy = null; this.defaultButton = false; this.displayStyle = Button.DisplayStyle.DEFAULT; this.gridDataHints.fillHorizontal = false; this.iconId = null; this.keyStroke = null; this.keyStrokeScope = null; this.processButton = true; this.selected = false; this.statusVisible = false; this.systemType = Button.SystemType.NONE; this.preventDoubleClick = false; this.preventClickFocus = true; this.stackable = true; this.shrinkable = false; this.$buttonLabel = null; this.buttonKeyStroke = new ButtonKeyStroke(this, null); this._doubleClickSupport = new DoubleClickSupport(); this._addCloneProperties(['defaultButton', 'displayStyle', 'iconId', 'keyStroke', 'processButton', 'selected', 'systemType', 'preventDoubleClick', 'stackable', 'shrinkable']); } static SystemType = { NONE: 0, CANCEL: 1, CLOSE: 2, OK: 3, RESET: 4, SAVE: 5 } as const; static DisplayStyle = { DEFAULT: 0, TOGGLE: 1, RADIO: 2, LINK: 3, BORDERLESS: 4 } as const; static SUBMENU_ICON = icons.ANGLE_DOWN_BOLD; protected override _init(model: InitModelOf) { super._init(model); this.resolveIconIds(['iconId']); this._setKeyStroke(this.keyStroke); this._setKeyStrokeScope(this.keyStrokeScope); this._setInheritAccessibility(this.inheritAccessibility && !this._isIgnoreAccessibilityFlags()); } override getContextMenuItems(onlyVisible = true): Menu[] { return []; } protected override _initKeyStrokeContext() { super._initKeyStrokeContext(); this._initDefaultKeyStrokes(); this.formKeyStrokeContext = new KeyStrokeContext(); this.formKeyStrokeContext.invokeAcceptInputOnActiveValueField = true; this.formKeyStrokeContext.registerKeyStroke(this.buttonKeyStroke); this.formKeyStrokeContext.$bindTarget = () => { if (this.keyStrokeScope) { return this.keyStrokeScope.$container; } // use form if available let form = this.getForm(); if (form) { return form.$container; } // use desktop otherwise return this.session.desktop.$container; }; } protected _isIgnoreAccessibilityFlags(): boolean { return this.systemType === Button.SystemType.CANCEL || this.systemType === Button.SystemType.CLOSE; } protected _initDefaultKeyStrokes() { this.keyStrokeContext.registerKeyStrokes([ new ButtonKeyStroke(this, 'ENTER'), new ButtonKeyStroke(this, 'SPACE') ]); } protected override _createLoadingSupport(): LoadingSupport { return new LoadingSupport({ widget: this, $container: () => { return this.$field; } }); } protected override _render() { let $button: JQuery; if (this.displayStyle === Button.DisplayStyle.LINK) { // Render as link-button $button = this.$parent.makeDiv('link-button menu-item'); aria.role($button, 'link'); this.$buttonLabel = $button.appendSpan('button-label text'); } else { // render as button $button = this.$parent.makeElement('