projects/wms-framework/src/lib/regionsframework/commands/CompositeCommand.ts
Command that contains several nested commands.
Properties |
|
Methods |
|
Accessors |
constructor(checkActivity?: boolean)
|
||||||
Creates an instance of CompositeCommand.
Parameters :
|
CanExecuteChanged |
Type : SubscriptionEvent<any>
|
Default value : new SubscriptionEvent()
|
Event trigerred when |
Private innerCommands |
Default value : new SimpleList<ICommand>()
|
Stores the commands which should be executed. |
CanExecute | ||||||
CanExecute(parameter: any)
|
||||||
Verifies if the command could be executed.
Parameters :
Returns :
boolean
|
Execute | ||||||
Execute(parameter: any)
|
||||||
Execute registered commands.
Parameters :
Returns :
void
|
Private OnCanExecuteChanged |
OnCanExecuteChanged()
|
Returns :
void
|
Private OnRegisteredCommandCanExecuteChanged |
OnRegisteredCommandCanExecuteChanged(sender: any, e: any)
|
Returns :
void
|
RegisterCommand | ||||||
RegisterCommand(command: ICommand)
|
||||||
Registers a new command.
Parameters :
Returns :
void
|
Protected ShouldExecute | ||||||
ShouldExecute(command: ICommand)
|
||||||
Verifies if a command could be executed.
Parameters :
Returns :
boolean
{boolean} |
UnregisterCommand | ||||||
UnregisterCommand(command: ICommand)
|
||||||
Unregisters one command.
Parameters :
Returns :
void
|
RegisteredCommands |
getRegisteredCommands()
|
Gets a sequence of commands.
Returns :
IList<ICommand>
|
import { IList, SimpleList } from '../../baseframework/collections';
import { Exception } from '../../baseframework/Exceptions';
import { IActiveAware } from '../../baseframework/IActiveAware';
import { ICommand } from '../../baseframework/ICommand';
import {
ReflectionHelper,
tryToConvertType,
} from '../../baseframework/ReflectionSupport';
import { SubscriptionEvent } from '../../utils/SubscriptionEvent';
/**
* Command that contains several nested commands.
*
* @export
* @class CompositeCommand
* @implements {ICommand}
* @wType Microsoft.Practices.Prism.Commands.CompositeCommand
*/
export class CompositeCommand implements ICommand {
/**
* Event trigerred when `CanExecute` changes its value.
*
* @type {SubscriptionEvent<any>}
* @memberof CompositeCommand
*/
CanExecuteChanged: SubscriptionEvent<any> = new SubscriptionEvent();
/**
* Stores the commands which should be executed.
*
* @private
* @memberof CompositeCommand
*/
private innerCommands = new SimpleList<ICommand>();
/**
* Creates an instance of CompositeCommand.
*
* @param {boolean} [checkActivity]
* @memberof CompositeCommand
*/
constructor(private checkActivity?: boolean) {}
/**
* Verifies if the command could be executed.
*
* @param {*} parameter
* @return {*}
* @memberof CompositeCommand
*/
CanExecute(parameter: any) {
let hasCommandThatShouldBeExecuted = false;
for (const command of this.innerCommands) {
if (this.ShouldExecute(command)) {
if (!command.CanExecute(parameter)) {
return false;
}
hasCommandThatShouldBeExecuted = true;
}
}
return hasCommandThatShouldBeExecuted;
}
/**
* Execute registered commands.
*
* @param {*} parameter
* @memberof CompositeCommand
*/
Execute(parameter: any) {
for (const command of this.innerCommands) {
if (this.ShouldExecute(command)) {
command.Execute(parameter);
}
}
}
/**
* Registers a new command.
*
* @param {ICommand} command
* @memberof CompositeCommand
*/
RegisterCommand(command: ICommand) {
if (command == null) {
throw new Exception('Argument null exception command.');
}
if (command == this) {
throw new Exception('Cannot register itself into composite command.');
}
if (this.innerCommands.contains(command)) {
throw new Exception(
'Already registered command. It cannot be registered twice.'
);
}
this.innerCommands.add(command);
command.CanExecuteChanged.addHandler(
this.OnRegisteredCommandCanExecuteChanged,
this
);
this.OnCanExecuteChanged();
if (this.checkActivity) {
const activeAwareCommand = tryToConvertType<IActiveAware>(
command,
ReflectionHelper.getInterfaceRuntimeTypeInfo(
'Microsoft.Practices.Prism.IActiveAware'
)
);
if (activeAwareCommand) {
activeAwareCommand.IsActiveChanged.addHandler(
this.OnCanExecuteChanged,
this
);
}
}
}
/**
* Unregisters one command.
*
* @param {ICommand} command
* @memberof CompositeCommand
*/
UnregisterCommand(command: ICommand) {
const removed = this.innerCommands.remove(command);
if (removed) {
command.CanExecuteChanged.removeHandler(
this.OnRegisteredCommandCanExecuteChanged,
this
);
this.OnCanExecuteChanged();
if (this.checkActivity) {
const activeAwareCommand = tryToConvertType<IActiveAware>(
command,
ReflectionHelper.getInterfaceRuntimeTypeInfo(
'Microsoft.Practices.Prism.IActiveAware'
)
);
if (activeAwareCommand) {
activeAwareCommand.IsActiveChanged.removeHandler(
this.OnCanExecuteChanged,
this
);
}
}
}
}
/**
* Gets a sequence of commands.
*
* @readonly
* @type {IList<ICommand>}
* @memberof CompositeCommand
*/
get RegisteredCommands(): IList<ICommand> {
return this.innerCommands;
}
/**
* Verifies if a command could be executed.
*
* @protected
* @param {ICommand} command
* @return {*} {boolean}
* @memberof CompositeCommand
*/
protected ShouldExecute(command: ICommand): boolean {
if (this.checkActivity) {
const activeAwareCommand = tryToConvertType<IActiveAware>(
command,
ReflectionHelper.getInterfaceRuntimeTypeInfo(
'Microsoft.Practices.Prism.IActiveAware'
)
);
if (activeAwareCommand) {
return activeAwareCommand.IsActive;
}
}
return true;
}
private OnCanExecuteChanged() {
this.CanExecuteChanged.fire([this, null]);
}
private OnRegisteredCommandCanExecuteChanged(sender: any, e: any) {
this.OnCanExecuteChanged();
}
}