|
$(document).ready(function(){ |
|
var addToCartBtn = $('.wrapper-components .addtocart'); |
|
var totalQty = 0; |
|
$(".wrapper-component-group").each(function(){ |
|
totalQty += parseInt($(this).attr('data-max')); |
|
}); |
|
disableCompBtn(); |
|
calculateInputAmt(); |
|
|
|
function disableCompBtn(){ |
|
$(".component-config-input").each(function(){ |
|
if (($(this).attr('max')) === ($(this).attr('min'))) { |
|
$(this).val($(this).attr('min')).attr('disabled','disabled'); |
|
} |
|
}); |
|
} |
|
function disableGroupBtns(){ |
|
if($('.wrapper-component-group').attr('data-max') > 0){ |
|
$(".wrapper-component-group").each(function(){ |
|
if(parseInt($(this).attr('data-counter')) >= parseInt($(this).attr('data-max'))){ |
|
$(this).find(".component-config-input").each(function(){ |
|
if($(this).val() <= 0){ |
|
$(this).attr('disabled','disabled'); |
|
} |
|
}); |
|
}else{ |
|
$(this).find(".component-config-input").removeAttr('disabled'); |
|
} |
|
}); |
|
disableCompBtn(); |
|
} |
|
} |
|
function calculateInputAmt(){ |
|
if($('.wrapper-component-group').length <= 1){ |
|
var totalInput = 0; |
|
$(".wrapper-component-group").find(".component-config-input").each(function(){ |
|
if (!isNaN(this.value) && this.value.length != 0) { |
|
totalInput += parseFloat(this.value); |
|
} |
|
}); |
|
$(this).find($(".wrapper-component-group").attr('data-counter', totalInput)); |
|
}else{ |
|
$(".wrapper-component-group").each(function(){ |
|
var totalInput = 0; |
|
$(this).find(".component-config-input").each(function(){ |
|
if (!isNaN(this.value) && this.value.length != 0) { |
|
totalInput += parseFloat(this.value); |
|
|
|
$(this).parents('.wrapper-component-group').attr('data-counter', totalInput); |
|
} |
|
}); |
|
}); |
|
} |
|
addtocartBtnChange(); |
|
} |
|
function addtocartBtnChange(){ |
|
var totalInput = 0; |
|
if($('.wrapper-component-group').length <= 1){ |
|
var totalInput = parseInt($(".wrapper-component-group").attr('data-counter')); |
|
}else{ |
|
$(".wrapper-component-group").each(function(){ |
|
totalInput += parseInt($(this).attr('data-counter')); |
|
}); |
|
} |
|
if(totalQty == 0 ){ |
|
if (parseInt(totalInput) > 0) { |
|
addToCartBtn |
|
.removeClass("btn-warning btn-danger") |
|
.addClass("btn-success") |
|
.text("Add to Cart") |
|
.removeAttr('disabled'); |
|
}else { |
|
addToCartBtn |
|
.removeClass("btn-warning btn-success") |
|
.addClass("btn-danger") |
|
.text("Choose More Items") |
|
.attr('disabled','disabled'); |
|
} |
|
}else{ |
|
if (parseInt(totalInput) == parseInt(totalQty)) { |
|
addToCartBtn |
|
.removeClass("btn-warning btn-danger") |
|
.addClass("btn-success") |
|
.text("Add to Cart") |
|
.removeAttr('disabled'); |
|
}else if (parseInt(totalInput) < parseInt(totalQty)) { |
|
var chooseMoreQty = parseInt(totalQty) - parseInt(totalInput); |
|
addToCartBtn |
|
.removeClass("btn-success btn-danger") |
|
.addClass("btn-warning") |
|
.text("Choose "+ chooseMoreQty + " More Item(s)") |
|
.attr('disabled','disabled'); |
|
}else { |
|
addToCartBtn |
|
.removeClass("btn-warning btn-success") |
|
.addClass("btn-danger") |
|
.text("Max of "+ totalQty + " Items") |
|
.attr('disabled','disabled'); |
|
} |
|
} |
|
} |
|
function createPopup(type, amount){ |
|
var type = type; |
|
var amount = amount; |
|
var popup = '<div class="stock-alert ' + type +' ">'; |
|
popup += '<div class="tooltip bottom" role="tooltip">'; |
|
popup += '<div class="tooltip-arrow"></div>'; |
|
popup += '<div class="tooltip-inner"><strong>' + amount + '</strong> is the ' + type +' this field can go</div>'; |
|
popup += '</div>'; |
|
popup += '</div>'; |
|
return popup; |
|
} |
|
$('.component.selector') |
|
.on('keyup', '.components-thumbnail input', function(){ |
|
var value = parseInt($(this).val()); |
|
var intMax = parseInt($(this).attr('max')); |
|
var popupMax = createPopup('maximum', intMax); |
|
var intMin = parseInt($(this).attr('min')); |
|
var popupMin = createPopup('minimum', intMin); |
|
|
|
if(value > intMax){ |
|
if($(this).parent().find('.stock-alert.maximum').length <= 0){ |
|
$(this).parent().append(popupMax); |
|
} |
|
$(this).val(intMax); |
|
}else{ |
|
$(this).parent().find('.stock-alert').remove(); |
|
} |
|
if(value < intMin){ |
|
if($(this).parent().find('.stock-alert.minimum').length <= 0){ |
|
$(this).parent().append(popupMin); |
|
} |
|
addToCartBtn.attr('disabled', 'disabled'); |
|
}else{ |
|
addToCartBtn.removeAttr('disabled'); |
|
} |
|
calculateInputAmt(); |
|
disableGroupBtns(); |
|
}) |
|
.on('focus', '.components-thumbnail input', function(){ |
|
if($(this).val() == 0){ |
|
$(this).val(''); |
|
} |
|
}) |
|
.on('blur', '.components-thumbnail input', function(){ |
|
var value = parseInt($(this).val()); |
|
var intMin = parseInt($(this).attr('min')); |
|
var popupMin = createPopup('minimum', intMin); |
|
if(value < intMin){ |
|
$(this).val(intMin); |
|
addToCartBtn.removeAttr('disabled'); |
|
}else{ |
|
$(this).parent().find('.stock-alert').remove(); |
|
addToCartBtn.removeAttr('disabled'); |
|
} |
|
if($(this).val() == '' || isNaN(value)){ |
|
$(this).val(0); |
|
} |
|
calculateInputAmt(); |
|
}) |
|
.on("change", '.components-thumbnail input', function(){ |
|
calculateInputAmt(); |
|
disableGroupBtns(); |
|
}); |
|
}); |