-
Notifications
You must be signed in to change notification settings - Fork 2
/
ActionButtonItemDef.tsx
65 lines (55 loc) · 2.33 KB
/
ActionButtonItemDef.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Item
*/
import type { CommandHandler, OnItemExecutedFunc } from "@itwin/appui-abstract";
import type { SizeProps } from "@itwin/core-react";
import { Orientation } from "@itwin/core-react";
import { ItemDefBase } from "./ItemDefBase";
import type { ItemProps } from "./ItemProps";
/** Abstract base class that is used by classes to execute an action when pressed.
* @public
*/
export abstract class ActionButtonItemDef extends ItemDefBase {
private _onItemExecuted?: OnItemExecutedFunc;
/** Command Handler for the action button */
protected _commandHandler?: CommandHandler;
/** Parameters passed to the Command Handler */
public parameters?: any;
/** Size of the action button, as set by handleSizeKnown */
public size?: SizeProps;
/** The default button size for all action buttons */
public static defaultButtonSize = 42;
constructor(itemProps: ItemProps, onItemExecuted?: OnItemExecutedFunc) {
super(itemProps);
this.execute = this.execute.bind(this);
this._onItemExecuted = onItemExecuted;
}
/** Called when the action button is invoked by a click or touch */
public execute(): void {
if (this._commandHandler && this._commandHandler.execute) {
if (this._commandHandler.getCommandArgs)
this._commandHandler.execute(this._commandHandler.getCommandArgs());
else this._commandHandler.execute(this._commandHandler.parameters);
}
// istanbul ignore else
if (this._onItemExecuted) this._onItemExecuted(this);
}
/** Called when the size of the action button is initialized and the size is known */
public handleSizeKnown = (size: SizeProps) => {
this.size = size;
};
/** Determines the dimension in a given orientation */
public getDimension(orientation: Orientation): number {
let dimension = ActionButtonItemDef.defaultButtonSize;
if (this.size)
dimension =
orientation === Orientation.Horizontal
? this.size.width
: this.size.height;
return dimension;
}
}