-
Notifications
You must be signed in to change notification settings - Fork 1
/
deals-sortable.js
172 lines (163 loc) · 7.3 KB
/
deals-sortable.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
// Function to add sorting functionality to the deals list
// We added onClick listeners to elements in the dropdown.
$(function () {
const DealsSortable = (() => {
return {
/****************** Price ******************/
sortByPriceAscending: function () {
var sortedElementsByAsc = $('.shop > .deals').sort(function (a, b) {
const priceClassName = 'div.div-block-16 > div.div-block-7.w-clearfix > div.featured-deals.final-price';
const priceA = $(a).find(priceClassName).html();
const priceB = $(b).find(priceClassName).html();
const priceAFloat = (priceA)
? parseFloat(priceA.substring(2))
: 9999999999; // Phantom Element should always be at the back
const priceBFloat = (priceB)
? parseFloat(priceB.substring(2))
: 9999999999; // Phantom Element should always be at the back
return priceAFloat - priceBFloat;
});
$('.shop').html(sortedElementsByAsc);
DealsSortable.removeAndReaddPhantomElements();
},
sortByPriceDescending: function () {
const sortedElementsByDsc = $('.shop > .deals').sort(function (a, b) {
const priceClassName = 'div.div-block-16 > div.div-block-7.w-clearfix > div.featured-deals.final-price';
const priceA = $(a).find(priceClassName).html();
const priceB = $(b).find(priceClassName).html();
const priceAFloat = (priceA)
? parseFloat(priceA.substring(2))
: -9999999999; // Phantom Element should always be at the back
const priceBFloat = (priceB)
? parseFloat(priceB.substring(2))
: -9999999999; // Phantom Element should always be at the back
return priceBFloat - priceAFloat;
});
$('.shop').html(sortedElementsByDsc);
DealsSortable.removeAndReaddPhantomElements();
},
/****************** Savings ******************/
sortBySavingsAscending: function () {
var sortedElements = $('.shop > .deals').sort(function (a, b) {
const savingsClassName = 'div.savings';
// Grab the timestamp in webflow format
const savingsA = $(a).find(savingsClassName).html();
const savingsB = $(b).find(savingsClassName).html();
const savingsAFloat = (savingsA)
? parseFloat(savingsA.substring(1))
: 9999999999; // Phantom Element should always be at the back
const savingsBFloat = (savingsB)
? parseFloat(savingsB.substring(1))
: 9999999999; // Phantom Element should always be at the back
return savingsAFloat - savingsBFloat;
});
$('.shop').html(sortedElements);
DealsSortable.removeAndReaddPhantomElements();
},
sortBySavingsDescending: function () {
const sortedElementsByDsc = $('.shop > .deals').sort(function (a, b) {
const savingsClassName = 'div.savings';
// Grab the timestamp in webflow format
const savingsA = $(a).find(savingsClassName).html();
const savingsB = $(b).find(savingsClassName).html();
const savingsAFloat = (savingsA)
? parseFloat(savingsA.substring(1))
: 9999999999; // Phantom Element should always be at the back
const savingsBFloat = (savingsB)
? parseFloat(savingsB.substring(1))
: 9999999999; // Phantom Element should always be at the back
return savingsBFloat - savingsAFloat;
});
$('.shop').html(sortedElementsByDsc);
DealsSortable.removeAndReaddPhantomElements();
},
/****************** Time Added ******************/
sortByTimeAddedAscending: function () {
var sortedElementsByAsc = $('.shop > .deals').sort(function (a, b) {
const timestampClassName = 'div.date-of-post';
// Grab the timestamp in webflow format
const timestampA = $(a).find(timestampClassName).html();
const timestampB = $(b).find(timestampClassName).html();
// Handling phantom elements
if (!timestampB) return false;
if (!timestampA) return true;
const timestampAInISO = parseWebflowDateToISO(timestampA);
const unixEpochA = (new Date(timestampAInISO)).getTime();
const timestampBInISO = parseWebflowDateToISO(timestampB);
const unixEpochB = (new Date(timestampBInISO)).getTime();
return unixEpochA - unixEpochB
});
$('.shop').html(sortedElementsByAsc);
DealsSortable.removeAndReaddPhantomElements();
},
sortByTimeAddedDescending: function () {
var sortedElements = $('.shop > .deals').sort(function (a, b) {
const timestampClassName = 'div.date-of-post';
// Grab the timestamp in webflow format
const timestampA = $(a).find(timestampClassName).html();
const timestampB = $(b).find(timestampClassName).html();
// Handling phantom elements
if (!timestampB) return false;
if (!timestampA) return true;
const timestampAInISO = parseWebflowDateToISO(timestampA);
const unixEpochA = (new Date(timestampAInISO)).getTime();
const timestampBInISO = parseWebflowDateToISO(timestampB);
const unixEpochB = (new Date(timestampBInISO)).getTime();
return unixEpochB - unixEpochA;
});
$('.shop').html(sortedElements);
DealsSortable.removeAndReaddPhantomElements();
},
/* Hack-ish function to remove phantom elements and add them again */
removeAndReaddPhantomElements: function () {
const parentContainer = $('.w-dyn-list .w-dyn-items.shop')[0];
// Remove
$('.shop > .deals').each(function (idx, item) {
if ($(item).hasClass('phantom-element')) {
// Phantom element found
$(item).remove();
}
});
// Add
const totalNumberOfDummyElements = 10;
const element = '<div class="phantom-element card-item deals w-dyn-item" style="margin-top: 0; margin-bottom: 0; padding: 0!important; height: 0 !important; opacity: 0;" />';
for (var i = 0; i < totalNumberOfDummyElements; i++) {
$(parentContainer).append(element);
}
},
}
})();
// Set up listeners
const DropdownOptionClickedHandler = (elementId, value) => {
const dropdownTextElement = $($('.w-dropdown > .w-dropdown-toggle > div')[1]);
dropdownTextElement.html(value); // Set up value of dropdown
switch(elementId) {
case 'price-ascending':
DealsSortable.sortByPriceAscending();
break;
case 'price-descending':
DealsSortable.sortByPriceDescending();
break;
case 'savings-ascending':
DealsSortable.sortBySavingsAscending();
break;
case 'savings-descending':
DealsSortable.sortBySavingsDescending();
break;
case 'time-newest':
DealsSortable.sortByTimeAddedDescending();
break;
case 'time-oldest':
DealsSortable.sortByTimeAddedAscending();
break;
}
};
const dropdownOptionsSelector = 'div.w-dropdown > nav > a';
$(dropdownOptionsSelector).each((idx, item) => {
const identifier = item.id;
const value = $(item).html();
$(`#${identifier}`).on("click", () => {
DropdownOptionClickedHandler(identifier, value);
});
});
});