-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cea049d
commit 46bee7a
Showing
3 changed files
with
88 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* This singleton EventEmitter can be used throughout your application to handle custom events. | ||
*/ | ||
export class BasketStorage extends EventTarget { | ||
items = [] | ||
static instance | ||
|
||
static getInstance() { | ||
if (!BasketStorage.instance) { | ||
BasketStorage.instance = new BasketStorage(); | ||
} | ||
return BasketStorage.instance; | ||
} | ||
|
||
addItem(item) { | ||
this.items = [...this.items, item] | ||
this.dispatchEvent(new CustomEvent("updated", { detail: { item } })) | ||
} | ||
getItems() { | ||
return this.items | ||
} | ||
} | ||
|
||
const basketStorage = BasketStorage.getInstance(); | ||
export default basketStorage | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>WYSIWYG Editor</title> | ||
</head> | ||
<body> | ||
<script type="module" src="./main.js"></script> | ||
<app-basket></app-basket> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { html, css, LitElement } from "https://cdn.jsdelivr.net/npm/lit@2/+esm"; | ||
import basketStorage from './basketStorage.js' | ||
|
||
export class Basket extends LitElement { | ||
static get properties() { | ||
return { | ||
items: { type: Array }, | ||
}; | ||
} | ||
|
||
constructor() { | ||
super() | ||
} | ||
|
||
connectedCallback() { | ||
super.connectedCallback(); | ||
basketStorage.addEventListener("updated", (i) => { | ||
this.items = basketStorage.getItems() | ||
}) | ||
} | ||
|
||
submit(e) { | ||
e.preventDefault() | ||
|
||
const searchNode = this.renderRoot.querySelector('#search') | ||
if (searchNode) { | ||
const v = searchNode.value | ||
basketStorage.addItem(v) | ||
} | ||
searchNode.value = "" | ||
} | ||
|
||
render() { | ||
return html` | ||
<form @submit=${this.submit}> | ||
<div> | ||
<input type="text" id="search"/> | ||
<button type="submit">Submit</button> | ||
</div> | ||
<div> | ||
${this.items ? html`${this.items.map(i => { | ||
return html`<div>${i}</div>` | ||
})}` : html`<h2>No items</h2>`} | ||
</div> | ||
</form> | ||
`; | ||
} | ||
} | ||
customElements.define('app-basket', Basket); |