File

projects/wms-framework/src/lib/basecomponentmodel/Control.ts

Description

Class representing a control template

Index

Properties
Methods

Constructor

constructor(templateModelClass: any, declaringContext?: any)

Creates an instance of ControlTemplate.

Parameters :
Name Type Optional Description
templateModelClass any No

the class of the model for the template component

declaringContext any Yes

Properties

Public declaringContext
Type : any

The declaring context

Static PropertiesToSynchronize
Type : object
Default value : { Visibility: true, Width: true, Height: true, }

Properties to synchronize with original contorl model

Private syncHandler
Type : function

Event handler for synchronization with original model

Public templateModelClass
Type : any
the class of the model for the template component

Methods

StopSynchronizationWithModelProperties
StopSynchronizationWithModelProperties(control: Control)

Stops synchronization with original model

Parameters :
Name Type Optional
control Control No
Returns : void
SynchronizeWithModelProperties
SynchronizeWithModelProperties(control: Control)

Adds handlers to synchronize with original model

Parameters :
Name Type Optional
control Control No
Returns : void
import { RuntimeTypeInfo } from '../baseframework/ReflectionSupport';
import { Brush } from '../media/Brush';
import { KeyEventArgs } from '../models/events/KeyEventArgs';
import { RoutedEventArgs } from '../models/events/RoutedEventArgs';
import { SubscriptionEvent } from '../utils/SubscriptionEvent';
import { Dependency } from './Dependency';
import { DependencyObject } from './DependencyObject';
import { DependencyProperty } from './DependencyProperty';
import { DependencyPropertyChangedEventArgs } from './DependencyPropertyChangedEventArgs';
import { FrameworkElement } from './FrameworkElement';
import { ResourceFileRegistry } from './ResourceFileRegistry';
import { RuntimeStyleInfo } from './RuntimeStyleInfo';
/**
 * Control class
 *
 * @export
 * @class Control
 * @extends {FrameworkElement}
 * @wType System.Windows.Controls.Control
 */
export class Control extends FrameworkElement {
  /**
   * BackgroundProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static BackgroundProperty: DependencyProperty = new DependencyProperty(
    'Background',
    null,
    null
  );

  /**
   * BorderBrushProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   * @wProperty BorderBrushProperty
   */
  static BorderBrushProperty: DependencyProperty = new DependencyProperty(
    'BorderBrush',
    null,
    null
  );

  /**
   * BorderThicknessProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static BorderThicknessProperty: DependencyProperty = new DependencyProperty(
    'BorderThickness',
    null,
    null
  );

  /**
   * CharacterSpacingProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static CharacterSpacingProperty: DependencyProperty = new DependencyProperty(
    'CharacterSpacing',
    null,
    null
  );

  /**
   * DefaultStyleKeyProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static DefaultStyleKeyProperty: DependencyProperty = new DependencyProperty(
    'DeafaultStyleKey',
    null,
    null
  );

  /**
   * FontFamilyProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static FontFamilyProperty: DependencyProperty = new DependencyProperty(
    'FontFamily',
    null,
    null
  );

  /**
   * FontSizeProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static FontSizeProperty: DependencyProperty = new DependencyProperty(
    'FontSize',
    null,
    null
  );

  /**
   * FontStretchProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static FontStretchProperty: DependencyProperty = new DependencyProperty(
    'FontStretch',
    null,
    null
  );

  /**
   * FontStyleProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static FontStyleProperty: DependencyProperty = new DependencyProperty(
    'FontStyle',
    null,
    null
  );

  /**
   * FontWeightProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static FontWeightProperty: DependencyProperty = new DependencyProperty(
    'FontWeight',
    null,
    null
  );

  /**
   * ForegroundProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static ForegroundProperty: DependencyProperty = new DependencyProperty(
    'Foreground',
    null,
    null
  );

  /**
   * IsTabStopProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static IsTabStopProperty: DependencyProperty = new DependencyProperty(
    'IsTabStop',
    true,
    null
  );

  /**
   * PaddingProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static PaddingProperty: DependencyProperty = new DependencyProperty(
    'Padding',
    null,
    null
  );

  /**
   * TabIndexProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static TabIndexProperty: DependencyProperty = new DependencyProperty(
    'TabIndex',
    0,
    null
  );

  /**
   * TabNavigationProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static TabNavigationProperty: DependencyProperty = new DependencyProperty(
    'TabNavigation',
    null,
    null
  );

  /**
   * TemplateProperty dependency property
   *
   * @static
   * @type {DependencyProperty}
   * @memberof Control
   */
  static TemplateProperty: DependencyProperty = new DependencyProperty(
    'TemplateProperty',
    null,
    Control.TemplateCallback
  );

