From dc9ee02231f90e7792aa23d01b32341ba72bd04b Mon Sep 17 00:00:00 2001 From: archi Date: Thu, 28 Jan 2021 00:48:37 +0300 Subject: [PATCH 1/2] lesson5 --- css/normalize.css | 348 ++++++++++++++++++++++++++++++++++++++++++ css/style.css | 171 +++++++++++++++++++++ getProducts.json | 12 ++ index.html | 79 ++++++++++ js/main.js | 377 ++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 987 insertions(+) create mode 100644 css/normalize.css create mode 100644 css/style.css create mode 100644 getProducts.json create mode 100644 index.html create mode 100644 js/main.js diff --git a/css/normalize.css b/css/normalize.css new file mode 100644 index 0000000..16e4c01 --- /dev/null +++ b/css/normalize.css @@ -0,0 +1,348 @@ +/*! normalize.css v8.0.1 | MIT License | github.com/necolas/normalize.css */ + +/* Document + ========================================================================== */ + +/** + * 1. Correct the line height in all browsers. + * 2. Prevent adjustments of font size after orientation changes in iOS. + */ +html { + line-height: 1.15; /* 1 */ + -webkit-text-size-adjust: 100%; /* 2 */ +} + +/* Sections + ========================================================================== */ + +/** + * Remove the margin in all browsers. + */ + +body { + margin: 0; +} + +/** + * Render the `main` element consistently in IE. + */ + +main { + display: block; +} + +/** + * Correct the font size and margin on `h1` elements within `section` and + * `article` contexts in Chrome, Firefox, and Safari. + */ + +h1 { + font-size: 2em; + margin: 0.67em 0; +} + +/* Grouping content + ========================================================================== */ + +/** + * 1. Add the correct box sizing in Firefox. + * 2. Show the overflow in Edge and IE. + */ + +hr { + box-sizing: content-box; /* 1 */ + height: 0; /* 1 */ + overflow: visible; /* 2 */ +} + +/** + * 1. Correct the inheritance and scaling of font size in all browsers. + * 2. Correct the odd `em` font sizing in all browsers. + */ + +pre { + font-family: monospace, monospace; /* 1 */ + font-size: 1em; /* 2 */ +} + +/* Text-level semantics + ========================================================================== */ + +/** + * Remove the gray background on active links in IE 10. + */ + +a { + background-color: transparent; +} + +/** + * 1. Remove the bottom border in Chrome 57- + * 2. Add the correct text decoration in Chrome, Edge, IE, Opera, and Safari. + */ + +abbr[title] { + border-bottom: none; /* 1 */ + text-decoration: underline; /* 2 */ + text-decoration: underline dotted; /* 2 */ +} + +/** + * Add the correct font weight in Chrome, Edge, and Safari. + */ + +b, +strong { + font-weight: bolder; +} + +/** + * 1. Correct the inheritance and scaling of font size in all browsers. + * 2. Correct the odd `em` font sizing in all browsers. + */ + +code, +kbd, +samp { + font-family: monospace, monospace; /* 1 */ + font-size: 1em; /* 2 */ +} + +/** + * Add the correct font size in all browsers. + */ + +small { + font-size: 80%; +} + +/** + * Prevent `sub` and `sup` elements from affecting the line height in + * all browsers. + */ + +sub, +sup { + font-size: 75%; + line-height: 0; + position: relative; + vertical-align: baseline; +} + +sub { + bottom: -0.25em; +} + +sup { + top: -0.5em; +} + +/* Embedded content + ========================================================================== */ + +/** + * Remove the border on images inside links in IE 10. + */ + +img { + border-style: none; +} + +/* Forms + ========================================================================== */ + +/** + * 1. Change the font styles in all browsers. + * 2. Remove the margin in Firefox and Safari. + */ + +button, +input, +optgroup, +select, +textarea { + font-family: inherit; /* 1 */ + font-size: 100%; /* 1 */ + line-height: 1.15; /* 1 */ + margin: 0; /* 2 */ +} + +/** + * Show the overflow in IE. + * 1. Show the overflow in Edge. + */ + +button, +input { /* 1 */ + overflow: visible; +} + +/** + * Remove the inheritance of text transform in Edge, Firefox, and IE. + * 1. Remove the inheritance of text transform in Firefox. + */ + +button, +select { /* 1 */ + text-transform: none; +} + +/** + * Correct the inability to style clickable types in iOS and Safari. + */ + +button, +[type="button"], +[type="reset"], +[type="submit"] { + -webkit-appearance: button; +} + +/** + * Remove the inner border and padding in Firefox. + */ + +button::-moz-focus-inner, +[type="button"]::-moz-focus-inner, +[type="reset"]::-moz-focus-inner, +[type="submit"]::-moz-focus-inner { + border-style: none; + padding: 0; +} + +/** + * Restore the focus styles unset by the previous rule. + */ + +button:-moz-focusring, +[type="button"]:-moz-focusring, +[type="reset"]:-moz-focusring, +[type="submit"]:-moz-focusring { + outline: 1px dotted ButtonText; +} + +/** + * Correct the padding in Firefox. + */ + +fieldset { + padding: 0.35em 0.75em 0.625em; +} + +/** + * 1. Correct the text wrapping in Edge and IE. + * 2. Correct the color inheritance from `fieldset` elements in IE. + * 3. Remove the padding so developers are not caught out when they zero out + * `fieldset` elements in all browsers. + */ + +legend { + box-sizing: border-box; /* 1 */ + color: inherit; /* 2 */ + display: table; /* 1 */ + max-width: 100%; /* 1 */ + padding: 0; /* 3 */ + white-space: normal; /* 1 */ +} + +/** + * Add the correct vertical alignment in Chrome, Firefox, and Opera. + */ + +progress { + vertical-align: baseline; +} + +/** + * Remove the default vertical scrollbar in IE 10+. + */ + +textarea { + overflow: auto; +} + +/** + * 1. Add the correct box sizing in IE 10. + * 2. Remove the padding in IE 10. + */ + +[type="checkbox"], +[type="radio"] { + box-sizing: border-box; /* 1 */ + padding: 0; /* 2 */ +} + +/** + * Correct the cursor style of increment and decrement buttons in Chrome. + */ + +[type="number"]::-webkit-inner-spin-button, +[type="number"]::-webkit-outer-spin-button { + height: auto; +} + +/** + * 1. Correct the odd appearance in Chrome and Safari. + * 2. Correct the outline style in Safari. + */ + +[type="search"] { + -webkit-appearance: textfield; /* 1 */ + outline-offset: -2px; /* 2 */ +} + +/** + * Remove the inner padding in Chrome and Safari on macOS. + */ + +[type="search"]::-webkit-search-decoration { + -webkit-appearance: none; +} + +/** + * 1. Correct the inability to style clickable types in iOS and Safari. + * 2. Change font properties to `inherit` in Safari. + */ + +::-webkit-file-upload-button { + -webkit-appearance: button; /* 1 */ + font: inherit; /* 2 */ +} + +/* Interactive + ========================================================================== */ + +/* + * Add the correct display in Edge, IE 10+, and Firefox. + */ + +details { + display: block; +} + +/* + * Add the correct display in all browsers. + */ + +summary { + display: list-item; +} + +/* Misc + ========================================================================== */ + +/** + * Add the correct display in IE 10+. + */ + +template { + display: none; +} + +/** + * Add the correct display in IE 10. + */ + +[hidden] { + display: none; +} diff --git a/css/style.css b/css/style.css new file mode 100644 index 0000000..801db80 --- /dev/null +++ b/css/style.css @@ -0,0 +1,171 @@ +body{ + font-family: 'SF Pro Display', sans-serif; +} +header{ + display: flex; + background-color: #2f2a2d; + justify-content: space-between; + color: #fafafa; + padding: 30px 80px; +} +button:focus{ + outline: none; +} +.logo{ + + text-transform: uppercase; + font-weight: bold; +} +.btn-cart{ + background-color: #fafafa; + padding: 10px 20px; + border: 1px solid transparent; + color: #2f2a2d; + border-radius: 5px; + transition: all ease-in-out .4s; + cursor: pointer; +} +.btn-cart:hover{ + background-color: transparent; + border-color: #fafafa; + color: #fafafa; +} +.btn-cart, .logo{ + align-self: center; +} +/*.products {*/ +/*display: flex;*/ +/*justify-content: space-between;*/ +/*flex-wrap: wrap;*/ +/*padding: 40px 80px;*/ +/*}*/ +.products{ + column-gap: 30px; + display: grid; + grid-template-columns: repeat(auto-fit, 200px); + grid-template-rows: 1fr; + padding: 40px 80px; + justify-content: space-between; +} +p { + margin: 0 0 5px 0; +} +.product-item{ + display: flex; + flex-direction: column; + width: 200px; + border-radius: 5px; + overflow: hidden; + margin: 20px 0; +} +img { + max-width: 100%; + height: auto +} +.desc { + border: 1px solid #c0c0c040; + padding: 15px +} +.cart{ + position: relative; +} +.cart-block{ + box-shadow: 0 0 5px rgba(0, 0, 0, 0.62); + border-radius: 5px; + box-sizing: border-box; + right: 0; + top: 130%; + position: absolute; + background-color: white; + padding: 20px; + color: black; + width: 300px; +} + +.invisible{ + display: none; +} +.cart-block:before{ + content: ''; + width: 0; + height: 0; + position: absolute; + top: -10px; + right: 35px; + border-left: 10px solid transparent; + border-right: 10px solid transparent; + border-bottom: 10px solid white; +} + +.buy-btn, .del-btn{ + margin-top: 5px; + background-color: #2f2a2d; + padding: 5px 15px; + border: 1px solid transparent; + color: #fafafa; + border-radius: 5px; + transition: all ease-in-out .4s; + cursor: pointer; +} +.buy-btn:hover, .del-btn:hover{ + background-color: #fafafa; + color: #2f2a2d; + border: 1px solid #2f2a2d; +} +.cart-item { + display: flex; + justify-content: space-between; +} +.cart-item:not(:last-child){ + margin-bottom: 20px; +} +.product-bio{ + display: flex; +} +.cart-item img{ + align-self: flex-start; + margin-right: 15px; +} +.product-single-price{ + color: #474747; + font-size: 0.5em; +} +.product-price{ + margin-left: 30px; +} +.product-desc{ + max-width: 150px; +} +.product-quantity { + margin-top: 15px; + font-size: 0.75em; +} +.right-block{ + text-align: right; +} +.btn-search { + background-color: transparent; + border: none; + color: #fafafa; + font-size: 1.2em; + position: absolute; + bottom: 5px; + right: 0; +} +.search-form{ + position: relative; + margin-right: 50px; + display: inline-block; +} +.search-field:focus{ + outline: none; +} +.search-field { + box-sizing: border-box; + width: 200px; + color: #fafafa; + padding: 10px; + background-color: transparent; + border: none; + border-bottom: 2px solid #fafafa; +} diff --git a/getProducts.json b/getProducts.json new file mode 100644 index 0000000..f541d81 --- /dev/null +++ b/getProducts.json @@ -0,0 +1,12 @@ +[ + { + "id_product": 124, + "product_name": "Keyboard", + "price": 1600 + }, + { + "id_product": 457, + "product_name": "Gamepad", + "price": 3000 + } +] \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..9ff81a9 --- /dev/null +++ b/index.html @@ -0,0 +1,79 @@ + + + + + + Store + + + + + +
+
+ +
+
+ + +
+ +
+

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

+
+
+ +
+

{{ el.product_name }}

+

Quantity: {{ el.quantity }}

+

${{ el.price }} each

+
+
+
+

${{ el.quantity*el.price }}

+ +
+
+
+
+
+
+
+
+ +
+

{{ el.product_name }}

+

{{ el.price }}

+ +
+
+
+
+

{{ products }}

+

{{ searchMsg }}

+
+ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/js/main.js b/js/main.js new file mode 100644 index 0000000..4cdc2d4 --- /dev/null +++ b/js/main.js @@ -0,0 +1,377 @@ +const Shop = { + data() { + return { + API: 'https://raw.githubusercontent.com/GeekBrainsTutorial/online-store-api/master/responses', + catalogUrl: '/catalogData.json', + cartUrl: '/getBasket.json', + products: [], + cartItems: [], + isInvisibleCart: true, + isEmptyCart: true, + imgCatalog: 'https://placehold.it/200x150', + imgCart: 'https://placehold.it/50x100', + searchMsg: '' + } + }, + methods: { + getJson(url) { + return fetch(url) + .then(result => result.json()) + .catch(error => console.log(error)); + }, + addProduct(product) { + this.getJson(`${this.API}/addToBasket.json`) + .then(data => { + if (data.result) { + let find = this.cartItems.find(el => el.id_product === product.id_product); + if (find) { + find.quantity++; + } else { + this.cartItems.push(Object.assign({ quantity: 1 }, product)); + } + } else { + console.log('error'); + } + }); + this.showCartEmpty(); + }, + deletProduct(product) { + this.getJson(`${this.API}/deleteFromBasket.json`) + .then(data => { + if (data.result) { + if (product.quantity > 1) { + product.quantity -= 1; + } else { + this.cartItems.splice(this.cartItems.indexOf(product), 1); + } + } else { + console.log('error'); + } + }); + this.showCartEmpty(); + }, + filter() { + const regexp = new RegExp(this.searchMsg, 'i'); + for (let product of this.products) { + if (!regexp.test(product.product_name)) { + product.filtered = true; + } else { + delete product.filtered; + } + } + }, + toggleInvisible() { + this.isInvisibleCart ? this.isInvisibleCart = false : this.isInvisibleCart = true; + this.showCartEmpty(); + }, + showCartEmpty() { + if (this.cartItems != undefined && this.cartItems.length > 0) { + this.isEmptyCart = false; + console.log(this.isEmptyCart); + console.log(this.cartItems.length); + console.log(this.cartItems); + } else { + this.isEmptyCart = true; + console.log(this.isEmptyCart); + console.log(this.cartItems.length); + console.log(this.cartItems); + } + } + }, + mounted() { + this.getJson(`${this.API + this.catalogUrl}`) + .then(data => { + for (let product of data) { + this.products.push(product); + } + }); + this.getJson(`getProducts.json`) + .then(data => { + for (let product of data) { + this.products.push(product); + } + }); + + this.getJson(`${this.API + this.cartUrl}`) + .then(data => { + for (let product of data.contents) { + this.cartItems.push(product); + } + }); + } +}; + +Vue.createApp(Shop).mount('#app'); + +// let getData = (url) => { +// return new Promise((resolve, reject) => { +// let xhr = new XMLHttpRequest(); +// // window.ActiveXObject -> new ActiveXObject(); +// xhr.open('GET', url, true); +// xhr.onreadystatechange = () => { +// if (xhr.readyState === 4) { +// if (xhr.status !== 200) { +// reject('error'); +// } else { +// resolve(xhr.responseText); +// } +// } +// } +// }); +// }; + +// class Item { +// constructor(product, img = 'https://placehold.it/100x50') { +// let { product_name, price = 0, id_product } = product; +// this.title = product_name; +// this.img = img; +// this.price = price; +// this.id = id_product; +// this.rendered = false; +// } +// +// render() { +// this.rendered = true; +// return `
+// ${this.title} +//
+//

${this.title}

+//

${this.price}

+// +//
+//
` +// } +// } +// +// class Product extends Item {} +// +// class CartItem extends Item { +// constructor(el, img = 'https://placehold.it/50x100') { +// super(el, img); +// this.quantity = el.quantity; +// } +// +// changeQuantity(count) { +// this.quantity += count; +// this._updateItem(); +// } +// +// remove() { +// document.querySelector(`.cart-item[data-id="${this.id}"]`).remove(); +// } +// +// render() { +// this.rendered = true; +// return `
+//
+// Some image +//
+//

${this.title}

+//

Quantity: ${this.quantity}

+//

$${this.price} each

+//
+//
+//
+//

$${this.quantity*this.price}

+// +//
+//
` +// } +// +// _updateItem() { +// const block = document.querySelector(`.cart-item[data-id="${this.id}"]`); +// block.querySelector('.product-quantity').textContent = `Quantity: ${this.quantity}`; +// block.querySelector('.product-price').textContent = `$${this.quantity*this.price}`; +// } +// } +// +// class List { +// static API = 'https://raw.githubusercontent.com/GeekBrainsTutorial/online-store-api/master/responses'; +// static itemsMap = { +// List: Item, +// Cart: CartItem, +// ProductsList: Product +// }; +// +// +// constructor(url, container = '.products') { +// this.url = url; +// this.products = []; +// this.filtered = []; +// this.container = document.querySelector(container); +// this.init(); +// } +// +// init() { +// return false; +// } +// +// calcSum() { +// return this.products.reduce((accum, item) => accum += item.price, 0); +// } +// +// getJson(url) { +// return fetch(url ? url : `${List.API + this.url}`) +// .then(result => result.json()) +// .catch(error => console.log(error)); +// } +// +// handleData(data) { +// for (let dataEl of data) { +// const product = new List.itemsMap[this.constructor.name](dataEl); +// this.products.push(product); +// } +// +// this._render(); +// } +// +// getItem(id) { +// return this.products.find(product => product.id === id); +// } +// +// filter(value) { +// const regexp = new RegExp(value, 'i'); +// this.filtered = this.products.filter(product => regexp.test(product.title)); +// this.products.forEach(product => { +// const block = document.querySelector(`.product-item[data-id="${product.id}"]`); +// +// if (this.filtered.includes(product)) { +// block.classList.remove('invisible'); +// } else { +// block.classList.add('invisible'); +// } +// }) +// } +// +// _render() { +// for (let product of this.products) { +// if (product.rendered) { +// continue; +// } +// +// this.container.insertAdjacentHTML('beforeend', product.render()) +// } +// } +// } +// +// class ProductsList extends List{ +// constructor(cart, url = '/catalogData.json', container = '.products') { +// super(url, container); +// this.cart = cart; +// this.getJson() +// .then((data) => this.handleData(data)); +// } +// +// init() { +// this.container.addEventListener('click', e => { +// if (e.target.classList.contains('buy-btn')) { +// const id = +e.target.dataset['id']; +// this.cart.addProduct(this.getItem(id)); +// } +// }); +// +// document.querySelector('.search-form').addEventListener('submit', e => { +// e.preventDefault(); +// this.filter(document.querySelector(`.search-field`).value); +// }) +// } +// } +// +// class Cart extends List { +// constructor(url = '/getBasket.json', container = '.cart-block') { +// super(url, container); +// this.getJson() +// .then((data) => this.handleData(data.contents)); +// } +// +// init() { +// this.container.addEventListener('click', e => { +// if (e.target.classList.contains('del-btn')) { +// const id = +e.target.dataset['id']; +// this.removeProduct(this.getItem(id)); +// } +// }); +// document.querySelector('.btn-cart').addEventListener('click', () => { +// this.container.classList.toggle('invisible'); +// }) +// } +// +// addProduct(product) { +// this.getJson(`${List.API}/addToBasket.json`) +// .then(data => { +// if (data.result) { +// let find = this.products.find(el => el.id === product.id); +// if (find) { +// find.changeQuantity(1); +// } else { +// let prod = Object.assign({ quantity: 1, product_name: product.title, id_product: product.id }, product); +// this.handleData([prod]); +// } +// } else { +// console.log('error'); +// } +// }) +// } +// +// removeProduct(product) { +// this.getJson(`${List.API}/deleteFromBasket.json`) +// .then(data => { +// if (data.result) { +// if (product.quantity > 1) { +// product.changeQuantity(-1) +// } else { +// this.products.splice(this.products.indexOf(product), 1); +// product.remove(); +// } +// } else { +// console.log('error'); +// } +// }) +// } +// } +// +// let cart = new Cart(); +// const list = new ProductsList(cart); +// list.getJson('getProducts.json').then(data => list.handleData(data)); + + +// const products = [ +// { id: 1, title: 'Notebook', price: 2000 }, +// { id: 2, title: 'Keyboard', price: 200 }, +// { id: 3, title: 'Mouse', price: 100 }, +// { id: 4, title: 'Gamepad' }, +// ]; +// +// const renderProduct = (product, img = 'https://placehold.it/100x50') => { +// return `
+// ${product.title} +//
+//

${product.title}

+//

${product.price}

+// +//
+//
` +// }; + +// const renderProduct = (product, img = 'https://placehold.it/100x50') => { +// const { title, price = 10 } = product; +// return `
+// ${title} +//
+//

${title}

+//

${price}

+// +//
+//
` +// }; + +// const renderPage = productsList => { +// // document.querySelector(`.products`).innerHTML = productsList.map(product => renderProduct(product)).join(''); +// const element = document.querySelector(`.products`); +// let strElements = ''; +// for (let product of productsList) { +// element.insertAdjacentHTML('beforeend', renderProduct(product)); +// } +// }; +// +// renderPage(products); \ No newline at end of file From daa2930f4a8c3ca4c3b6097850a7befb2e6912d7 Mon Sep 17 00:00:00 2001 From: archi Date: Fri, 29 Jan 2021 19:11:27 +0300 Subject: [PATCH 2/2] =?UTF-8?q?=D1=80=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit поиск заглушка "корзина пуста" --- index.html | 13 +++++++------ js/main.js | 36 ++++++------------------------------ 2 files changed, 13 insertions(+), 36 deletions(-) diff --git a/index.html b/index.html index 9ff81a9..9d373e3 100644 --- a/index.html +++ b/index.html @@ -13,15 +13,15 @@
-
- + +
- -
-

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

+ +
+

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

@@ -41,7 +41,8 @@

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

-
+

{{ el.product_name }}

diff --git a/js/main.js b/js/main.js index 4cdc2d4..59e3e6f 100644 --- a/js/main.js +++ b/js/main.js @@ -6,8 +6,7 @@ const Shop = { cartUrl: '/getBasket.json', products: [], cartItems: [], - isInvisibleCart: true, - isEmptyCart: true, + showCart: false, imgCatalog: 'https://placehold.it/200x150', imgCart: 'https://placehold.it/50x100', searchMsg: '' @@ -33,7 +32,6 @@ const Shop = { console.log('error'); } }); - this.showCartEmpty(); }, deletProduct(product) { this.getJson(`${this.API}/deleteFromBasket.json`) @@ -48,34 +46,12 @@ const Shop = { console.log('error'); } }); - this.showCartEmpty(); }, - filter() { - const regexp = new RegExp(this.searchMsg, 'i'); - for (let product of this.products) { - if (!regexp.test(product.product_name)) { - product.filtered = true; - } else { - delete product.filtered; - } - } - }, - toggleInvisible() { - this.isInvisibleCart ? this.isInvisibleCart = false : this.isInvisibleCart = true; - this.showCartEmpty(); - }, - showCartEmpty() { - if (this.cartItems != undefined && this.cartItems.length > 0) { - this.isEmptyCart = false; - console.log(this.isEmptyCart); - console.log(this.cartItems.length); - console.log(this.cartItems); - } else { - this.isEmptyCart = true; - console.log(this.isEmptyCart); - console.log(this.cartItems.length); - console.log(this.cartItems); - } + + }, + computed: { + getRegExp() { + return new RegExp(this.searchMsg, 'i'); } }, mounted() {