Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | 21x 31x 134x 368x 134x 134x 134x 134x 5x 5x 5x 368x 368x 291x 368x 253x 253x 253x 120x 253x 19x 234x 13x 368x 368x 368x 368x 134x 134x 134x 1349x 114x 1349x 134x | import { CustomElementMetadata, CustomElementPropertyMetadata } from "../../interfaces";
/**
* Initializes the properties of a custom element from the metadata configured
* @param Base The base class to extend
* @returns The mixin class
*/
export default function PropertyMetadataInitializerMixin(Base): any {
return class PropertyMetadataInitializer extends Base {
/**
* The properties to track in the class
*/
static properties: () => Record<string, CustomElementPropertyMetadata>;
protected static initalizeProperties(metadata: CustomElementMetadata): void {
const properties = this.getAllProperties();
Object.entries(properties).forEach(([key, value]) => this._initializeProperty(key, value, metadata));
// Merge the properties of the base class if any so we can validate and initialize
// the values of the properties of the base class in the instance
let baseClass = Object.getPrototypeOf(this.prototype)?.constructor;
Eif (baseClass !== undefined) {
const baseClassMetadata = baseClass.metadata;
if (baseClassMetadata !== undefined) {
metadata.properties = new Map([...metadata.properties, ...baseClassMetadata.properties]);
metadata.propertiesByAttribute = new Map([...metadata.propertiesByAttribute, ...baseClassMetadata.propertiesByAttribute]);
metadata.observedAttributes = [...metadata.observedAttributes, ...baseClassMetadata.observedAttributes];
}
}
}
/**
* Creates the setters and getters for the property
* @param name
* @param propertyMetadata
* @returns The name of the observed attribute for that property
*/
private static _initializeProperty(name: string, propertyMetadata: CustomElementPropertyMetadata, metadata: CustomElementMetadata): void {
propertyMetadata.name = name; // Set the name of the property
// Set the name of the attribute as same as the name of the property if no attribute name was provided
if (propertyMetadata.attribute === undefined) {
propertyMetadata.attribute = name;
}
Object.defineProperty(
this.prototype,
name,
{
get(): any {
let {
type,
defer
} = propertyMetadata;
const value = this._properties[name];
if (!Array.isArray(type)) {
type = [type];
}
if (type.includes(Function) &&
typeof value === 'function' &&
defer !== true) { // Only call the function if the type is a Function and it is not deferred
return value();
}
return value;
},
set(this: any, value: unknown) {
this.setProperty(name, value);
},
configurable: true,
enumerable: true,
}
);
// Add it to the metadata properties so the properties of the instances can be validated and initialized
metadata.properties.set(name, propertyMetadata);
const {
attribute
} = propertyMetadata;
// Index the property descriptor by the attribute name
metadata.propertiesByAttribute.set(attribute, propertyMetadata); // Index by attribute name
// Add the observed attribute
metadata.observedAttributes.push(propertyMetadata.attribute.toLowerCase());
}
/**
* Retrieve the state of this and the base mixins
* @returns The merged state
*/
static getAllProperties(): Record<string, CustomElementPropertyMetadata> {
let properties = this.properties || {};
let baseClass = Object.getPrototypeOf(this.prototype)?.constructor;
while (baseClass._isMetadataInitializer === true) {
if (baseClass.properties !== undefined) {
properties = { ...properties, ...baseClass.properties };
}
baseClass = Object.getPrototypeOf(baseClass.prototype)?.constructor;
}
return properties;
}
}
}
|