diff --git a/public/js/website_utils.js b/public/js/website_utils.js index d1b5ab7f206..73fb04b1078 100644 --- a/public/js/website_utils.js +++ b/public/js/website_utils.js @@ -70,16 +70,17 @@ $(document).ready(function() { // update login var full_name = getCookie("full_name"); if(full_name) { - $("#user-tools").html(repl('%(full_name)s | \ - My Account | \ - \ - ', { - full_name: full_name, - count: getCookie("cart_count") || "0" - })); - $("#user-tools a").tooltip({"placement":"bottom"}); + $("#user-tools").addClass("hide"); + $("#user-tools-post-login").removeClass("hide"); + $("#user-full-name").text(full_name); } -}) + + wn.cart.update_display(); + $("#user-tools a").tooltip({"placement":"bottom"}); + $("#user-tools-post-login a").tooltip({"placement":"bottom"}); + + $(window).on("storage", function() { wn.cart.update_display(); }); +}); // Utility functions @@ -162,3 +163,43 @@ if (typeof Array.prototype.map !== "function") { return a; }; } + +// shopping cart +if(!wn.cart) wn.cart = {}; +$.extend(wn.cart, { + get_count: function() { + return Object.keys(this.get_cart()).length; + }, + + add_to_cart: function(itemprop) { + var cart = this.get_cart(); + cart[itemprop.item_code] = $.extend(itemprop, {qty: 1}); + this.set_cart(cart); + console.log(this.get_cart()); + }, + + remove_from_cart: function(item_code) { + var cart = this.get_cart(); + delete cart[item_code]; + this.set_cart(cart); + console.log(this.get_cart()); + }, + + get_cart: function() { + if( !("localStorage" in window) ) { + alert("Your browser seems to be ancient. Please use a modern browser."); + throw "ancient browser error"; + } + + return JSON.parse(localStorage.getItem("cart")) || {}; + }, + + set_cart: function(cart) { + localStorage.setItem("cart", JSON.stringify(cart)); + wn.cart.update_display(); + }, + + update_display: function() { + $(".cart-count").text("( " + wn.cart.get_count() + " )"); + } +}); \ No newline at end of file diff --git a/website/templates/html/outer.html b/website/templates/html/outer.html index ec71476f77b..cc1181cbd2c 100644 --- a/website/templates/html/outer.html +++ b/website/templates/html/outer.html @@ -3,8 +3,17 @@ {% block body %}
Item Code: {{ name }}
+Item Code: {{ name }}
@ ' + item.price + '
') : ""; + + $(repl('%(description)s
\ +No Items added to cart.
').appendTo($cart_wrapper); + } + } +}); \ No newline at end of file diff --git a/website/templates/js/product_page.js b/website/templates/js/product_page.js index 69e9cd52feb..338f25331d9 100644 --- a/website/templates/js/product_page.js +++ b/website/templates/js/product_page.js @@ -28,16 +28,41 @@ $(document).ready(function() { if(data.message.price) { $("