This repository has been archived by the owner on Nov 19, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
mfilter.js
173 lines (154 loc) · 4.82 KB
/
mfilter.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
$(document).ready(function() {
// Слайдеры фильтра
$('.mFilter_slider').each(function() {
var form = $(this).parents('form');
var idx = $(this).parents('.mFilter_wrapper').data('idx');
var vmin = Number($('#min_'+idx).val());
var vmax = Number($('#max_'+idx).val());
$(this).slider({
min: vmin
,max: vmax
,values: [vmin, vmax]
,range: true
,stop: function(event, ui) {
$('input#min_'+idx).val($(this).slider('values',0));
$('input#max_'+idx).val($(this).slider('values',1));
$(form).find('input[name=page]').val(1);
$(form).submit();
},
slide: function(event, ui){
$('input#min_'+idx).val($(this).slider('values',0));
$('input#max_'+idx).val($(this).slider('values',1));
}
})
})
$(document).on('change', '#mFilter input', function() {
if ($(this).attr('type') == 'checkbox') {
$('#mFilter input[name=page]').val(1);
$(this).parent().find('sup').toggle();
}
$(this).parents('form').submit()
})
$(document).on('click', '#mItems .mLimit', function(e) {
var limit = $(this).data('limit');
$('#mFilter input[name=page]').val(1);
$('#mFilter input[name=limit]').val(limit).trigger('change');
e.preventDefault();
})
$(document).on('click', '#mItems .mSort', function(e) {
var sortby = $(this).data('sort');
$('#mFilter input[name=sort]').val(sortby).trigger('change');
e.preventDefault();
})
$(document).on('click', '#mItems .pagination a', function(e) {
var href = $(this).attr('href').split('=');
$('#mFilter input[name=page]').val(href[href.length - 1]).trigger('change');
e.preventDefault();
})
$(document).on('submit', '#mFilter', function(e) {
$(this).ajaxSubmit({
success: function(res,status,form) {
var data = $.parseJSON(res)
if (data.total) {
if ($('#mFilter_total'))
$('#mFilter_total').text(' ('+data.total+')');
else
$('h1 span').text(' ('+data.total+')');
}
$('#mFilter input[type=checkbox]').each(function() {
var name = $(this).attr('name').replace(/\[\]/, '');
var val = $(this).val();
if (data.filter[name] == undefined) {tmp = 0;}
else {tmp = data.filter[name][val];}
if (tmp != 0) {
$(this).removeAttr('disabled').parent().find('sup').text(tmp);
}
else {
$(this).attr('disabled','disabled').parent().find('sup').text(0);
}
})
$('#mItems').html(data.rows).css('opacity',1)
}
,beforeSubmit: function showRequest(formData, jqForm, options) {
$('#mItems').css('opacity',.5)
var tmp = new Object();
for (var i in formData) {
key = formData[i].name
if (key == 'query' || key == 'action' || key == 'cat_id') {continue;}
if (tmp[key] == undefined) {
tmp[key] = new Array();
}
tmp[key].push(formData[i].value)
}
var tmp2 = new Object();
for (var i in tmp) {
tmp2[i] = tmp[i].join('--')
}
document.location.hash = $.param(tmp2)
return true;
}
})
e.preventDefault();
})
$(document).on('change', '#mFilter .minCost, #mFilter .maxCost', function() {
var fieldset = $(this).parents('fieldset');
var vmin = fieldset.find('.minCost').val();
var vmax = fieldset.find('.maxCost').val();
if (Number(vmin) > Number(vmax)) {
vmin = vmax;
fieldset.find('.minCost').val(vmin);
fieldset.find('.maxCost').val(vmax);
}
fieldset.find('.mFilter_slider').slider("values",0,vmin);
fieldset.find('.mFilter_slider').slider("values",1,vmax);
})
// Если есть хэш в url - пробуем отправить форму
if (vars = getUrlVars()) {
if ($('#mFilter').length == 0) {return;}
for (i in vars) {
if (/\[\]/.test(i)) {
for (i2 in vars[i]) {
el = $('input[name="'+i+'"][value="'+vars[i][i2]+'"]');
if (el.length > 0) {
el.attr('checked','checked').parent().find('sup').hide();
}
else {
var name = i.replace(/\[\]/, '');
if (i2 == 0) {
$('#mfilter_'+name+' .vmin').val(vars[i][0]).parent().find('.mFilter_slider').slider("values",0,vars[i][0]);
}
else {
$('#mfilter_'+name+' .vmax').val(vars[i][1]).parent().find('.mFilter_slider').slider("values",1,vars[i][1])
}
}
}
}
else {
$('input[name='+i+']').val(vars[i]);
}
}
$('#mFilter').submit()
}
else if ($('#mFilter').length) {
$('#mFilter').submit();
}
})
// Разбор хэша url на значения
function getUrlVars() {
var vars = new Object(), hash;
var hashes = decodeURIComponent(window.location.hash.substr(1));
if (hashes.length == 0) {return false;}
else {hashes = hashes.split('&');}
for (var i in hashes) {
hash = hashes[i].split('=');
if (hash[1] != undefined && /\[\]/.test(hash[0])) {
var tmp = hash[1].split('--');
hash[1] = new Array();
for (var i2 in tmp) {
hash[1].push(tmp[i2]);
}
}
vars[hash[0]] = hash[1];
}
return vars;
}