Skip to content

Commit 3f58a60

Browse files
Revert "Update to latest simplified WC implementation"
This reverts commit 8dff6f2.
1 parent 190a149 commit 3f58a60

File tree

16 files changed

+228
-160
lines changed

16 files changed

+228
-160
lines changed

resources/todomvc/vanilla-examples/javascript-web-components/dist/components/todo-app/todo-app.component.js

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@ import appStyles from "../../styles/app.constructable.js";
66
import mainStyles from "../../styles/main.constructable.js";
77
class TodoApp extends HTMLElement {
88
#isReady = false;
9-
#numberOfItems = 0;
10-
#numberOfCompletedItems = 0;
9+
#data = [];
1110
constructor() {
1211
super();
1312

@@ -43,28 +42,37 @@ class TodoApp extends HTMLElement {
4342

4443
addItem(event) {
4544
const { detail: item } = event;
46-
this.list.addItem(item, this.#numberOfItems++);
45+
46+
this.#data.push(item);
47+
this.list.addItem(item);
48+
4749
this.update("add-item", item.id);
4850
}
4951

5052
toggleItem(event) {
51-
if (event.detail.completed)
52-
this.#numberOfCompletedItems++;
53-
else
54-
this.#numberOfCompletedItems--;
53+
this.#data.forEach((entry) => {
54+
if (entry.id === event.detail.id)
55+
entry.completed = event.detail.completed;
56+
});
5557

5658
this.update("toggle-item", event.detail.id);
5759
}
5860

5961
removeItem(event) {
60-
if (event.detail.completed)
61-
this.#numberOfCompletedItems--;
62+
this.#data.forEach((entry, index) => {
63+
if (entry.id === event.detail.id)
64+
this.#data.splice(index, 1);
65+
});
6266

63-
this.#numberOfItems--;
6467
this.update("remove-item", event.detail.id);
6568
}
6669

6770
updateItem(event) {
71+
this.#data.forEach((entry) => {
72+
if (entry.id === event.detail.id)
73+
entry.title = event.detail.title;
74+
});
75+
6876
this.update("update-item", event.detail.id);
6977
}
7078

@@ -76,12 +84,13 @@ class TodoApp extends HTMLElement {
7684
this.list.removeCompletedItems();
7785
}
7886

