jetformbuilder
/
modules
/
framework
/
blocks-style-manager
/
assets
/
src
/
css-engine
/
generator.js
jetformbuilder
/
modules
/
framework
/
blocks-style-manager
/
assets
/
src
/
css-engine
Last commit date
fields
2 months ago
generator.js
2 months ago
generator.js
177 lines
| 1 | import * as Fields from './fields'; |
| 2 | import { getBreakpoints, withMediaQuery } from '../helpers/breakpoints'; |
| 3 | |
| 4 | export class Generator { |
| 5 | |
| 6 | constructor( blockName, attributes ) { |
| 7 | |
| 8 | this.blockName = blockName; |
| 9 | this.attributes = attributes; |
| 10 | this.uniqueClassName = attributes._uniqueClassName || false; |
| 11 | this.css = ''; |
| 12 | this.cssVariables = {}; |
| 13 | this.responsiveCSS = {}; |
| 14 | |
| 15 | this.controlHandlers = { |
| 16 | [ Fields.Color.getType() ]: Fields.Color, |
| 17 | [ Fields.Typography.getType() ]: Fields.Typography, |
| 18 | [ Fields.Border.getType() ]: Fields.Border, |
| 19 | [ Fields.Dimensions.getType() ]: Fields.Dimensions, |
| 20 | [ Fields.Range.getType() ]: Fields.Range, |
| 21 | [ Fields.Choose.getType() ]: Fields.Choose, |
| 22 | [ Fields.Toggle.getType() ]: Fields.Toggle, |
| 23 | }; |
| 24 | } |
| 25 | |
| 26 | getControlHandler( control ) { |
| 27 | |
| 28 | const controlType = control.type || false; |
| 29 | |
| 30 | if ( ! controlType ) { |
| 31 | return false; |
| 32 | } |
| 33 | |
| 34 | return this.controlHandlers[ controlType ] || false; |
| 35 | } |
| 36 | |
| 37 | getCSS() { |
| 38 | |
| 39 | // Ensure we reset CSS on block re-render |
| 40 | this.css = ''; |
| 41 | |
| 42 | const { blockName } = this; |
| 43 | |
| 44 | if ( ! window.crocoBlockStyleEditor.blocks[ blockName ] ) { |
| 45 | return this.css; |
| 46 | } |
| 47 | |
| 48 | for ( const control of window.crocoBlockStyleEditor.blocks[ blockName ] ) { |
| 49 | this.generateControlStyles( control ); |
| 50 | } |
| 51 | |
| 52 | for ( const device in this.responsiveCSS ) { |
| 53 | |
| 54 | if ( ! this.responsiveCSS[ device ] ) { |
| 55 | continue; |
| 56 | } |
| 57 | |
| 58 | const mediaQueryCSS = withMediaQuery( this.responsiveCSS[ device ], device ); |
| 59 | |
| 60 | if ( mediaQueryCSS ) { |
| 61 | this.css += mediaQueryCSS; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | return { |
| 66 | styles: this.css, |
| 67 | variables: this.cssVariables, |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | generateResponsiveStyles( control, ControlHandler ) { |
| 72 | |
| 73 | const controlID = control.id || false; |
| 74 | const cssSelectors = control.css_selector || false; |
| 75 | |
| 76 | if ( ! controlID || ! cssSelectors ) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | for ( const device in getBreakpoints() ) { |
| 81 | |
| 82 | if ( ! this.attributes?.[ device ]?.[ controlID ] ) { |
| 83 | continue; |
| 84 | } |
| 85 | |
| 86 | const controlInstance = new ControlHandler( |
| 87 | this.uniqueClassName, |
| 88 | this.attributes[ device ][ controlID ] |
| 89 | ); |
| 90 | |
| 91 | for ( const selector in cssSelectors ) { |
| 92 | const selectorStyles = controlInstance.parseSelector( |
| 93 | selector, |
| 94 | cssSelectors[ selector ] |
| 95 | ); |
| 96 | |
| 97 | if ( ! this.responsiveCSS[ device ] ) { |
| 98 | this.responsiveCSS[ device ] = ''; |
| 99 | } |
| 100 | |
| 101 | this.responsiveCSS[ device ] += selectorStyles; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | generateResponsiveVariables( controlID, cssVar, ControlHandler ) { |
| 107 | |
| 108 | for ( const device in getBreakpoints() ) { |
| 109 | |
| 110 | cssVar.suffix = ''; |
| 111 | |
| 112 | if ( ! this.attributes?.[ device ]?.[ controlID ] ) { |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | cssVar.suffix = device; |
| 117 | |
| 118 | const controlInstance = new ControlHandler( |
| 119 | this.uniqueClassName, |
| 120 | this.attributes[ device ][ controlID ] |
| 121 | ); |
| 122 | |
| 123 | this.cssVariables = { |
| 124 | ...this.cssVariables, |
| 125 | ...controlInstance.parseVariable( cssVar ), |
| 126 | }; |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | generateControlStyles( control ) { |
| 131 | |
| 132 | const ControlHandler = this.getControlHandler( control ); |
| 133 | const controlID = control.id || false; |
| 134 | const { attributes } = this; |
| 135 | |
| 136 | if ( ControlHandler && controlID ) { |
| 137 | |
| 138 | if ( attributes[ controlID ] ) { |
| 139 | |
| 140 | const controlInstance = new ControlHandler( |
| 141 | this.uniqueClassName, |
| 142 | this.attributes[ controlID ] |
| 143 | ); |
| 144 | |
| 145 | if ( control.css_selector ) { |
| 146 | for ( const selector in control.css_selector ) { |
| 147 | this.css += controlInstance.parseSelector( |
| 148 | selector, |
| 149 | control.css_selector[ selector ] |
| 150 | ); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if ( control.css_var ) { |
| 155 | this.cssVariables = { |
| 156 | ...this.cssVariables, |
| 157 | ...controlInstance.parseVariable( control.css_var ), |
| 158 | }; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | if ( control.css_selector ) { |
| 163 | this.generateResponsiveStyles( control, ControlHandler ); |
| 164 | } |
| 165 | |
| 166 | if ( control.css_var ) { |
| 167 | this.generateResponsiveVariables( controlID, control.css_var, ControlHandler ); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if ( control.children ) { |
| 172 | for ( const childControl of control.children ) { |
| 173 | this.generateControlStyles( childControl ); |
| 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |