Browse Source

MINOR,FEAT,REFA: introduced modifier passing to generator

dev-feat-component_preview
chris 2 months ago
parent
commit
578d26c2e5
  1. 93
      src/baseComponents.js
  2. 13
      src/component.js

93
src/baseComponents.js

@ -68,6 +68,10 @@ class FlexContainerComponent extends Component {
* @type {boolean} * @type {boolean}
*/ */
_distributeEvenglyAfterGenerate; _distributeEvenglyAfterGenerate;
/**
* @type {string}
*/
_flexDirection;
/** /**
* *
* @param {Attr} attr * @param {Attr} attr
@ -75,6 +79,7 @@ class FlexContainerComponent extends Component {
*/ */
constructor(attr = {}, modifier = null, baseElement = "div") { constructor(attr = {}, modifier = null, baseElement = "div") {
super(document.createElement(baseElement), attr); super(document.createElement(baseElement), attr);
this._flexDirection = "";
this._distributeEvenglyAfterGenerate = false; this._distributeEvenglyAfterGenerate = false;
this.addStyleClass("flex-container-component"); this.addStyleClass("flex-container-component");
if (modifier) { if (modifier) {
@ -130,6 +135,37 @@ class FlexContainerComponent extends Component {
this._distributeEvenglyAfterGenerate = true; this._distributeEvenglyAfterGenerate = true;
return this; return this;
} }
/**
* @override
* @inheritdoc
* @extends Component.generate()
*/
generate(modifier = null, styleStore = null, functionStore = null) {
if (this._children && this._children.length > 0) {
if (this._distributeEvenglyAfterGenerate) {
let childDistributionFraction = Math.floor(
((100 - this._children.length) / this._children.length) * 100
) / 100;
let direction = (this._flexDirection === 'column' ? 'height' : "width");
for (const icomp of this._children) {
/* sets the width for all elements,
to avoid overlapping or line break because of lacking width,
a percent is subtracted for every child element */
/* To enable "override" a new Modifier is generated and joined
with the modifier of the component */
icomp._modifier._modifications[direction] = childDistributionFraction + "%";
icomp._modifier._modifications["max-"+direction] = childDistributionFraction + "%";
}
}
}
return super.generate(modifier, styleStore, functionStore);
}
} }
/** /**
@ -146,25 +182,10 @@ class Column extends FlexContainerComponent {
constructor(attr = {}, modifier = null) { constructor(attr = {}, modifier = null) {
super(attr, modifier); super(attr, modifier);
this.addStyleClass("column-component"); this.addStyleClass("column-component");
this._flexDirection = "column";
this.setFlexDirection(); this.setFlexDirection();
} }
/**
* @override
* @inheritdoc
* @extends Component.generate()
*/
generate() {
if (this._distributeEvenglyAfterGenerate) {
for (const child of this._children) {
child.modifier(
new Modifier()
.fillMaxHeight(((100 - this._children.length) / this._children.length) / 100)
);
}
}
return super.generate();
}
} }
/** /**
@ -182,6 +203,7 @@ class Row extends FlexContainerComponent {
constructor(attr = {}, modifier = null) { constructor(attr = {}, modifier = null) {
super(attr, modifier); super(attr, modifier);
this.addStyleClass("row-component") this.addStyleClass("row-component")
this._flexDirection = "row";
//this.modifier(new Modifier().fillMaxWidth()); //this.modifier(new Modifier().fillMaxWidth());
this.setFlexDirection(false); this.setFlexDirection(false);
} }
@ -192,38 +214,23 @@ class Row extends FlexContainerComponent {
* @returns {Row} * @returns {Row}
*/ */
childContext(innerComponent) { childContext(innerComponent) {
if (innerComponent instanceof Array) { function setFloat(comp, side = "right") {
innerComponent comp._modifier = new Modifier()
.map(cl => (cl instanceof Component ? cl : cl.ensureModifier().toComponent())) .setStyleRule("float", side)
.forEach((icomp, i) => { .join(comp._modifier);
/* sets the width for all elements,
to avoid overlapping or line break because of lacking width,
a percent is subtracted for every child element */
/* To enable "override" a new Modifier is generated and joined
with the modifier of the component */
icomp._modifier = new Modifier()
.setStyleRule("float", (i === 0 ? "left" : "right"))
.join(icomp._modifier)
})
}
return super.childContext(innerComponent)
} }
/** super.childContext(innerComponent);
* @override
* @inheritdoc
* @extends Component.generate()
*/
generate() {
if (this._distributeEvenglyAfterGenerate) {
for (const child of this._children) { for (const child of this._children) {
child.modifier( onSingleOrArray(
new Modifier() child,
.fillMaxWidth(((100 - this._children.length) / this._children.length) / 100) (e, i) => setFloat(e, (i === 0 ? "left" : "right"))
); );
} }
return this;
} }
return super.generate();
}
} }

13
src/component.js

@ -410,11 +410,18 @@ class Component extends StyleAndScriptStoringComponent {
* Returns the constructed HTMLElement of this Component. * Returns the constructed HTMLElement of this Component.
* *
* *
* * @param {Modifier | undefined} [modifier=null]
* @param {ExtStorage | undefined} [styleStore=null]
* @param {ExtStorage | undefined} [functionStore=null]
* @param {ExtStorage} * @param {ExtStorage}
* @returns {WebTrinity} the constructed HTMLElement of this Component. * @returns {WebTrinity} the constructed HTMLElement of this Component.
*/ */
generate(styleStore = null, functionStore = null) { generate(modifier = null, styleStore = null, functionStore = null) {
if (modifier) {
this._modifier = modifier
.join(this._modifier);
}
this._wenity = new WebTrinity(); this._wenity = new WebTrinity();
if (!styleStore) { if (!styleStore) {
@ -446,7 +453,7 @@ class Component extends StyleAndScriptStoringComponent {
if (Page._useCssCalc) { if (Page._useCssCalc) {
child._modifier._updateDimensionsBy(this._modifier._paddingValues); child._modifier._updateDimensionsBy(this._modifier._paddingValues);
} }
child = child.generate(); child = child.generate(modifier, styleStore, functionStore);
let wenity = this._appendChildComponent(child); let wenity = this._appendChildComponent(child);

Loading…
Cancel
Save