Commit 6be61ee9b124ab6f441a7623188c827da3b056a0

Authored by Rodrigo Souto
1 parent d100def5

[shopping-cart] Fixing double colorbox open and improving requests

We improved some extra requests on the shopping cart, like: visibility
and list actions. They are now being retrieved on the get action all
together.

Also forbidding the user to request more the one action on the cart
simultaneously because the cart in the cookie doesn't supports it.
(We need the session stored in the database!)
plugins/shopping_cart/controllers/shopping_cart_plugin_controller.rb
... ... @@ -13,9 +13,15 @@ class ShoppingCartPluginController < PublicController
13 13 def get
14 14 config =
15 15 if cart.nil?
16   - { 'enterprise_id' => nil, 'hasProducts' => false }
  16 + { :enterprise_id => nil,
  17 + :has_products => false,
  18 + :visible => false,
  19 + :products => []}
17 20 else
18   - { 'enterprise_id' => cart[:enterprise_id], 'hasProducts' => (cart[:items].keys.size > 0) }
  21 + { :enterprise_id => cart[:enterprise_id],
  22 + :has_products => (cart[:items].keys.size > 0),
  23 + :visible => visible?,
  24 + :products => products}
19 25 end
20 26 render :text => config.to_json
21 27 end
... ... @@ -56,26 +62,6 @@ class ShoppingCartPluginController < PublicController
56 62  
57 63 def list
58 64 if validate_cart_presence
59   - products = self.cart[:items].collect do |id, quantity|
60   - product = Product.find_by_id(id)
61   - if product
62   - { :id => product.id,
63   - :name => product.name,
64   - :price => get_price(product, product.enterprise.environment),
65   - :description => product.description,
66   - :picture => product.default_image(:minor),
67   - :quantity => quantity
68   - }
69   - else
70   - { :id => id,
71   - :name => _('Undefined product'),
72   - :price => 0,
73   - :description => _('Wrong product id'),
74   - :picture => '',
75   - :quantity => quantity
76   - }
77   - end
78   - end
79 65 render :text => {
80 66 :ok => true,
81 67 :error => {:code => 0},
... ... @@ -138,7 +124,7 @@ class ShoppingCartPluginController < PublicController
138 124 end
139 125  
140 126 def visibility
141   - render :text => self.cart.has_key?(:visibility) ? self.cart[:visibility].to_json : true.to_json
  127 + render :text => visible?.to_json
142 128 end
143 129  
144 130 def show
... ... @@ -327,4 +313,31 @@ class ShoppingCartPluginController < PublicController
327 313 :_noosfero_plugin_shopping_cart
328 314 end
329 315  
  316 + def visible?
  317 + !self.cart.has_key?(:visibility) || self.cart[:visibility]
  318 + end
  319 +
  320 + def products
  321 + self.cart[:items].collect do |id, quantity|
  322 + product = Product.find_by_id(id)
  323 + if product
  324 + { :id => product.id,
  325 + :name => product.name,
  326 + :price => get_price(product, product.enterprise.environment),
  327 + :description => product.description,
  328 + :picture => product.default_image(:minor),
  329 + :quantity => quantity
  330 + }
  331 + else
  332 + { :id => id,
  333 + :name => _('Undefined product'),
  334 + :price => 0,
  335 + :description => _('Wrong product id'),
  336 + :picture => '',
  337 + :quantity => quantity
  338 + }
  339 + end
  340 + end
  341 + end
  342 +
330 343 end
... ...
plugins/shopping_cart/public/cart.js
... ... @@ -6,51 +6,36 @@ function Cart(config) {
6 6 this.contentBox = $("#cart1 .cart-content");
7 7 this.itemsBox = $("#cart1 .cart-items");
8 8 this.items = {};
9   - this.empty = !config.hasProducts;
  9 + this.empty = !config.has_products;
10 10 this.visible = false;
11 11 $(".cart-buy", this.cartElem).button({ icons: { primary: 'ui-icon-cart'} });
12 12 if (!this.empty) {
13 13 $(this.cartElem).show();
14   - me = this;
15   - $.ajax({
16   - url: '/plugin/shopping_cart/visibility',
17   - dataType: 'json',
18   - success: function(data, status, ajax){
19   - me.visible = /^true$/i.test(data);
20   - me.listProducts();
21   - },
22   - cache: false,
23   - error: function(ajax, status, errorThrown) {
24   - log.error('Visibility - HTTP '+status, errorThrown);
25   - }
26   - });
27   - $(".cart-buy", this.cartElem).colorbox({ href: '/plugin/shopping_cart/buy' });
  14 + this.visible = config.visible;
  15 + this.addToList(config.products, true)
28 16 }
29 17 }
30 18  
31 19 (function($){
32 20  
33   - Cart.prototype.listProducts = function() {
  21 + // Forbidding the user to request more the one action on the cart
  22 + // simultaneously because the cart in the cookie doesn't supports it.
  23 + Cart.prototype.ajax = function(config){
34 24 var me = this;
35   - $.ajax({
36   - url: '/plugin/shopping_cart/list',
37   - dataType: 'json',
38   - success: function(data, ststus, ajax){
39   - if ( !data.ok ) log.error(data.error);
40   - else me.addToList(data, true);
41   - },
42   - cache: false,
43   - error: function(ajax, status, errorThrown) {
44   - log.error('List cart items - HTTP '+status, errorThrown);
45   - }
46   - });
  25 + this.disabled = true;
  26 + var completeCallback = config.complete;
  27 + config.complete = function(){
  28 + me.disabled = false;
  29 + if (completeCallback) completeCallback();
  30 + };
  31 + $.ajax(config);
47 32 }
48 33  
49   - Cart.prototype.addToList = function(data, clear) {
  34 + Cart.prototype.addToList = function(products, clear) {
50 35 if( clear ) this.itemsBox.empty();
51 36 var me = this;
52   - this.productsLength = data.products.length;
53   - for( var item,i=0; item=data.products[i]; i++ ) {
  37 + this.productsLength = products.length;
  38 + for( var item,i=0; item=products[i]; i++ ) {
54 39 this.items[item.id] = { price:item.price, quantity:item.quantity };
55 40 this.updateTotal();
56 41 var liId = "cart-item-"+item.id;
... ... @@ -85,7 +70,7 @@ function Cart(config) {
85 70 if (!clear && this.empty) $(this.cartElem).show();
86 71 if((!clear && this.empty) || (this.visible && clear)) {
87 72 this.contentBox.hide();
88   - this.show();
  73 + this.show(!clear);
89 74 }
90 75 this.empty = false;
91 76 }
... ... @@ -97,7 +82,7 @@ function Cart(config) {
97 82 input.style.backgroundImage = "url(/images/loading-small.gif)";
98 83 var me = this;
99 84 if( quantity == NaN ) return input.value = input.lastValue;
100   - $.ajax({
  85 + this.ajax({
101 86 url: '/plugin/shopping_cart/update_quantity/'+ itemId +'?quantity='+ quantity,
102 87 dataType: 'json',
103 88 success: function(data, status, ajax){
... ... @@ -142,24 +127,24 @@ function Cart(config) {
142 127 steps = ['w', 'n', 'e', 's'];
143 128 if( !link.step || link.step==3 ) link.step = 0;
144 129 link.step++;
145   - $(link).button({ icons: { primary: 'ui-icon-arrowrefresh-1-'+steps[link.step]}, disable: true })
  130 + $(link).button({ icons: { primary: 'ui-icon-arrowrefresh-1-'+steps[link.step]}})
146 131 }, 100);
147 132 var stopBtLoading = function() {
148 133 clearInterval(link.intervalId);
149 134 $(link).removeClass('loading');
150   - $(link).button({ icons: { primary: 'ui-icon-cart'}, disable: false });
  135 + $(link).button({ icons: { primary: 'ui-icon-cart'}});
151 136 };
152 137 this.instance.addItem(itemId, stopBtLoading);
153 138 }
154 139  
155 140 Cart.prototype.addItem = function(itemId, callback) {
156 141 var me = this;
157   - $.ajax({
  142 + this.ajax({
158 143 url: '/plugin/shopping_cart/add/'+ itemId,
159 144 dataType: 'json',
160 145 success: function(data, status, ajax){
161 146 if ( !data.ok ) log.error('Shopping cart data failure', data.error);
162   - else me.addToList(data);
  147 + else me.addToList(data.products);
163 148 },
164 149 cache: false,
165 150 error: function(ajax, status, errorThrown) {
... ... @@ -177,7 +162,7 @@ function Cart(config) {
177 162 Cart.prototype.removeItem = function(itemId) {
178 163 if ($("li", this.itemsBox).size() < 2) return this.clean();
179 164 var me = this;
180   - $.ajax({
  165 + this.ajax({
181 166 url: '/plugin/shopping_cart/remove/'+ itemId,
182 167 dataType: 'json',
183 168 success: function(data, status, ajax){
... ... @@ -192,36 +177,41 @@ function Cart(config) {
192 177 }
193 178  
194 179 Cart.toggle = function(link) {
  180 + if(this.instance.disabled) return alert(shoppingCartPluginL10n.waitLastRequest);
195 181 link.parentNode.parentNode.cartObj.toggle();
196 182 }
197 183 Cart.prototype.toggle = function() {
198   - this.visible ? this.hide() : this.show();
  184 + this.visible ? this.hide(true) : this.show(true);
199 185 }
200 186  
201   - Cart.prototype.show = function() {
202   - $.ajax({
203   - url: '/plugin/shopping_cart/show',
204   - dataType: 'json',
205   - cache: false,
206   - error: function(ajax, status, errorThrown) {
207   - log.error('Show - HTTP '+status, errorThrown);
208   - }
209   - });
  187 + Cart.prototype.show = function(register) {
  188 + if(register) {
  189 + this.ajax({
  190 + url: '/plugin/shopping_cart/show',
  191 + dataType: 'json',
  192 + cache: false,
  193 + error: function(ajax, status, errorThrown) {
  194 + log.error('Show - HTTP '+status, errorThrown);
  195 + }
  196 + });
  197 + }
210 198 this.visible = true;
211 199 this.contentBox.slideDown(500);
212 200 $(".cart-toggle .str-show", this.cartElem).hide();
213 201 $(".cart-toggle .str-hide", this.cartElem).show();
214 202  
215 203 }
216   - Cart.prototype.hide = function() {
217   - $.ajax({
218   - url: '/plugin/shopping_cart/hide',
219   - dataType: 'json',
220   - cache: false,
221   - error: function(ajax, status, errorThrown) {
222   - log.error('Hide - HTTP '+status, errorThrown);
223   - }
224   - });
  204 + Cart.prototype.hide = function(register) {
  205 + if(register) {
  206 + this.ajax({
  207 + url: '/plugin/shopping_cart/hide',
  208 + dataType: 'json',
  209 + cache: false,
  210 + error: function(ajax, status, errorThrown) {
  211 + log.error('Hide - HTTP '+status, errorThrown);
  212 + }
  213 + });
  214 + }
225 215 this.visible = false;
226 216 this.contentBox.slideUp(500);
227 217 $(".cart-toggle .str-show", this.cartElem).show();
... ... @@ -251,7 +241,7 @@ function Cart(config) {
251 241  
252 242 Cart.prototype.clean = function() {
253 243 var me = this;
254   - $.ajax({
  244 + this.ajax({
255 245 url: '/plugin/shopping_cart/clean',
256 246 dataType: 'json',
257 247 success: function(data, status, ajax){
... ... @@ -281,7 +271,7 @@ function Cart(config) {
281 271  
282 272 Cart.prototype.send_request = function(params) {
283 273 var me = this;
284   - $.ajax({
  274 + this.ajax({
285 275 type: 'POST',
286 276 url: '/plugin/shopping_cart/send_request',
287 277 data: params,
... ...