<commit_before>/**
 *
 *  Shopping Cart
 *
 */

(function($) {
    var ShoppingCart = {

        name: 'shoppingCart',

        models: {
            cartInfo: {},
            products: {}
        },

        init: function() {
            log('initializing cart');
            var localStorageCart = JSON.parse(localStorage.getItem(ShoppingCart.name));
            RenderCartDom(localStorageCart);
            this.bindRemoveItem();
        },

        add: function(product){
            log('adding to cart');
            log(product);
            addItemToCart(product);
        },

        remove: function(product_id){
            RemoveItemFromCart(product_id);
        },

        bindRemoveItem: function(){
            var ShoppingCart = this;
            $('#shopping-list').on('click', 'li', function(){
                ShoppingCart.remove($(this).attr('id'));
                $(this).remove();
            });
        }
    };

    window.ShoppingCart = ShoppingCart;

    $(document).ready(function() {

        ShoppingCart.init();

    });

})(jQuery);


function addItemToCart(product){
    var localStorageCart = addElementToLocalStorage(product);
    log(localStorage.getItem(ShoppingCart.name));

    RenderCartDom(localStorageCart);
}

function RenderCartDom(localStorageCart){
    if (localStorageCart == undefined){
        return true
    }
    var shoppingCartLayout = '<% _.each(products, function(product) { %>' +
                                '<li class="shopping-cart-item" id= <%= product.id %> >' +
                                    '<%= product.name %> x <%= product.quantity%></li>' +
                             '<% });%>';

    $('#shopping-items').html(_.template(shoppingCartLayout,{products : localStorageCart[ShoppingCart.models.products]}));
    //TODO - find a better way to bind events
//    ShoppingCart.bindRemoveItem();
}

function addElementToLocalStorage(product){
    var localStorageCart    = getCartFromLocalStorage();

    localStorageCart        = smartAddToProducts(localStorageCart, product);
    setCartInLocalStorage(localStorageCart);
    return localStorageCart;
}

function smartAddToProducts(cartObject, productToAdd){
    var products            = cartObject[ShoppingCart.models.products] || {};

    cartObject[ShoppingCart.models.products] = addOrSetItemToCart(products, productToAdd);

    return cartObject;
}

function getCartFromLocalStorage(){
    return JSON.parse(localStorage.getItem(ShoppingCart.name)) || {};
}

function addOrSetItemToCart(products, productToAdd){
    if (doesNotHaveKey(products, productToAdd.id)){
        products[productToAdd.id] = {id: productToAdd.id, name: productToAdd.name, quantity: 1};
    }else{
        products[productToAdd.id]['quantity'] += 1;
    }
    return products;
}<commit_msg>adding total price<commit_after>/**
 *
 *  Shopping Cart
 *
 */

(function($) {
    var ShoppingCart = {

        name: 'shoppingCart',

        models: {
            cartInfo: {},
            products: {}
        },

        init: function() {
            log('initializing cart');
            var localStorageCart = JSON.parse(localStorage.getItem(ShoppingCart.name));
            RenderCartDom(localStorageCart);
            this.bindRemoveItem();
        },

        add: function(product){
            log('adding to cart');
            log(product);
            addItemToCart(product);
        },

        remove: function(product_id){
            RemoveItemFromCart(product_id);
        },

        bindRemoveItem: function(){
            var ShoppingCart = this;
            $('#shopping-list').on('click', 'li', function(){
                ShoppingCart.remove($(this).attr('id'));
                $(this).remove();
            });
        }
    };

    window.ShoppingCart = ShoppingCart;

    $(document).ready(function() {

        ShoppingCart.init();

    });

})(jQuery);


function addItemToCart(product){
    var localStorageCart = addElementToLocalStorage(product);
    log(localStorage.getItem(ShoppingCart.name));

    RenderCartDom(localStorageCart);
}

function RenderCartDom(localStorageCart){
    if (localStorageCart == undefined) return true;


    var productsHtml    = productsDom(localStorageCart);
    var priceHtml       = priceDom(localStorageCart);

    $('#shopping-items').html(productsHtml + priceHtml);

}
function productsDom(localStorageCart){
    var ProductsLayout = '<% _.each(products, function(product) { %>' +
                                '<li id= <%= product.id %> >' +
                                    '<div class="shopping-cart-item">' +
                                         '<%= product.quantity %> <%= product.name %>  - <%= product.price * product.quantity %>' +
                                    '</div>' +
                                '</li>' +
                           '<% });%>';


    return _.template(ProductsLayout,{products : localStorageCart[ShoppingCart.models.products]});
}

function priceDom(localStorageCart){
    var totalPriceHtml;

    //TODO better performance with adding to total price and not recalcing every time
    totalPriceHtml = calcTotalPrice(localStorageCart);
    return totalPriceHtml;
}

function calcTotalPrice(localStorageCart){
    var products    = localStorageCart[ShoppingCart.models.products];
    var totalPrice  = 0;
    _.each(products, function(product) {
        totalPrice += (product.price || 0) * product.quantity;
    });

    if (totalPrice == 0){
        return '';
    }else{
        return '<div id="shopping-cart-price"> Total Price '+ totalPrice +'$</div>'
    }
}


function addElementToLocalStorage(product){
    var localStorageCart    = getCartFromLocalStorage();

    localStorageCart        = smartAddToProducts(localStorageCart, product);
    setCartInLocalStorage(localStorageCart);
    return localStorageCart;
}

function smartAddToProducts(cartObject, productToAdd){
    var products            = cartObject[ShoppingCart.models.products] || {};

    cartObject[ShoppingCart.models.products] = addOrSetItemToCart(products, productToAdd);

    return cartObject;
}

function getCartFromLocalStorage(){
    return JSON.parse(localStorage.getItem(ShoppingCart.name)) || {};
}

function addOrSetItemToCart(products, productToAdd){
    if (doesNotHaveKey(products, productToAdd.id)){
        var productToSave = $.extend(productToAdd, {quantity: 1});
        products[productToAdd.id] = productToSave;
    }else{
        products[productToAdd.id]['quantity'] += 1;
    }
    return products;
}
{'fraction_non_alphanumeric': 0.13000767459708365, 'fraction_numerical': 0.001074443591711435, 'mean_word_length': 2.493833780160858, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5494529', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1760, 'n_words': 305}