Skip to content

Commit bbbcc85

Browse files
Implement add items step
1 parent 16ff92c commit bbbcc85

File tree

3,249 files changed

+205162
-12
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

3,249 files changed

+205162
-12
lines changed

resources/benchmark-runner.mjs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ class Page {
119119
_wrapElement(element) {
120120
return new PageElement(element);
121121
}
122+
123+
addEventListener(name, listener) {
124+
this._frame.contentWindow.addEventListener(name, listener);
125+
}
122126
}
123127

124128
const NATIVE_OPTIONS = {

resources/tests.mjs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,50 @@ Suites.push({
241241
],
242242
});
243243

244+
245+
Suites.push({
246+
name: "TodoMVC-WebComponents-IndexedDB",
247+
url: "resources/todomvc/vanilla-examples/javascript-wc-indexeddb/dist/index.html",
248+
tags: ["todomvc", "webcomponents"],
249+
type: "async",
250+
async prepare(page) {
251+
await page.waitForElement("todo-app");
252+
await page.waitForElement(".indexeddb-ready");
253+
254+
},
255+
tests: [
256+
new BenchmarkTestStep(`Adding${numberOfItemsToAdd}Items`, async (page) => {
257+
const input = page.querySelector(".new-todo-input", ["todo-app", "todo-topbar"]);
258+
const indexedDBAddPromise = new Promise((resolve) => {
259+
page.addEventListener("indexeddb-add-completed", () => {
260+
resolve();
261+
});
262+
});
263+
for (let i = 0; i < numberOfItemsToAdd; i++) {
264+
input.setValue(getTodoText(defaultLanguage, i));
265+
input.dispatchEvent("input");
266+
input.enter("keyup");
267+
}
268+
await indexedDBAddPromise;
269+
}),
270+
// new BenchmarkTestStep("CompletingAllItems", (page) => {
271+
// const items = page.querySelectorAll("todo-item", ["todo-app", "todo-list"]);
272+
// for (let i = 0; i < numberOfItemsToAdd; i++) {
273+
// const item = items[i].querySelectorInShadowRoot(".toggle-todo-input");
274+
// item.click();
275+
// }
276+
// }),
277+
// new BenchmarkTestStep("DeletingAllItems", (page) => {
278+
// const items = page.querySelectorAll("todo-item", ["todo-app", "todo-list"]);
279+
// for (let i = numberOfItemsToAdd - 1; i >= 0; i--) {
280+
// const item = items[i].querySelectorInShadowRoot(".remove-todo-button");
281+
// item.click();
282+
// }
283+
// }),
284+
],
285+
});
286+
287+
244288
Suites.push({
245289
name: "TodoMVC-WebComponents",
246290
url: "resources/todomvc/vanilla-examples/javascript-web-components/dist/index.html",

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class TodoApp extends HTMLElement {
2929
this.toggleItems = this.toggleItems.bind(this);
3030
this.clearCompletedItems = this.clearCompletedItems.bind(this);
3131
this.routeChange = this.routeChange.bind(this);
32+
this.moveToNextPage = this.moveToNextPage.bind(this);
33+
this.moveToPreviousPage = this.moveToPreviousPage.bind(this);
3234

3335
this.router = useRouter();
3436
}
@@ -48,16 +50,17 @@ class TodoApp extends HTMLElement {
4850
}
4951

5052
toggleItem(event) {
51-
if (event.detail.completed === "true") {
53+
if (event.detail.completed) {
5254
this.#numberOfCompletedItems++;
5355
} else {
5456
this.#numberOfCompletedItems--;
5557
}
58+
this.list.toggleItem(event.detail.itemNumber, event.detail.completed);
5659
this.update("toggle-item", event.detail.id);
5760
}
5861

5962
removeItem(event) {
60-
if (event.datail.completed === "true") {
63+
if (event.detail.completed) {
6164
this.#numberOfCompletedItems--;
6265
}
6366
this.#numberOfItems--;
@@ -76,6 +79,17 @@ class TodoApp extends HTMLElement {
7679
this.list.removeCompletedItems();
7780
}
7881

82+
moveToNextPage() {
83+
this.list.moveToNextPage();
84+
}
85+
86+
moveToPreviousPage() {
87+
// Skeleton implementation of previous page navigation
88+
this.list.moveToPreviousPage().then(() => {
89+
this.bottombar.reenablePreviousPageButton();
90+
});
91+
}
92+
7993
update() {
8094
const totalItems = this.#numberOfItems;
8195
const completedItems = this.#numberOfCompletedItems;
@@ -100,6 +114,8 @@ class TodoApp extends HTMLElement {
100114
this.list.listNode.addEventListener("update-item", this.updateItem);
101115

102116
this.bottombar.addEventListener("clear-completed-items", this.clearCompletedItems);
117+
this.bottombar.addEventListener("next-page", this.moveToNextPage);
118+
this.bottombar.addEventListener("previous-page", this.moveToPreviousPage);
103119
}
104120

105121
removeListeners() {
@@ -111,6 +127,8 @@ class TodoApp extends HTMLElement {
111127
this.list.listNode.removeEventListener("update-item", this.updateItem);
112128

113129
this.bottombar.removeEventListener("clear-completed-items", this.clearCompletedItems);
130+
this.bottombar.removeEventListener("next-page", this.moveToNextPage);
131+
this.bottombar.removeEventListener("previous-page", this.moveToPreviousPage);
114132
}
115133

116134
routeChange(route) {

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

Lines changed: 55 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,38 @@ 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 additionalStyles = new CSSStyleSheet();
7+
additionalStyles.replaceSync(`
8+
9+
.clear-completed-button, .clear-completed-button:active,
10+
.todo-status,
11+
.filter-list
12+
{
13+
position: unset;
14+
transform: unset;
15+
}
16+
17+
.bottombar {
18+
display: grid;
19+
grid-template-columns: repeat(7, 1fr);
20+
align-items: center;
21+
justify-items: center;
22+
}
23+
24+
.bottombar > * {
25+
grid-column: span 1;
26+
}
27+
28+
.filter-list {
29+
grid-column: span 3;
30+
}
31+
32+
.bottombar.display-none {
33+
display: none;
34+
}
35+
`);
36+
37+
638
class TodoBottombar extends HTMLElement {
739
static get observedAttributes() {
840
return ["total-items", "active-items"];
@@ -20,17 +52,19 @@ class TodoBottombar extends HTMLElement {
2052
this.shadow = this.attachShadow({ mode: "open" });
2153
this.htmlDirection = document.dir || "ltr";
2254
this.setAttribute("dir", this.htmlDirection);
23-
this.shadow.adoptedStyleSheets = [globalStyles, bottombarStyles];
55+
this.shadow.adoptedStyleSheets = [globalStyles, bottombarStyles, additionalStyles];
2456
this.shadow.append(node);
2557

2658
this.clearCompletedItems = this.clearCompletedItems.bind(this);
59+
this.MoveToNextPage = this.MoveToNextPage.bind(this);
60+
this.MoveToPreviousPage = this.MoveToPreviousPage.bind(this);
2761
}
2862

2963
updateDisplay() {
3064
if (parseInt(this["total-items"]) !== 0)
31-
this.element.style.display = "block";
65+
this.element.classList.remove("display-none");
3266
else
33-
this.element.style.display = "none";
67+
this.element.classList.add("display-none");
3468

3569
this.todoStatus.textContent = `${this["active-items"]} ${this["active-items"] === "1" ? "item" : "items"} left!`;
3670
}
@@ -47,13 +81,27 @@ class TodoBottombar extends HTMLElement {
4781
clearCompletedItems() {
4882
this.dispatchEvent(new CustomEvent("clear-completed-items"));
4983
}
84+
85+
MoveToNextPage() {
86+
console.log("Moving to next page");
87+
this.dispatchEvent(new CustomEvent("next-page"));
88+
}
89+
90+
MoveToPreviousPage() {
91+
this.element.querySelector(".previous-page-button").disabled = true;
92+
this.dispatchEvent(new CustomEvent("previous-page"));
93+
}
5094

5195
addListeners() {
5296
this.clearCompletedButton.addEventListener("click", this.clearCompletedItems);
97+
this.element.querySelector(".next-page-button").addEventListener("click", this.MoveToNextPage);
98+
this.element.querySelector(".previous-page-button").addEventListener("click", this.MoveToPreviousPage);
5399
}
54100

55101
removeListeners() {
56102
this.clearCompletedButton.removeEventListener("click", this.clearCompletedItems);
103+
this.getElementById("next-page-button").removeEventListener("click", this.MoveToNextPage);
104+
this.getElementById("previous-page-button").removeEventListener("click", this.MoveToPreviousPage);
57105
}
58106

59107
attributeChangedCallback(property, oldValue, newValue) {
@@ -65,6 +113,10 @@ class TodoBottombar extends HTMLElement {
65113
this.updateDisplay();
66114
}
67115

116+
reenablePreviousPageButton() {
117+
this.element.querySelector(".previous-page-button").disabled = false;
118+
}
119+
68120
connectedCallback() {
69121
this.updateDisplay();
70122
this.addListeners();

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

Lines changed: 3 additions & 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" style="display:none">
5+
<footer class="bottombar 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">
@@ -16,6 +16,8 @@ template.innerHTML = `
1616
</li>
1717
</ul>
1818
<button id="clear-completed" class="clear-completed-button">Clear completed</button>
19+
<button id="next-page" class="next-page-button"> Next </button>
20+
<button id="previous-page" class="previous-page-button"> Previous </button>
1921
</footer>
2022
`;
2123

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class TodoItem extends HTMLElement {
1919
this.itemid = "";
2020
this.itemtitle = "Todo Item";
2121
this.itemcompleted = "false";
22+
this.itemIndex = null;
2223

2324
const node = document.importNode(template.content, true);
2425
this.item = node.querySelector(".todo-item");
@@ -92,7 +93,7 @@ class TodoItem extends HTMLElement {
9293

9394
this.dispatchEvent(
9495
new CustomEvent("toggle-item", {
95-
detail: { completed: this.toggleInput.checked },
96+
detail: { completed: this.toggleInput.checked, itemNumber: this.itemIndex },
9697
bubbles: true,
9798
})
9899
);

0 commit comments

Comments
 (0)