From 2c5cf5a3da25d7c430e35d12361978bf3a8129cd Mon Sep 17 00:00:00 2001
From: novikovrabota433 <32838534+novikovrabota433@users.noreply.github.com>
Date: Wed, 6 May 2020 16:39:42 +0300
Subject: [PATCH] Add files via upload
---
1.css | 15 ++++
1.json | 25 ++++++
index.html | 51 ++++++++++++
main.js | 235 +++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 326 insertions(+)
create mode 100644 1.css
create mode 100644 1.json
create mode 100644 index.html
create mode 100644 main.js
diff --git a/1.css b/1.css
new file mode 100644
index 0000000..3fe1615
--- /dev/null
+++ b/1.css
@@ -0,0 +1,15 @@
+body {
+ padding-top: 80px;
+ }
+
+ .show-cart li {
+ display: flex;
+ }
+ .card {
+ margin-bottom: 20px;
+ }
+ .card-img-top {
+ width: 200px;
+ height: 200px;
+ align-self: center;
+ }
\ No newline at end of file
diff --git a/1.json b/1.json
new file mode 100644
index 0000000..672f11b
--- /dev/null
+++ b/1.json
@@ -0,0 +1,25 @@
+[{
+ "name": "Orange",
+ "description": "Orange",
+ "img": "http://www.azspagirls.com/files/2010/09/orange.jpg",
+ "nasklade": "73",
+ "price": "0.5",
+ "data-price": "0.5"
+
+}, {
+ "name": "Banana",
+ "description": "Banana",
+ "img": "http://images.all-free-download.com/images/graphicthumb/vector_illustration_of_ripe_bananas_567893.jpg",
+ "nasklade": "49",
+ "price": "1.22",
+ "data-price": "1.22"
+
+}, {
+ "name": "Lemon",
+ "description": "Lemon",
+ "img": "https://3.imimg.com/data3/IC/JO/MY-9839190/organic-lemon-250x250.jpg",
+ "nasklade": "100",
+ "price": "5",
+ "data-price": "5"
+
+}]
\ No newline at end of file
diff --git a/index.html b/index.html
new file mode 100644
index 0000000..66de65f
--- /dev/null
+++ b/index.html
@@ -0,0 +1,51 @@
+
+
+
+
+
+ Document
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/main.js b/main.js
new file mode 100644
index 0000000..227a8b1
--- /dev/null
+++ b/main.js
@@ -0,0 +1,235 @@
+
+const xhr = new XMLHttpRequest();
+ xhr.open('GET', `1.json`, true);
+ xhr.send(null);
+ xhr.onload = () => {
+ const data = JSON.parse(xhr.responseText);
+ console.log(data);
+ renderList(data);
+ }
+
+
+ function renderList(SetList) {
+ SetList.forEach(Set => {
+ const ul = document.querySelector('.container .row');
+ ul.insertAdjacentHTML('beforeend', `
+
+
+

+
+
${Set.name}
+
Price: $${Set.price}
+
${Set.description}
+
${Set.nasklade}
+
Add to cart
+
+
+ `);
+
+ })
+ var shoppingCart = (function() {
+ // =============================
+ // Private methods and propeties
+ // =============================
+ cart = [];
+
+ // Constructor
+ function Item(name, price, count) {
+ this.name = name;
+ this.price = price;
+ this.count = count;
+ }
+
+ // Save cart
+ function saveCart() {
+ localStorage.setItem('shoppingCart', JSON.stringify(cart));
+ }
+
+ // Load cart
+ function loadCart() {
+ cart = JSON.parse(localStorage.getItem('shoppingCart'));
+ }
+ if (localStorage.getItem("shoppingCart") != null) {
+ loadCart();
+ }
+
+
+ // =============================
+ // Public methods and propeties
+ // =============================
+ var obj = {};
+
+ // Add to cart
+ obj.addItemToCart = function(name, price, count) {
+ for(var item in cart) {
+ if(cart[item].name === name) {
+ cart[item].count ++;
+ saveCart();
+ return;
+ }
+ }
+ var item = new Item(name, price, count);
+ cart.push(item);
+ saveCart();
+ }
+ // Set count from item
+ obj.setCountForItem = function(name, count) {
+ for(var i in cart) {
+ if (cart[i].name === name) {
+ cart[i].count = count;
+ break;
+ }
+ }
+ };
+ // Remove item from cart
+ obj.removeItemFromCart = function(name) {
+ for(var item in cart) {
+ if(cart[item].name === name) {
+ cart[item].count --;
+ if(cart[item].count === 0) {
+ cart.splice(item, 1);
+ }
+ break;
+ }
+ }
+ saveCart();
+ }
+
+ // Remove all items from cart
+ obj.removeItemFromCartAll = function(name) {
+ for(var item in cart) {
+ if(cart[item].name === name) {
+ cart.splice(item, 1);
+ break;
+ }
+ }
+ saveCart();
+ }
+
+ // Clear cart
+ obj.clearCart = function() {
+ cart = [];
+ saveCart();
+ }
+
+ // Count cart
+ obj.totalCount = function() {
+ var totalCount = 0;
+ for(var item in cart) {
+ totalCount += cart[item].count;
+ }
+ return totalCount;
+ }
+
+ // Total cart
+ obj.totalCart = function() {
+ var totalCart = 0;
+ for(var item in cart) {
+ totalCart += cart[item].price * cart[item].count;
+ }
+ return Number(totalCart.toFixed(2));
+ }
+
+ // List cart
+ obj.listCart = function() {
+ var cartCopy = [];
+ for(i in cart) {
+ item = cart[i];
+ itemCopy = {};
+ for(p in item) {
+ itemCopy[p] = item[p];
+
+ }
+ itemCopy.total = Number(item.price * item.count).toFixed(2);
+ cartCopy.push(itemCopy)
+ }
+ return cartCopy;
+ }
+
+ // cart : Array
+ // Item : Object/Class
+ // addItemToCart : Function
+ // removeItemFromCart : Function
+ // removeItemFromCartAll : Function
+ // clearCart : Function
+ // countCart : Function
+ // totalCart : Function
+ // listCart : Function
+ // saveCart : Function
+ // loadCart : Function
+ return obj;
+ })();
+
+
+ // *****************************************
+ // Triggers / Events
+ // *****************************************
+ // Add item
+ $('.add-to-cart').click(function(event) {
+ event.preventDefault();
+ var name = $(this).data('name');
+ var price = Number($(this).data('price'));
+ shoppingCart.addItemToCart(name, price, 1);
+ displayCart();
+ });
+
+ // Clear items
+ $('.clear-cart').click(function() {
+ shoppingCart.clearCart();
+ displayCart();
+ });
+
+
+ function displayCart() {
+ var cartArray = shoppingCart.listCart();
+ var output = "";
+ for(var i in cartArray) {
+ output += "
"
+ + "" + cartArray[i].name + " | "
+ + "(" + cartArray[i].price + ") | "
+ + ""
+ + ""
+ + " | "
+ + " | "
+ + " = "
+ + "" + cartArray[i].total + " | "
+ + "
";
+ }
+ $('.show-cart').html(output);
+ $('.total-cart').html(shoppingCart.totalCart());
+ $('.total-count').html(shoppingCart.totalCount());
+ }
+
+ // Delete item button
+
+ $('.show-cart').on("click", ".delete-item", function(event) {
+ var name = $(this).data('name')
+ shoppingCart.removeItemFromCartAll(name);
+ displayCart();
+ })
+
+
+ // -1
+ $('.show-cart').on("click", ".minus-item", function(event) {
+ var name = $(this).data('name')
+ shoppingCart.removeItemFromCart(name);
+ displayCart();
+ })
+ // +1
+ $('.show-cart').on("click", ".plus-item", function(event) {
+ var name = $(this).data('name')
+ shoppingCart.addItemToCart(name);
+ displayCart();
+ })
+
+ // Item count input
+ $('.show-cart').on("change", ".item-count", function(event) {
+ var name = $(this).data('name');
+ var count = Number($(this).val());
+ shoppingCart.setCountForItem(name, count);
+ displayCart();
+ });
+
+ displayCart();
+
+ }