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(true, { form_id: undefined, entry_type_id: undefined, backing_type_id: undefined, gtm_data: {}, 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, sidebar_id: 'sidebar', 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 = $('#' + 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.tooltip_obj = 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_selected_partners = this.summary_partners.find('.partner-intro'); if (!this.summary_selected_partners.length) { alert('Element .partner-intro is not found.'); return; } 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.initTooltipster(); this.entry_type.on('change', $.proxy(this.onEntryTypeChange, this)); this.onEntryTypeChange(); this.sidebar = $('#' + this.options.sidebar_id); this.sidebarOffsetTop = this.sidebar.offset().top; this.window = $(window); this.window.on('scroll', _.throttle(_.bind(this.onWindowScroll, this), 100)); this.onWindowScroll(); this.window.on('resize', _.throttle(_.bind(this.onWindowResize, this), 100)); fasoon_gtm.company_foundation(1, "Initiated", this.collectGtmData()); this.form.on('submit', $.proxy(this.onSubmit, this)); }; proto.collectGtmData = function () { var data = this.options.gtm_data; var $legal_form = this.entry_type.find('option:selected'); data.legal_form = $legal_form.data('entry_type_gtm_title'); data.backing_type = $legal_form.data('backing_type_gtm_title'); data.items = []; $('ul>li', this.summary_partners).each($.proxy(function (i, el) { var $li = $(el); data.items.push({ "item_name": $li.data('apiname'), "currency" : this.options.currency, "item_category": "Partner", "discount": this.parsePrice($li.find('.discount').data('discount')), }); }, this)); data.price = this.parsePrice($legal_form.data('price')); data.price_total = this.parsePrice(this.summary_total.text()); return data; }; proto.onSubmit = function () { var data = this.collectGtmData(); data.click_name = 'Set up'; fasoon_gtm.company_foundation(1, "Complete", data); return true; }; proto.onWindowScroll = function () { var wt = this.window.scrollTop(); if (wt >= this.sidebarOffsetTop) { if (!this.sidebar.hasClass('free-sidebar')) { this.sidebar.addClass('free-sidebar'); } } else { if (this.sidebar.hasClass('free-sidebar')) { this.sidebar.removeClass('free-sidebar'); } } }; proto.onWindowResize = function() { var contentArea = $('#content-area').first(); if (this.tooltip_obj) { var instances = $.tooltipster.instances(); $.each(instances, function(i, instance){ instance.close(function(){ instance.destroy(); }); }); setTimeout($.proxy(this.initTooltipster(), this), 500); } }; proto.onEntryTypeChange = function () { var option = $('option:selected', this.entry_type); 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) { // partner block $('.add_btn', el).on('click', $.proxy(this.onPartnerAdd, this)); $('.remove_btn', el).on('click', $.proxy(this.onPartnerRemove, this)); $('.partner-info-btn', el).on('click', $.proxy(this.onPartnerInfo, 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.summary_selected_partners.on('click', $.proxy(this.toggleSelectedPartners, this)); }; proto.toggleSelectedPartners = function () { if (this.summary_selected_partners.hasClass('open')) { this.summary_selected_partners.removeClass('open'); this.summary_selected_partners.addClass('close'); this.summary_partners.find('.summary-partners-list').slideUp('fast'); } else { this.summary_selected_partners.removeClass('close'); this.summary_selected_partners.addClass('open'); this.summary_partners.find('.summary-partners-list').slideDown('fast'); } }; proto.initTooltipster = function() { var pageContainer = $('#page-container').first(); var contentArea = $('#content-area').first(); this.partners.find('.tooltip').tooltipster({ side: ['bottom'], contentCloning: true, interactive: true, trigger: 'click', maxWidth: contentArea.width(), trackOrigin: true, trackTooltip: true, functionReady: $.proxy(function (instance, helper) { var pageContainerPaddingBottom = $(helper.tooltip).height() - ( pageContainer.height() - $(helper.tooltip).offset().top ); pageContainer.css('padding-bottom', pageContainerPaddingBottom + 'px'); var partner = $(helper.origin).closest('div.' + this.options.partner_class); partner.addClass('active'); this.tooltip_obj = $(helper.tooltip); var add_btn = this.tooltip_obj.find('.add_btn'); var remove_btn = this.tooltip_obj.find('.remove_btn'); if (partner.find('.add_btn').hasClass('hidden')) { add_btn.addClass('hidden'); } else { add_btn.removeClass('hidden'); } if (partner.find('.remove_btn').hasClass('hidden')) { remove_btn.addClass('hidden'); } else { remove_btn.removeClass('hidden'); } add_btn.off('click'); add_btn.on('click', $.proxy(this.onPartnerAdd, this)); remove_btn.off('click'); remove_btn.on('click', $.proxy(this.onPartnerRemove, this)); }, this), functionAfter: $.proxy(function (instance, helper) { $(helper.origin).closest('div.' + this.options.partner_class + '.active').removeClass('active'); this.tooltip_obj = undefined; pageContainer.css('padding-bottom', '0'); }, this), functionPosition: function(instance, helper, position){ position.coord.top += 0; position.coord.left = contentArea.offset().left; $(helper.tooltip).css('max-width', contentArea.width() + 'px'); return position; } }); }; proto.getEntryTypePrice = function () { return parseFloat($('option:selected', this.entry_type).data('price')); }; proto.getEntryTypeId = function () { return $('option:selected', this.entry_type).val(); }; proto.getBackingTypeId = function () { return $('option:selected', this.entry_type).data('backing_type'); }; proto.partnerGtmEvent = function (action, partner) { fasoon_gtm.push({ "event": "company_foundation", "step_number": "1", "step_progress": "In Progress", "item_category": "Partner", "item_action": action, "item_name": partner.title, "item_currency": this.options.currency, "item_discount": partner.discount, }); }; proto.onPartnerAdd = function (ev) { var target = $(ev.currentTarget); var partner_id = target.data('id'); var partner_title = target.data('title'); var api_name = target.data('apiname'); var partner_discounts = target.data('discounts'); var hide_discount = target.data('hide-discount'); 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('.remove_btn').attr('data-id', partner_id); li.find('.title').html(partner_title); li.attr('data-id', partner_id); li.attr('data-discounts', JSON.stringify(partner_discounts)); li.attr('data-hide-discount', hide_discount); li.attr('data-apiname', api_name); $('ul', this.summary_partners).append(li); this.summary_partners.show(); this.partners.find('.add_btn[data-id=' + partner_id + ']').addClass('hidden'); this.partners.find('.remove_btn[data-id=' + partner_id + ']').removeClass('hidden'); if (this.tooltip_obj) { this.tooltip_obj.find('.add_btn').addClass('hidden'); this.tooltip_obj.find('.remove_btn').removeClass('hidden'); } this.updateSummary(); this.partnerGtmEvent('Add', { title: api_name, discount: this.summary_partners.find('li[data-id=' + partner_id + ']').find('.discount').data('discount'), }); }; proto.onPartnerRemove = function (ev) { var partner_id = $(ev.currentTarget).data('id'); var li = $('li[data-id=' + partner_id + ']', this.summary_partners); this.partnerGtmEvent('Remove', { title: li.data('apiname'), discount: li.find('.discount').data('discount'), }); li.remove(); this.partners.find('.remove_btn[data-id=' + partner_id + ']').addClass('hidden'); this.partners.find('.add_btn[data-id=' + partner_id + ']').removeClass('hidden'); if (this.tooltip_obj) { this.tooltip_obj.find('.remove_btn').addClass('hidden'); this.tooltip_obj.find('.add_btn').removeClass('hidden'); } if (!$('ul>li', this.summary_partners).length) { this.summary_partners.hide(); } this.updateSummary(); }; proto.onPartnerInfo = function (ev) { var container = $(ev.currentTarget).closest('.fasoon_form1_partners'); this.partnerGtmEvent('Info', { title: container.find('.add_btn').data('title'), discount: this.parsePrice(container.find('.discount-percent span').text()), }); }; 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 entity_id = this.getEntryTypeId(); var backing_id = this.getBackingTypeId(); var total = price; var partners_discount = 0; $('ul>li', this.summary_partners).each($.proxy(function (i, el) { var li = $(el); var partner_discount = 0.0; var discounts = li.data('discounts'); var hide_discount = li.data('hide-discount'); if (hide_discount) { li.find('.discount').addClass('hidden'); } if (discounts) { if (backing_id) { if (discounts.hasOwnProperty(entity_id) && discounts[entity_id].hasOwnProperty(backing_id)) { partner_discount = discounts[entity_id][backing_id]; } } else { if (discounts.hasOwnProperty(entity_id)) { partner_discount = discounts[entity_id]; } } } var discount = price * partner_discount / 100; if (discount > total) { discount = total; } li.find('.discount').attr('data-discount', discount); li.find('.discount').html('- ' + this.options.currency + ' ' + this.formatPrice(discount)); total -= discount; if (total < 0) { total = 0; } partners_discount += discount; }, this)); this.summary_selected_partners.find('.discount').text(this.options.currency + ' ' + this.formatPrice(partners_discount)); this.summary_total.text(this.options.currency + ' ' + this.formatPrice(total)); this.updatePartners(total); }; proto.updatePartners = function (total) { var price = this.getEntryTypePrice(); var entity_id = this.getEntryTypeId(); var backing_id = this.getBackingTypeId(); var obj = this; this.partners.each(function () { var partner = $(this); var partner_discount = 0.0; var discounts = partner.data('discounts'); var hide_discount = partner.data('hide-discount'); if (discounts) { if (backing_id) { if (discounts.hasOwnProperty(entity_id) && discounts[entity_id].hasOwnProperty(backing_id)) { partner_discount = discounts[entity_id][backing_id]; } } else { if (discounts.hasOwnProperty(entity_id)) { partner_discount = discounts[entity_id]; } } } if (!partner.hasClass('hidden')) { if (hide_discount) { $('.discount-special-offer', partner).removeClass('hidden'); $('.discount-percent', partner).addClass('hidden'); } else { if (total > 0) { var discount = price * partner_discount / 100; if (discount > total) { discount = total; } $('.discount-special-offer', partner).addClass('hidden'); $('.discount-percent', partner).removeClass('hidden'); //$('.discount-percent span', partner).html(partner_discount + '%'); $('.discount-percent span', partner).html(obj.options.currency + ' ' + obj.formatPrice(discount)); } 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]; }); }; proto.parsePrice = function (price) { price = '' + price; if (price) { price = price.replace(this.options.currency, ''); price = price.replace('.-', ''); price = price.replace('-', ''); price = price.replace('.00', ''); price = price.trim(); price = parseFloat(price); if (isNaN(price)) { price = 0; } } else { price = 0; } return price; }; })(fasoon, jQuery);