| 1 |
import { getDeviceType } from "./helper"; |
| 2 |
import { softMinifyCssStrings } from "../util/helpers"; |
| 3 |
|
| 4 |
// let stylesWithProperty = {} |
| 5 |
|
| 6 |
class Property { |
| 7 |
hasNoProperty = []; |
| 8 |
mapCSSProperty = {}; |
| 9 |
|
| 10 |
constructor(property, attributes) { |
| 11 |
// this.hasNoProperty = []; |
| 12 |
// this.mapCSSProperty = {}; |
| 13 |
|
| 14 |
for (const key in property) { |
| 15 |
Object.defineProperty(this, key, { |
| 16 |
value: property[key], |
| 17 |
writable: (key != 'id' && key != 'type') |
| 18 |
}) |
| 19 |
} |
| 20 |
|
| 21 |
this.attributes = attributes; |
| 22 |
this.args.attributes = attributes; |
| 23 |
|
| 24 |
this.generate(); |
| 25 |
|
| 26 |
if (this?.class) { |
| 27 |
this.generateClasses(); |
| 28 |
} |
| 29 |
|
| 30 |
if (this?.attr) { |
| 31 |
this.generateHTMLAttrs(); |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* this key will contains styles with selectors and property |
| 36 |
* |
| 37 |
* i.e: |
| 38 |
* |
| 39 |
* { |
| 40 |
* '.css-selector' : { |
| 41 |
* 'desktop' : { |
| 42 |
* 'transition': [ '#fff', '#fff' ] |
| 43 |
* } |
| 44 |
* } |
| 45 |
* } |
| 46 |
* |
| 47 |
*/ |
| 48 |
this.stylesWithProperty = {}; |
| 49 |
} |
| 50 |
|
| 51 |
getStylesProperty(){ |
| 52 |
return this.stylesWithProperty ?? {}; |
| 53 |
} |
| 54 |
|
| 55 |
deviceType(key) { |
| 56 |
return getDeviceType(key); |
| 57 |
} |
| 58 |
|
| 59 |
getCssStrings() { |
| 60 |
const cssStrings = {} |
| 61 |
let unknownStyles = {}; |
| 62 |
|
| 63 |
this.forEach(function (cssProperties, key) { |
| 64 |
if (cssProperties == undefined) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
if( this.id == 'col' ) { |
| 69 |
if( this?.args?.property?.length > 0 && this.args.value?.length > 0 && this.args.value == 'repeat' ){ |
| 70 |
cssProperties = `${this?.args?.property}:${this.args.value}${this.args.subvalues.replace('$col', cssProperties)}`; |
| 71 |
} else { |
| 72 |
cssProperties = cssProperties > 0 ? `--column: ${cssProperties};` : ''; |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
let value = softMinifyCssStrings( String( cssProperties ) ); |
| 77 |
|
| 78 |
let deviceType = this.deviceType(key); |
| 79 |
|
| 80 |
if( this.hasNoProperty.includes(key) ) { |
| 81 |
let property = this.mapCSSProperty[key]; |
| 82 |
|
| 83 |
// if( this.mapCSSProperty[key] !== undefined ) { |
| 84 |
// if( stylesWithProperty[ this.cssSelector ] && stylesWithProperty[ this.cssSelector ][ property ] && ! stylesWithProperty[ this.cssSelector ][ property ].includes( value ) ) { |
| 85 |
// stylesWithProperty[ this.cssSelector ][ property ].push( value ); |
| 86 |
// } else if( stylesWithProperty[ this.cssSelector ] ) { |
| 87 |
// stylesWithProperty[ this.cssSelector ][ property ] = [ value ]; |
| 88 |
// } else { |
| 89 |
// stylesWithProperty[ this.cssSelector ] = { }; |
| 90 |
// stylesWithProperty[ this.cssSelector ][ property ] = [ value ]; |
| 91 |
// } |
| 92 |
// } |
| 93 |
|
| 94 |
|
| 95 |
unknownStyles[ this.cssSelector ] = unknownStyles[ this.cssSelector ] ?? {}; |
| 96 |
|
| 97 |
if( this.mapCSSProperty[key] !== undefined ) { |
| 98 |
unknownStyles[ this.cssSelector ][ property ] = unknownStyles[ this.cssSelector ][ property ] ?? []; |
| 99 |
unknownStyles[ this.cssSelector ][ property ].push( value ); |
| 100 |
|
| 101 |
this.stylesWithProperty = unknownStyles; |
| 102 |
} |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
cssStrings[deviceType] = cssStrings[deviceType] ? cssStrings[deviceType] + value : value; |
| 107 |
}); |
| 108 |
|
| 109 |
return cssStrings; |
| 110 |
} |
| 111 |
|
| 112 |
generateClasses() { |
| 113 |
this.classNames = ''; |
| 114 |
let classes = {} |
| 115 |
this.forEach(function (value, key) { |
| 116 |
let deviceType = this.deviceType(key); |
| 117 |
classes[deviceType] = `${this.classPrefix}${deviceType}-${value != '' ? value : this.defaultData[deviceType]}`; |
| 118 |
}) |
| 119 |
this.classNamesAsObject = classes; |
| 120 |
|
| 121 |
this.classNames += Object.values(classes).join(' '); |
| 122 |
} |
| 123 |
|
| 124 |
generateHTMLAttrs() { |
| 125 |
this.htmlAttrs = {}; |
| 126 |
this.forEach(function (value, key) { |
| 127 |
let deviceType = this.deviceType(key); |
| 128 |
this.htmlAttrs[`data-${this.attrPrefix}_${deviceType}`] = value != '' ? value : this.defaultData[deviceType]; |
| 129 |
}) |
| 130 |
} |
| 131 |
|
| 132 |
getData() { |
| 133 |
return this.data; |
| 134 |
} |
| 135 |
|
| 136 |
generate() { |
| 137 |
this.data = {}; |
| 138 |
return this; |
| 139 |
} |
| 140 |
|
| 141 |
filterData() { |
| 142 |
if (this.filter) { |
| 143 |
return this.forEach(this.filter); |
| 144 |
} |
| 145 |
|
| 146 |
return this; |
| 147 |
} |
| 148 |
|
| 149 |
forEach(callback, thisArgs) { |
| 150 |
for (const key in this.data) { |
| 151 |
callback.call(thisArgs ?? this, this.data[key], key, this.data); |
| 152 |
} |
| 153 |
|
| 154 |
return this; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
// Property.prototype.forEach = function (callback, thisArgs) { |
| 159 |
// for (const key in this.data) { |
| 160 |
// callback.call(thisArgs ?? this, this.data[key], key, this.data); |
| 161 |
// } |
| 162 |
// return this; |
| 163 |
// } |
| 164 |
|
| 165 |
export default Property; |
| 166 |
|