@@ -16,7 +16,7 @@ import Components from './index.js'
1616import Data from './data.js'
1717import animate from '../common/animation.js'
1818import Controls from './controls/index.js'
19- import { get } from '../common/utils/object.mjs'
19+ import { get , set } from '../common/utils/object.mjs'
2020import { toTitleCase } from '../common/utils/string.mjs'
2121
2222export default class Component extends Data {
@@ -355,6 +355,7 @@ export default class Component extends Data {
355355
356356 /**
357357 * Method for handling onAdd for all components
358+ * @todo improve readability of this method
358359 * @param {Object } evt
359360 * @return {Object } Component
360361 */
@@ -397,10 +398,14 @@ export default class Component extends Data {
397398
398399 const onAddConditions = {
399400 controls : ( ) => {
400- const { controlData } = Controls . get ( item . id )
401401 const {
402- meta : { id : metaId } ,
403- } = controlData
402+ controlData : {
403+ meta : { id : metaId } ,
404+ ...elementData
405+ } ,
406+ } = Controls . get ( item . id )
407+
408+ set ( elementData , 'config.controlId' , metaId )
404409
405410 const controlType = metaId . startsWith ( 'layout-' ) ? metaId . replace ( / ^ l a y o u t - / , '' ) : 'field'
406411 const targets = {
@@ -424,7 +429,7 @@ export default class Component extends Data {
424429 const depth = get ( targets , `${ this . name } .${ controlType } ` )
425430 const action = depthMap . get ( depth ) ( )
426431 dom . remove ( item )
427- const component = action ( controlData , newIndex )
432+ const component = action ( elementData , newIndex )
428433
429434 return component
430435 } ,
@@ -501,10 +506,23 @@ export default class Component extends Data {
501506 events . onRender && dom . onRender ( this . dom , events . onRender )
502507 }
503508
509+ /**
510+ * Sets the configuration for the component. See src/demo/js/options/config.js for example
511+ * @param {Object } config - Configuration object with possible structures:
512+ * @param {Object } [config.all] - Global configuration applied to all components
513+ * @param {Object } [config[controlId]] - Configuration specific to a control type
514+ * @param {Object } [config[id]] - Configuration specific to a component instance
515+ * @description Merges configurations in order of precedence:
516+ * 1. Existing config (this.configVal)
517+ * 2. Global config (all)
518+ * 3. Control type specific config
519+ * 4. Instance specific config
520+ * The merged result is stored in this.configVal
521+ */
504522 set config ( config ) {
505- const metaId = get ( this . data , 'meta.id' )
506523 const allConfig = get ( config , 'all' )
507- const typeConfig = metaId && get ( config , metaId )
524+ const controlId = get ( this . data , 'config.controlId' )
525+ const typeConfig = controlId && get ( config , controlId )
508526 const idConfig = get ( config , this . id )
509527 const mergedConfig = [ allConfig , typeConfig , idConfig ] . reduce (
510528 ( acc , cur ) => ( cur ? merge ( acc , cur ) : acc ) ,
0 commit comments