File

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

Description

Represents a command which must be executed without parameters.

Implements

ICommand

Index

Properties
Methods
Accessors

Constructor

constructor(f: () => void, canExecute?: () => void)

Creates an instance of SimpleDelegateCommand.

Parameters :
Name Type Optional Description
f function No

The function to call when the command is executed.

canExecute function Yes

The function to determine if the command can be executed.

Properties

CanExecuteChanged
Type : SubscriptionEvent<any>
Default value : new SubscriptionEvent()

Event trigerred when CanExecute changes its value.

Private isActive
Default value : true

Stored the value of IsActive.

IsActiveChanged
Type : SubscriptionEvent<any>
Default value : new SubscriptionEvent()

Event triguerred when IsActive changes its value.

Methods

CanExecute
CanExecute()

Query if the command can be executed.

Returns : boolean

{boolean}

Execute
Execute()

Executes the command if it can be executed.

Returns : void
RaiseCanExecuteChanged
RaiseCanExecuteChanged()

Alerts that CanExecute() has changed its value.

Returns : void

Accessors

IsActive
getIsActive()

The active status of the command.

Returns : boolean
setIsActive(value: boolean)
Parameters :
Name Type Optional
value boolean No
Returns : void
import { ICommand } from '../../baseframework/ICommand';
import { SubscriptionEvent } from '../../utils/SubscriptionEvent';

/**
 * Represents a command which must be executed without parameters.
 *
 * @export
 * @class SimpleDelegateCommand
 * @implements {ICommand}
 * @wType Microsoft.Practices.Prism.Commands.DelegateCommand
 */
export class SimpleDelegateCommand implements ICommand {
  /**
   * Event trigerred when `CanExecute` changes its value.
   *
   * @type {SubscriptionEvent<any>}
   * @memberof SimpleDelegateCommand
   */
  CanExecuteChanged: SubscriptionEvent<any> = new SubscriptionEvent();

  /**
   * Event triguerred when `IsActive` changes its value.
   *
   * @type {SubscriptionEvent<any>}
   * @memberof SimpleDelegateCommand
   */
  IsActiveChanged: SubscriptionEvent<any> = new SubscriptionEvent();

  /**
   * Stored the value of `IsActive`.
   *
   * @private
   * @memberof SimpleDelegateCommand
   */
  private isActive = true;

  /**
   * Creates an instance of SimpleDelegateCommand.
   *
   * @param {() => void} f The function to call when the command is executed.
   * @param {(() => boolean)} [canExecute] The function to determine if the command can be executed.
   * @memberof SimpleDelegateCommand
   */
  constructor(private f: () => void, private canExecute?: () => boolean) {}

  /**
   * The active status of the command.
   *
   * @type {boolean}
   * @memberof SimpleDelegateCommand
   */
  get IsActive(): boolean {
    return this.isActive;
  }

  set IsActive(value: boolean) {
    if (this.isActive !== value) {
      this.isActive = value;
      this.IsActiveChanged.fire([this, {}]);
    }
  }

  /**
   * Query if the command can be executed.
   *
   * @return {*}  {boolean}
   * @memberof SimpleDelegateCommand
   */
  CanExecute(): boolean {
    return this.canExecute?.() ?? true;
  }

  /**
   * Executes the command if it can be executed.
   *
   * @memberof SimpleDelegateCommand
   */
  Execute(): void {
    if (this.CanExecute()) {
      this.f();
    }
  }

  /**
   * Alerts that `CanExecute()` has changed its value.
   *
   * @memberof SimpleDelegateCommand
   */
  RaiseCanExecuteChanged() {
    this.CanExecuteChanged?.fire([]);
  }
}

result-matching ""

    No results matching ""