Browse Source

FEAT: chainChild - chain of single elements

dev-feat-component_preview
chris 2 months ago
parent
commit
512551c4f4
  1. 61
      src/builder.js
  2. 7
      src/component.js
  3. 39
      src/componentAncestry/wrapperComponent.js
  4. 11
      src/modifier.js

61
src/builder.js

@ -11,11 +11,21 @@ const builder = {
components: { components: {
parent: {}, parent: {},
current: {}, current: {},
previous: {}, previous: null,
next: {}, next: {},
openedChain: {} openedChain: {}
}, },
/**
* Convenience function for the ChildbearerComponent.chainChild() method.
* @param {Component} callComponent
* @returns {builder}
*/
_nextComponent(callComponent) {
this.components.previous = callComponent;
return this;
},
/** /**
* @property {Function(Component|*): Component} * @property {Function(Component|*): Component}
*/ */
@ -139,6 +149,10 @@ const builder = {
if (modifier) { if (modifier) {
return compel.modifier(modifier); return compel.modifier(modifier);
} }
if (this.components.previous) {
compel._parentComponent = this.components.previous;
this.components.previous = null;
}
return compel; return compel;
}, },
@ -171,12 +185,19 @@ const builder = {
* @returns {InputComponent} * @returns {InputComponent}
*/ */
input: function (type, attr = {}, modifier = null) { input: function (type, attr = {}, modifier = null) {
return new InputComponent( let comp = new InputComponent(
document.createElement("input"), document.createElement("input"),
Object.assign({ "type": type }, attr), Object.assign({ "type": type }, attr),
modifier modifier
) )
.addStyleClass(`el-input`); .addStyleClass(`el-input`);
if (this.components.previous) {
comp._parentComponent = this.components.previous;
this.components.previous = null;
}
return comp;
}, },
@ -298,12 +319,19 @@ const builder = {
* @returns {InputComponent} * @returns {InputComponent}
*/ */
textarea: function (attr = {}, modifier = null) { textarea: function (attr = {}, modifier = null) {
return new InputComponent( let comp = new InputComponent(
document.createElement("textarea"), document.createElement("textarea"),
attr, attr,
modifier modifier
) )
.addStyleClass(`el-textarea`); .addStyleClass(`el-textarea`);
if (this.components.previous) {
comp._parentComponent = this.components.previous;
this.components.previous = null;
}
return comp;
}, },
/** /**
@ -420,7 +448,14 @@ const builder = {
* @param {Modifier} modifier * @param {Modifier} modifier
* @returns {Row} * @returns {Row}
*/ */
row: function (attr = {}, modifier = null) { return new Row(attr, modifier) }, row: function (attr = {}, modifier = null) {
let comp = new Row(attr, modifier);
if (this.components.previous) {
comp._parentComponent = this.components.previous;
this.components.previous = null;
}
return comp;
},
/** /**
* *
@ -429,7 +464,14 @@ const builder = {
* @param {Modifier} modifier * @param {Modifier} modifier
* @returns {Column} * @returns {Column}
*/ */
column: function (attr = {}, modifier = null) { return new Column(attr, modifier) }, column: function (attr = {}, modifier = null) {
let comp = new Column(attr, modifier);
if (this.components.previous) {
comp._parentComponent = this.components.previous;
this.components.previous = null;
}
return comp;
},
/** /**
* *
@ -437,7 +479,14 @@ const builder = {
* @param {Modifier} modifier * @param {Modifier} modifier
* @returns {FlexContainerComponent} * @returns {FlexContainerComponent}
*/ */
section: function (attr = {}, modifier = null) { return new FlexContainerComponent(attr, modifier, "section") }, section: function (attr = {}, modifier = null) {
let comp = new FlexContainerComponent(attr, modifier, "section");
if (this.components.previous) {
comp._parentComponent = this.components.previous;
this.components.previous = null;
}
return comp;
},
/** /**
* *

7
src/component.js

@ -417,6 +417,13 @@ class Component extends StyleAndScriptStoringComponent {
* @returns {WebTrinity} the constructed HTMLElement of this Component. * @returns {WebTrinity} the constructed HTMLElement of this Component.
*/ */
generate(modifier = null, styleStore = null, functionStore = null) { generate(modifier = null, styleStore = null, functionStore = null) {
if (this._parentComponent) {
let parent = this._parentComponent;
this._parentComponent = null;
return parent.childContext(this)
.generate(modifier, styleStore, functionStore);
}
if (modifier) { if (modifier) {
this._modifier = modifier this._modifier = modifier
.join(this._modifier); .join(this._modifier);

39
src/componentAncestry/wrapperComponent.js

@ -28,6 +28,10 @@ class ElementWrapper {
* @type {string} * @type {string}
*/ */
_givenName; _givenName;
/**
* @type {Component}
*/
_parentComponent;
/** /**
* Initializes the component * Initializes the component
@ -197,9 +201,42 @@ class ChildbearerComponent extends ElementWrapper {
if (!(component instanceof Component)) { if (!(component instanceof Component)) {
this.childContext(component.toComponent()) this.childContext(component.toComponent())
} else { } else {
this._children.push(component); this._children.push(component.end());
} }
} }
return this; return this;
} }
/**
* Ends chain for the current component.
* Returns the builder object.
* The Component that is selected there will be set as child to this one.
*
* This funciton is a convenience function.
* Mainly to offer the possibility to reduce the depth of method chains.
* Especially in the case of components with only one child.
*
* @returns {builder} the given
*/
chainChild() {
return builder._nextComponent(this);
}
/**
* Ends a chainChild - chain.
* If components are setup as chainChild
* they would be wrongfully taken through childContext().
* Therefore thoose chains are recursively resolved through this method.
* @returns {Component}
*/
end() {
let parent = this._parentComponent;
if (parent) {
this._parentComponent = null;
return parent
.childContext(this)
.end();
}
return this;
}
} }

11
src/modifier.js

@ -416,4 +416,15 @@ class ChainableModifier extends Modifier {
.modifier(this) .modifier(this)
.childContext(innerComponent); .childContext(innerComponent);
} }
/**
* Calls chainChild() from Component (ChildbearerComponent)
* @see {ChildbearerComponent.chainChild}
* @returns {builder}
*/
chainChild() {
return this._component
.modifier(this)
.chainChild()
}
} }

Loading…
Cancel
Save