projects/wms-framework/src/lib/regionsframework/commands/CommandBehaviorBase.ts
Abstract class for the command behavior base
Properties |
Methods |
|
Accessors |
constructor(targetObject: T)
|
||||||
Creates an instance of CommandBehaviorBase.
Parameters :
|
command |
Type : ICommand
|
commandParameter |
Type : any
|
Private CommandCanExecuteChanged |
CommandCanExecuteChanged(object: any, e: any)
|
Handler for the command CanExecuteChanged event
Returns :
void
|
Protected ExecuteCommand |
ExecuteCommand()
|
Executes the command
Returns :
void
|
UpdateEnabledState |
UpdateEnabledState()
|
Updates the enable state of the target object
Returns :
void
|
TargetObject |
getTargetObject()
|
Gets the Target Object
Returns :
T
|
Command | ||||||
getCommand()
|
||||||
setCommand(value: ICommand)
|
||||||
Gets or sets the command
Parameters :
Returns :
void
|
CommandParameter | ||||||
getCommandParameter()
|
||||||
setCommandParameter(value: any)
|
||||||
Gets or sets the command parameter
Parameters :
Returns :
void
|
import { ICommand } from '../../baseframework/ICommand';
import { ButtonBaseModel } from '../../models/controls/ButtonBaseModel';
/**
* Abstract class for the command behavior base
*
* @export
* @abstract
* @class CommandBehaviorBase
* @template T
* @wType Microsoft.Practices.Prism.Commands.CommandBehaviorBase`1
*/
export abstract class CommandBehaviorBase<T> {
#command: ICommand;
#commandParameter: any;
/**
* Creates an instance of CommandBehaviorBase.
* @param {T} targetObject
* @memberof CommandBehaviorBase
*/
constructor(private targetObject: T) {}
/**
* Gets the Target Object
*
* @readonly
* @protected
* @type {T}
* @memberof CommandBehaviorBase
*/
protected get TargetObject(): T {
return this.targetObject;
}
/**
* Gets or sets the command
*
* @memberof CommandBehaviorBase
*/
set Command(value: ICommand) {
if (this.#command) {
this.#command.CanExecuteChanged.removeHandler(
this.CommandCanExecuteChanged,
this
);
}
this.#command = value;
if (this.#command) {
this.#command.CanExecuteChanged.addHandler(
this.CommandCanExecuteChanged,
this
);
this.UpdateEnabledState();
}
}
get Command(): ICommand {
return this.#command;
}
/**
* Gets or sets the command parameter
*
* @memberof CommandBehaviorBase
*/
set CommandParameter(value: any) {
if (this.#commandParameter !== value) {
this.#commandParameter = value;
this.UpdateEnabledState();
}
}
get CommandParameter(): any {
return this.#commandParameter;
}
/**
* Updates the enable state of the target object
*
* @memberof CommandBehaviorBase
*/
UpdateEnabledState() {
if (!this.TargetObject) {
this.Command = null;
this.CommandParameter = null;
} else {
if (this.TargetObject instanceof ButtonBaseModel) {
this.TargetObject.IsEnabled =
this.Command?.CanExecute(this.CommandParameter) ?? true;
}
}
}
/**
* Executes the command
*
* @protected
* @memberof CommandBehaviorBase
*/
protected ExecuteCommand() {
if (this.Command) {
this.Command.Execute(this.CommandParameter);
}
}
/**
* Handler for the command CanExecuteChanged event
*
* @private
* @param {*} object
* @param {*} e
* @memberof CommandBehaviorBase
*/
private CommandCanExecuteChanged(object: any, e: any) {
this.UpdateEnabledState();
}
}