ShoppingList zu einem custom html-element gemacht
This commit is contained in:
1 parent
725879970f
commit
e6ccba0219
1 file changed
+25
-31
@@ -1,31 +1,24 @@
|
||||
export class ShoppingList extends HTMLElement {
|
||||
|
||||
static get observedAttributes() { return ["list-name"] }
|
||||
|
||||
export class ShoppingList {
|
||||
constructor(name) {
|
||||
this.listName = name;
|
||||
this.lastModified = "";
|
||||
this.created = "";
|
||||
this.listID = 0;
|
||||
constructor() {
|
||||
super();
|
||||
this._rendered = false
|
||||
}
|
||||
|
||||
static fromJSON(listAsJSON) {
|
||||
const list = new ShoppingList(listAsJSON.listName);
|
||||
list.listID = listAsJSON.listID;
|
||||
return list;
|
||||
}
|
||||
connectedCallback() {
|
||||
if (this._rendered) return;
|
||||
|
||||
render() {
|
||||
const li = document.createElement("li");
|
||||
const a = document.createElement("a");
|
||||
const editButton = document.createElement("button");
|
||||
const deleteButton = document.createElement("button");
|
||||
|
||||
li.classList.add("list");
|
||||
li.dataset.id = this.listID;
|
||||
const listID = this.closest("li").dataset.id;
|
||||
|
||||
a.setAttribute("href", `/list/${this.listID}`);
|
||||
a.setAttribute("href", `/list/${listID}`);
|
||||
a.classList.add("list__name");
|
||||
a.textContent = this.listName;
|
||||
a.textContent = this.getAttribute("list-name");
|
||||
|
||||
editButton.classList.add("list__editButton");
|
||||
editButton.textContent = "Liste bearbeiten";
|
||||
@@ -33,23 +26,24 @@ export class ShoppingList {
|
||||
deleteButton.classList.add("list__deleteButton");
|
||||
deleteButton.textContent = "Liste löschen";
|
||||
|
||||
li.append(a, editButton, deleteButton);
|
||||
|
||||
return li;
|
||||
this.append(a, editButton, deleteButton);
|
||||
this._rendered = true;
|
||||
}
|
||||
|
||||
renderNavElement() {
|
||||
const li = document.createElement("li");
|
||||
const a = document.createElement("a");
|
||||
attributeChangedCallback(name, oldVal, newVal) {
|
||||
if (!this._rendered) return;
|
||||
if (oldVal == newVal) return;
|
||||
|
||||
li.classList.add("navbar__list");
|
||||
li.dataset.id = this.listID;
|
||||
const listLi = this.closest("li");
|
||||
|
||||
a.setAttribute("href", `/list/${this.listID}`);
|
||||
a.textContent = this.listName;
|
||||
|
||||
li.append(a);
|
||||
|
||||
return li;
|
||||
switch (name) {
|
||||
case "list-name":
|
||||
this.querySelector("list__name").textContent = name;
|
||||
document.querySelector(`.navbar__list[data-id='${listLi.dataset.id}'] > a`).textContent = newVal;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user