activator-oce-exporter
Version:
Extract Activator binder and convert it to valid OCE mono pacakge
54 lines (48 loc) • 1.83 kB
JavaScript
import { FusionApi } from '../api';
import { store } from '../../store.js';
import { intersectMap } from '../utils';
/**
*
* @description Mixin for properties synchronizing from parent to children.
* The parent should contain an object of properties which will be synchronizing with children.
* The default `synchronizableProperties` object is empty.
*/
export function ChildrenStylist(superClass) {
return class extends superClass {
update(changedProps) {
super.update(changedProps);
if (this.isRendered) {
this.setupItemsStyleAttributes(changedProps);
}
}
/**
* @description Properties which should be synchronized from parent to children
* @return {Object.<string, LitElementProperty>}
*/
static get synchronizableProperties() {
return {};
}
/**
* @description Set attribute from parent to children
* @param {Object.<string, LitElementProperty>} changedProps - changed properties
*/
setupItemsStyleAttributes(changedProps) {
const properties = this.getChangedProperties(changedProps);
const { registeredComponents } = store.getState().app;
const elements = [...this.querySelectorAll(`${registeredComponents.join()}`)];
elements.forEach(element => FusionApi.setAttributes({ properties, element }));
}
/**
*
* @param {Map<string>} changedProps name
* @returns {Object.<string, LitElementProperty>} properties
*/
getChangedProperties(changedProps) {
const filteredProps = intersectMap(changedProps, Object.keys(this.constructor.synchronizableProperties));
return Array.from(filteredProps.keys()).reduce((changedChildProps, property) => {
changedChildProps[property] = { value: this[property] };
return changedChildProps;
}, {});
}
};
}