From dd593502a79a30e138acf0a7ad62161bf3637510 Mon Sep 17 00:00:00 2001 From: Aurélio A. Heckert Date: Wed, 20 Mar 2013 16:09:26 -0300 Subject: [PATCH] Fixing the ShoppingCartPlugin --- plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb | 26 ++++++++++++++++++-------- plugins/shopping_cart/public/cart.js | 46 ++++++++++++++++++++++++++++------------------ plugins/shopping_cart/views/cart.html.erb | 14 +++++++++++++- public/javascripts/application.js | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+), 27 deletions(-) diff --git a/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb b/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb index 28da890..d7bacec 100644 --- a/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb +++ b/plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb @@ -57,14 +57,24 @@ class ShoppingCartPluginController < PublicController def list if validate_cart_presence products = self.cart[:items].collect do |id, quantity| - product = Product.find(id) - { :id => product.id, - :name => product.name, - :price => get_price(product, product.enterprise.environment), - :description => product.description, - :picture => product.default_image(:minor), - :quantity => quantity - } + product = Product.find_by_id(id) + if product + { :id => product.id, + :name => product.name, + :price => get_price(product, product.enterprise.environment), + :description => product.description, + :picture => product.default_image(:minor), + :quantity => quantity + } + else + { :id => id, + :name => _('Undefined product'), + :price => 0, + :description => _('Wrong product id'), + :picture => '', + :quantity => quantity + } + end end render :text => { :ok => true, diff --git a/plugins/shopping_cart/public/cart.js b/plugins/shopping_cart/public/cart.js index 58f3ff9..0016215 100644 --- a/plugins/shopping_cart/public/cart.js +++ b/plugins/shopping_cart/public/cart.js @@ -21,7 +21,7 @@ function Cart(config) { }, cache: false, error: function(ajax, status, errorThrown) { - alert('Visibility - HTTP '+status+': '+errorThrown); + log.error('Visibility - HTTP '+status, errorThrown); } }); $(".cart-buy", this.cartElem).colorbox({ href: '/plugin/shopping_cart/buy' }); @@ -36,12 +36,12 @@ function Cart(config) { url: '/plugin/shopping_cart/list', dataType: 'json', success: function(data, ststus, ajax){ - if ( !data.ok ) alert(data.error.message); + if ( !data.ok ) log.error(data.error); else me.addToList(data, true); }, cache: false, error: function(ajax, status, errorThrown) { - alert('List cart items - HTTP '+status+': '+errorThrown); + log.error('List cart items - HTTP '+status, errorThrown); } }); } @@ -49,6 +49,7 @@ function Cart(config) { Cart.prototype.addToList = function(data, clear) { if( clear ) this.itemsBox.empty(); var me = this; + this.productsLength = data.products.length; for( var item,i=0; item=data.products[i]; i++ ) { this.items[item.id] = { price:item.price, quantity:item.quantity }; this.updateTotal(); @@ -75,9 +76,7 @@ function Cart(config) { input.onchange = function() { me.updateQuantity(this, this.productId, this.value); }; -// document.location.href = "#"+liId; -// document.location.href = "#"+this.cartElem.id; -// history.go(-2); + // TODO: Scroll to newest item var liBg = li.css("background-color"); li[0].style.backgroundColor = "#FF0"; li.animate({ backgroundColor: liBg }, 1000); @@ -103,7 +102,7 @@ function Cart(config) { dataType: 'json', success: function(data, status, ajax){ if ( !data.ok ) { - alert(data.error.message); + log.error(data.error); input.value = input.lastValue; } else { @@ -114,7 +113,7 @@ function Cart(config) { }, cache: false, error: function(ajax, status, errorThrown) { - alert('Add item - HTTP '+status+': '+errorThrown); + log.error('Add item - HTTP '+status, errorThrown); input.value = input.lastValue; }, complete: function(){ @@ -132,7 +131,14 @@ function Cart(config) { } Cart.addItem = function(itemId, link) { + if ( this.productsLength > 100 ) { + // This limit protect the user from losing data on cookie limit. + // This is NOT limiting to 100 products, is limiting to 100 kinds of products. + alert(shoppingCartPluginL10n.maxNumberOfItens); + return false; + } link.intervalId = setInterval(function() { + $(link).addClass('loading'); steps = ['w', 'n', 'e', 's']; if( !link.step || link.step==3 ) link.step = 0; link.step++; @@ -140,6 +146,7 @@ function Cart(config) { }, 100); var stopBtLoading = function() { clearInterval(link.intervalId); + $(link).removeClass('loading'); $(link).button({ icons: { primary: 'ui-icon-cart'}, disable: false }); }; this.instance.addItem(itemId, stopBtLoading); @@ -151,12 +158,12 @@ function Cart(config) { url: '/plugin/shopping_cart/add/'+ itemId, dataType: 'json', success: function(data, status, ajax){ - if ( !data.ok ) alert(data.error.message); + if ( !data.ok ) log.error('Shopping cart data failure', data.error); else me.addToList(data); }, cache: false, error: function(ajax, status, errorThrown) { - alert('Add item - HTTP '+status+': '+errorThrown); + log.error('Add item - HTTP '+status, errorThrown); }, complete: callback }); @@ -174,12 +181,12 @@ function Cart(config) { url: '/plugin/shopping_cart/remove/'+ itemId, dataType: 'json', success: function(data, status, ajax){ - if ( !data.ok ) alert(data.error.message); + if ( !data.ok ) log.error(data.error); else me.removeFromList(data.product_id); }, cache: false, error: function(ajax, status, errorThrown) { - alert('Remove item - HTTP '+status+': '+errorThrown); + log.error('Remove item - HTTP '+status, errorThrown); } }); } @@ -197,7 +204,7 @@ function Cart(config) { dataType: 'json', cache: false, error: function(ajax, status, errorThrown) { - alert('Show - HTTP '+status+': '+errorThrown); + log.error('Show - HTTP '+status, errorThrown); } }); this.visible = true; @@ -212,7 +219,7 @@ function Cart(config) { dataType: 'json', cache: false, error: function(ajax, status, errorThrown) { - alert('Hide - HTTP '+status+': '+errorThrown); + log.error('Hide - HTTP '+status, errorThrown); } }); this.visible = false; @@ -248,7 +255,7 @@ function Cart(config) { url: '/plugin/shopping_cart/clean', dataType: 'json', success: function(data, status, ajax){ - if ( !data.ok ) alert(data.error.message); + if ( !data.ok ) log.error(data.error); else{ me.items = {}; $(me.cartElem).slideUp(500, function() { @@ -261,7 +268,7 @@ function Cart(config) { }, cache: false, error: function(ajax, status, errorThrown) { - alert('Remove item - HTTP '+status+': '+errorThrown); + log.error('Remove item - HTTP '+status, errorThrown); } }); } @@ -288,7 +295,7 @@ function Cart(config) { }, cache: false, error: function(ajax, status, errorThrown) { - alert('Send request - HTTP '+status+': '+errorThrown); + log.error('Send request - HTTP '+status, errorThrown); }, complete: function() { $.colorbox.close(); @@ -311,7 +318,10 @@ function Cart(config) { }, cache: false, error: function(ajax, status, errorThrown) { - alert('Error getting shopping cart - HTTP '+status+': '+errorThrown); + log.error('Error getting shopping cart - HTTP '+status, errorThrown); + if ( confirm(shoppingCartPluginL10n.getProblemConfirmReload) ) { + document.location.reload(); + } } }); }); diff --git a/plugins/shopping_cart/views/cart.html.erb b/plugins/shopping_cart/views/cart.html.erb index dcd5ab6..7cbccb3 100644 --- a/plugins/shopping_cart/views/cart.html.erb +++ b/plugins/shopping_cart/views/cart.html.erb @@ -7,7 +7,7 @@ <%=_('Clean basket')%>
<%=_('Total:')%>
- <%=_('Shopping checkout')%> + <%=_('Shopping checkout')%> <%=_('Show basket')%> @@ -15,3 +15,15 @@ + diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 9c271ee..9b3ef6d 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -918,6 +918,41 @@ function facet_options_toggle(id, url) { }); } +if ( !console ) console = {}; +if ( !console.log ) console.log = function(){}; + +// Two ways to call it: +// log(mixin1[, mixin2[, ...]]); +// log('', mixin1[, mixin2[, ...]]); +// Where may be: log, info warn, or error +window.log = function log() { + var type = arguments[0]; + var argsClone = jQuery.merge([], arguments); // cloning the read-only arguments array. + if ( ['info', 'warn', 'error'].indexOf(type) == -1 ) { + type = 'log'; + } else { + argsClone.shift(); + } + var method = type; + if ( !console[method] ) method = 'log'; + console[method].apply( console, jQuery.merge([(new Date).toISOString()], argsClone) ); +} + +// Call log.info(mixin1[, mixin2[, ...]]); +log.info = function() { + window.log.apply(window, jQuery.merge(['info'], arguments)); +} + +// Call log.warn(mixin1[, mixin2[, ...]]); +log.warn = function() { + window.log.apply(window, jQuery.merge(['warn'], arguments)); +} + +// Call log.error(mixin1[, mixin2[, ...]]); +log.error = function() { + window.log.apply(window, jQuery.merge(['error'], arguments)); +} + jQuery(function($) { $('.colorbox').live('click', function() { $.fn.colorbox({ -- libgit2 0.21.2