Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Иниациализация фильтров диапазона при загрузке
- $('.af_pf_input').each(function(){
- var af_pf_input = $(this),
- min = af_pf_input.data('min'),
- max = af_pf_input.data('max'),
- after = af_pf_input.data('after'),
- value = af_pf_input.val() != '' ? af_pf_input.val().split(',') : new Array('', '');
- af_pf_input.ionRangeSlider({
- type: "double",
- min: min,
- max: max,
- from: value[0],
- to: value[1],
- hide_min_max: true,
- hide_from_to: true,
- grid: false,
- postfix: "грн.",
- onStart: function(data){ // Срабатывает после инициализации
- af_pf_input.parent().prepend('<div class="af_range_val"><i>'+data.from+' - '+data.to+' '+after+'</i><span class="af_delete"></span></div>');
- if(data.from != min || data.to != max){
- af_pf_input.parent().find('.af_delete').css('display', 'inline-block');
- }
- af_search_active_filters();
- },
- onChange: function(data){ // Срабатывает при изменении
- if(data.from == min && data.to == max){
- af_pf_input.removeAttr('readonly').attr('value', '');
- af_pf_input.parent().find('.af_delete').hide();
- }else{
- af_pf_input.removeAttr('readonly').attr('value', data.from+','+data.to);
- af_pf_input.parent().find('.af_delete').css('display', 'inline-block');
- }
- af_pf_input.parent().find('.af_range_val i').text(data.from+' - '+data.to+' '+after);
- af_search_active_filters();
- },
- onFinish: function(data){ // Срабатыва
- ет при изменении
- change_slider_filter(af_pf_input);
- }
- });
- // Сброс конкретного фильтра диапазона при клике
- af_pf_input.parent().find('.af_delete').click(function(){
- af_pf_input.data("ionRangeSlider").update({from: min, to: max});
- af_pf_input.removeAttr('readonly').attr('value', '');
- af_pf_input.parent().find('.af_range_val i').text(min+' - '+max+' '+after);
- $(this).hide();
- af_search_active_filters();
- var filter = af_pf_input;
- var name = $(filter).attr('name');
- var url = find_right_url();
- $.ajax({
- type: 'POST',
- url: '/mods/a.function.php',
- data: 'aj=102&name='+name+'&url='+url,
- dataType: "json",
- success: function (response) {
- update_apartments_html(response);
- }
- });
- });
- });
- // Открытие/закрытие выпадающего списка первого фильтра
- $(document).on('click mouseenter', '.af_dropdown', function(){
- var parent = $(this).parents('.af_item');
- if(parent.hasClass('active')){
- parent.removeClass('active').find('.af_dropdown_block').slideUp(100);
- return
- }
- parent.addClass('active').find('.af_dropdown_block').slideDown(100);
- });
- $(document).on('mouseleave', '.af_item', function(){
- $(this).removeClass('active').find('.af_dropdown_block').slideUp(100);
- });
- var window_w = $(window).width();
- $(window).resize(function() {
- window_w = $(window).width();
- });
- // Закрытие выпадающего списка первого фильтра если клик не по нему
- $(document).click(function(e){
- if( window_w > 767){
- var af_dropdown_block = $('.af_item_1');
- if (!af_dropdown_block.is(e.target)
- && af_dropdown_block.has(e.target).length === 0
- && $('.af_item_1 .af_dropdown_block').css('display') == 'block'){
- af_dropdown_block.removeClass('active').find('.af_dropdown_block').slideUp(100);
- }
- }
- });
- // Закрытие выпадающего списка второго фильтра если клик не по нему
- $(document).click(function(e){
- if( window_w > 767){
- var af_dropdown_block = $('.af_item_2');
- if (!af_dropdown_block.is(e.target)
- && af_dropdown_block.has(e.target).length === 0
- && $('.af_item_2 .af_dropdown_block').css('display') == 'block'){
- af_dropdown_block.removeClass('active').find('.af_dropdown_block').slideUp(100);
- }
- }
- });
- // Поиск активного параметра первого фильтра при загрузке
- $('.af_dropdown_block .af_dropdown_section a.active').each(function(){
- af_dropdown_set_title($(this));
- });
- // Установка заголовка у первого фильтра при клике
- $('.af_dropdown_block a').click(function(){
- $(this).parents('.af_dropdown_block').find('a').removeClass('active');
- $(this).addClass('active');
- if(af_dropdown_set_title($(this))){
- $(this).parents('.af_item').find('.af_queue span').html($(this).text());
- }
- af_search_active_filters();
- return false;
- });
- //Ф-ция изменения заголовка у первого фильтра
- function af_dropdown_set_title(a_active){
- return true;
- if(a_active.is('.af_all_sections')){return true;}
- var a_parent = a_active.parent(),
- parent_span_text = a_parent.find('span').text();
- num_section = parent_span_text.match(/\d+-[а-яА-ЯёЁ]\s/i);
- num_section = num_section[0].replace(/\s/g, '');
- year = parent_span_text.match(/([а-яА-ЯёЁ]{3}\.\s\d{4})/i);
- year = year[0].substr(0,1).toUpperCase() + year[0].substr(1);
- result = num_section+', '+a_active.text().toLowerCase()+' '+year;
- a_parent.parents('.af_item').find('.af_queue span').html(result);
- $('.af_all_sections').removeClass('active');
- return false;
- }
- // Сброс фильтров при клике
- $('.af_reset_filters, .af_mobile_reset').click(function(){
- if($('.af_item_1 .af_dropdown_block').find('a').not('.af_all_sections').hasClass('active')){
- $('.af_item_1 .af_dropdown_block').find('a').removeClass('active');
- $('.af_item_1 .af_dropdown_block').parent().find('.af_queue span').html($('.af_all_sections').text());
- af_search_active_filters();
- }
- $('.af_item_2').find('input[type="checkbox"]').not('.all_variant').each(function(){
- $(this).prop('checked', false);
- });
- $('.af_item_2').find('.af_queue span').html($('.af_item_2 .all_variant').parent().find('span').text());
- // if($('.af_item').find('.af_delete').css('display') == 'inline-block'){
- $('.af_pf_input').each(function(){
- var min = $(this).data('min'),
- max = $(this).data('max'),
- after = $(this).data('after');
- $(this).data("ionRangeSlider").update({from: min, to: max});
- $(this).removeAttr('readonly').attr('value', '');
- $(this).parent().find('.af_range_val i').text(min+' - '+max+' '+after);
- $(this).parents('.pf_rngstyle_html5').find('.af_delete').hide();
- });
- af_search_active_filters();
- reset_all_filters();
- // }
- });
- //Ф-ция возвращает true когда хотябы один фильтр найден, иначе false
- function af_search_active_filters(){
- var isset_filters = false;
- $('.af_item').each(function(){
- if($(this).hasClass('af_item_1')){
- if($(this).find('a').not('.af_all_sections').hasClass('active')){
- isset_filters = true;
- }
- }else if($(this).hasClass('af_item_2')){
- $(this).find('input[type="checkbox"]').not('.all_variant').each(function(){
- if($(this).prop('checked')){
- isset_filters = true;
- return false;
- }
- });
- }else{
- if($(this).find('.af_delete').css('display') == 'inline-block'){
- isset_filters = true;
- }
- }
- });
- if(isset_filters){
- $('.af_reset_filters, .af_filters_mob').addClass('show');
- }else{
- $('.af_reset_filters, .af_filters_mob').removeClass('show');
- }
- }
- $('.af_mobile_close, .af_filters_mob').click(function(){
- $('.apartments_filter').fadeToggle();
- });
- // Изменеие заголовка второго фильтра при клике
- $('.af_item_2 input[type="checkbox"]').change(function(){
- af_dropdown2_set_title($(this));
- af_search_active_filters();
- });
- // Поиск активных параметров второго фильтра при загрузке
- $('.af_item_2 input[type="checkbox"]').each(function(){
- af_dropdown2_set_title($(this));
- af_search_active_filters();
- });
- //Ф-ция изменения заголовка второго фильтра
- function af_dropdown2_set_title(checkbox){
- if(checkbox.hasClass('all_variant')){ // Если это .all_variant
- if(checkbox.prop('checked')){ // Если .all_variant отмечено
- // Перебираем все чекбоксы кроме .all_variant и сбрасываем
- checkbox.parents('.af_dropdown_block').find('input[type="checkbox"]').not(checkbox).each(function(){
- $(this).prop('checked', false);
- });
- checkbox.parents('.af_item').find('.af_queue span').html(checkbox.parent().find('span').text());
- }
- }else{ // Если это не .all_variant
- // Ищем .all_variant и сбрасываем
- var all_variant = checkbox.parents('.af_dropdown_block').find('.all_variant');
- if(all_variant.prop('checked')) all_variant.prop('checked', false);
- var params_title = new Array();
- checkbox.parents('.af_dropdown_block').find('input:checkbox:checked').not('.all_variant').each(function(){
- params_title.push($(this).parent().find('span').text());
- });
- if(params_title.length == 0){ // Не выбрано параметров
- checkbox.parents('.af_item').find('.af_queue span').html(all_variant.parent().find('span').text());
- }else{
- var rooms = '', befor_rooms = '';
- for (var i=0; i < params_title.length; i++) {
- string = params_title[i].match(/\d+/i);
- if(string !== null){
- rooms+= (rooms == '') ? string : ', '+string;
- }else{
- var title_arr = params_title[i].split(' ');
- for (var j=0; j < title_arr.length; j++) {
- if(j+1 == title_arr.length){
- befor_rooms+= title_arr[j].substr(0, 4)+'.';
- }else{
- befor_rooms+= title_arr[j].substr(0, 4)+'. ';
- }
- }
- befor_rooms+=', ';
- }
- }
- if(rooms != '') rooms+=' комнат';
- var result = befor_rooms+rooms;
- result = result.replace(/,\s$/, '').toLowerCase();
- result = result.charAt(0).toUpperCase() + result.substr(1);
- checkbox.parents('.af_item').find('.af_queue span').html(result);
- }
- }
- };
- // Обновление ссылок в фильтре очередей/секций
- function update_stage_section_url(full_new_url) {
- var new_url = full_new_url.replace(/^\/.*?(\/|$)/, '/')
- .replace(/.*?\/stage-.*?(\/|$)/, '/')
- .replace(/.*?\/section-.*?\//, '/');
- $('a[section]').each(function(index, element){
- element = $(element);
- var base_link = element.attr('base-link');
- element.attr('href', base_link + new_url);
- });
- }
- function update_apartments_html(response) {
- if (window.filterurl === undefined){
- window.filterurl = '/apartmentsearch/'
- }
- if (window.location.pathname.indexOf('/apartmentsearch') != -1){
- $($.find('div#filter_apartments')).html(response.html);
- $($.find('div#filter_pagination')).html(response.pagination);
- window.history.pushState(response.new_url, "", response.new_url);
- $($.find('h2#meta-text span')).html(response.metatext);
- $($.find('span#count1')).html(response.count);
- update_stage_section_url(response.new_url);
- } else {
- window.filterurl = response.new_url;
- }
- }
- //определяем откуда нам брать значения фильтров
- // в случае страницы фильтров это урл
- // в случае остальных это спрятанная переменная window.filterurl mk
- function find_right_url() {
- var url;
- if (window.location.pathname.indexOf('/apartmentsearch') != -1){
- url = window.location.pathname;
- } else {
- url = window.filterurl;
- }
- return url;
- url = find_right_url()
- }
- //обработчик кнопки фильтров для блока на сторонних страницах
- $('button#filter-bottom').on('click',function (event) {
- event.preventDefault();
- if (window.filterurl === undefined){
- window.filterurl = '/apartmentsearch/'
- }
- window.location.href = window.filterurl;
- event.stopPropagation();
- })
- function reset_all_filters() {
- $.ajax({
- type: 'POST',
- url: '/mods/a.function.php',
- data: 'aj=101',
- dataType: "json",
- success: function (response) {
- update_apartments_html(response);
- }
- });
- }
- // Обработчик изменения значений ползунков
- function change_slider_filter(filter){
- var name = $(filter).attr('name');
- var value = $(filter).val();
- var url = find_right_url();
- $.ajax({
- type: 'POST',
- url: '/mods/a.function.php',
- data: 'aj=100&name='+name+'&value='+value+'&url='+url,
- dataType: "json",
- success: function (response) {
- update_apartments_html(response);
- }
- });
- }
- // Обработчик изменения сортировки
- $('select#sort_apartments').on('change', function (event) {
- var url = find_right_url();
- var sort = $(this).val();
- $.ajax({
- type: 'POST',
- url: '/mods/a.function.php',
- data: 'aj=103&url='+url+'&sort='+sort,
- dataType: "json",
- success: function (response) {
- update_apartments_html(response); }
- });
- })
- // Обработчик выбора секции/очереди
- $('a[section]').on('click', function (event) {
- var url = $(this).attr('href');
- var stage = url.match(/\/stage-(.*?)(\/|$)/);
- var section = url.match(/\/section-(.*?)(\/|$)/);
- var sectionParameter = '';
- if ((section !== null) && (section[1] !== undefined)) {
- sectionParameter = '§ion='+section[1];
- }
- $.ajax({
- type: 'POST',
- url: '/mods/a.function.php',
- data: 'aj=104&stage='+stage[1]+sectionParameter+'&new_url='+url,
- dataType: "json",
- success: function (response) {
- update_apartments_html(response); }
- });
- });
- // Обработчик выбора секции/очереди
- $('input[name="count_room[]"]').on('change', function (event) {
- var url = find_right_url();
- var checkedRooms = $('input:checkbox[name="count_room[]"]:checked')
- .map(function() {
- return $(this).val();
- }).get().join(';');
- $.ajax({
- type: 'POST',
- url: '/mods/a.function.php',
- data: 'aj=100&name=number_rooms' + '&value=' + checkedRooms + '&url=' + url,
- dataType: "json",
- success: function (response) {
- update_apartments_html(response);
- }
- });
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement