From 607965d74952a991f409d347803a38a523ae87c5 Mon Sep 17 00:00:00 2001 From: AlexPL2201 <99511840+AlexPL2201@users.noreply.github.com> Date: Tue, 8 Mar 2022 14:57:53 +0300 Subject: [PATCH 1/2] Create script.js --- Ex_1/script.js | 123 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 Ex_1/script.js diff --git a/Ex_1/script.js b/Ex_1/script.js new file mode 100644 index 0000000..b5ea25a --- /dev/null +++ b/Ex_1/script.js @@ -0,0 +1,123 @@ +const myCatalog = { + cart, + divCatalog: null, + products: [{ + id: '54321', + title: "Ноутбук", + price: 70000 + }, + { + id: '12345', + title: "Видеокарта", + price: 40000 + }, + { + id: '9876', + title: "Процессор", + price: 35000 + }], + + init() { + this.divCatalog = document.getElementById("catalog"); + this.addProduct(); + this.addEventHandlers(); + }, + + addProduct() { + for (let i = 0; i < this.products.length; i++) { + const productTitle = document.createElement('h2'); + const productPrice = document.createElement('h2'); + const btnAdd = document.createElement("button"); + + productTitle.innerHTML = "Наименование: " + "" + this.products[i].title + ""; + productPrice.innerHTML = "Стоимость: " + "" + this.products[i].price + ""; + + btnAdd.innerHTML = "

Добавить в корзину

"; + btnAdd.className = 'btn'; + btnAdd.setAttribute('data-id', this.products[i].id); + + const divProduct = document.createElement("div"); + divProduct.id = i; + divProduct.classList.add('product'); + divProduct.appendChild(productTitle); + divProduct.appendChild(productPrice); + divProduct.appendChild(btnAdd); + + this.divCatalog.appendChild(divProduct); + } + }, + + addEventHandlers() { + this.divCatalog.addEventListener('click', event => this.addToBusket(event)); + }, + + addToBusket(event) { + if (!event.target.classList.contains('btn')) return; + const productId = event.target.dataset.id; + const product = this.products.find(product => product.id == productId); + myCart.addToCart(product); + } +}; + +const myCart = { + divCart: null, + cart: [], + + init() { + addToCart(product); + }, + + addToCart(product) { + const productChecked = this.cart.find(productCheck => productCheck.id == product.id); + if (productChecked == undefined) { + product.count = 1; + this.cart.push(product); + } else { + product.count += 1; + } + this.countMe(); + this.showCart(); + }, + + showCart() { + this.divCart = document.getElementById("list"); + this.divCart.innerText = ""; + for (let i = 0; i < this.cart.length; i++) { + const divCartProduct = document.createElement("div"); + divCartProduct.classList.add("product" + i, 'product') + const productTitle = document.createElement('h2'); + const productPrice = document.createElement('h2'); + const productCount = document.createElement('h2'); + + productTitle.innerHTML = "Наименование: " + "" + this.cart[i].title + ""; + productPrice.innerHTML = "Стоимость: " + "" + this.cart[i].price + ""; + productCount.innerHTML = "Количество: " + "" + this.cart[i].count + ""; + + divCartProduct.appendChild(productTitle); + divCartProduct.appendChild(productPrice); + divCartProduct.appendChild(productCount); + + this.divCart.appendChild(divCartProduct); + } + }, + + countMe() { + let res = 0; + let len = 0; + for (let i = 0; i < this.cart.length; i++) { + res += this.cart[i].price * this.cart[i].count; + len += this.cart[i].count + } + const divCounted = document.createElement('div'); + if (res > 0) { + divCounted.innerHTML = "

Выбрано " + "" + len + " товара(ов) на сумму: " + "" + res +"

"; + } else { + divCounted.innerHTML = "

Корзина пуста!

"; + } + divCalculate = document.getElementById("calculate"); + divCalculate.innerHTML = ''; + divCalculate.appendChild(divCounted); + } +} + +myCatalog.init() From 19ac89ac4784e944f1c07eaccc1fc64416f153fa Mon Sep 17 00:00:00 2001 From: AlexPL2201 <99511840+AlexPL2201@users.noreply.github.com> Date: Tue, 8 Mar 2022 15:00:55 +0300 Subject: [PATCH 2/2] Create index.html --- Ex_1/index.html | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 Ex_1/index.html diff --git a/Ex_1/index.html b/Ex_1/index.html new file mode 100644 index 0000000..ab30401 --- /dev/null +++ b/Ex_1/index.html @@ -0,0 +1,37 @@ + + + + + Title + + + + +
+

Каталог:

+
+ +
+

Корзина:

+
+ +
+
+ +
+
+ +