projects/wms-framework/src/lib/basecomponentmodel/ResourceDictionary.ts
Contains resources for the application.
Properties |
|
Methods |
Accessors |
Private resourceMap |
Default value : new Map<string, unknown>()
|
Contains the resources directly mapped by this ResourceDictionary. |
Private source |
Type : Uri
|
Default value : null
|
Stores the URI from which to fetch resources from. |
Public Add | ||||||||||||
Add(key: string, value: unknown)
|
||||||||||||
Adds a resource into this ResourceDictionary.
Parameters :
Returns :
void
|
Public Contains | ||||||||
Contains(key: unknown)
|
||||||||
Check if a resource with the given key is present in this resource dictionary, or in its merged dictionaries.
Parameters :
Returns :
boolean
{boolean} |
Public Get | ||||||||
Get(key: unknown)
|
||||||||
Gets the resource associated with the given key.
Parameters :
Returns :
any
The resource associated with the key, or |
Public Remove | ||||||||
Remove(key: string)
|
||||||||
Removes the resource with the given key from this resource dictionary. Note: this method does not remove the key from merged dictionaries, even if it is present on those.
Parameters :
Returns :
void
|
()
|
Iterates throw key-resource pairs in this resource dictionary. Note: this iterator does not iterate over merged dictionaries.
Returns :
Iterator<any>
{Iterator |
MergedDictionaries |
getMergedDictionaries()
|
Gets the resource dictionaries which have been merged with this ResourceDictionary.
Returns :
ObjectModelCollection<ResourceDictionary>
|
Source | ||||||
getSource()
|
||||||
The source URI from which to fetch resources from.
Returns :
Uri
|
||||||
setSource(value: Uri)
|
||||||
Parameters :
Returns :
void
|
import { ObjectModelCollection } from '../baseframework/collections';
import { Uri } from '../baseframework/Uri';
import { ResourceFileRegistry } from './ResourceFileRegistry';
/**
* Contains resources for the application.
*
* @export
* @class ResourceDictionary
* @wType System.Windows.ResourceDictionary
*/
export class ResourceDictionary {
/**
* Contains the resources directly mapped by this ResourceDictionary.
*
* @private
* @memberof ResourceDictionary
*/
private resourceMap = new Map<string, unknown>();
/**
* Contains the resource dictionaries that have
* been merged with this ResourceDictionary.
*
* @private
* @type {ObjectModelCollection<ResourceDictionary>}
* @memberof ResourceDictionary
*/
private mergedDictionaries: ObjectModelCollection<ResourceDictionary> =
new ObjectModelCollection<ResourceDictionary>();
/**
* Stores the URI from which to fetch resources from.
*
* @private
* @type {Uri}
* @memberof ResourceDictionary
*/
private source: Uri = null;
/**
* Gets the resource dictionaries which have been
* merged with this ResourceDictionary.
*
* @readonly
* @type {ObjectModelCollection<ResourceDictionary>}
* @memberof ResourceDictionary
*/
public get MergedDictionaries(): ObjectModelCollection<ResourceDictionary> {
return this.mergedDictionaries;
}
/**
* The source URI from which to fetch resources from.
*
* @type {Uri}
* @memberof ResourceDictionary
*/
public get Source(): Uri {
return this.source;
}
public set Source(value: Uri) {
const objFunc = ResourceFileRegistry.ResourceFiles.get(
value.OriginalString
);
if (objFunc instanceof Function) {
const obj = objFunc() as ResourceDictionary;
this.mergedDictionaries = obj.mergedDictionaries;
this.resourceMap = obj.resourceMap;
}
this.source = value;
}
/**
* Adds a resource into this ResourceDictionary.
*
* @param {string} key A key to the resource.
* @param {unknown} value The resource to add.
* @throws If there is a resource with the given key is already present on
* this resource dictionary, it throws an exception. You can't
* overwrite a resource: you have to remove the existing resource
* before adding a new one with the same key.
* @throws If this resource dictionary fetch its resources from a remote
* source. You can't modify resources when fetched from a remote
* source.
* @memberof ResourceDictionary
*/
public Add(key: string, value: unknown): void {
if (this.Source != null) {
throw Error(`Can't add elements to a remote ResourceDictionary`);
}
if (!this.resourceMap.has(key)) {
this.resourceMap.set(key, value);
} else {
throw Error(`Key '${key}' already exist in the ResourceDictionary`);
}
}
/**
* Check if a resource with the given key is present in this resource
* dictionary, or in its merged dictionaries.
*
* @param {unknown} key The key to check.
* @return {*} {boolean} `true` if the key is present, `false` otherwise.
* @memberof ResourceDictionary
*/
public Contains(key: unknown): boolean {
if (typeof key !== 'string') {
throw Error(
`Keys of type ${typeof key} are not supported. Only string keys are supported`
);
}
const strKey = key as string;
let found = this.resourceMap.has(strKey);
if (!found) {
const mergedDictionaries = this.mergedDictionaries.internalArray;
for (let i = mergedDictionaries.length - 1; i >= 0 && !found; i--) {
found = mergedDictionaries[i].Contains(key);
}
}
return found;
}
/**
* Gets the resource associated with the given key.
*
* @param {unknown} key The key of the resource to get.
* @return {*} The resource associated with the key, or `undefined` if
* the key is not present.
* @memberof ResourceDictionary
* @wIgnore
*/
public Get(key: unknown): any {
if (typeof key !== 'string') {
throw Error(
`Keys of type ${typeof key} are not supported. Only string keys are supported`
);
}
const strKey = key as string;
let value = this.resourceMap.get(strKey);
if (value === undefined) {
const mergedDictionaries = this.mergedDictionaries.internalArray;
for (
let i = mergedDictionaries.length - 1;
i >= 0 && value === undefined;
i--
) {
value = mergedDictionaries[i].Get(key);
}
}
return value;
}
/**
* Iterates throw key-resource pairs in this resource dictionary.
* Note: this iterator does not iterate over merged dictionaries.
*
* @return {*} {Iterator<any>}
* @memberof ResourceDictionary
*/
[Symbol.iterator](): Iterator<any> {
return this.resourceMap[Symbol.iterator]();
}
/**
* Removes the resource with the given key from this resource dictionary.
* Note: this method does not remove the key from merged dictionaries, even
* if it is present on those.
*
* @param {string} key The key of the resource to remove.
* @throws If this resource dictionary fetch its resources from a remote
* source. You can't modify resources when fetched from a remote
* source.
* @memberof ResourceDictionary
*/
public Remove(key: string): void {
if (this.Source != null) {
throw Error(`Can't remove elements from a remote ResourceDictionary`);
}
this.resourceMap.delete(key);
}
}