mirror of
https://github.com/sergeyyarkov/digital-store.git
synced 2026-10-11 10:19:15 +03:00
пагинация теперь стала независимой от других компонентов
This commit is contained in:
parent
a78cd3e7c7
commit
557f7967b3
@ -49,7 +49,7 @@ MongoClient.connect(db_config.url, {useNewUrlParser: true, useUnifiedTopology: t
|
||||
})
|
||||
.catch((error) => {
|
||||
server.get('*', (req, res) => {
|
||||
res.send('Ошибка соединения с БД. Настройте файл database.js');
|
||||
res.send('Ошибка соединения с БД.');
|
||||
});
|
||||
})
|
||||
.finally(() => {
|
||||
|
||||
@ -3,35 +3,20 @@ import { apiService } from "../services/api.service";
|
||||
import { renderItems } from "../templates/items.template";
|
||||
|
||||
export class ItemsComponent extends Component {
|
||||
constructor(id, {loader}) {
|
||||
constructor(id, {pagination}) {
|
||||
super(id);
|
||||
this.loader = loader;
|
||||
this.pagination = pagination;
|
||||
}
|
||||
|
||||
async init() {
|
||||
const html = await this.getItemsByPage(1);
|
||||
this.$table = this.$el.querySelector('table tbody');
|
||||
this.$table.insertAdjacentHTML('afterbegin', html);
|
||||
this.onHide();
|
||||
}
|
||||
|
||||
onHide() {
|
||||
this.loader.hide();
|
||||
}
|
||||
|
||||
onShow() {
|
||||
this.loader.show();
|
||||
this.$table.innerHTML = '';
|
||||
}
|
||||
|
||||
async insertItems(page) {
|
||||
const html = await this.getItemsByPage(page);
|
||||
this.$table.insertAdjacentHTML('afterbegin', html);
|
||||
const items = await apiService.getItems();
|
||||
this.$table = this.$el.querySelector('table tbody');
|
||||
this.pagination.initialize(items.length, this.$table, this.getItemsByPage);
|
||||
}
|
||||
|
||||
async getItemsByPage(page) {
|
||||
const items = await apiService.getItemsOffset(page),
|
||||
categories = await apiService.getCategories();
|
||||
return renderItems(items, categories);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,39 +1,18 @@
|
||||
import { Component } from "../core/component";
|
||||
import { apiService } from "../services/api.service";
|
||||
|
||||
export class PaginationComponent extends Component {
|
||||
constructor(id, {items}) {
|
||||
constructor(id, {loader}) {
|
||||
super(id);
|
||||
this.limit = 5;
|
||||
this.items = items;
|
||||
this.loader = loader;
|
||||
}
|
||||
|
||||
async init() {
|
||||
await this.insertPages();
|
||||
// вставляем кнопки со страницами и назначаем ивент
|
||||
initialize(pages, insertTo, request) {
|
||||
this.insertButtons(pages);
|
||||
this.$pages = this.$el.querySelectorAll('li');
|
||||
this.$el.addEventListener('click', this.buttonHandler.bind(this));
|
||||
this.$el.querySelector('li').nextElementSibling.classList.add('active');
|
||||
}
|
||||
|
||||
buttonHandler(e) {
|
||||
e.preventDefault();
|
||||
if (e.target.dataset.move) {
|
||||
const page = parseInt(e.target.dataset.move),
|
||||
target = e.target.parentNode;
|
||||
|
||||
this.removeActive();
|
||||
target.classList.add('active');
|
||||
this.moveTo(page);
|
||||
} else if (e.target.tagName.toLowerCase() === 'i') {
|
||||
const moveType = e.target.parentNode.dataset.type,
|
||||
page = parseInt(this.$el.dataset.page);
|
||||
|
||||
moveType === 'prev' ? this.movePrev(page, this.$pages) : this.moveNext(page, this.$pages);
|
||||
}
|
||||
}
|
||||
|
||||
updatePage(value) {
|
||||
this.$el.setAttribute('data-page', value);
|
||||
this.$el.onclick = (e) => this.buttonHandler(e, insertTo, request);
|
||||
this.moveTo(1, insertTo, request); // показываем первую страницу
|
||||
this.$el.querySelector('li').nextElementSibling.classList.add('active');
|
||||
}
|
||||
|
||||
removeActive() {
|
||||
@ -44,40 +23,53 @@ export class PaginationComponent extends Component {
|
||||
li.forEach(li => {const page = parseInt(li.querySelector('a').dataset.move);page === value ? li.classList.add('active') : false;});
|
||||
}
|
||||
|
||||
movePrev(value, li) {
|
||||
if (value === 1) {
|
||||
return 0;
|
||||
} else {
|
||||
--value;
|
||||
buttonHandler(e, insertTo, request) {
|
||||
e.preventDefault();
|
||||
if (e.target.dataset.move) {
|
||||
const page = parseInt(e.target.dataset.move),
|
||||
target = e.target.parentNode;
|
||||
|
||||
this.removeActive();
|
||||
this.setActive(li, value);
|
||||
this.updatePage(value);
|
||||
this.itemsInit(value);
|
||||
target.classList.add('active');
|
||||
this.moveTo(page, insertTo, request);
|
||||
} else if (e.target.tagName.toLowerCase() === 'i') {
|
||||
const moveType = e.target.parentNode.dataset.type,
|
||||
page = parseInt(this.$el.dataset.page);
|
||||
|
||||
moveType === 'prev' ? this.movePrev(page, this.$pages, insertTo, request) : this.moveNext(page, this.$pages, insertTo, request);
|
||||
}
|
||||
}
|
||||
|
||||
moveTo(value) {
|
||||
this.updatePage(value);
|
||||
this.itemsInit(value);
|
||||
movePrev(page, li, insertTo, request) {
|
||||
if (page === 1) return 0; else {
|
||||
--page;
|
||||
this.removeActive();
|
||||
this.setActive(li, page);
|
||||
this.moveTo(page, insertTo, request);
|
||||
}
|
||||
}
|
||||
|
||||
moveNext(value, li) {
|
||||
// выполняем запрос на сервер и выводим на данные
|
||||
async moveTo(page, insertTo, request) {
|
||||
this.$el.setAttribute('data-page', page);
|
||||
insertTo.innerHTML = '';
|
||||
this.loader.show();
|
||||
insertTo.insertAdjacentHTML('afterbegin', await request(page));
|
||||
this.loader.hide();
|
||||
}
|
||||
|
||||
moveNext(page, li, insertTo, request) {
|
||||
const length = this.$pages.length - 2;
|
||||
if (value === length) {
|
||||
return 0;
|
||||
} else {
|
||||
++value;
|
||||
if (page === length) return 0; else {
|
||||
++page;
|
||||
this.removeActive();
|
||||
this.setActive(li, value);
|
||||
this.updatePage(value);
|
||||
this.itemsInit(value);
|
||||
this.setActive(li, page);
|
||||
this.moveTo(page, insertTo, request);
|
||||
}
|
||||
}
|
||||
|
||||
async insertPages() {
|
||||
const pages = await apiService.getItems(),
|
||||
count = pages.length / this.limit;
|
||||
|
||||
insertButtons(pages) {
|
||||
const count = Math.ceil(pages / 5);
|
||||
let html = '';
|
||||
|
||||
// назад
|
||||
@ -92,11 +84,4 @@ export class PaginationComponent extends Component {
|
||||
html += '<li><a class="disabled" href="#" data-type="next"><i class="material-icons">chevron_right</i></a></li>';
|
||||
this.$el.insertAdjacentHTML('afterbegin', html);
|
||||
}
|
||||
|
||||
// определяем методы для инициализации наших компонентов
|
||||
itemsInit(value) {
|
||||
this.items.onShow();
|
||||
this.items.insertItems(value);
|
||||
this.items.onHide();
|
||||
}
|
||||
}
|
||||
@ -32,7 +32,7 @@ export class MaterializeConfig {
|
||||
document.querySelector('#type') ? M.textareaAutoResize(document.querySelector('#type')) : false
|
||||
M.updateTextFields();
|
||||
|
||||
// Окраска li по нажатию
|
||||
// окраска li по нажатию
|
||||
if (document.querySelector('#currentPage')) {
|
||||
const currentPage = document.querySelector('#currentPage').dataset.current;
|
||||
const pages = Array.from(document.querySelectorAll('.collapsible li'));
|
||||
|
||||
@ -6,9 +6,9 @@ import { LoaderComponent } from "./components/loader.component.js";
|
||||
import { PaginationComponent } from "./components/pagination.component.js";
|
||||
|
||||
const loader = new LoaderComponent('loader');
|
||||
const pagination = new PaginationComponent('pagination', {loader});
|
||||
const categories = new CategoriesComponent('categories');
|
||||
const items = new ItemsComponent('items', {loader});
|
||||
const pagination = new PaginationComponent('pagination', {items});
|
||||
const items = new ItemsComponent('items', {pagination});
|
||||
const editCategory = new editCategoryModal('editCategory', 'edit', 'editCategoryClose');
|
||||
const materialize = new MaterializeConfig();
|
||||
console.log('Control-Panel v1.0.0');
|
||||
Loading…
x
Reference in New Issue
Block a user