-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path9a-addons-index.html
117 lines (101 loc) · 4.06 KB
/
9a-addons-index.html
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
{layout='layouts/_html-wrapper'}
{partial_search_functions}
<div class="container my-12 mx-auto px-4 md:px-12 w-10 inline-block align-top">
<div id="addon-results" class="flex flex-wrap -mx-1 lg:-mx-4">
</div>
</div>
{exp:channel:entries channel="add_on_store"}
<h2>{title}</h2>
<p>
{exp:favorites:form
entry_id="{entry_id}"
}
<input type="submit" name="submit" value="Add" />
{/exp:favorites:form}
</p>
{/exp:channel:entries}
<script>
//we're going to need the baseURL for several things here
function baseUrl() {
return location.protocol + '//' + location.hostname + (location.port ? ':' + location.port: '');
}
const resultsTarget = document.getElementById("addon-results");
const rp = baseUrl() + '/ajax/addon-results'
//on load create AJAX request
document.addEventListener('DOMContentLoaded', event =>{
//submit the response with fetch()
fetch(baseUrl() + "/ajax/addon-results")
// take our response and get the HTML we really want
.then(response => {
return response.text();
})
// take our HTML and replace the contents of our #addon-results element
.then(data => {
resultsTarget.innerHTML = data;
paginationLinks();
addClicktoFavoritesToggle();
});
});
function ajaxFormSubmit () {
//get the form data
var searchForm = document.getElementById("addon_filter");
var formData = new FormData(searchForm);
//submit the data via AJAX
fetch(searchForm.action, {
method: 'post',
body: formData
})
// take our response and get the HTML we really want
.then(response => {
return response.text();
})
// take our HTML and replace the contents of our #addon-results element
.then(data =>{
resultsTarget.innerHTML = data;
paginationLinks();
})
}
//paginationLinks
//adds event listners to pagination
//must be called AFTER ajax loads
function paginationLinks() {
document.querySelectorAll('.pagination__page').forEach(pageLink => {
pageLink.addEventListener('click', (event) => {
event.preventDefault();
var pageUrl = pageLink.href;
fetch(pageUrl)
.then(response => {
return response.text();
})
.then(data => {
resultsTarget.innerHTML = data;
paginationLinks();
});
})
});
}
//addClicktoFavoritesToggle
//toggles favorited items
//must be called AFTER ajax calls are complete.
function addClicktoFavoritesToggle () {
//find all buttons in our add-on cards
favoriteToggles = document.querySelectorAll('.addon-card button');
favoriteToggles.forEach(favToggle => {
favToggle.addEventListener('click', event => {
var favHeart = favToggle.querySelector('.favorite-toggle');
//We are using Font-awesome style prefixes to switch between the regular style ("far") and the solid style ("fas")
//remove font-awesome classes, then add respective classes
favHeart.classList.remove('fas');
favHeart.classList.remove('far');
//if the heart is already active then switch back to the outline only (regular) style.
if (favHeart.classList.contains('active')) {
favHeart.classList.remove('active');
favHeart.classList.add('far');
}else{
favHeart.classList.add('active');
favHeart.classList.add('fas');
}
});
});
}
</script>