79-
update() {
80-
const totalItems = this.#numberOfItems;
81-
const completedItems = this.#numberOfCompletedItems;
82-
const activeItems = totalItems - completedItems;
87+
update(type = "", id = "") {
88+
const totalItems = this.#data.length;
89+
const activeItems = this.#data.filter((entry) => !entry.completed).length;
90+
const completedItems = totalItems - activeItems;
8391

8492
this.list.setAttribute("total-items", totalItems);
93+
this.list.updateElements(type, id);
8594

8695
this.topbar.setAttribute("total-items", totalItems);
8796
this.topbar.setAttribute("active-items", activeItems);

resources/todomvc/vanilla-examples/javascript-web-components/dist/components/todo-bottombar/todo-bottombar.component.js

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@ import template from "./todo-bottombar.template.js";
33
import globalStyles from "../../styles/global.constructable.js";
44
import bottombarStyles from "../../styles/bottombar.constructable.js";
55

6-
const customStyles = new CSSStyleSheet();
7-
customStyles.replaceSync(`
8-
.bottombar {
9-
display: block;
10-
}
11-
12-
:host([total-items="0"]) > .bottombar {
13-
display: none;
14-
}
15-
`);
16-
176
class TodoBottombar extends HTMLElement {
187
static get observedAttributes() {
198
return ["total-items", "active-items"];
@@ -31,13 +20,18 @@ class TodoBottombar extends HTMLElement {
3120
this.shadow = this.attachShadow({ mode: "open" });
3221
this.htmlDirection = document.dir || "ltr";
3322
this.setAttribute("dir", this.htmlDirection);
34-
this.shadow.adoptedStyleSheets = [globalStyles, bottombarStyles, customStyles];
23+
this.shadow.adoptedStyleSheets = [globalStyles, bottombarStyles];
3524
this.shadow.append(node);
3625

3726
this.clearCompletedItems = this.clearCompletedItems.bind(this);
3827
}
3928

4029
updateDisplay() {
30+
if (parseInt(this["total-items"]) !== 0)
31+
this.element.style.display = "block";
32+
else
33+
this.element.style.display = "none";
34+
4135
this.todoStatus.textContent = `${this["active-items"]} ${this["active-items"] === "1" ? "item" : "items"} left!`;
4236
}
4337

resources/todomvc/vanilla-examples/javascript-web-components/dist/components/todo-bottombar/todo-bottombar.template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const template = document.createElement("template");
22

33
template.id = "todo-bottombar-template";
44
template.innerHTML = `
5-
<footer class="bottombar">
5+
<footer class="bottombar" style="display:none">
66
<div class="todo-status"><span class="todo-count">0</span> item left</div>
77
<ul class="filter-list">
88
<li class="filter-item">

resources/todomvc/vanilla-examples/javascript-web-components/dist/components/todo-item/todo-item.component.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class TodoItem extends HTMLElement {
6565
}
6666
break;
6767
case "itemcompleted":
68-
this.toggleInput.checked = this.itemcompleted === "true";
68+
this.toggleInput.checked = this.itemcompleted === "true" ? true : false;
6969
break;
7070
}
7171
});
@@ -92,7 +92,7 @@ class TodoItem extends HTMLElement {
9292

9393
this.dispatchEvent(
9494
new CustomEvent("toggle-item", {
95-
detail: { completed: this.toggleInput.checked },
95+
detail: { id: this.itemid, completed: this.toggleInput.checked },
9696
bubbles: true,
9797
})
9898
);
@@ -103,7 +103,7 @@ class TodoItem extends HTMLElement {
103103
// (therefore the removal has to happen after the list is updated)
104104
this.dispatchEvent(
105105
new CustomEvent("remove-item", {
106-
detail: { completed: this.toggleInput.checked },
106+
detail: { id: this.itemid },
107107
bubbles: true,
108108
})
109109
);
@@ -112,10 +112,17 @@ class TodoItem extends HTMLElement {
112112

113113
updateItem(event) {
114114
if (event.target.value !== this.itemtitle) {
115-
if (!event.target.value.length)
115+
if (!event.target.value.length) {
116116
this.removeItem();
117-
else
117+
} else {
118118
this.setAttribute("itemtitle", event.target.value);
119+
this.dispatchEvent(
120+
new CustomEvent("update-item", {
121+
detail: { id: this.itemid, title: event.target.value },
122+
bubbles: true,
123+
})
124+
);
125+
}
119126
}
120127

121128
this.cancelEdit();

resources/todomvc/vanilla-examples/javascript-web-components/dist/components/todo-list/todo-list.component.js

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,12 @@ import TodoItem from "../todo-item/todo-item.component.js";
44
import globalStyles from "../../styles/global.constructable.js";
55
import listStyles from "../../styles/todo-list.constructable.js";
66

7-
const customListStyles = new CSSStyleSheet();
8-
customListStyles.replaceSync(`
9-
.todo-list > todo-item {
10-
display: block;
11-
}
12-
13-
.todo-list[route="completed"] > [itemcompleted="false"] {
14-
display: none;
15-
}
16-
17-
.todo-list[route="active"] > [itemcompleted="true"] {
18-
display: none;
19-
}
20-
21-
.todo-list {
22-
display: block;
23-
}
24-
25-
:host([total-items="0"]) > .todo-list {
26-
display: none;
7+
class TodoList extends HTMLElement {
8+
static get observedAttributes() {
9+
return ["total-items"];
2710
}
28-
`);
2911

30-
class TodoList extends HTMLElement {
12+
#elements = [];
3113
#route = undefined;
3214

3315
constructor() {
@@ -39,7 +21,7 @@ class TodoList extends HTMLElement {
3921
this.shadow = this.attachShadow({ mode: "open" });
4022
this.htmlDirection = document.dir || "ltr";
4123
this.setAttribute("dir", this.htmlDirection);
42-
this.shadow.adoptedStyleSheets = [globalStyles, listStyles, customListStyles];
24+
this.shadow.adoptedStyleSheets = [globalStyles, listStyles];
4325
this.shadow.append(node);
4426
this.classList.add("show-priority");
4527

@@ -50,51 +32,96 @@ class TodoList extends HTMLElement {
5032
}
5133
}
5234

53-
addItem(entry, itemIndex) {
35+
addItem(entry) {
5436
const { id, title, completed } = entry;
5537
const element = new TodoItem();
5638

5739
element.setAttribute("itemid", id);
5840
element.setAttribute("itemtitle", title);
5941
element.setAttribute("itemcompleted", completed);
60-
element.setAttribute("data-priority", 4 - (itemIndex % 5));
6142

43+
const elementIndex = this.#elements.length;
44+
this.#elements.push(element);
6245
this.listNode.append(element);
46+
element.setAttribute("data-priority", 4 - (elementIndex % 5));
6347
}
6448

6549
addItems(items) {
6650
items.forEach((entry) => this.addItem(entry));
6751
}
6852

6953
removeCompletedItems() {
70-
Array.from(this.listNode.children).forEach((element) => {
54+
this.#elements = this.#elements.filter((element) => {
7155
if (element.itemcompleted === "true")
7256
element.removeItem();
57+
58+
return element.itemcompleted === "false";
7359
});
7460
}
7561

7662
toggleItems(completed) {
77-
Array.from(this.listNode.children).forEach((element) => {
63+
this.#elements.forEach((element) => {
7864
if (completed && element.itemcompleted === "false")
7965
element.toggleInput.click();
8066
else if (!completed && element.itemcompleted === "true")
8167
element.toggleInput.click();
8268
});
8369
}
8470

85-
updateRoute(route) {
86-
this.#route = route;
87-
switch (route) {
71+
updateStyles() {
72+
if (parseInt(this["total-items"]) !== 0)
73+
this.listNode.style.display = "block";
74+
else
75+
this.listNode.style.display = "none";
76+
}
77+
78+
updateView(element) {
79+
switch (this.#route) {
8880
case "completed":
89-
this.listNode.setAttribute("route", "completed");
81+
element.style.display = element.itemcompleted === "true" ? "block" : "none";
9082
break;
9183
case "active":
92-
this.listNode.setAttribute("route", "active");
84+
element.style.display = element.itemcompleted === "true" ? "none" : "block";
9385
break;
9486
default:
95-
this.listNode.setAttribute("route", "all");
87+
element.style.display = "block";
9688
}
9789
}
90+
91+
updateElements(type = "", id = "") {
92+
switch (type) {
93+
case "route-change":
94+
this.#elements.forEach((element) => this.updateView(element));
95+
break;
96+
case "toggle-item":
97+
case "add-item":
98+
this.#elements.forEach((element) => {
99+
if (element.itemid === id)
100+
this.updateView(element);
101+
});
102+
break;
103+
case "remove-item":
104+
this.#elements = this.#elements.filter((element) => element.itemid !== id);
105+
break;
106+
}
107+
}
108+
109+
updateRoute(route) {
110+
this.#route = route;
111+
this.updateElements("route-change");
112+
}
113+
114+
attributeChangedCallback(property, oldValue, newValue) {
115+
if (oldValue === newValue)
116+
return;
117+
this[property] = newValue;
118+
if (this.isConnected)
119+
this.updateStyles();
120+
}
121+
122+
connectedCallback() {
123+
this.updateStyles();
124+
}
98125
}
99126

100127
customElements.define("todo-list", TodoList);

resources/todomvc/vanilla-examples/javascript-web-components/dist/components/todo-list/todo-list.template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const template = document.createElement("template");
22

33
template.id = "todo-list-template";
44
template.innerHTML = `
5-
<ul class="todo-list"></ul>
5+
<ul class="todo-list" style="display:none"></ul>
66
`;
77

88
export default template;

resources/todomvc/vanilla-examples/javascript-web-components/dist/components/todo-topbar/todo-topbar.component.js

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,9 @@ import { nanoid } from "../../utils/nanoid.js";
55
import globalStyles from "../../styles/global.constructable.js";
66
import topbarStyles from "../../styles/topbar.constructable.js";
77

8-
const customListStyles = new CSSStyleSheet();
9-
customListStyles.replaceSync(`
10-
.toggle-all-container {
11-
display: block;
12-
}
13-
:host([total-items="0"]) .toggle-all-container {
14-
display: none;
15-
}
16-
`);
17-
188
class TodoTopbar extends HTMLElement {
199
static get observedAttributes() {
20-
return ["active-items", "completed-items"];
10+
return ["total-items", "active-items", "completed-items"];
2111
}
2212

2313
#route = undefined;
@@ -33,7 +23,7 @@ class TodoTopbar extends HTMLElement {
3323
this.shadow = this.attachShadow({ mode: "open" });
3424
this.htmlDirection = document.dir || "ltr";
3525
this.setAttribute("dir", this.htmlDirection);
36-
this.shadow.adoptedStyleSheets = [globalStyles, topbarStyles, customListStyles];
26+
this.shadow.adoptedStyleSheets = [globalStyles, topbarStyles];
3727
this.shadow.append(node);
3828

3929
this.keysListeners = [];
@@ -68,6 +58,13 @@ class TodoTopbar extends HTMLElement {
6858
}
6959

7060
updateDisplay() {
61+
if (!parseInt(this["total-items"])) {
62+
this.toggleContainer.style.display = "none";
63+
return;
64+
}
65+
66+
this.toggleContainer.style.display = "block";
67+
7168
switch (this.#route) {
7269
case "active":
7370
this.toggleInput.checked = false;
@@ -78,7 +75,7 @@ class TodoTopbar extends HTMLElement {
7875
this.toggleInput.disabled = !parseInt(this["completed-items"]);
7976
break;
8077
default:
81-
this.toggleInput.checked = !parseInt(this["active-items"]);
78+
this.toggleInput.checked = this["total-items"] === this["completed-items"];
8279
this.toggleInput.disabled = false;
8380
}
8481
}

resources/todomvc/vanilla-examples/javascript-web-components/dist/components/todo-topbar/todo-topbar.template.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ template.innerHTML = `
77
<label for="new-todo" class="visually-hidden">Enter a new todo.</label>
88
<input id="new-todo" class="new-todo-input" placeholder="What needs to be done?" autofocus />
99
</div>
10-
<div class="toggle-all-container">
10+
<div class="toggle-all-container" style="display:none">
1111
<input id="toggle-all" class="toggle-all-input" type="checkbox" />
1212
<label for="toggle-all" class="toggle-all-label">Mark all todos as complete.</label>
1313
</div>

0 commit comments

Comments
 (0)