News:

Looking for documentation? Take a look on our wiki

Main Menu

A lot of js code in frontpage Virtuemart

Started by zama85, June 28, 2013, 00:39:15 AM

Previous topic - Next topic

zama85

Hello everyone.

I hope someone can help me because I tried to figure out why happend that but i din't find any solution.

My problem is I have a lot of repeated js code of virtuemart written in the frontpage in the browser around in 2000 lines. It's crazy.

I show you the code.


//<![CDATA[
jQuery( function($) {
//var customfield_id = {};
//var selecteds = [];//all selected options
//var found_id=0;//found child id
var stockhandle = "disableadd";
var stockable_53 =$.parseJSON('{"275":{"selectoptions1":["0mg","0mg"],"in_stock":20},"276":{"selectoptions1":["9mg","9mg"],"in_stock":0},"277":{"selectoptions1":["0mg","0mg"],"in_stock":50}}') ;
var selections_53 = [];
//var original=[];
// var totalattribut_53 = $("select.attribute_list.customfield_id_53").length+1;
var totalattribut_53 = [];
// get all initial select list values
/*$.each($(".attribute_list"), function(idx,selec) {
original[selec.name] = $.map($(this).find("option"), function(idx, opt) {
return [[ idx.value ,idx.text ]];
});
});*/

// if ( $("#selectoptions1.attribute_list.customfield_id_53").length ) {
if ( $("select.attribute_list.customfield_id_53").length ) {
var stockableBlockIndex = 0;
$(".stockable_block_53").each(function() {
$(this).attr("id", "stockableBlockIndex_53_" + stockableBlockIndex);
totalattribut_53[stockableBlockIndex] = $(this).find("select.attribute_list.customfield_id_53").length+1;
recalculate_53(stockableBlockIndex, $(this).find("select.attribute_list.customfield_id_53").eq(0));
stockableBlockIndex++;
});
}
$("select.attribute_list.customfield_id_53").unbind("change");
$("select.attribute_list.customfield_id_53").change(function(){
var stockableBlockIndex = $(this).parents(".stockable_block_53").attr("id").split("_");
recalculate_53(stockableBlockIndex[stockableBlockIndex.length-1], $(this));

});
function recalculate_53(stockableBlockIndex, Opt){
var found_id = 0;
var currentIndex = $("#stockableBlockIndex_53_"+stockableBlockIndex+" select.attribute_list.customfield_id_53").index(Opt) +1;

selections_53[stockableBlockIndex] = [];
var i=1;
$("#stockableBlockIndex_53_"+stockableBlockIndex+" select.attribute_list.customfield_id_53").each(function() {
selections_53[stockableBlockIndex][i] = $(this).val();
// Clear the following selects
if (i > currentIndex) {
$(this).empty();
}

i++;
});

// Find current values
/*for(var i=1; i<totalattribut_53[stockableBlockIndex]; i++){
selections_53[stockableBlockIndex][i] = $("#selectoptions"+i+".customfield_id_53").val();
}*/

// Clear the following selects
/*for(var i=currentIndex+1; i<totalattribut_53[stockableBlockIndex]; i++){
$("#stockableBlockIndex_53_"+stockableBlockIndex+" #selectoptions"+i+".customfield_id_53").empty();
}*/

// Repopulate the following selects
jQuery.each(stockable_53, function(child_id, child_attrib) {
if (isChildValid_53(stockableBlockIndex, child_attrib, currentIndex)) {
populateNextSelect_53(stockableBlockIndex, child_attrib, currentIndex+1);
}
});

// Identify the current child
jQuery.each(stockable_53, function(child_id, child_attrib) {
var i;
for(i = 1; i < totalattribut_53[stockableBlockIndex]; i++){
if (child_attrib["selectoptions"+i][0] != selections_53[stockableBlockIndex][i]) {
break;
}
}
if (totalattribut_53[stockableBlockIndex] == i) {
found_id = child_id;
return false;
}
});

if ("disableadd" == stockhandle && stockable_53[found_id].in_stock <= 0) {
$(".addtocart-bar>span").remove();
$(".addtocart-bar>div").remove();
$(".addtocart-bar>a.notify").remove();
$(".addtocart-bar").append('<a href="/ind'+'ex.php?option=com_virtuemart&view=productdetails&layout=notify&virtuemart_product_id=' + found_id + '" class="notify">Notificarme!</a>');
} else {
var quantity = $.parseJSON('0') ;
if (quantity == 0){
quantity = 1;
}
$(".addtocart-bar>span").remove();
$(".addtocart-bar>div").remove();
$(".addtocart-bar>a.notify").remove();
$(".addtocart-bar").append('<span class="quantity-box"><input type="text" class="quantity-input js-recalculate" name="quantity[]" value="' + quantity + '" /></span><span class="quantity-controls js-recalculate"><input type="button" class="quantity-controls quantity-plus" /><input type="button" class="quantity-controls quantity-minus" /></span><span class="addtocart-button"><input type="submit" name="addtocart" class="addtocart-button" value="Agregar al carrito" title="Agregar al carrito" /></span><div class="clear"></div>');
Virtuemart.product($("form.product"));
}

$(".availability").remove();

if ("risetime" == stockhandle && stockable_53[found_id].product_availability) {
$(".addtocart-area").after('<div class="availability">' + stockable_53[found_id].product_availability + '</div>');
}

// recalculate the price by found product child id;
formProduct = Opt.parents("form.product");
virtuemart_product_id = formProduct.find('input[name="virtuemart_product_id[]"]').val();
//formProduct.find("#selectedStockable").remove();
//formProduct.append('<input id="stockableChild" type="hidden" value="'+customfield_id[found_id]+'" name="customPrice[1]['+found_id+']">');
formProduct.find('input[name*="customPlugin[53][stockable][child_id]"]').val(found_id);

//('<input id="stockableChild" type="hidden" value="'+customfield_id[found_id]+'" name="customPrice[1]['+found_id+']">');
Virtuemart.setproducttype(formProduct,virtuemart_product_id);
}
function isChildValid_53(stockableBlockIndex, child_attrib, currentIndex) {
return_value = true;
for (var i = currentIndex; i > 0; i--) {
if (child_attrib["selectoptions"+i][0] != selections_53[stockableBlockIndex][i]) {
return_value = false;
}
}
return return_value;
}
function populateNextSelect_53(stockableBlockIndex, child_attrib, nextIndex) {
var selectList = $("#stockableBlockIndex_53_"+stockableBlockIndex+" select.attribute_list.customfield_id_53");
var nextSelect = selectList.eq(nextIndex-1);
// if the select exists
if ("undefined" !== typeof(nextSelect) && nextSelect.length > 0) {
// if it doesn't already contain this option, add it
if (nextSelect.find("option[value='" + child_attrib["selectoptions"+nextIndex][0] + "']").length == 0) {
nextSelect.append("<option value='" + child_attrib["selectoptions"+nextIndex][0] + "'>" + child_attrib["selectoptions"+nextIndex][1] + "</option>");
}

// if there is only one option, make it selected
if (1 == nextSelect.find("option").length) {
nextSelect.find("option").attr("selected","selected");
selections_53[stockableBlockIndex][nextIndex] = child_attrib["selectoptions"+nextIndex][0];
}
// if this is the selected value, populate the next select too
if (nextSelect.val() == child_attrib["selectoptions"+nextIndex][0]) {
populateNextSelect_53(stockableBlockIndex, child_attrib, nextIndex+1);
}
}
}
});
//]]>


Thank you very much for your help.