File

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

Description

Represents a command which must be executed with one parameter.

Implements

ICommand IActiveAware

Index

Properties
Methods
Accessors

Constructor

constructor(f: any, canExecute?: any)

Creates an instance of DelegateCommand.

Parameters :
Name Type Optional Description
f any No

The function to call when the command is executed.

canExecute any 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 : false

Stored the value of IsActive.

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

Event triguerred when IsActive changes its value.

Methods

CanExecute
CanExecute(parameter: T)

Query if the command can be executed.

Parameters :
Name Type Optional
parameter T No
Returns : boolean

{boolean}

Execute
Execute(parameter: T)

Executes the command if it can be executed.

Parameters :
Name Type Optional
parameter T No
Returns : void
RaiseCanExecuteChanged
RaiseCanExecuteChanged()

Alerts that CanExecute() has changed its value.

Returns : void

Accessors

IsActive
getIsActive()

Gets the active status of the command.

Returns : boolean
setIsActive(value: boolean)

Sets the active status of the command.

Parameters :
Name Type Optional
value boolean No
Returns : void
import { IActiveAware } from '../../baseframework/IActiveAware';
import { ICommand } from '../../baseframework/ICommand';
import { ClassInfo } from '../../decorators/ClassInfo';
import { SubscriptionEvent } from '../../utils/SubscriptionEvent';

/**
 * Represents a command which must be executed with one parameter.
 *
 * @export
 * @class DelegateCommand
 * @implements {ICommand}
 * @implements {IActiveAware}
 * @template T
 * @wType Microsoft.Practices.Prism.Commands.DelegateCommand`1
 */
@ClassInfo({
  classId: 'DelegateCommand',
  implements: [
    'Microsoft.Practices.Prism.IActiveAware',
    'System.Windows.Input.ICommand',
  ],
})
export class DelegateCommand<T> implements ICommand, IActiveAware {
  /**
   * Event trigerred when `CanExecute` changes its value.
   *
   * @type {SubscriptionEvent<any>}
   * @memberof DelegateCommand
   */
  CanExecuteChanged: SubscriptionEvent<any> = new SubscriptionEvent();

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

  /**
   * Stored the value of `IsActive`.
   *
   * @private
   * @memberof DelegateCommand
   */
  private isActive = false;

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

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

  /**
   * Sets the active status of the command.
   *
   * @type {boolean}
   * @memberof DelegateCommand
   */
  set IsActive(value: boolean) {
    if (this.isActive !== value) {
      this.isActive = value;
      this.IsActiveChanged?.fire([this, {}]);
    }
  }

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

  /**
   * Executes the command if it can be executed.
   *
   * @param {T} parameter
   * @memberof DelegateCommand
   */
  Execute(parameter: T): void {
    this.f(parameter);
  }

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

result-matching ""

    No results matching ""