/// /// ec.cart = { api: "", datas: [], init: function (_datas) { if (_datas) { this.datas = _datas; this.render(); } this.api = new ec.api("cart"); }, render: function () { var selected = this.getSelected(); var hasData = this.datas.length > 0; var hasSelected = selected.length > 0; var isCart = window.location.href.indexOf("/cart") > -1; var amount = 0; if (isCart) { var total = 0; var selAmount = 0; selected.forEach(function (d) { selAmount += d.Amount; }); if (selected.length>0) this.calculate(selected); else $("#cart_total").html("0.00"); var selectAll = true; this.datas.forEach(function (d) { if (!d.selected) selectAll = false; }); $("#cart").html(ke.template.get("cart_html").renderForeach(this.datas)); $("#cart_notfind").toggle(!hasData); $("#cart_amount_sel").html(selAmount); $(".select-del").toggle(hasSelected); $(".btn-area .btn1").toggleClass("btn-disable", !hasSelected); $(".select-all").find("i").toggleClass("ico-checkbox-hover", selectAll); $(".cart-box").toggle(hasData); } $("#cart_head").html(ke.template.get("cart_head_html").render({ arr: this.datas.slice(0, 5) })); $("#cart_head").toggle(hasData); $("#cart_head_notfind").toggle(!hasData); this.datas.forEach(function (d) { amount += d.Amount; }); $(".cart-amount").html(amount); }, getSelected: function () { return this.datas.get({ selected: true }, false, true); }, select: function (priceRid) { var item = this.datas.get({ ProductPriceRID: priceRid }); item.selected = !item.selected; this.render(true); }, selectAll: function (r) { this.datas.forEach(function (d) { d.selected = r; }); this.render(true); }, calculate: function (info) { var data = []; for (var i = 0; i < info.length;i++){ data.push(info[i].RID); } this.api.post("calculate", data, function (r) { $("#cart_total").html(r.toFixed(2)); }); }, add: function (info, callbck) { this.api.post("insert", info, function (r) { ec.cart.refresh(r); if (callbck) callbck(r); }); }, refresh: function (r) { r.forEach(function (d) { var temp = ec.cart.datas.get({ ProductPriceRID: d.ProductPriceRID }); d.selected = (temp && temp.selected); }); ec.cart.datas = r; ec.cart.render(); }, askRemove: function (priceRid) { var p = [priceRid]; if (!priceRid) { p.length = 0; this.getSelected().forEach(function (d) { p.push(d.ProductPriceRID); }); }; var callbck = function () { ec.cart.remove(p, true); }; ec.confirm("" + L.G("确定要删除吗?"), L.G("将选中的产品从购物车中移除"), callbck); }, remove: function (p, isCart) { ec.cart.api.post("delete", p, function (r) { if (isCart) ec.notify(L.G("删除成功")); ec.cart.refresh(r); }); }, alterTime: function (priceRid, amount, mins) { clearTimeout(ec.cart.timer); ec.cart.timer = setTimeout(function () { ec.cart.alter(priceRid, amount, mins); }, 300); }, alter: function (priceRid, amount, mins) { var item = this.datas.get({ ProductPriceRID: priceRid }); amount = ((parseInt(amount) > 0 && parseInt(amount) <= 100) ? parseInt(amount) : item.Amount); if (amount > 99) { ec.notify(L.G("数量应在1-100之间"),"error"); ec.cart.refresh(this.datas); return; } amount = amount - item.Amount; amount = (mins || amount); item.Amount = amount; var info = []; info.push(item); this.add(info); }, confirm: function () { var list = this.getSelected(); if (list.length) window.location.href = L.route + "/order/confirm?cartIds=" + list.joinField('RID'); } }