-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcustom.js
431 lines (402 loc) · 15.5 KB
/
custom.js
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
$(function () {
/* Function
------------------------- */
//* Load Content
function loadContent(pathUrl) {
window.scrollTo(0, 0);
$.ajax({
url: pathUrl,
success: function (data) {
$('#content').html(data);
AOS.init();
}
});
}
//* Check loged in
function checkLoged() {
let result;
$.ajax({
url: './backend/check_logged.php',
async: false,
success: function (data) {
data == 'true' ? result = true : result = false;
}
});
return result;
}
//* Load Content when refresh page
window.addEventListener('load', function(){
let url = new URL(window.location.href);
let path = url.pathname;
let id = path.replace('/', '');
let loadPage = ['', 'about', 'account', `checkout`, 'contact', 'login', 'register', `shop`, 'viewcart', 'detail_product', 'order_view'];
if (!loadPage.includes(id)) {
loadContent('./404.php');
return;
}
if (id == 'detail_product' || id == 'order_view') {
loadContent('/' + id + '.php' + url.search);
return;
} else {
if (checkLoged() && (id == 'login' || id == 'register')) { //đã đăng nhập
id = 'account';
} else if (!checkLoged() && (id == 'account' || id == 'viewcart' || id == 'checkout')) {
id = 'login';
}
}
loadContent('./' + id + '.php');
})
//* Listen back & forward button to load content
window.addEventListener('popstate', function () {
let url = new URL(window.location.href);
let path = url.pathname;
let id = path.replace('/', '');
if (id == '') id = 'home';
loadContent('/' + id + '.php' + url.search);
});
//* choose num page paginator page
function choosePage(id) {
let short_by = $("option:selected", '.nice-select').val();
if (short_by != 'default') {
let total = parseInt($('.shop-top-show').text().trim().replace(/[^0-9\.]+/g, ''));
$.ajax({
type: 'post',
url: './content/short-by-shop.php',
data: {
page: id,
short_by: short_by,
total: total
},
success: function (data) {
$('#product-content').html(data);
AOS.init();
}
});
} else {
$.ajax({
type: "get",
url: './content/filter-shop.php',
data: {
page: id
},
success: function (data) {
$("#shop-content").html(data);
AOS.init();
},
});
}
}
//* Check product exist or not in cart by id
function checkProductExistCart(id) {
let check = $(".cart-product-wrapper .cart-product-inner");
for (let i = 0; i < check.length; i++) {
if (check[i].id == "product_id" + id) return true;
}
return false;
}
/*-------------------------
Ajax Load Data Nagivation
---------------------------*/
// *Popup Notification
function notify(type, icon, position, msg) {
Lobibox.notify(type, {
pauseDelayOnHover: true,
size: 'mini',
rounded: true,
icon: icon,
continueDelayOnInactiveTab: false,
position: position,
msg: msg
});
}
$(document).on('click', '.load-product', function () {
let id = 'detail_product';
let id_product = $(this).attr('id_product');
window.history.pushState(id, id.toUpperCase(), '/detail_product?id=' + id_product);
loadContent('./detail_product.php?id=' + id_product);
});
$(document).on('click', '.load-order', function () {
let id = 'order_view';
let id_order = $(this).attr('id');
window.history.pushState(id, id.toUpperCase(), '/order_view?id=' + id_order);
loadContent('./order_view.php?id=' + id_order);
});
$(document).on('click', '.load-checkout', function () {
let count_cart = parseInt($('#count-cart').text());
if (count_cart > 0) {
let id = 'checkout';
window.history.pushState(id, id.toUpperCase(), '/checkout');
loadContent('./checkout.php');
} else if(count_cart == 0){
notify('info', 'fa-duotone fa-bags-shopping', 'right', 'Bạn chưa có sản phẩm nào trong giỏ hàng');
} else {
notify('warning', 'fa-duotone fa-right-to-bracket', 'bottom', 'Bạn chưa đăng nhập, hãy đăng nhập');
}
});
//* Listen click to load content
$(document).on('click', '.nav-content', function () {
let url = new URL(window.location.href).pathname;
let id = $(this).attr('id');
let path;
if (checkLoged() && (id == 'login' || id == 'register')) { //đã đăng nhập
notify('info', 'fa-duotone fa-info', 'bottom right', 'Bạn đã đăng nhập rồi');
return;
} else if (!checkLoged() && (id == 'account' || id == 'viewcart')) {
notify('warning', 'fa-duotone fa-right-to-bracket', 'bottom right', 'Bạn chưa đăng nhập, hãy đăng nhập');
return;
} else {
if (id == 'home') path = '/';
else path = '/' + id;
}
loadContent('./' + id + '.php');
if (url != path) {
window.history.pushState(id, id.toUpperCase(), path);
}
});
$(document).on('click', '.category-filter', function () {
let category_filter = $(this).attr('id');
let active = $(this).parent();
active.addClass('active').siblings().removeClass('active');
$.ajax({
type: "post",
url: "./content/filter-shop.php",
data: {
category_filter: category_filter,
},
success: function (data) {
$("#shop-content").html(data);
AOS.init();
},
});
});
// SHOP
$(document).on("keypress", "#searchFilterProduct", function (e) {
if (e.which == 13) {
let search_filter = $(this).val();
$.ajax({
type: "post",
url: "./content/filter-shop.php",
data: {
search_filter: search_filter
},
success: function (data) {
$("#shop-content").html(data);
AOS.init();
}
});
}
});
$(document).on("click", ".search-box button", function () {
let search_filter = $(this).siblings('input').val();
$.ajax({
type: "post",
url: "./content/filter-shop.php",
data: {
search_filter: search_filter
},
success: function (data) {
$("#shop-content").html(data);
AOS.init();
}
});
});
$(document).on("change", ".nice-select", function () {
let short_by = $("option:selected", this).val();
let total = parseInt($('.shop-top-show').text().trim().replace(/[^0-9\.]+/g, ''));
let category = $('.sidebar-list li.active a').attr('id');
$.ajax({
type: "post",
url: "./content/short-by-shop.php",
data: {
short_by: short_by,
total: total,
category: category
},
success: function (data) {
$("#product-content").html(data);
AOS.init();
}
});
});
//* search Product by name, listener via Enter
$(document).on("keypress", "#searchProduct", function (e) {
if (e.which == 13) {
let search = $("#searchProduct input").val();
$.ajax({
type: "post",
url: "./shop.php",
data: {
search: search,
},
success: function (data) {
$("#content").html(data);
AOS.init();
},
});
}
});
function getQtyProductCart(id) {
let amount = parseInt(
$("#quantity" + id)
.text()
.replace(/\D/g, "")
);
return amount;
};
function addProduct(id, qty) {
if (checkProductExistCart(id)) {
// có hàng trong giỏ chỉ cần tăng số lượng
let add_qty = getQtyProductCart(id);
let money = parseFloat(
$("#product_id" + id + " .price .new")
.text()
.replace("$", "")
);
let total_qty = add_qty + qty;
let total = parseFloat($("#totalmoney").text().replace("$", ""));
let totalMoney = parseFloat(total + money * qty).toFixed(2);
$("#quantity" + id + " > strong").text(total_qty);
$("#totalmoney").text(totalMoney + "$");
} else {
// không có hàng trong giỏ thì thêm mới
let amount = $("#count-cart").text();
$("#count-cart").text(parseInt(amount) + 1);
let image = $("#img-product" + id).attr("src");
let name = $("#product" + id + " .product-title").text();
if (name == '') name = $(".product-title").text();
let newprice = $("#product" + id + " .price .new").text();
if (newprice == '') newprice = $(".regular-price").text();
let oldprice = $("#product" + id + " .price .old").text();
if (oldprice == '') oldprice = $(".old-price").text();
let total = parseFloat($("#totalmoney").text().replace("$", ""));
let totalMoney = parseFloat(newprice.replace("$", "")) * qty + total;
let html = `<div class="cart-product-inner p-b-20 m-b-20 border-bottom" id="product_id${id}">
<div class="single-cart-product">
<div class="cart-product-thumb">
<a href="./frontend/detail_product.php"><img src="${image}" alt="Cart Product" class="rounded"></a>
</div>
<div class="cart-product-content">
<h3 class="title"><a href="./frontend/detail_product.php">${name}</a></h3>
<div class="product-quty-price">
<span class="cart-quantity" id="quantity${id}">Số lượng: <strong> ${qty} </strong></span>
<span class="price">
`;
if (oldprice != "") {
html += `
<span class="new">${newprice}</span>
<span class="old" style="text-decoration: line-through;color: #DC3545;opacity: 0.5;">${oldprice}</span>
</span>
`;
} else {
html += `<span class='new'>${newprice}</span>
</span>`;
}
html += `
</div>
</div>
</div>
<div class="cart-product-remove">
<a class="remove-cart" id="product${id}">
<i class="fa-duotone fa-trash-can"></i>
</a>
</div>
</div>`;
$(".cart-product-wrapper").prepend(html);
$("#product_id" + id).hide().fadeIn();
$("#totalmoney").text(parseFloat(totalMoney).toFixed(2) + "$");
}
$.ajax({
type: "post",
url: "./backend/add_product_cart.php",
data: {
id: id,
qty: qty,
},
});
}
/*-------------------------
Ajax Shop Page
---------------------------*/
$(document).on("click", ".add-to_cart", function () {
let id = parseInt($(this).attr("id").replace("product", ""));
let qty = parseInt($(".cart-plus-minus-box").val());
addProduct(id, qty);
});
$(document).on('click', 'a#plus_product', function () {
let id = parseInt($(this).parent().attr('id').replace('wrapper', ''));
let qty = 1;
addProduct(id, qty);
});
$(document).keydown('.shop_wrapper grid_4', function (e) {
let next = $('.page-item a[aria-label="Next"]');
let prev = $('.page-item a[aria-label="Prev"]');
switch (e.which) {
case 37: //left arrow key
if (prev.length > 0) {
let id = parseInt($(prev).attr('name').split('page=')[1]);
choosePage(id - 1);
}
break;
case 39: //right arrow key
if (next.length > 0) {
let id = parseInt($(next).attr('name').split('page=')[1]);
choosePage(id + 1);
}
break;
}
})
$(document).on('click', '.page-item a[aria-label="Next"]', function () {
let id = parseInt($(this).attr('name').split('page=')[1]);
choosePage(id + 1);
});
$(document).on('click', '.page-item a[aria-label="Prev"]', function () {
let id = parseInt($(this).attr('name').split('page=')[1]);
choosePage(id - 1);
});
$(document).on('click', '#page-choose', function () {
let id = parseInt($(this).attr('name').split('page=')[1]);
choosePage(id);
});
/*-------------------------
Ajax Cart View
---------------------------*/
$(document).on('click', '#clear-cart', function () {
$('#table-cart').hide('normal', function () {
$(this).remove();
});
$('.cart-product-wrapper').children().hide('normal', function () {
$(this).remove();
});
$('#count-cart').text('0');
$('#totalmoney').text('0.00$');
$('#total-money').text('0.00$');
$('#total-bill').text('0.00$');
$.ajax({
type: 'post',
url: './backend/clear_cart.php',
});
})
/*-------------------------
Ajax Remove Product Cart
---------------------------*/
$(document).on('click', '.remove-cart', function () {
let id = $(this).attr('id').replace('product', '');
$('#product_id' + id).fadeOut('normal', function () {
let amount = parseInt($('#count-cart').text());
$('#count-cart').text(amount - 1);
let money = parseFloat($('#product_id' + id + " .price .new").text().replace('$', ''));
let total = parseFloat($('#totalmoney').text().replace('$', ''));
let qty = parseInt($('#quantity' + id).text().replace(/\D/g, ''));
let totalMoney = (total - money * qty).toFixed(2);
$('#totalmoney').text(totalMoney + '$');
$(this).remove();
});
$.ajax({
type: 'post',
url: './backend/delete_product_cart.php',
data: {
delete_id: id
},
});
});
});