File

projects/wms-framework/src/lib/regionsframework/commands/CommandBehaviorBase.ts

Description

Abstract class for the command behavior base

Index

Properties
Methods
Accessors

Constructor

constructor(targetObject: T)

Creates an instance of CommandBehaviorBase.

Parameters :
Name Type Optional
targetObject T No

Properties

command
Type : ICommand
commandParameter
Type : any

Methods

Private CommandCanExecuteChanged
CommandCanExecuteChanged(object: any, e: any)

Handler for the command CanExecuteChanged event

Parameters :
Name Type Optional
object any No
e any No
Returns : void
Protected ExecuteCommand
ExecuteCommand()

Executes the command

Returns : void
UpdateEnabledState
UpdateEnabledState()

Updates the enable state of the target object

Returns : void

Accessors

TargetObject
getTargetObject()

Gets the Target Object

Returns : T
Command
getCommand()
setCommand(value: ICommand)

Gets or sets the command

Parameters :
Name Type Optional
value ICommand No
Returns : void
CommandParameter
getCommandParameter()
setCommandParameter(value: any)

Gets or sets the command parameter

Parameters :
Name Type Optional
value any No
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();
  }
}

result-matching ""

    No results matching ""