  /**
   * Gets or sets the background property
   *
   * @type {Brush}
   * @memberof Control
   * @wProperty Background
   */
  @Dependency(Control.BackgroundProperty)
  public Background: Brush;

  /**
   * Gets or sets the border brush
   *
   * @type {*}
   * @memberof Control
   */
  @Dependency(Control.BorderBrushProperty)
  public BorderBrush: any;

  /**
   * Gets or sets the border thickness
   *
   * @type {*}
   * @memberof Control
   */
  @Dependency(Control.BorderThicknessProperty)
  public BorderThickness: any;

  /**
   * Gets or sets the font family
   *
   * @type {*}
   * @memberof Control
   */
  @Dependency(Control.FontFamilyProperty)
  public FontFamily: any;

  /**
   * Gets or sets the font size
   *
   * @type {number}
   * @memberof Control
   */
  @Dependency(Control.FontSizeProperty)
  public FontSize: number;

  /**
   * Gets or sets the font stretch
   *
   * @type {*}
   * @memberof Control
   */
  @Dependency(Control.FontStretchProperty)
  public FontStretch: any;

  /**
   * Gets or sets the font style
   *
   * @type {*}
   * @memberof Control
   */
  @Dependency(Control.FontStyleProperty)
  public FontStyle: any;

  /**
   * Gets or sets the font weight
   *
   * @type {*}
   * @memberof Control
   */
  @Dependency(Control.FontWeightProperty)
  public FontWeight: any;

  /**
   * Gets or sets the foreground
   *
   * @type {*}
   * @memberof Control
   */
  @Dependency(Control.ForegroundProperty)
  public Foreground: any;

  /**
   * Gets or sets the is tab stop property
   *
   * @type {boolean}
   * @memberof Control
   */
  @Dependency(Control.IsTabStopProperty)
  public IsTabStop: boolean;

  /**
   * Gets or sets the padding
   *
   * @type {*}
   * @memberof Control
   */
  @Dependency(Control.PaddingProperty)
  public Padding: any;

  /**
   * Gets or sets the tab index
   *
   * @type {number}
   * @memberof Control
   */
  @Dependency(Control.TabIndexProperty)
  public TabIndex: number;

  /**
   * Gets or sets the template
   *
   * @type {*}
   * @memberof Control
   */
  @Dependency(Control.TemplateProperty)
  public Template: any;

  TabNavigation: any = null;

  public DefaultStyleKey: unknown;

  /**
   * The IsEnabledChanged event
   *
   * @memberof Control
   */
  public IsEnabledChanged: SubscriptionEvent<
    (sender: any, e: DependencyPropertyChangedEventArgs) => void
  > = new SubscriptionEvent();

  /**
   *  Gets a control model defined inside the control template model (if assigned)
   *
   * @param {string} name
   * @return {*}  {DependencyObject}
   * @memberof Control
   * @wMethod GetTemplateChild
   */
  public GetTemplateChild(name: string): DependencyObject {
    if (this.templateModel) {
      for (const field in this.templateModel) {
        if (field === name) {
          return this.templateModel[field];
        }
      }
    }
    return null;
  }

  /**
   * Focus control
   *
   * @memberof Control
   * @wMethod Focus
   */
  public Focus(): void {
    const args = new RoutedEventArgs();
    args.OriginalSource = this;
    this.OnGotFocus(args);
    this.GotFocus.fire([this, args]);
  }

  /**
   * Model for the control template
   *
   * This property has value when a `control template` is assigned to this model
   *
   * @type {*}
   * @memberof Control
   */
  templateModel: any;

  /**
   * Flag to determine if the default template must used instead of the dynamic template
   * when resolving the component to use to render the current control.
   *
   *  When a control model has a control template associated, the component of
   * the control template is used to render the current model
   *
   * @type {boolean}
   * @memberof Control
   * @wIgnore
   */
  avoidTemplateModelResolution: boolean;

  ApplyTemplate(): any {
    this.LayoutUpdated.fire([this, null]);
  }

  OnMouseEnter(a0?, args?: any): any {}
  OnMouseLeave(a0?): any {}
  OnMouseLeftButtonDown(a0?): any {}
  OnMouseMove(a0?): any {}
  OnMouseLeftButtonUp(a0?): any {}
  OnKeyDown(args: KeyEventArgs): void {}
  OnKeyUp(sender: any, args?: KeyEventArgs): void {}
  /**
   * OnGotFocus handler
   *
   * @param {RoutedEventArgs} args
   * @memberof Control
   */
  OnGotFocus(args: RoutedEventArgs): void {
    this.hasFocus = true;
  }
  /**
   * OnLostFocus handler
   *
   * @param {RoutedEventArgs} args
   * @memberof Control
   */
  OnLostFocus(args: RoutedEventArgs): void {
    this.hasFocus = false;
  }

