forked from Nefro9/woocommerce_filter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter.js
125 lines (93 loc) · 2.59 KB
/
filter.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
/// Custom Filter - DK
var WooFilter = (function () {
var url = location.protocol + '//' + location.host + location.pathname + '?outlet_filter=1';
var data = {};
// Init - get all taxonomies
$('[taxonomy]').each(function(e) {
var taxonomy = $(this).attr('taxonomy');
var params = getUrlParameter(taxonomy);
if (typeof params !== "undefined" && params.length > 0) {
params = params.split(',');
data[taxonomy] = params;
} else {
data[taxonomy] = [];
}
});
function push() {
generate_url();
window.location.href = url;
}
function generate_url() {
//Add orderby to url
var orderby = getUrlParameter('orderby');
if (typeof orderby !== "undefined" && orderby.length > 0) {
url += '&orderby='+orderby;
}
for (var key in data) {
if(data[key].length > 0){
var group = '&'+key+'=';
for (var subkey in data[key]) {
if (subkey != 0) {
group += ',';
}
group += data[key][subkey];
}
url += group;
}
}
}
function getUrlParameter(sParam) {
var sPageURL = decodeURIComponent(window.location.search.substring(1)),
sURLVariables = sPageURL.split('&'),
sParameterName,
i;
for (i = 0; i < sURLVariables.length; i++) {
sParameterName = sURLVariables[i].split('=');
if (sParameterName[0] === sParam) {
return sParameterName[1] === undefined ? true : sParameterName[1];
}
}
}
function encodeData(data) {
return Object.keys(data).map(function(key) {
return [data[key]];
}).join(",");
}
return {
exist: function(taxonomy, ele) {
if ( $.inArray(ele, data[taxonomy]) >= 0 ) {
return true;
} else {
return false;
}
},
add: function(taxonomy, ele) {
data[taxonomy].push(ele);
push();
},
remove: function(taxonomy, ele) {
for (var key in data[taxonomy]) {
if (data[taxonomy][key] == ele) {
data[taxonomy].splice(key, 1);
}
}
push();
},
debug: function() {
console.log(data);
// push();
}
};
})();
$('li > .filter_param').click(function(e) {
e.preventDefault();
var element = $(this).data('slug');
var taxonomy = $(this).closest('[data-taxonomy]').data('taxonomy');
if ( WooFilter.exist(taxonomy, element) ) {
$(this).removeClass('active');
WooFilter.remove(taxonomy, element);
} else {
$(this).addClass('active');
WooFilter.add(taxonomy, element);
}
});