-
Notifications
You must be signed in to change notification settings - Fork 5
/
category.es6
186 lines (165 loc) · 5.2 KB
/
category.es6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
(() => {
const DOM = {
$h1: $('.category-title'),
$productsOnPage: $('.js-products-showed-count'),
$productsList: $('#products-wrapper'),
$viewType: $('#category-right'),
$loadMoreBtn: $('#btn-load-products'),
addToCart: '.js-product-to-cart',
totalProducts: '.js-total-products',
tileView: {
$: $('.js-icon-mode-tile'),
mode: 'tile',
},
listView: {
$: $('.js-icon-mode-list'),
mode: 'list',
},
$sorting: $('.selectpicker'),
$pagination: $('.js-catalog-pagination'),
};
const init = () => {
setUpListeners();
updateLoadMoreBtnState();
hidePaginationButtons();
};
/**
* Subscribing on events using mediator.
*/
function setUpListeners() {
mediator.subscribe('onViewTypeChange', updateViewType, server.sendViewType);
mediator.subscribe(
'onProductsLoad',
updateProductsList,
updateLoadedCount,
updateLoadMoreBtnState,
configs.initTouchspin,
);
DOM.tileView.$.click(() => mediator.publish('onViewTypeChange', DOM.tileView.mode));
DOM.listView.$.click(() => mediator.publish('onViewTypeChange', DOM.listView.mode));
DOM.$loadMoreBtn.click(loadProducts);
DOM.$sorting.change(reloadPageWithSorting);
$(document).on('click', DOM.addToCart, buyProduct);
}
const getSelectedSortingOption = () => DOM.$sorting.find(':selected');
/**
* Get number of already loaded products
*
* @returns {int} - number of products which are presented in DOM
*/
const getProductsOnPageCount = () => parseInt(DOM.$productsOnPage.first().text(), 10);
/**
* Hide catalog's pagination buttons.
* Buttons should be visible only for web search engines.
*/
function hidePaginationButtons() {
DOM.$pagination.hide();
}
/**
* Get number of already loaded products
*
* @param {string} products
* @returns {number}
*/
function getLoadedProductsCount(products) {
return (products.match(new RegExp('js-product-to-cart', 'g')) || []).length;
}
/**
* Number of Products remain un-loaded from server.
* Formula:
* productsLeft = total products on server - already loaded products on front
*
* @returns {Number} - number of products left to load
*/
function productsLeft() {
const totalProductsCount = parseInt($(DOM.totalProducts).first().text(), 10);
return totalProductsCount - getProductsOnPageCount();
}
function reloadPageWithSorting() {
const tags = helpers.getUrlEndpointParam('tags');
const selectedSorting = getSelectedSortingOption().attr('data-path').trim();
if (tags === '') {
window.location.href = selectedSorting;
} else {
window.location.href = `${selectedSorting}tags/${tags}/`;
}
}
/**
* Append loaded Products from server in DOM.
*
* @param {string} products
*/
function updateProductsList(_, products) {
DOM.$productsList.append(products);
}
/**
* Update loaded Products counter.
*/
function updateLoadedCount(_, products) {
// Aggregate `js-products-showed-count` by `getLoadedProductsCount()`.
DOM.$productsOnPage.text(getProductsOnPageCount() + getLoadedProductsCount(products));
}
/**
* Add 'hidden' class to button if there are no more Products to load.
*/
function updateLoadMoreBtnState() {
if (productsLeft() !== 0) return;
DOM.$loadMoreBtn.addClass('hidden');
}
/**
* Update Product list view.
* Toggles classes depends on what view type was selected.
* @param {string} viewType: list|tile
*/
function updateViewType(_, viewType) {
DOM.$viewType
.removeClass('view-mode-tile view-mode-list')
.addClass(`view-mode-${viewType}`);
if (viewType === DOM.listView.mode) {
DOM.listView.$.addClass('active');
DOM.tileView.$.removeClass('active');
} else {
DOM.tileView.$.addClass('active');
DOM.listView.$.removeClass('active');
}
}
/**
* Load Products from server.
* Publish 'onProductLoad' event on success.
*/
function loadProducts() {
const path = DOM.$loadMoreBtn.data('url');
const offset = getProductsOnPageCount();
const sorting = getSelectedSortingOption().val();
const filterParams = helpers.getUrlEndpointParam('tags');
let url = `${path}load-more/${offset}/${sorting}/`;
if (filterParams) url += `tags/${filterParams}/`;
server.loadProducts(url)
.then(products => mediator.publish('onProductsLoad', products));
}
/**
* Send Product's id and quantity on server.
* Publish 'onCartUpdate' event on success.
*/
function buyProduct(event) {
const getProductData = () => {
const $product = $(event.target);
const quantity = $product.closest('.js-order').find('.js-product-count').val();
return {
id: parseInt($product.attr('productId'), 10),
name: $product.attr('productName'),
quantity: parseInt(quantity, 10),
category: DOM.$h1.data('name'),
brand: $product.attr('productBrand'),
};
};
const data = getProductData();
const { id, quantity } = data;
server.addToCart(id, quantity)
.then((newData) => {
mediator.publish('onCartUpdate', newData);
mediator.publish('onProductAdd', [data]);
});
}
init();
})();