  /**
   * OnTextChanged handler
   *
   * @param {*} sender
   * @param {*} [args]
   * @memberof Control
   * @wIgnore
   */
  OnTextChanged(sender: any, args?: any): void {}

  /**
   * Operation to apply when changing control template
   *
   * @param {*} sender
   * @param {DependencyPropertyChangedEventArgs} args
   * @memberof Control
   * @wIgnore
   */
  public OnTemplateChanged(
    sender: any,
    args: DependencyPropertyChangedEventArgs
  ): void {
    if (args.NewValue instanceof ControlTemplate) {
      this.templateModel = new args.NewValue.templateModelClass(
        this,
        args.NewValue.declaringContext
      );
      args.NewValue.SynchronizeWithModelProperties(this);
      if (args.OldValue instanceof ControlTemplate) {
        args.OldValue.StopSynchronizationWithModelProperties(this);
      }

      setTimeout(() => {
        this.OnApplyTemplate();
      });
    }
  }

  /**
   * Callback for the template changed property
   *
   * @static
   * @param {DependencyObject} sender
   * @param {DependencyPropertyChangedEventArgs} args
   * @memberof Control
   * @wIgnore
   */
  static TemplateCallback(
    sender: DependencyObject,
    args: DependencyPropertyChangedEventArgs
  ): void {
    if (sender instanceof Control) {
      sender.OnTemplateChanged(sender, args);
    }
  }

  /**
   * Loads the model for the child elements defined in component.
   *
   * @protected
   * @memberof UserControlModel
   * @wIgnore
   */
  protected loadModels() {}

  /**
   * Tries to apply style from parent or from default
   *
   * @protected
   * @memberof Control
   * @wIgnore
   */
  protected tryApplyStyleFromParentOrDefault(): void {
    if (this.Style == null || this.isDefaultStyleApplied) {
      var style =
        this.getResourceByKey(
          '__DEFAULT_STYLE__' + this.constructor.name.replace(/Model$/, '')
        ) ?? this.tryGetDefaultThemeStyle();
      this.Style = style instanceof RuntimeStyleInfo ? style : null;
      this.isDefaultStyleApplied = this.Style ? true : false;
    }
  }

  /**
   * Tries to resolve style from default theme
   *
   * @private
   * @returns
   * @memberof Control
   */
  protected tryGetDefaultThemeStyle() {
    if (this.DefaultStyleKey instanceof RuntimeTypeInfo) {
      const themeResources =
        ResourceFileRegistry.ResourceFiles.get(
          `/${this.DefaultStyleKey.CompatibilityAssemblyInfo.FullName};component/Themes/Generic.xaml`
        )?.() ?? null;
      return themeResources?.Get?.(
        `__DEFAULT_STYLE__${this.DefaultStyleKey.Name.replace(/Model$/, '')}`
      );
    }
    return null;
  }
}

/**
 *  Class representing a control template
 *
 * @export
 * @class ControlTemplate
 * @wType System.Windows.Controls.ControlTemplate
 */
export class ControlTemplate {
  /**
   *  Properties to synchronize with original contorl model
   *
   * @static
   * @memberof ControlTemplate
   * @wIgnore
   */
  static PropertiesToSynchronize = {
    Visibility: true,
    Width: true,
    Height: true,
  };
  /**
   * The declaring context
   *
   * @type {*}
   * @memberof ControlTemplate
   * @wIgnore
   */
  public declaringContext: any;

  /**
   * Event handler for synchronization with original model
   *
   * @private
   * @memberof ControlTemplate
   */
  private syncHandler: (propertyName: string) => void;

  /**
   * Creates an instance of ControlTemplate.
   *
   * @param {*} templateModelClass the class of the model for the template component
   * @memberof ControlTemplate
   */
  constructor(public templateModelClass: any, declaringContext?: any) {
    this.declaringContext = declaringContext;
  }

  /**
   * Adds handlers to synchronize with original model
   *
   * @param {Control} control
   * @memberof ControlTemplate
   * @wIgnore
   */
  SynchronizeWithModelProperties(control: Control) {
    this.syncHandler = control.change.addHandler((prop) => {
      if (
        prop in control.templateModel &&
        prop in ControlTemplate.PropertiesToSynchronize
      ) {
        control.templateModel[prop] = control[prop];
      }
    });
  }

  /**
   *  Stops synchronization with original model
   *
   * @memberof ControlTemplate
   * @wIgnore
   */
  StopSynchronizationWithModelProperties(control: Control) {
    if (this.syncHandler) {
      control.change.removeHandler(this.syncHandler);
    }
  }
}

result-matching ""

    No results matching ""