File

projects/wms-framework/src/lib/regionsframework/directives/RegionManager.directive.ts

Description

Directive for specifying region names

Implements

OnInit

Metadata

Selector [regionName]

Index

Properties
Methods
Inputs

Constructor

constructor(modelProvider: ModelProviderService)
Parameters :
Name Type Optional
modelProvider ModelProviderService No

Inputs

model
Type : unknown
regionName
Type : string
Default value : null

Methods

ngOnInit
ngOnInit()
Returns : void

Properties

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
      );
    }
  }
}

result-matching ""

    No results matching ""