Skip to content

Commit

Permalink
structured logical block
Browse files Browse the repository at this point in the history
  • Loading branch information
GlauberF committed May 6, 2021
1 parent 97abaaa commit 83786c6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 30 deletions.
15 changes: 9 additions & 6 deletions example/example.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ const Templates = {
if: [
{
operator: '===',
content: 1,
value: 1
value: 1,
analyzes: 1
}
],
},
Expand All @@ -32,15 +32,18 @@ const Templates = {
if: [
{
operator: '!==',
content: obj.name,
value: 'Test action buttons'
value: obj.name,
analyzes: 'Test action buttons'
}
],
},
tag: 'div',
classes: ['doc-icon-header'],
"classes": [
"card-priority",
"card-priority--medium"
],
children: [
{ tag: 'h3', content: obj.name }
{ tag: 'span', content: obj.name }
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion lib/br-dom.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brdom.js",
"version": "1.2.0",
"version": "2.1.1",
"description": "Generate HTML at runtime with javascript",
"main": "lib/br-dom.min.js",
"scripts": {
Expand Down
17 changes: 8 additions & 9 deletions src/br-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,36 @@ export default class BrDom {
this.children = childObjects;
this.element = this.domElement;
}
_checkConditions(domElement) {
var _a;
_checkConditions(conditions, domElement) {
// IF
if (((_a = this.conditions) === null || _a === void 0 ? void 0 : _a.if) && Array.isArray(this.conditions.if)) {
if ((conditions === null || conditions === void 0 ? void 0 : conditions.if) && Array.isArray(conditions.if)) {
for (const condition of this.conditions.if) {
if (condition.operator === '==' || condition.operator === '===') {
if (condition.content !== condition.value) {
if (condition.value !== condition.analyzes) {
return domElement;
}
}
if (condition.operator === '!=' || condition.operator === '!==') {
if (condition.content === condition.value) {
if (condition.value === condition.analyzes) {
return domElement;
}
}
if (condition.operator === '>' || condition.operator === '>=') {
if (condition.content < condition.value) {
if (condition.value < condition.analyzes) {
return domElement;
}
}
if (condition.operator === '<' || condition.operator === '<=') {
if (condition.content > condition.value) {
if (condition.value > condition.analyzes) {
return domElement;
}
}
}
}
}
get domElement() {
const domElement = document.createElement(this.tag);
if (this._checkConditions(domElement)) {
let domElement = document.createElement(this.tag);
if (this._checkConditions(this.conditions, domElement)) {
this.element = '';
return this.element;
}
Expand Down
31 changes: 19 additions & 12 deletions src/br-dom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@ export default class BrDom {
private id: any;
private conditions: {
[k: string]: {
operator: string;
content: any;
value: any
operator: '=='|'==='|'!='|'!=='|'>'|'>='|'<'|'<=';
value: any;
analyzes: any;
reservedStructure?: {
classes?: string[];
properties?: {
[k: string]: any
};
content?: any
}
};
};
private content: any;
Expand Down Expand Up @@ -36,30 +43,31 @@ export default class BrDom {

/**
* Analyzes if it has if condition and if it meets to continue
* @param conditions
* @param domElement
* @private
*/
private _checkConditions(domElement) {
private _checkConditions(conditions, domElement) {
// IF
if(this.conditions?.if && Array.isArray(this.conditions.if)) {
if(conditions?.if && Array.isArray(conditions.if)) {
for (const condition of this.conditions.if) {
if (condition.operator === '==' || condition.operator === '===') {
if(condition.content !== condition.value) {
if(condition.value !== condition.analyzes) {
return domElement;
}
}
if (condition.operator === '!=' || condition.operator === '!==') {
if(condition.content === condition.value) {
if(condition.value === condition.analyzes) {
return domElement;
}
}
if (condition.operator === '>' || condition.operator === '>=') {
if(condition.content < condition.value) {
if(condition.value < condition.analyzes) {
return domElement;
}
}
if (condition.operator === '<' || condition.operator === '<=') {
if(condition.content > condition.value) {
if(condition.value > condition.analyzes) {
return domElement;
}
}
Expand All @@ -74,10 +82,10 @@ export default class BrDom {
* console.log(template1.domElement);
*/
get domElement(): HTMLElement {
const domElement = document.createElement(this.tag);
let domElement = document.createElement(this.tag);

// Conditions
if(this._checkConditions(domElement)) {
if(this._checkConditions(this.conditions, domElement)) {
this.element = '';
return this.element;
}
Expand All @@ -90,7 +98,6 @@ export default class BrDom {
}
if (this.properties) {
for (const prop in this.properties) {
// domElement[prop] = this.properties[prop];
domElement.setAttribute(prop, this.properties[prop]);
}
}
Expand Down

0 comments on commit 83786c6

Please sign in to comment.