springInterview/src/main/resources/static/js/jquery.mycart.min.js
2020-04-30 16:09:07 +07:00

1 line
6.5 KiB
JavaScript

(function(t){"use strict";var e=function(){var e={},a=null,r={currencySymbol:"$",classCartIcon:"my-cart-icon",classCartBadge:"my-cart-badge",classProductQuantity:"my-product-quantity",classProductRemove:"my-product-remove",classCheckoutCart:"my-cart-checkout",affixCartIcon:!0,showCheckoutModal:!0,numberOfDecimals:2,cartItems:null,clickOnAddToCart:function(t){},afterAddOnCart:function(t,e,a){},clickOnCartIcon:function(t,e,a,r){},checkoutCart:function(t,e,a){return!1},getDiscountPrice:function(t,e,a){return null}},n=function(e){a=t.extend({},r),"object"==typeof e&&t.extend(a,e)},o=function(){return a};return e.loadOptions=n,e.getOptions=o,e}(),a=function(){var t={},a=function(t){if(isNaN(t))throw new Error("Parameter is not a Number");t*=1;var a=e.getOptions();return t.toFixed(a.numberOfDecimals)};return t.getRoundedNumber=a,t}(),r=function(){var e={};localStorage.products=localStorage.products?localStorage.products:"";var r=function(e){var a=-1,r=i();return t.each(r,function(t,r){if(r.id==e)return void(a=t)}),a},n=function(t){localStorage.products=JSON.stringify(t)},o=function(t,e,a,r,o,c){var d=i();d.push({id:t,name:e,summary:a,price:r,quantity:o,image:c}),n(d)},i=function(){try{var t=JSON.parse(localStorage.products);return t}catch(t){return[]}},c=function(t,e){var a=r(t);if(a<0)return!1;var o=i();return o[a].quantity="undefined"==typeof e?1*o[a].quantity+1:e,n(o),!0},d=function(e,a,r,n,i,d){return"undefined"==typeof e?(console.error("id required"),!1):"undefined"==typeof a?(console.error("name required"),!1):"undefined"==typeof d?(console.error("image required"),!1):t.isNumeric(n)?t.isNumeric(i)?(r="undefined"==typeof r?"":r,void(c(e)||o(e,a,r,n,i,d))):(console.error("quantity is not a number"),!1):(console.error("price is not a number"),!1)},u=function(){n([])},s=function(e){var a=i();a=t.grep(a,function(t,a){return t.id!=e}),n(a)},l=function(){var e=0,a=i();return t.each(a,function(t,a){e+=1*a.quantity}),e},m=function(){var e=i(),r=0;return t.each(e,function(t,e){r+=e.quantity*e.price,r=1*a.getRoundedNumber(r)}),r};return e.getAllProducts=i,e.updatePoduct=c,e.setProduct=d,e.clearProduct=u,e.removeProduct=s,e.getTotalQuantity=l,e.getTotalPrice=m,e}(),n=function(n){var o=e.getOptions(),i=t("."+o.classCartIcon),c=t("."+o.classCartBadge),d=o.classProductQuantity,u=o.classProductRemove,s=o.classCheckoutCart,l="my-cart-modal",m="my-cart-table",y="my-cart-grand-total",f="my-cart-empty-message",g="my-cart-discount-price",p="my-product-total",h="my-cart-icon-affix";o.cartItems&&o.cartItems.constructor===Array&&(r.clearProduct(),t.each(o.cartItems,function(){r.setProduct(this.id,this.name,this.summary,this.price,this.quantity,this.image)})),c.text(r.getTotalQuantity()),t("#"+l).length||t("body").append('<div class="modal fade" id="'+l+'" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"><div class="modal-dialog" role="document"><div class="modal-content"><div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button><h4 class="modal-title" id="myModalLabel"><span class="glyphicon glyphicon-shopping-cart"></span> My Cart</h4></div><div class="modal-body"><table class="table table-hover table-responsive" id="'+m+'"></table></div><div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button><button type="button" class="btn btn-primary '+s+'">Checkout</button></div></div></div></div>');var v=function(){var e=t("#"+m);e.empty();var n=r.getAllProducts();t.each(n,function(){var t=this.quantity*this.price;e.append('<tr title="'+this.summary+'" data-id="'+this.id+'" data-price="'+this.price+'"><td class="text-center" style="width: 30px;"><img width="30px" height="30px" src="'+this.image+'"/></td><td>'+this.name+'</td><td title="Unit Price" class="text-right">'+o.currencySymbol+a.getRoundedNumber(this.price)+'</td><td title="Quantity"><input type="number" min="1" style="width: 70px;" class="'+d+'" value="'+this.quantity+'"/></td><td title="Total" class="text-right '+p+'">'+o.currencySymbol+a.getRoundedNumber(t)+'</td><td title="Remove from Cart" class="text-center" style="width: 30px;"><a href="javascript:void(0);" class="btn btn-xs btn-danger '+u+'">X</a></td></tr>')}),e.append(n.length?'<tr><td></td><td><strong>Total</strong></td><td></td><td></td><td class="text-right"><strong id="'+y+'"></strong></td><td></td></tr>':'<div class="alert alert-danger" role="alert" id="'+f+'">Your cart is empty</div>');var i=o.getDiscountPrice(n,r.getTotalPrice(),r.getTotalQuantity());n.length&&null!==i&&e.append('<tr style="color: red"><td></td><td><strong>Total (including discount)</strong></td><td></td><td></td><td class="text-right"><strong id="'+g+'"></strong></td><td></td></tr>'),C(),x()},b=function(){v(),t("#"+l).modal("show")},P=function(){t.each(t("."+d),function(){var e=t(this).closest("tr").data("id");r.updatePoduct(e,t(this).val())})},C=function(){t("#"+y).text(o.currencySymbol+a.getRoundedNumber(r.getTotalPrice()))},x=function(){t("#"+g).text(o.currencySymbol+a.getRoundedNumber(o.getDiscountPrice(r.getAllProducts(),r.getTotalPrice(),r.getTotalQuantity())))};if(o.affixCartIcon){var T=1*i.offset().top+1*i.css("height").match(/\d+/);i.css("position");t(window).scroll(function(){t(window).scrollTop()>=T?i.addClass(h):i.removeClass(h)})}i.click(function(){o.showCheckoutModal?b():o.clickOnCartIcon(i,r.getAllProducts(),r.getTotalPrice(),r.getTotalQuantity())}),t(document).on("input","."+d,function(){var e=t(this).closest("tr").data("price"),n=t(this).closest("tr").data("id"),i=t(this).val();t(this).parent("td").next("."+p).text(o.currencySymbol+a.getRoundedNumber(e*i)),r.updatePoduct(n,i),c.text(r.getTotalQuantity()),C(),x()}),t(document).on("keypress","."+d,function(t){38!=t.keyCode&&40!=t.keyCode&&t.preventDefault()}),t(document).on("click","."+u,function(){var e=t(this).closest("tr"),a=e.data("id");e.hide(500,function(){r.removeProduct(a),v(),c.text(r.getTotalQuantity())})}),t(document).on("click","."+s,function(){var e=r.getAllProducts();if(!e.length)return void t("#"+f).fadeTo("fast",.5).fadeTo("fast",1);P();var a=o.checkoutCart(r.getAllProducts(),r.getTotalPrice(),r.getTotalQuantity());a!==!1&&(r.clearProduct(),c.text(r.getTotalQuantity()),t("#"+l).modal("hide"))}),t(document).on("click",n,function(){var e=t(this);o.clickOnAddToCart(e);var a=e.data("id"),n=e.data("name"),i=e.data("summary"),d=e.data("price"),u=e.data("quantity"),s=e.data("image");r.setProduct(a,n,i,d,u,s),c.text(r.getTotalQuantity()),o.afterAddOnCart(r.getAllProducts(),r.getTotalPrice(),r.getTotalQuantity())})};t.fn.myCart=function(t){return e.loadOptions(t),n(this.selector),this}})(jQuery);