2020-01-29 11:10:25 +01:00
export default class ClassBuilder {
2020-02-03 19:38:09 +01:00
constructor ( block , customDefaults ) {
2020-01-29 11:10:25 +01:00
this . block = ` mdc- ${ block } ` ;
2020-02-03 19:38:09 +01:00
this . customDefaults = customDefaults ; //will be ignored when building custom classes
}
// classParams: {modifiers:[] (mdc), custom:[] (bbmd), extra:[] (any)}
blocks ( classParams ) {
let base = this . block ;
if ( classParams == undefined ) return base ;
return this . buildClass ( base , classParams ) ;
}
//elementName: string, classParams: {}
elements ( elementName , classParams ) {
let base = ` ${ this . block } __ ${ elementName } ` ;
if ( classParams == undefined ) return base ;
return this . buildClass ( base , classParams ) ;
}
2020-02-07 21:56:00 +01:00
//TODO: Classparams modifier and custom could take an object. Booleans or numbers use key value for classes, strings use property value for classes. Extras to stay as is
2020-02-03 19:38:09 +01:00
buildClass ( base , classParams ) {
let cls = base ;
const { modifiers , customs , extras } = classParams ;
2020-02-07 21:56:00 +01:00
if ( ! ! modifiers )
cls += modifiers . map ( m => ( ! ! m ? ` ${ base } -- ${ m } ` : "" ) ) . join ( " " ) ;
2020-02-03 19:38:09 +01:00
if ( ! ! customs )
cls += Object . entries ( customs )
. map ( ( [ property , value ] ) => {
//disregard falsy and values set by customDefaults constructor param
2020-02-07 21:56:00 +01:00
if (
! ! value &&
( ! this . customDefaults || ! this . customDefaults . includes ( value ) )
) {
2020-02-03 19:38:09 +01:00
//custom scss name convention = bbmd-[block | element]--[property]-[value]
return ` bbmd- ${ base } -- ${ property } - ${ value } ` ;
}
} )
. join ( "" ) ;
if ( ! ! extras ) cls += ` ${ extras . join ( " " ) } ` ;
return cls . trim ( ) ;
2020-01-29 11:10:25 +01:00
}
}