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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | 3x 3x 3x 9x 7x 7x 7x 13x 7x 7x 13x 13x 7x 7x 7x 7x 7x 7x 6x 6x 6x 6x 6x 6x 6x 17x 17x 17x 4x 13x 17x 17x 3x | import valueConverter from "../helpers/valueConverter";
import { CustomElementPropertyMetadata } from "../interfaces";
import PropertyMetadataInitializerMixin from "./PropertyMetadataInitializerMixin";
const AttributeChangeHandlerMixin = Base =>
class AttributeChangeHandler extends PropertyMetadataInitializerMixin(Base) { // This mixin requires an implementation of setProperty
/**
* The properties of the instance
*/
private _properties: Record<string, any> = {};
/**
* Validates that all the required properties have been set
* @param propertiesMetadata
*/
private _validateRequiredProperties(propertiesMetadata: Map<string, CustomElementPropertyMetadata>) {
const missingValueAttributes: string[] = [];
for (const [name, property] of propertiesMetadata) {
Iif (property.required === true &&
this._properties[name] === undefined) { // No value set
missingValueAttributes.push(property.attribute);
}
}
Iif (missingValueAttributes.length > 0) {
throw Error(`The attributes: [${missingValueAttributes.join(', ')}] must have a value`)
}
}
/**
* Initializes the properties that have a default value
* @param propertiesMetadata
*/
private _initializePropertiesWithDefaultValues(propertiesMetadata: Map<string, CustomElementPropertyMetadata>) {
for (const [name, property] of propertiesMetadata) {
const {
attribute,
value,
reflect
} = property;
if (this._properties[name] === undefined &&
value !== undefined) {
const reflectOnAttribute = reflect === true ? attribute : undefined;
this.setProperty(name, value, reflectOnAttribute);
}
}
}
connectedCallback() {
super.connectedCallback?.();
const {
properties
} = (this.constructor as any).metadata;
this._validateRequiredProperties(properties);
this._initializePropertiesWithDefaultValues(properties);
}
// Without defining this method, the observedAttributes getter will not be called
// Also no need to check that the property was configured because if it is not configured,
// it will not generate the observedAttribute and therefore this method won't be called for that attribute
/**
* Called when there is a change in an attribute
* @param attributeName
* @param oldValue
* @param newValue
*/
attributeChangedCallback(attributeName: string, oldValue, newValue) {
super.attributeChangedCallback?.(attributeName, oldValue, newValue);
this._setAttribute(attributeName, newValue);
}
// /**
// * Overrides the parent method to verify that it is accessing a configured property
// * @param attribute
// * @param value
// */
// setAttribute(attribute: string, value: any) {
// // Verify that the property is one of the configured in the custom element
// if ((this.constructor as any)._propertiesByAttribute[attribute] === undefined &&
// !(this.constructor as any).metadata.htmlElementProperties.has(attribute)) {
// throw Error(`There is no configured property for attribute: '${attribute}' in type: '${this.constructor.name}'`)
// }
// super.setAttribute(attribute, value);
// }
private _setAttribute(attribute: string, value: any): boolean {
// Verify that the property is one of the configured in the custom element
let propertyMetadata = (this.constructor as any)._propertiesByAttribute[attribute];
const {
name,
type,
reflect
} = propertyMetadata;
value = valueConverter.toProperty(value, type); // Covert from the value returned by the parameter
const reflectOnAttribute = reflect === true ? attribute : undefined;
return this.setProperty(name, value, reflectOnAttribute);
}
protected setProperty(name: string, value: any, reflectOnAttribute: string): boolean {
const oldValue = this._properties[name];
Iif (oldValue === value) {
return false;
}
if (typeof value === 'function') {
this._properties[name] = (value as Function).bind(this);
}
else {
this._properties[name] = value;
}
Iif (reflectOnAttribute !== undefined) { // Synchronize with the attribute of the element
value = valueConverter.toAttribute(value);
// This will trigger the attributeChangedCallback
this.setAttribute(reflectOnAttribute, value);
}
return true;
}
}
export default AttributeChangeHandlerMixin;
|