var pickup = { _changeStatus : { type: false, price: false, size: false, ingredeents: false, is_lush: false }, _startPrice : [], /** * Инициализируем ползунок цены */ _initPriceSlider: function () { var _minPrice = parseFloat($('.pizzaPriceMin').val()), _maxPrice = parseFloat($('.pizzaPriceMax').val()), _this = this; $('.pizzaPriceMinBYN').html(priceBYR(_minPrice)); $('.pizzaPriceMaxBYN').html(priceBYR(_maxPrice)); _this._startPrice = [parseFloat($('input[name=price_start]').val()), parseFloat($('input[name=price_end]').val())]; if (_this._startPrice[0] != _minPrice || _this._startPrice[1] != _maxPrice) { _this._changeStatus.price = true; } $('.priceSlider').slider({ range: true, min: _this._startPrice[0], max: _this._startPrice[1], values: [_minPrice, _maxPrice], step: .01, slide: function( event, ui ) { var _currentMin = ui.values[0], _currentMax = ui.values[1]; /* между ценами не больше 5-ти тысяч брб */ /* { if (_currentMax - ui.values[0] <= 5000 && $('.ui-slider-handle:first').is('.ui-state-active')) { _currentMin = _currentMax - 5000; } if (ui.values[1] - _currentMin <= 5000 && $('.ui-slider-handle:last').is('.ui-state-active')) { _currentMax = _currentMin + 5000; } } */ /* диапазон цен */ { if (_currentMin <= _this._startPrice[0]) { _currentMin = _this._startPrice[0]; } if (_currentMax >= _this._startPrice[1]) { _currentMax = _this._startPrice[1]; } } $('.priceSlider').slider('option', 'values' , [_currentMin, _currentMax]); $('.pizzaPriceMin').val(_currentMin); $('.pizzaPriceMax').val(_currentMax); //$('.pizzaPriceMinBYN').html(priceBYR(_currentMin)); //$('.pizzaPriceMaxBYN').html(priceBYR(_currentMax)); return false; }, stop: function( event, ui ) { if (ui.values[0] == ui.values[1]) { _this._changeStatus.price = false; } else { _this._changeStatus.price = true; } _this._updatePickup(); } }); $('input[name=price_from]').blur(function () { _this._initPriceSlider(); _this._updatePickup(); }); $('input[name=price_to]').blur(function () { _this._initPriceSlider(); _this._updatePickup(); }); }, _initCheckbox: function (input, status) { var _this = this; $(input).click(function () { _this._changeStatus[status] = false; $(input).each(function () { if ($(this).is(':checked')) { _this._changeStatus[status] = true; } }); _this._updatePickup(); }); }, /** * Инициализируем checkbox тип пиццы */ _initTypes: function () { this._initCheckbox($('input[name*=type]'), 'type'); }, /** * Инициализируем checkbox тип пиццы */ _initIsLush: function () { this._initCheckbox($('input[name=is_lush]'), 'is_lush'); }, /** * Инициализируем checkbox размер */ _initSize: function () { this._initCheckbox($('input[name*=size]'), 'size'); }, /** * Инициализируем checkbox ингридиенты */ _initIngredeents: function () { this._initCheckbox($('input[name*=ingredeents]'), 'ingredeents'); }, /** * Обновляем окно выборщика */ _updatePickup: function () { var _this = this; var _data = $('#pickup_pizza').serializeArray(); _data.push({name: 'ajax', value: 1}); _data.push({name: 'pickup', value: 1}); _data.push({name: 'action', value: 'AjaxData'}); /* если ползунок цены не трогали игнорируем цену */ if (!_this._changeStatus.price) { for (var i in _data) { if (_data[i].name == 'price_from') { _data.splice(i, 1); } } for (var i in _data) { if (_data[i].name == 'price_to') { _data.splice(i, 1); } } } $('#pickup_pizza').addClass('ajax'); $.get(_link_catalog_pizza, _data, function (data) { console.log(_link_catalog_pizza); _this._updateStatusInputs($('input[name^=type]'), data.type, _this._changeStatus.type); _this._updateStatusInputs($('input[name=is_lush]'), data.is_lush, _this._changeStatus.is_lush); _this._updateStatusInputs($('input[name^=size]'), data.size, _this._changeStatus.size); _this._updateStatusInputs($('input[name^=ingredeents]'), data.ingredeents, _this._changeStatus.ingredeents); _this._updatePriceSlider(data.price, _this._changeStatus.price); $('#count_pizza').html(data.count); if (data.count > 0) { $('#getResultPickup').removeClass('disable'); } else { $('#getResultPickup').addClass('disable'); } $('#pickup_pizza').removeClass('ajax'); // pickup.init(); /* bas */ }, 'json'); }, /** * Обновляем статусы checkbox */ _updateStatusInputs : function (inputs, data, enable) { if (false == enable) { $(inputs).attr('disabled', 'disabled'); $(inputs).parents('label').addClass('disable'); for(var i in data) { $(inputs).each(function () { if ($(this).val() == data[i]) { $(this).removeAttr('disabled'); $(this).parents('label').removeClass('disable'); } }) } } }, /** * Обновляем слайдер цены */ _updatePriceSlider : function (data, enable) { if (false == enable) { $('.pizzaPriceMin').val(data.from); $('.pizzaPriceMax').val(data.to); $('.priceSlider').slider('option', 'values' , [data.from, data.to]); } this._startPrice = [data.min, data.max]; var values = $('.priceSlider').slider('option', 'values'), minVal = (data.min > values[0]) ? data.min : values[0], maxVal = (data.max < values[1]) ? data.max : values[1]; $('.pizzaPriceMin').val(minVal); $('.pizzaPriceMax').val(maxVal); $('.priceSlider').slider('option', 'values' , [minVal, maxVal]); }, _initResetForm: function () { var _this = this; $('#resetForm').click(function() { $('#pickup_pizza :checked').removeAttr('checked'); $('.pizzaPriceMin').val('1000'); $('.pizzaPriceMax').val('999999'); //$('#pickup_pizza').get(0).reset(); _this._changeStatus.price = false _this._updatePickup(); return false; }); }, _initSubmitForm: function () { var _this = this; $('#getResultPickup').click(function() { if (false == $(this).is('.disable')) { $('#pickup_pizza').submit(); } return false; }); }, init : function () { this._initPriceSlider(); this._initTypes(); this._initIsLush(); this._initSize(); this._initIngredeents(); this._initResetForm(); this._initSubmitForm(); this._updatePickup(); } }; $(function () { pickup.init(); });