/*------------------------------------------------------------------------------
Find the minimum sub level for base components
------------------------------------------------------------------------------*/
window.COMMONLIB_base_component_min_level = function() {
var min_level = 4;
for( var i = 0; i < available_subs.length; i ++ ) {
var missing = 0;
$('.configurator-base-component').each(function() {
var levels = "|" + $(this).attr('data-level') + "|";
if( levels.indexOf("|"+available_subs[i]+"|") == -1 ) {
missing = 1;
}
});
if( ! missing ) {
if( parseInt(available_subs[i]) < min_level ) {
min_level = parseInt(available_subs[i]);
}
}
}
return min_level;
}
/*------------------------------------------------------------------------------
Find the minimum sub level for extra components
------------------------------------------------------------------------------*/
window.COMMONLIB_extra_component_min_level = function() {
var min_level = 999;
for( var i = 0; i < available_subs.length; i ++ ) {
var missing = 0;
$('.COMMONLIB-extra-qty').each(function() {
var levels = "|" + $(this).attr('data-level') + "|";
var qty = parseInt($(this).val());
if( qty > 0 ) {
if( levels.indexOf("|"+available_subs[i]+"|") == -1 ) {
missing = 1;
}
}
});
if( ! missing ) {
if( parseInt(available_subs[i]) < min_level ) {
min_level = parseInt(available_subs[i]);
}
}
}
return min_level;
}
/*------------------------------------------------------------------------------
Find the actual minimum sub level in total. Can be less than the desired one
------------------------------------------------------------------------------*/
window.COMMONLIB_min_required_level = function() {
var min_base_level = COMMONLIB_base_component_min_level();
var min_extra_level = COMMONLIB_extra_component_min_level();
var min_level = ( min_base_level >= min_extra_level ) ? min_base_level : min_extra_level;
return min_level;
}
/*------------------------------------------------------------------------------
Update the subscription price output
------------------------------------------------------------------------------*/
window.COMMONLIB_update_sub_renewal_price = function() {
console.log(COMMONLIB_configurator_settings)
$('#configurator-subscription-cost').html(FormatPriceString(COMMONLIB_configurator_settings['prices']['current_sub_price']));
}
/*------------------------------------------------------------------------------
Set the desired subscription level. If the required level is less
than the desired, the desired will be used, otherwise the required
------------------------------------------------------------------------------*/
window.COMMONLIB_set_wished_subscription_level = function(level) {
if( $('#configurator-subscription-row-'+level).hasClass('disabled') ) {
return;
}
COMMONLIB_configurator_settings['wished_level'] = level;
// $('.configurator-subscription-list-container').hide();
COMMONLIB_update_configurator_settings();
}
/*------------------------------------------------------------------------------
Runs when extra addon qtys -/+ are pressed
Will change the qty to the new value and run a level check
------------------------------------------------------------------------------*/
window.COMMONLIB_change_extra_qty = function(id,change) {
$('.iconset-plus-mdc').css('visibility','visible');
// Set needed variables
var max = $('#max_components').val();
var old_qty = parseInt($('#COMMONLIB-extra-qty-'+id).val());
var new_qty = parseInt($('#COMMONLIB-extra-qty-'+id).val()) + change;
var pre_totals = 0;
$('.COMMONLIB-extra-qty').each(function() {
var qty = parseInt($(this).val());
pre_totals += qty;
});
// Can't go below 0
if( new_qty < 0 ) {
new_qty = 0;
change = 0;
}
var post_totals = pre_totals + change;
// Can't go over max, if there is a max
if( max ) {
if( post_totals > max ) {
post_totals = pre_totals;
change = 0;
new_qty = old_qty;
}
if( post_totals == max ) {
$('.iconset-plus-mdc').css('visibility','hidden');
}
}
$('#plugin_max_comp_counter').html(post_totals);
$('#COMMONLIB-extra-qty-'+id).val(new_qty);
$('.configurator-extra-component').each(function() {
if( $(this).attr('data-id') == id ) {
if( new_qty <= 0 ) {
$(this).removeClass('selected');
} else {
$(this).addClass('selected');
}
}
});
COMMONLIB_update_configurator_settings();
}
/*------------------------------------------------------------------------------
When the qty is updated manually instead of with the -/+ buttons
------------------------------------------------------------------------------*/
window.COMMONLIB_change_extra_qty_manual = function(id) {
var new_qty = parseInt( $('#COMMONLIB-extra-qty-'+id).val() );
if( isNaN(new_qty) || new_qty < 0 ) {
new_qty = 0;
}
$('#COMMONLIB-extra-qty-'+id).val(new_qty);
COMMONLIB_update_configurator_settings();
}
/*------------------------------------------------------------------------------
Update all required areas
------------------------------------------------------------------------------*/
window.COMMONLIB_update_output = function() {
// Subscriptions. All less than required shall be disabled
$('.configurator-subscription-row').removeClass('selected');
$('.configurator-subscription-row').each(function() {
if( $(this).attr('data-level') < COMMONLIB_configurator_settings['required_level'] ) {
$(this).addClass('disabled');
} else {
$(this).removeClass('disabled');
}
});
// Subscriptions. Mark the current one
$('#configurator-subscription-row-'+COMMONLIB_configurator_settings['current_level']).addClass('selected');
$('.configurator-subscription-current').hide();
$('.configurator-subscription-current-mobile').hide();
$('.configurator-subscription-current-'+COMMONLIB_configurator_settings['current_level']).show();
$('.configurator-subscription-current-mobile-'+COMMONLIB_configurator_settings['current_level']).show();
// Prices. First subsctiption price
COMMONLIB_update_sub_renewal_price();
// Regular price
if( $('.oldprice').attr('data-baseprice') > 0 ) {
var main_oldprice = parseFloat( $('.oldprice').attr('data-baseprice') ) + parseFloat( COMMONLIB_configurator_settings['prices']['total_addon_pricelist'] );
$('.oldprice').html( "(" + FormatPriceString(main_oldprice) + ")" );
}
// Main price
var main_price = parseFloat( COMMONLIB_configurator_settings['prices']['base_price'] ) + parseFloat( COMMONLIB_configurator_settings['prices']['total_addon_price'] );
$('#configurator-start-price').html( FormatPriceString(main_price) );
}
/*------------------------------------------------------------------------------
Put together the string to be put in the cart
full|[product_id sub]:[variant_id sub]|[product_id addon1]:[variant_id addon1]:[qty];[product_id addon2]:[variant_id addon2]:[qty] etc
------------------------------------------------------------------------------*/
window.COMMONLIB_assemble_to_cart = function() {
var string = "full|";
var extras = [];
if( ! COMMONLIB_configurator_settings['current_level'] ) {
COMMONLIB_configurator_settings['current_level'] = COMMONLIB_base_component_min_level();
}
string += $('#configurator-subscription-row-'+COMMONLIB_configurator_settings['current_level']).attr('data-product');
string += ":" + $('#configurator-subscription-row-'+COMMONLIB_configurator_settings['current_level']).attr('data-variant');
$('.COMMONLIB-extra-qty').each(function() {
if( $(this).val() > 0 ) {
extras.push( $(this).attr('data-product') + ":" + $(this).attr('data-variant') + ":" + $(this).val() );
}
});
string += "|" + extras.join(';');
$('#qualifier').val(string);
var total_price_ex = parseFloat(COMMONLIB_configurator_settings['prices']['base_price_ex']) + parseFloat(COMMONLIB_configurator_settings['prices']['total_addon_price_ex']);
var total_price_inc = parseFloat(COMMONLIB_configurator_settings['prices']['base_price_inc']) + parseFloat(COMMONLIB_configurator_settings['prices']['total_addon_price_inc']);
$('#qualifier2').val( total_price_ex + "|" + total_price_inc + "|" + COMMONLIB_configurator_settings['prices']['is_campaign'] );
}
/*------------------------------------------------------------------------------
Make total update of all configurator settings
------------------------------------------------------------------------------*/
window.COMMONLIB_update_configurator_settings = function() {
// Set levels
var min_level = COMMONLIB_min_required_level();
// Check if the min level is accessible, otherwise, go one further until a good level is found
// This required that the configurator is correctly set up in admin
var found = 0;
for( var i = min_level; i <= max_sub; i ++ ) {
i += "";
if( ! found && available_subs.indexOf(i) !== -1 ) {
found = 1;
min_level = i;
}
}
COMMONLIB_configurator_settings['required_level'] = min_level;
if( COMMONLIB_configurator_settings['wished_level'] > min_level ) {
min_level = COMMONLIB_configurator_settings['wished_level'];
}
COMMONLIB_configurator_settings['current_level'] = min_level;
// Update subscription price
COMMONLIB_configurator_settings['prices']['current_sub_price'] = $('#configurator-subscription-row-'+min_level).attr('data-renewal');
var free_components = $('#free_components').val();
var price_array = new Array;
var price_struct = {};
var check_price_struct = {};
$('.configurator-extra-component').each(function() {
var key = COMMONLIB_format_price_key( parseFloat( $(this).attr('data-priceex') ) );
if( ! price_array.includes(key)) {
price_array.push(key);
}
if( ! Array.isArray(price_struct[key]) ) {
price_struct[key] = [];
}
price_struct[key].push($(this).attr('data-id'));
});
price_array.sort(function(a,b) { return a - b;});
// Update extras price
var total_addon_price = 0;
var total_addon_price_inc = 0;
var total_addon_price_ex = 0;
var total_addon_pricelist = 0;
$('.configurator-extra-component').each(function() {
var price = parseFloat( $(this).attr('data-price') ) || 0;
var price_inc = parseFloat( $(this).attr('data-priceinc') ) || 0;
var price_ex = parseFloat( $(this).attr('data-priceex') ) || 0;
var pricelist = parseFloat( $(this).attr('data-pricelist') );
var id = $(this).attr('data-id');
var qty = parseInt( $('#COMMONLIB-extra-qty-'+id).val() );
total_addon_price += ( price * qty );
total_addon_price_inc += ( price_inc * qty );
total_addon_price_ex += ( price_ex * qty );
total_addon_pricelist += ( pricelist * qty );
check_price_struct[id] = {
qty: qty,
price: price,
price_inc: price_inc,
price_ex: price_ex,
pricelist: pricelist
};
});
// Make price adjustments if needed
if( free_components ) {
var free_done = 0;
var safety_counter = 1;
while( ! free_done ) {
var key = price_array.shift();
if( key ) {
for( var m = 0; m < price_struct[key].length; m ++ ) {
// There is a qty here. Start checking
var qty = parseInt(check_price_struct[price_struct[key][m]]['qty']);
if( qty > 0 ) {
var to_subsctract = 0;
if( qty <= free_components ) {
to_subsctract = qty;
free_components -= qty;
}
else if( qty > free_components ) {
to_subsctract = free_components;
free_components = 0;
}
if( free_components <= 0 ) {
free_done = 1;
}
if( to_subsctract ) {
total_addon_price -= ( parseFloat(check_price_struct[price_struct[key][m]]['price']) * to_subsctract );
total_addon_price_inc -= ( parseFloat(check_price_struct[price_struct[key][m]]['price_inc']) * to_subsctract );
total_addon_price_ex -= ( parseFloat(check_price_struct[price_struct[key][m]]['price_ex']) * to_subsctract );
total_addon_pricelist -= ( parseFloat(check_price_struct[price_struct[key][m]]['pricelist']) * to_subsctract );
}
}
}
}
// Make sure there is no infinite loop by mistake
safety_counter ++;
if( safety_counter >= 30 ) {
free_done = 1;
}
}
}
COMMONLIB_configurator_settings['prices']['total_addon_price'] = total_addon_price;
COMMONLIB_configurator_settings['prices']['total_addon_price_inc'] = total_addon_price_inc;
COMMONLIB_configurator_settings['prices']['total_addon_price_ex'] = total_addon_price_ex;
COMMONLIB_configurator_settings['prices']['total_addon_pricelist'] = total_addon_pricelist;
// Output
COMMONLIB_update_output();
COMMONLIB_assemble_to_cart();
}
/*------------------------------------------------------------------------------
Takes a float and makes a ten digit key
------------------------------------------------------------------------------*/
window.COMMONLIB_format_price_key = function(key) {
key *= 10000;
key += "";
for( var k = key.length; k <= 10; k ++ ) {
key = "0" + key;
}
return key;
}
/*------------------------------------------------------------------------------
Start things off
------------------------------------------------------------------------------*/
var COMMONLIB_configurator_settings;
var to_cart;
var available_subs = $('#used_subs').val().split('|');
var max_sub = $('#max_sub').val();
$(document).ready(function() {
var min_base_level = COMMONLIB_base_component_min_level();
console.log("min_base_level",min_base_level)
// Build the settings
COMMONLIB_configurator_settings = {
'current_level': min_base_level,
'required_level': min_base_level,
'wished_level': min_base_level,
'prices': {
'base_price': parseFloat($('#configurator-base-price').val()),
'base_price_inc': parseFloat($('#configurator-base-price-inc').val()),
'base_price_ex': parseFloat($('#configurator-base-price-ex').val()),
'is_campaign': $('#configurator-is-campaign').val(),
'current_sub_price': parseFloat($('#configurator-subscription-row-'+min_base_level).attr('data-renewal')),
'current_sub_price_inc': parseFloat($('#configurator-subscription-row-'+min_base_level).attr('data-renewalinc')),
'current_sub_price_ex': parseFloat($('#configurator-subscription-row-'+min_base_level).attr('data-renewalex')),
'total_addon_price': 0,
'total_addon_price_inc': 0,
'total_addon_price_ex': 0,
},
};
COMMONLIB_update_configurator_settings();
});
/*------------------------------------------------------------------------------
Controls for the button toggling the summary bar in mobile and timeout for the summary bar animating in from the bottom
------------------------------------------------------------------------------*/
$(document).ready(function(){
$('.mobile-summary-toggler').click(function(){
$('.configurator-subscription-summary-mobile').slideToggle(300);
$('.configurator-subscription-list-container').slideToggle(300);
});
});
//setTimeout(function(){ $('.configurator-summary-bar').slideToggle(300); }, 1000);
/*------------------------------------------------------------------------------
Controls the info that appears if "info" is pressed on the extra products on the product page.
------------------------------------------------------------------------------*/
$(document).ready(function(){
$('.configurator-component-info').click(function(){
$(this).find('.configurator-component-info-container').slideToggle(300);
});
});
/*------------------------------------------------------------------------------
Popup when trying to add a config to the cart when there already is one
------------------------------------------------------------------------------*/
window.COMMONLIB_add_config_to_cart = function() {
COMMONLIB_assemble_to_cart();
$.post('/',{
'JSON': 'COMMONLIB_CONFIGURATOR_IN_CART',
},function(data) {
if( data['result'] < 1 ) {
$('#config-already-in-cart-popup').closest('form').submit();
} else {
$('#config-already-in-cart-popup').show();
}
});
}
window.COMMONLIB_hide_config_already_in_cart = function() {
$('#config-already-in-cart-popup').hide();
}
window.COMMONLIB_replace_config_already_in_cart = function() {
$.post('/',{
'COMMONLIB_DELETE_CONFIG_FROM_CART': 1,
},function() {
$('#config-already-in-cart-popup').closest('form').submit();
});
}