刚刚继承了下拉菜单的这个功能,它丑陋无比,在几个浏览器(主要是旧版本的 safari)中似乎很脆弱,但我什至不知道从哪里开始,任何建议都将不胜感激。我觉得我需要将其分解为几个较小的函数,并删除硬编码的 html(不知道到底该怎么做)
违规代码:
function mini_cart_populate(){
var order;
$.get('/get_current_order.json',
function(data) {
$('#minicart-thumbnails').html('');
order = data.order;
if (order.order.line_items.length == 1) {
$('#minicart_content').addClass('single');
$('.cart_pulldown').addClass('single');
$('#minicart_footer').addClass('single');
} else {
$('#minicart_content').removeClass('single');
$('.cart_pulldown').removeClass('single');
$('#minicart_footer').removeClass('single');
}
if (order.order.line_items.length > 0) {
$('#cart_pulldown').removeClass('empty_minicart');
$('#minicart_header').show();
$('#minicart_content').show();
$('#minicart_footer').show();
$('.clearBoth').show();
for (var i = 0; i < order.order.line_items.length ; i ++ ) {
var char_limit = 80;
var variant = data.variant_information[i];
var item_string = "<div class='minicart_item_wrapper'>";
//image string
item_string += "<a href='"+ variant.path +"'><img src='"+ variant.image_url +"'></a>";
//assume title and description for all items
item_string += "<div class='mini_item_detail'>"+
"<p class='mini_item_description'><strong>"+variant.title+
"</strong>"+"<br/>"+variant.description.slice(0,char_limit)+
(variant.description.slice(char_limit).length > 0 ? "..." : "" ) + "</p>";
//assume all items have price
item_string += "<li>"+"$"+Math.round(variant.price)+"</li>";
//check for variant measurements and foreign sizes; add '|' if there are
if (variant.variant_size) { item_string += " | <li>"+variant.variant_size+"</li>"; }
if (variant.foreign_size) { item_string += " | <li>"+variant.foreign_size+"</li>"; }
var delete_button_string = "<div class='cart-item-delete minicart' data-hook='cart_item_delete'>" +
"<a href='javascript:;' class='delete' id='delete_line_item_"+
variant.line_item_id +"' line_item_id='" +
variant.line_item_id + "'>X</a></div></div></div>"
if (window.location.pathname.split("/")[1] == "checkout"){
delete_button_string = '';
}
$('#minicart-thumbnails').append( item_string + delete_button_string )
bind_delete_button();
$('#minicart_info').show();
$('#minicart-thumbnails').show();
}
} else {
$('.cart_pulldown').addClass('empty_minicart');
$('#minicart_info').hide();
$('#minicart-thumbnails').hide();
$("#mini_empty_message").show();
$('#minicart_header').hide();
$('#minicart_content').hide();
$('#minicart_footer').hide();
$('.clearBoth').hide();
}
set_product_page_variant_state();
var new_count = order.order.line_items.length + " item";
if (order.order.line_items.length > 1) { new_count += "s" };
$("#mini-item-count").text(new_count);
}).done(function(){
var item_total = parseFloat(order.order.item_total);
var subtotal = item_total.toFixed(2);
$('#minicart_subtotal').text('$' + subtotal);
})
}