mirror of
https://github.com/sergeyyarkov/digital-store.git
synced 2026-10-11 10:19:15 +03:00
Breadcrumb component
This commit is contained in:
parent
bfb1d32df0
commit
7cd582f168
18
dist/public/js/bundle.js
vendored
18
dist/public/js/bundle.js
vendored
File diff suppressed because one or more lines are too long
2
dist/views/product.pug
vendored
2
dist/views/product.pug
vendored
@ -17,7 +17,7 @@ main.main-section
|
|||||||
li
|
li
|
||||||
a(href='/') Главная
|
a(href='/') Главная
|
||||||
li
|
li
|
||||||
a(href='/') #{category}
|
a#breadcrumbBtn(href='/' data-category=category) #{category}
|
||||||
li #{title}
|
li #{title}
|
||||||
hr
|
hr
|
||||||
.product-title.block-title
|
.product-title.block-title
|
||||||
|
|||||||
22
src/js/components/breadcrumb.component.js
Normal file
22
src/js/components/breadcrumb.component.js
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
import { Component } from "../core/component";
|
||||||
|
|
||||||
|
export class BreadcrumbComponent extends Component {
|
||||||
|
constructor(id) {
|
||||||
|
super(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
init() {
|
||||||
|
if(this.$el) {
|
||||||
|
const btn = this.$el.querySelector('#breadcrumbBtn');
|
||||||
|
btn.addEventListener('click', buttonHandler);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function buttonHandler(e) {
|
||||||
|
e.preventDefault();
|
||||||
|
if (e.target.dataset.category) {
|
||||||
|
localStorage.setItem('category', e.target.dataset.category.toLowerCase());
|
||||||
|
window.location.href = '/';
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -12,6 +12,22 @@ export class CategoryComponent extends Component {
|
|||||||
init() {
|
init() {
|
||||||
if (this.$el) {
|
if (this.$el) {
|
||||||
this.$el.addEventListener('click', buttonHandler.bind(this));
|
this.$el.addEventListener('click', buttonHandler.bind(this));
|
||||||
|
|
||||||
|
if (localStorage.getItem('category')) {
|
||||||
|
const category = localStorage.getItem('category');
|
||||||
|
const bounded = breadcrumbActiveLink.bind(this);
|
||||||
|
document.querySelector('#loader').classList.add('hide');
|
||||||
|
bounded(category);
|
||||||
|
// отображем данные если был нажат breadcrumb
|
||||||
|
(async function(){
|
||||||
|
const fData = await apiService.getItemsOne(category);
|
||||||
|
const html = renderItemsOne(category, fData);
|
||||||
|
document.querySelector('#roster').insertAdjacentHTML('afterbegin', html);
|
||||||
|
}())
|
||||||
|
|
||||||
|
// очищаем ключ category чтобы данные не показывались всегда
|
||||||
|
localStorage.removeItem('category');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -20,7 +36,7 @@ async function buttonHandler(e) {
|
|||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
if (e.target.dataset.category && e.target.dataset.category != 'all') {
|
if (e.target.dataset.category && e.target.dataset.category != 'all') {
|
||||||
this.items.onShow();
|
this.items.onShow();
|
||||||
activeLink(e);
|
btnActiveLink(e);
|
||||||
|
|
||||||
const category = e.target.dataset.category.toLowerCase();
|
const category = e.target.dataset.category.toLowerCase();
|
||||||
const fData = await apiService.getItemsOne(category);
|
const fData = await apiService.getItemsOne(category);
|
||||||
@ -30,7 +46,7 @@ async function buttonHandler(e) {
|
|||||||
document.querySelector('#roster').insertAdjacentHTML('afterbegin', html);
|
document.querySelector('#roster').insertAdjacentHTML('afterbegin', html);
|
||||||
} else if (e.target.dataset.category === 'all') {
|
} else if (e.target.dataset.category === 'all') {
|
||||||
this.items.onShow();
|
this.items.onShow();
|
||||||
activeLink(e);
|
btnActiveLink(e);
|
||||||
const fData = await apiService.getItems(),
|
const fData = await apiService.getItems(),
|
||||||
categories = Object.keys(fData);
|
categories = Object.keys(fData);
|
||||||
|
|
||||||
@ -40,7 +56,17 @@ async function buttonHandler(e) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function activeLink(e) {
|
function btnActiveLink(e) {
|
||||||
Array.from(e.target.parentNode.parentNode.querySelectorAll('li a')).forEach(li => li.classList.remove('active'));
|
Array.from(e.target.parentNode.parentNode.querySelectorAll('li a')).forEach(li => li.classList.remove('active'));
|
||||||
e.target.classList.add('active');
|
e.target.classList.add('active');
|
||||||
|
}
|
||||||
|
|
||||||
|
function breadcrumbActiveLink(category) {
|
||||||
|
const links = this.$el.querySelectorAll('.category-items ul li a');
|
||||||
|
Array.from(links).forEach(li => li.classList.remove('active'));
|
||||||
|
Array.from(links).forEach(li => {
|
||||||
|
if (li.dataset.category.toLowerCase() === category) {
|
||||||
|
li.classList.add('active');
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
@ -11,17 +11,19 @@ export class ItemsComponent extends Component {
|
|||||||
async init() {
|
async init() {
|
||||||
|
|
||||||
if (this.$el) {
|
if (this.$el) {
|
||||||
const fData = await apiService.getItems(),
|
if (!localStorage.getItem('category')) {
|
||||||
|
const fData = await apiService.getItems(),
|
||||||
categories = Object.keys(fData);
|
categories = Object.keys(fData);
|
||||||
|
|
||||||
if (categories.length) {
|
if (categories.length) {
|
||||||
const html = renderItems(categories, fData);
|
const html = renderItems(categories, fData);
|
||||||
this.$el.querySelector('#roster').insertAdjacentHTML('afterbegin', html);
|
this.$el.querySelector('#roster').insertAdjacentHTML('afterbegin', html);
|
||||||
} else {
|
} else {
|
||||||
this.$el.querySelector('#roster').innerHTML = 'Товаров на сайте пока что нету';
|
this.$el.querySelector('#roster').innerHTML = 'Товаров на сайте пока что нету';
|
||||||
}
|
}
|
||||||
this.loader.hide();
|
this.loader.hide();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onHide() {
|
onHide() {
|
||||||
|
|||||||
@ -4,10 +4,12 @@ import { CartComponent } from "./components/cart.component.js";
|
|||||||
import { LoaderComponent } from "./components/loader.component.js";
|
import { LoaderComponent } from "./components/loader.component.js";
|
||||||
import { ItemsComponent } from "./components/items.component.js";
|
import { ItemsComponent } from "./components/items.component.js";
|
||||||
import { CategoryComponent } from "./components/category.component.js";
|
import { CategoryComponent } from "./components/category.component.js";
|
||||||
|
import { BreadcrumbComponent } from "./components/breadcrumb.component.js";
|
||||||
|
|
||||||
const header = new HeaderComponent('header');
|
const header = new HeaderComponent('header');
|
||||||
const loader = new LoaderComponent('loader');
|
const loader = new LoaderComponent('loader');
|
||||||
const information = new InformationComponent('information');
|
const information = new InformationComponent('information');
|
||||||
const cart = new CartComponent('cart');
|
const cart = new CartComponent('cart');
|
||||||
const items = new ItemsComponent('items', {loader});
|
const items = new ItemsComponent('items', {loader});
|
||||||
const category = new CategoryComponent('category', {items});
|
const category = new CategoryComponent('category', {items});
|
||||||
|
const breadcumb = new BreadcrumbComponent('breadcrumb');
|
||||||
Loading…
x
Reference in New Issue
Block a user