12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- import isEqual from 'lodash/isEqual';
- import isObject from 'lodash/isObject';
- import isArray from 'lodash/isArray';
- import isString from 'lodash/isString';
- import endsWith from 'lodash/endsWith';
- import forEach from 'lodash/forEach';
-
- import { Store } from '../components/Store';
- import { UniqueIdProvider } from '../adapters/UniqueIdProvider';
- import { ColorService } from '../adapters/ColorService';
- import { AssetService } from '../adapters/AssetResolver';
- import { Options } from '../interfaces/Options';
-
- export class OptionsProcessor {
- constructor(
- private store: Store,
- private uniqueIdProvider: UniqueIdProvider,
- private colorService: ColorService,
- private assetService: AssetService,
- ) {}
-
- public processOptions(options: Options) {
- this.processObject(options);
- }
-
- private processObject(objectToProcess: object) {
- forEach(objectToProcess, (value, key) => {
- this.processColor(key, value, objectToProcess);
-
- if (!value) {
- return;
- }
-
- this.processComponent(key, value, objectToProcess);
- this.processImage(key, value, objectToProcess);
- this.processButtonsPassProps(key, value);
-
- if (!isEqual(key, 'passProps') && (isObject(value) || isArray(value))) {
- this.processObject(value);
- }
- });
- }
-
- private processColor(key: string, value: any, options: Record<string, any>) {
- if (isEqual(key, 'color') || endsWith(key, 'Color')) {
- options[key] = value === null ? 'NoColor' : this.colorService.toNativeColor(value);
- }
- }
-
- private processImage(key: string, value: any, options: Record<string, any>) {
- if (
- isEqual(key, 'icon') ||
- isEqual(key, 'image') ||
- endsWith(key, 'Icon') ||
- endsWith(key, 'Image')
- ) {
- options[key] = isString(value) ? value : this.assetService.resolveFromRequire(value);
- }
- }
-
- private processButtonsPassProps(key: string, value: any) {
- if (endsWith(key, 'Buttons')) {
- forEach(value, (button) => {
- if (button.passProps && button.id) {
- this.store.updateProps(button.id, button.passProps);
- button.passProps = undefined;
- }
- });
- }
- }
-
- private processComponent(key: string, value: any, options: Record<string, any>) {
- if (isEqual(key, 'component')) {
- value.componentId = value.id ? value.id : this.uniqueIdProvider.generate('CustomComponent');
- if (value.passProps) {
- this.store.updateProps(value.componentId, value.passProps);
- }
- options[key].passProps = undefined;
- }
- }
- }
|