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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 | 6x 6x 6x 6x 6x 16x 15x 15x 15x 15x 15x 15x 15x 15x 17x 17x 13x 13x 13x 15x 15x 17x 17x 15x 4x 4x 4x 4x 4x 4x 4x 4x 4x 24x 24x 24x 4x 20x 24x 24x 24x 24x 24x 24x 5x 6x | import { attributeMarkerPrefix } from "../../../renderer/createTemplate";
import findPropertyValueUp from "../../helpers/findParentProperty";
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> = {};
/**
* Map of the metadata of the changed properties so that the "change" method can be called after the update of the DOM
*/
private _changedProperties: Map<string, CustomElementPropertyMetadata> = new Map<string, CustomElementPropertyMetadata>();
/**
* The properties that by the time the component gets connected, do not have any attribute explicitly set in the markup
*/
private _initiallyUndefinedProperties: Set<string> = new Set<string>();
connectedCallback() {
super.connectedCallback?.();
const {
properties
} = (this.constructor as any).metadata;
this._initializeDefaultProperties(properties);
this._validateRequiredProperties(properties);
}
/**
* Initializes the properties that have a default value
* @param propertiesMetadata
*/
private _initializeDefaultProperties(propertiesMetadata: Map<string, CustomElementPropertyMetadata>) {
for (const [name, property] of propertiesMetadata) {
const {
value
} = property;
if (this._properties[name] === undefined) { // Not explicitly set
this._initiallyUndefinedProperties.add(name);
Eif (value !== undefined) { // Set the default value
this.setProperty(name, value);
}
}
}
}
/**
* Validates that all the required properties have been set
* @param propertiesMetadata
*/
private _validateRequiredProperties(propertiesMetadata: Map<string, CustomElementPropertyMetadata>) {
const missingValueAttributes: string[] = [];
for (const [, property] of propertiesMetadata) {
const {
required,
attribute
} = property;
Iif (required === true &&
(this.attributes[attribute] === undefined &&
this[attribute] === undefined)) { // The attribute for that property has not been set
missingValueAttributes.push(attribute);
}
}
Iif (missingValueAttributes.length > 0) {
throw Error(`The attributes: [${missingValueAttributes.join(', ')}] must have a value`)
}
}
/**
* Called when the component added a child or it was added as a child
* @param parent
* @param child
*/
didAdoptChildCallback(parent: HTMLElement, child: HTMLElement) {
const {
metadata
} = child.constructor as any;
if (metadata === undefined) { // Probably not a custom component
return;
}
const {
properties
} = metadata;
(child as any).setInheritedProperties(properties, parent);
}
/**
* Sets the properties that can be inherited from the value of the parent if any
* @param propertiesMetadata
*/
protected setInheritedProperties(propertiesMetadata: Map<string, CustomElementPropertyMetadata>, parent: HTMLElement) {
for (const [name, property] of propertiesMetadata) {
const {
value,
inherit
} = property;
if (inherit !== true) {
continue; // Not inheritable
}
if (this._initiallyUndefinedProperties.has(name) === false) {
continue; // Its value was initially set in the attribute markup
}
const parentValue = findPropertyValueUp(parent as any, name);
if (parentValue !== undefined) {
this.setProperty(name, parentValue);
}
else if (this[name] !== value) { // It is different from the default value
this.setProperty(name, parentValue);
}
}
}
// 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: string | null, newValue: string | null) {
Iif (oldValue === newValue) {
return; // Nothing to change
}
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 {
Iif (value !== null &&
value.startsWith(attributeMarkerPrefix)) { // Coming from a template ... ignore
return false;
}
// Verify that the property is one of the configured in the custom element
let propertyMetadata: CustomElementPropertyMetadata = (this.constructor as any).metadata.propertiesByAttribute.get(attribute);
const {
name,
type,
transform
} = propertyMetadata;
value = valueConverter.toProperty(value, type); // Convert from the value returned by the parameter
Iif (transform !== undefined) {
value = transform.call(this, value); // Transform the data if necessary
}
return this.setProperty(name, value);
}
protected setProperty(name: string, value: any): 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;
}
// Verify that the property is one of the configured in the custom element
let propertyMetadata: CustomElementPropertyMetadata = (this.constructor as any).metadata.properties.get(name);
const {
attribute,
reflect,
//change We call change after the element was updated in the DOM
} = propertyMetadata;
const reflectOnAttribute = reflect === true ? attribute : undefined;
Iif (reflectOnAttribute !== undefined) { // Synchronize with the attribute of the element
value = valueConverter.toAttribute(value);
if (value === '') {
this.removeAttribute(reflectOnAttribute);
}
else {
// This will trigger the attributeChangedCallback
this.setAttribute(reflectOnAttribute, value);
}
}
this._changedProperties.set(name, (this.constructor as any).metadata.properties.get(name));
return true;
}
protected callAttributesChange() {
this._changedProperties.forEach((p, k) => {
if (p.afterUpdate !== undefined) { // Call the change function if defined
p.afterUpdate.call(this);
}
});
}
protected clearChangedProperties() {
this._changedProperties.clear();
}
}
export default AttributeChangeHandlerMixin;
|