-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathupdate.ts
115 lines (97 loc) · 3.29 KB
/
update.ts
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
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
import useQuantityInput from '@js/components/useQuantityInput';
// @TODO(NeOMakinG): Refactor this file, it comes from facetedsearch or classic
export const parseSearchUrl = function (event: {target: HTMLElement}) {
if (event.target.dataset.searchUrl !== undefined) {
return event.target.dataset.searchUrl;
}
if ($(event.target).parent()[0].dataset.searchUrl === undefined) {
throw new Error('Can not parse search URL');
}
return $(event.target).parent()[0].dataset.searchUrl;
};
export function updateProductListDOM(data: Record<string, never>) {
const {Theme} = window;
$(Theme.selectors.listing.searchFilters).replaceWith(
data.rendered_facets,
);
$(Theme.selectors.listing.activeSearchFilters).replaceWith(
data.rendered_active_filters,
);
$(Theme.selectors.listing.listTop).replaceWith(
data.rendered_products_top,
);
const renderedProducts = $(data.rendered_products);
const productSelectors = $(Theme.selectors.listing.product, renderedProducts);
const firstProductClasses = $(Theme.selectors.listing.product).first().attr('class');
if (productSelectors.length > 0 && firstProductClasses) {
productSelectors.removeClass().addClass(firstProductClasses);
}
$(Theme.selectors.listing.list).replaceWith(renderedProducts);
$(Theme.selectors.listing.listBottom).replaceWith(
data.rendered_products_bottom,
);
if (data.rendered_products_header) {
$(Theme.selectors.listing.listHeader).replaceWith(
data.rendered_products_header,
);
}
}
export default () => {
const {prestashop} = window;
const {Theme} = window;
const {events} = Theme;
$('body').on(
'change',
`${Theme.selectors.listing.searchFilters} input[data-search-url]`,
(event) => {
prestashop.emit(events.updateFacets, parseSearchUrl(event));
},
);
$('body').on(
'click',
Theme.selectors.listing.searchFiltersClearAll,
(event) => {
prestashop.emit(events.updateFacets, parseSearchUrl(event));
},
);
$('body').on('click', Theme.selectors.listing.searchLink, (event) => {
event.preventDefault();
prestashop.emit(
events.updateFacets,
$(event.target)?.closest('a')?.get(0)?.getAttribute('href'),
);
});
/**
* Pager links also scroll up
*/
$('body').on('click', Theme.selectors.listing.pagerLink, (event) => {
event.preventDefault();
document.querySelector(Theme.selectors.listing.listTop)?.scrollIntoView({block: 'start', behavior: 'auto'});
prestashop.emit(
events.updateFacets,
$(event.target)?.closest('a')?.get(0)?.getAttribute('href'),
);
});
if ($(Theme.selectors.listing.list).length) {
window.addEventListener('popstate', (e) => {
const {state} = e;
window.location.href = state && state.current_url ? state.current_url : history;
});
}
$('body').on(
'change',
`${Theme.selectors.listing.searchFilters} select`,
(event) => {
const form = $(event.target).closest('form');
prestashop.emit(events.updateFacets, `?${form.serialize()}`);
},
);
prestashop.on(events.updateProductList, (data: Record<string, never>) => {
updateProductListDOM(data);
useQuantityInput();
});
};