Skip to content

Commit 38d3afe

Browse files
committed
Refactor logic components
1 parent 138dc60 commit 38d3afe

File tree

2 files changed

+31
-51
lines changed

2 files changed

+31
-51
lines changed

src/components/Logic.js

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,38 +5,17 @@
55
import {Element} from '../element.js';
66
import {onWindowResize} from '../window.js';
77

8-
export const If = {
9-
True(condition, elements) {
10-
return new ConditionalElement(condition, elements, true);
11-
},
12-
False(condition, elements) {
13-
return new ConditionalElement(condition, elements, false);
14-
},
15-
DeviceMobile: (elements) => {
16-
return new MobileElement(elements);
17-
},
18-
DeviceNotMobile: (elements) => {
19-
return new NonMobileElement(elements);
20-
},
21-
DeviceSmall: (elements) => {
22-
return new SmallDeviceFilter(elements);
23-
},
24-
DeviceMedium: (elements) => {
25-
return new MediumDeviceFilter(elements);
26-
},
27-
DeviceLarge: (elements) => {
28-
return new LargeDeviceFilter(elements);
29-
}
30-
};
8+
export function If(condition, elements) {
9+
return new ConditionalElement(condition, elements, true);
10+
}
3111

3212
class ConditionalElement extends Element {
33-
constructor(condition, elements, evalTo) {
13+
constructor(condition, elements) {
3414
const element = document.createElement('span');
3515
super(element);
3616
this.element = element;
37-
this.evalTo = evalTo;
3817
this.condition = condition;
39-
if ((this.condition) === this.evalTo) {
18+
if ((this.condition) === true) {
4019
this.elements = elements;
4120
}
4221
}
@@ -49,7 +28,7 @@ class ConditionalElement extends Element {
4928
}
5029

5130
ElseIf(condition, elements) {
52-
if ((condition) === this.evalTo) {
31+
if ((condition) === true) {
5332
this.elements = elements;
5433
}
5534
return this;
@@ -75,7 +54,7 @@ class SwitchElement extends Element {
7554
Case(conditional, elements) {
7655
this.cases[`${conditional}`] = {
7756
conditional: conditional,
78-
elements: elements
57+
elements: elements,
7958
};
8059
return this;
8160
}
@@ -102,6 +81,24 @@ class SwitchElement extends Element {
10281
}
10382
}
10483

84+
export const Device = {
85+
Mobile: (elements) => {
86+
return new MobileElement(elements);
87+
},
88+
NotMobile: (elements) => {
89+
return new NonMobileElement(elements);
90+
},
91+
Small: (elements) => {
92+
return new SmallDeviceFilter(elements);
93+
},
94+
Medium: (elements) => {
95+
return new MediumDeviceFilter(elements);
96+
},
97+
Large: (elements) => {
98+
return new LargeDeviceFilter(elements);
99+
},
100+
};
101+
105102
class MobileElement extends Element {
106103
constructor(elements) {
107104
const element = document.createElement('span');
@@ -185,4 +182,4 @@ class LargeDeviceFilter extends DeviceFilter {
185182
this.element.style.display = 'none';
186183
}
187184
}
188-
}
185+
}

src/components/Repeat.js

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,21 @@
22
* Copyright (c) 2023 Brandon Jordan
33
*/
44

5-
import {Element} from "../element.js";
5+
import {Element} from '../element.js';
66

7-
export const Repeat = {
8-
For(items, callback) {
9-
return new ForElement(items, callback);
10-
},
11-
Each(items, callback) {
12-
return new ForEachElement(items, callback);
13-
}
7+
export function ForEach(items, callback) {
8+
return new ForEachElement(items, callback);
149
}
1510

16-
class ForElement extends Element {
11+
class ForEachElement extends Element {
1712
constructor(items, callback) {
1813
const element = document.createElement('span');
1914
super(element);
2015
this.element = element;
2116
this.elements = [];
2217
for (let key in items) {
2318
const value = items[key];
24-
this.elements.push(...callback(key, value));
19+
this.elements.push(...callback(value, key));
2520
}
2621
}
2722
}
28-
29-
class ForEachElement extends Element {
30-
constructor(items, callback) {
31-
const element = document.createElement('span');
32-
super(element);
33-
this.element = element;
34-
this.elements = [];
35-
items.forEach((item, index) => {
36-
this.elements.push(...callback(item, index));
37-
});
38-
}
39-
}

0 commit comments

Comments
 (0)