var fasoon = fasoon || {}; (function (ns, $) { "use strict"; ns.QuotePriceStep1 = function () { this.construct.apply(this, arguments); }; var proto = ns.QuotePriceStep1.prototype; proto.construct = function (options) { this.options = $.extend({ form_id: undefined, entry_type_id: undefined, backing_type_id: undefined, currency: 'CHF', partner_class: undefined, summary_title_class: undefined, summary_price_class: undefined, summary_partners_class: undefined, summary_partner_tpl_class: undefined, summary_total_class: undefined, errors: {} }, options); this.form = $('#' + this.options.form_id); if (!this.form.length) { alert('Form #' + this.options.form_id + ' is not found.'); return; } this.entry_type = $('[id^=' + this.options.entry_type_id + ']', this.form); if (!this.entry_type.length) { alert('Element #' + this.options.entry_type_id + ' is not found.'); return; } this.backing_type = $('#' + this.options.backing_type_id, this.form); if (!this.backing_type.length) { alert('Element #' + this.options.backing_type_id + ' is not found.'); return; } this.partners = undefined; this.summary_title = $('.' + this.options.summary_title_class, this.form); if (!this.summary_title.length) { alert('Element .' + this.options.summary_title_class + ' is not found.'); return; } this.summary_price = $('.' + this.options.summary_price_class, this.form); if (!this.summary_price.length) { alert('Element .' + this.options.summary_price_class + ' is not found.'); return; } this.summary_partner_tpl = $('.' + this.options.summary_partner_tpl_class, this.form)[0].outerHTML; if (!this.summary_partner_tpl) { alert('Summary partner template is not found.'); return; } $('.' + this.options.summary_partner_tpl_class, this.form).remove(); this.summary_partners = $('.' + this.options.summary_partners_class, this.form); if (!this.summary_partners.length) { alert('Element .' + this.options.summary_partners_class + ' is not found.'); return; } if (!$('ul>li', this.summary_partners).length) { this.summary_partners.hide(); } this.summary_total = $('.' + this.options.summary_total_class, this.form); if (!this.summary_total.length) { alert('Element .' + this.options.summary_total_class + ' is not found.'); return; } this.initPartners(); this.entry_type.on('click', $.proxy(this.onEntryTypeChange, this)); this.onEntryTypeChange(); }; proto.onEntryTypeChange = function () { var option = this.entry_type.filter(':checked'); if (!option.length) { option = this.entry_type.filter(':first'); option.prop('checked', true); } var backing_type = option.data('backing_type'); var title = option.data('title'); if (backing_type) { title += ' ' + option.data('glue') + ' ' + option.data('backing_title') + ''; } var price = option.data('price'); price = this.options.currency + ' ' + this.formatPrice(price); this.summary_title.html(title); this.summary_price.html(price); this.backing_type.val(backing_type); this.updateSummary(); }; proto.initPartners = function () { this.partners = $('.' + this.options.partner_class, this.form); if (this.partners.length) { this.partners.each($.proxy(function (i, el) { $('.add_btn', el).on('click', $.proxy(this.onPartnerAdd, this)); }, this)); } $('ul>li', this.summary_partners).each($.proxy(function (i, el) { var li = $(el); li.find('.remove_btn').on('click', $.proxy(this.onPartnerRemove, this)); }, this)); this.swiper = new Swiper('.swiper-container', { init: false, autoHeight: true, spaceBetween: 30, slidesPerView: 2, loop: false, // якщо true, то проблема з висотою і івентами бо слайди дублюються свіпером breakpoints: { 860: { // when window width is <= 640px slidesPerView: 1, spaceBetween: 30 } }, navigation: { nextEl: '.swiper-button-next', prevEl: '.swiper-button-prev' }, pagination: { el: '.swiper-pagination', type: 'bullets', clickable: true }, // on: { // init: $.proxy(this.alignSlideHeight, this), // resize: $.proxy(this.alignSlideHeight, this), // transitionStart: $.proxy(this.alignSlideHeight, this) // } }); this.swiper.init(); }; proto.alignSlideHeight = function() { if (this.partners.length) { var partner_max_height = 0; this.partners.each(function (i, el) { var height = $(el).height(); if (height > partner_max_height) { partner_max_height = height; } }); this.partners.each(function (i, el) { $(el).height(partner_max_height); }); $(this.partners[0]).closest('.swiper-wrapper').height(partner_max_height); this.swiper.update(); } }; proto.getEntryTypePrice = function () { return parseFloat(this.entry_type.filter(':checked').data('price')); }; proto.onPartnerAdd = function (ev) { var target = $(ev.target); var partner_id = target.data('id'); var partner_title = target.data('title'); var li = $(this.summary_partner_tpl).clone(); li.removeClass(this.options.summary_partner_tpl_class); li.find('input[type=hidden]').val(partner_id); li.find('.remove_btn').on('click', $.proxy(this.onPartnerRemove, this)); li.find('.title').html(partner_title); li.data('id', partner_id); $('ul', this.summary_partners).append(li); this.summary_partners.show(); target.closest('.' + this.options.partner_class).hide(); this.swiper.update(); this.updateSummary(); }; proto.onPartnerRemove = function (ev) { var li = $(ev.target).closest('li'); var partner_id = $('input[type=hidden]', li).val(); li.remove(); $('.' + this.options.partner_class + ' .add_btn[data-id=' + partner_id + ']').closest('.' + this.options.partner_class).show(); this.swiper.update(); if (!$('ul>li', this.summary_partners).length) { this.summary_partners.hide(); } this.updateSummary(); }; proto.formatPrice = function (price) { var price_f = parseFloat(price); var price_i = parseInt(price); if (price_f == price_i) { price = '' + price_i + '.-'; } else { price = '' + price_f.toFixed(2); } return price; }; proto.updateSummary = function () { var price = this.getEntryTypePrice(); var total = price; $('ul>li', this.summary_partners).each($.proxy(function (i, el) { var li = $(el); var partner_discount = parseFloat($('.' + this.options.partner_class + ' .add_btn').data('discount')); var discount = price * partner_discount / 100; if (discount > total) { discount = total; } li.find('.discount').html('- ' + this.options.currency + ' ' + this.formatPrice(discount)); total -= discount; if (total < 0) { total = 0; } }, this)); this.summary_total.text(this.options.currency + ' ' + this.formatPrice(total)); this.updatePartners(total); }; proto.updatePartners = function (total) { this.partners.each(function () { var partner = $(this); if (!partner.hasClass('hidden')) { if (total > 0) { $('.discount-special-offer', partner).addClass('hidden'); $('.discount-percent', partner).removeClass('hidden'); } else { $('.discount-percent', partner).addClass('hidden'); $('.discount-special-offer', partner).removeClass('hidden'); } } }); }; proto.escapeHtml = function (string) { var entityMap = { "&": "&", "<": "<", ">": ">", '"': '"', "'": ''', "/": '/' }; return String(string).replace(/[&<>"'\/]/g, function (s) { return entityMap[s]; }); }; })(fasoon, jQuery);