File

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

Description

Implementation of enumerator for resources

Implements

IUntypedDictionaryEnumerator

Index

Properties
Methods
Accessors

Constructor

constructor(collection: unknown)

Creates an instance of ResourceSetDictionaryEnumerator.

Parameters :
Name Type Optional
collection unknown No

Properties

Private currentkeyindex
Default value : -1
Private keys
Type : Array<string>
Default value : []

Methods

MoveNext
MoveNext()

Moves to the next entry in the resource sequence

Returns : boolean

{boolean}

Accessors

Current
getCurrent()

Returns the current key/value pair

Returns : unknown
Key
getKey()

Returns the current key

Returns : unknown
Value
getValue()

Returns the current value

Returns : unknown
import { IUntypedDictionaryEnumerator } from '../baseframework/IUntypedDictionaryEnumerator';
import { SmStream } from '../helpers/StreamsSupport';

/**
 * Regex used to check if a resource is a stream.
 *
 *  @type {*}
 */
const reIsStream =
  /;System\.IO\..*Stream, mscorlib, Version=.*, Culture=.*, PublicKeyToken=.*;AssemblyName=/;

/**
 *  Class to manage resources
 *
 * @export
 * @class ResourceManager
 * @wType System.Resources.ResourceManager
 */
export class ResourceManager {
  BaseName: any = null;

  /**
   *  Resources collection
   *
   * @type {*}
   * @memberof ResourceManager
   */
  collection: any = {};

  constructor(collectionId: string) {
    this.collection =
      GlobalResourceManager.resourceCollections[collectionId] || {};
  }

  /**
   *  Gets a string resource
   *
   * @param {string} key
   * @param {*} [resourceCulture]
   * @return {*}  {string}
   * @memberof ResourceManager
   */
  public GetString(key: string, resourceCulture?: any): string {
    return this.collection[key] || '';
  }

  /**
   * Gets stream information of resource with the given key
   *
   * @param {string} key
   * @param {*} resourceCulture
   * @return {*}  {SmStream}
   * @memberof ResourceManager
   */
  public GetStream(key: string, resourceCulture: any): SmStream {
    const result = new LoadedResourceImageStream();
    const entryParts = (this.collection[key] || '').split(';');
    let imageName = 'assets/';
    if (entryParts.length > 1) {
      const mainDirectoryName = entryParts[entryParts.length - 1].replace(
        /^AssemblyName=/,
        ''
      );
      imageName =
        imageName +
        mainDirectoryName +
        '/' +
        entryParts[0].replace(/^(\.\.\\)*/, '').replace(/\\/g, '/');
    }
    result.FileEntryInfo = imageName;
    return result;
  }

  /**
   * Checks if the resource associated with the given key is a stream.
   *
   * @param {string} key
   * @return {*}  {boolean}
   * @memberof ResourceManager
   * @wIgnore
   */
  public IsStream(key: string): boolean {
    const value = this.collection[key] || '';
    return reIsStream.test(value);
  }

  public GetResourceSet(...params: any[]): ResourceSet {
    return new ResourceSet(this);
  }
}

/**
 *  Special stream created to return information for converted resource(resx) image or file
 *
 * @export
 * @class LoadResourceImageStream
 * @extends {SmStream}
 */
export class LoadedResourceImageStream extends SmStream {
  /**
   *  File name information
   *
   * @type {string}
   * @memberof LoadResourceImageStream
   */
  public FileEntryInfo = '';
}

/**
 *  Implementation of enumerator for resources
 *
 * @class ResourceSetDictionaryEnumerator
 * @implements {IUntypedDictionaryEnumerator}
 */
class ResourceSetDictionaryEnumerator implements IUntypedDictionaryEnumerator {
  private keys: Array<string> = [];
  private currentkeyindex = -1;

  /**
   * Creates an instance of ResourceSetDictionaryEnumerator.
   *
   * @param {unknown} collection
   * @memberof ResourceSetDictionaryEnumerator
   */
  constructor(private collection: unknown) {
    this.keys = Object.keys(collection);
  }

  /**
   *  Moves to the next entry in the resource sequence
   *
   * @return {*}  {boolean}
   * @memberof ResourceSetDictionaryEnumerator
   */
  MoveNext(): boolean {
    if (this.currentkeyindex + 1 < this.keys.length) {
      this.currentkeyindex++;
      return true;
    }
    return false;
  }

  /**
   * Returns the current key/value pair
   *
   * @readonly
   * @type {unknown}
   * @memberof ResourceSetDictionaryEnumerator
   */
  get Current(): unknown {
    const key = this.keys[this.currentkeyindex];
    return [key, this.collection[key]];
  }

  /**
   *  Returns the current key
   *
   * @readonly
   * @type {unknown}
   * @memberof ResourceSetDictionaryEnumerator
   */
  get Key(): unknown {
    return this.keys[this.currentkeyindex];
  }

  /**
   * Returns the current value
   *
   * @readonly
   * @type {unknown}
   * @memberof ResourceSetDictionaryEnumerator
   */
  get Value(): unknown {
    return this.collection[this.Key as string];
  }
}

/**
 *  Class representing the current resource set
 *
 * @export
 * @class ResourceSet
 * @wType System.Resources.ResourceSet
 */
export class ResourceSet {
  /**
   * Creates an instance of ResourceSet.
   *
   * @param {ResourceManager} resManager
   * @memberof ResourceSet
   */
  constructor(private resManager: ResourceManager) {}

  /**
   * Returns an untyped enumerator for the resource set
   *
   * @return {*}  {IUntypedDictionaryEnumerator}
   * @memberof ResourceSet
   */
  GetEnumerator(): IUntypedDictionaryEnumerator {
    return new ResourceSetDictionaryEnumerator(this.resManager.collection);
  }

  /**
   * Returns iterator for the resource values
   *
   * @return {*}  {Iterator<any, any, undefined>}
   * @memberof ResourceSet
   */
  [Symbol.iterator](): Iterator<any, any, undefined> {
    const keys = Object.keys(this.resManager.collection);
    const mapped = keys.map((key) => {
      const value = this.resManager.IsStream(key)
        ? this.resManager.GetStream(key, null)
        : this.resManager.collection[key];
      return { Key: key, Value: value };
    });
    return mapped[Symbol.iterator]();
  }
}

export class GlobalResourceManager {
  static resourceCollections = {};

  public static getCollection(id: string): any {
    return GlobalResourceManager.resourceCollections[id];
  }

  public static registerResourceEntry(
    resourceCollectionKey: string,
    resourceKey: string,
    value: string
  ): void {
    let collection: any = null;
    if (
      !(collection =
        GlobalResourceManager.resourceCollections[resourceCollectionKey])
    ) {
      GlobalResourceManager.resourceCollections[resourceCollectionKey] =
        collection = {};
    }
    collection[resourceKey] = value;
  }
}

result-matching ""

    No results matching ""