File

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

Description

Class representing a property setter

Index

Properties
Methods

Constructor

constructor(property?: unknown, value?: unknown)

Creates an instance of Setter.

Parameters :
Name Type Optional
property unknown Yes
value unknown Yes

Properties

Public Property
Type : unknown

Property to set

Public Value
Type : unknown

Value to set

Methods

Apply
Apply(obj: any)

Applies this setter object to the given object instance

Parameters :
Name Type Optional
obj any No
Returns : boolean
Public resolvePropertyName
resolvePropertyName()

Resolves the property name

Returns : string
import { SimpleList } from '../baseframework/collections';
import { RuntimeTypeInfo } from '../baseframework/ReflectionSupport';
import { BindingInfo } from './Bindings/BindingInfo';
import { createBindingFromBindingInfo } from './Bindings/BindingUtils';
import { DependencyObject } from './DependencyObject';
import { DependencyProperty } from './DependencyProperty';

/**
 *  Class representing a property setter
 *
 * @export
 * @class Setter
 * @wType System.Windows.Setter
 */
export class Setter {
  /**
   *  Property to set
   *
   * @type {unknown}
   * @memberof Setter
   */
  public Property: unknown;

  /**
   *  Value to set
   *
   * @type {unknown}
   * @memberof Setter
   */
  public Value: unknown;

  /**
   * Creates an instance of Setter.
   * @param {unknown} [property]
   * @param {unknown} [value]
   * @memberof Setter
   */
  constructor(property?: unknown, value?: unknown) {
    this.Property = property;
    this.Value = value;
  }

  /**
   *  Applies this setter object to the given object instance
   *
   * @param {*} obj
   * @memberof Setter
   * @wIgnore
   */
  Apply(obj: any): boolean {
    let propertyName = '';
    if (typeof this.Property === 'string') {
      propertyName = this.Property;
    } else if (this.Property instanceof DependencyProperty) {
      propertyName = this.Property.name;
    }

    // Verify if the setter is setting a binding or a static resource
    if (
      typeof (this.Value as any)?.bindingPath !== 'undefined' ||
      typeof (this.Value as any)?.resourceKey !== 'undefined'
    ) {
      const binding = createBindingFromBindingInfo(
        this.Value as BindingInfo,
        obj
      );
      const dependencyProperty = obj?.constructor[`${propertyName}Property`];
      if (dependencyProperty instanceof DependencyProperty) {
        obj.SetBinding(dependencyProperty, binding);
      }
    } else if (
      propertyName in obj &&
      typeof this.Value !== undefined &&
      typeof obj.properties[propertyName] === 'undefined'
    ) {
      // Set literal value
      obj[propertyName] = this.Value;
      return true;
    }
    return false;
  }

  /**
   * Resolves the property name
   *
   * @returns
   * @memberof Setter
   */
  public resolvePropertyName(): string {
    let propertyName = null;
    if (typeof this.Property === 'string') {
      propertyName = this.Property;
    } else if (this.Property instanceof DependencyProperty) {
      propertyName = this.Property.name;
    }
    return propertyName;
  }
}

/**
 *  Class representing a collection of property setters
 *
 * @export
 * @class RuntimeStyleInfo
 * @extends {DependencyObject}
 * @wType System.Windows.Style
 */
export class RuntimeStyleInfo extends DependencyObject {
  public TargetType: RuntimeTypeInfo;

  /**
   * The runtime style name
   *
   * @type {string}
   * @memberof RuntimeStyleInfo
   * @wIgnore
   */
  public Name: string = null;

  /**
   * A collection of setters
   *
   * @type {SimpleList<Setter>}
   * @memberof RuntimeStyleInfo
   */
  Setters: SimpleList<Setter> = new SimpleList();

  /**
   * BaseOn property
   *
   * @type {RuntimeStyleInfo}
   * @memberof RuntimeStyleInfo
   */
  public BasedOn: RuntimeStyleInfo;

  /**
   * IsSealed property
   *
   * @type {boolean}
   * @memberof RuntimeStyleInfo
   */
  public IsSealed: boolean;

  /**
   * Creates an instance of RuntimeStyleInfo.
   * @param {RuntimeTypeInfo} [type]
   * @memberof RuntimeStyleInfo
   */
  constructor(type?: RuntimeTypeInfo) {
    super();
  }

  public withName(name: string): RuntimeStyleInfo {
    this.Name = name;
    return this;
  }
}

result-matching ""

    No results matching ""