File
Description
Directive for specifying region names
Implements
Index
Properties
|
|
Methods
|
|
Inputs
|
|
regionName
|
Type : string
|
Default value : null
|
|
model
|
Type : unknown
|
Decorators :
@Input()
|
|
regionName
|
Type : string
|
Default value : null
|
Decorators :
@Input()
|
|
import { Directive, Input, OnDestroy, OnInit, Optional } from '@angular/core';
import { DependencyProperty } from '../../basecomponentmodel/DependencyProperty';
import { DependencyPropertyChangedEventArgs } from '../../basecomponentmodel/DependencyPropertyChangedEventArgs';
import { UIElement } from '../../basecomponentmodel/UIElement';
import { DelayedRegionCreationBehavior } from '../DelayedRegionCreationBehavior';
import { regionManagerInjectionToken } from '../injectionTokens';
import { IRegionManager } from '../IRegionManager';
import { ServiceLocator } from '../servicelocation/ServiceLocator';
import { ModelProviderService } from '../services/modelaccessor.service';
export const regionNameProperty = new DependencyProperty(
'RegionName',
'',
changeCallback
);
export const regionManagerProperty = new DependencyProperty(
'RegionManager',
null,
null
);
export const regionCreationBehaviorProperty = new DependencyProperty(
'RegionCreationBehavior',
null,
null
);
function changeCallback(sender, args): void {
if (
sender instanceof UIElement &&
args instanceof DependencyPropertyChangedEventArgs
) {
const regionManager = ServiceLocator.current.getInstance(
regionManagerInjectionToken
) as IRegionManager;
const behavior = new DelayedRegionCreationBehavior(
args.ScopedRegionManager ? args.ScopedRegionManager : regionManager
);
behavior.TargetElement = sender;
behavior.Attach();
sender.setValue(regionCreationBehaviorProperty, behavior);
} else {
console.error('Incorrect arguments to change event');
}
}
/**
* Directive for specifying region names
*
* @export
* @class RegionNameDirective
* @implements {OnInit}
*/
@Directive({ selector: '[regionName]' })
export class RegionNameDirective implements OnInit {
@Input()
model: unknown;
@Input()
regionName: string = null;
constructor(@Optional() private modelProvider: ModelProviderService) {}
ngOnInit(): void {
const model = this.model ?? this.modelProvider.model;
if (model instanceof UIElement) {
const currentValue = model.getValue(regionNameProperty) ?? null;
if (currentValue !== this.regionName) {
model.setValue(regionNameProperty, this.regionName);
}
const existingBehavior = model.getValue(regionCreationBehaviorProperty);
if (existingBehavior instanceof DelayedRegionCreationBehavior) {
existingBehavior.ForceRegistration();
}
} else {
console.error(
'Region name property not configured properly: ' + this.regionName
);
}
}
}