diff --git a/coffeeComponent.js b/coffeeComponent.js new file mode 100644 index 0000000..f38d1b7 --- /dev/null +++ b/coffeeComponent.js @@ -0,0 +1,85 @@ +export default class coffeeComponent { + + props = {} + + constructor(number) { + this.props.number = number; + } + + render() { + if (!this.element) { + var parser = new DOMParser(); + var doc = parser.parseFromString(this.renderString(), 'text/html'); + this.element = doc.body.firstChild; + } + return this.element; + } + + renderString() { + return ` +
+ ` + } +} \ No newline at end of file diff --git a/coffeeList.js b/coffeeList.js new file mode 100644 index 0000000..edf137d --- /dev/null +++ b/coffeeList.js @@ -0,0 +1,40 @@ +import coffeeComponent from "./coffeeComponent.js"; + +export default class coffeeList { + _items = [] + _itemsNumbers = [] + _container + + constructor(container) { + this._container = container; + this.addItem(); + this.render(); + } + + addItem() { + this._items.push({id: this._items.length + 1, component: new coffeeComponent(this._items.length + 1)}) + this.render() + } + + removeItem(id) { + this._items.forEach((item, index) => { + if (item.id == id && index != 0) { + this._items.splice(index, 1); + } + }); + this.render() + } + + render() { + this._container.innerHTML = ''; + this._items.forEach(item => { + let node = item.component.render() + this._container.appendChild(node) + + this._container.querySelector(`#ButtonDelete${item.id}`).onclick = (e) => { + this.removeItem(item.id); + e.preventDefault(); + } + }); + } +} \ No newline at end of file diff --git a/index.html b/index.html index ddc8250..211fbff 100644 --- a/index.html +++ b/index.html @@ -3,67 +3,62 @@