Authored by ccbikai(👎🏻🍜)

删除多余文件

1 -/**  
2 - * 购物车Logic  
3 - * @author: xuqi<qi.xu@yoho.cn>  
4 - * @date: 2015/10/20  
5 - */  
6 -var $ = require('yoho-jquery'),  
7 - ellipsis = require('yoho-mlellipsis'),  
8 - Hammer = require('yoho-hammer');  
9 -  
10 -var chosePanel = require('./chose-panel'),  
11 - dialog = require('../plugin/dialog'),  
12 - loading = require('../plugin/loading'),  
13 - tip = require('../plugin/tip');  
14 -  
15 -var $cartContent = $('.cart-content'),  
16 - cartType = $('#cartType').val(),  
17 - $cartNav = $('.cart-nav'),  
18 - $cartPage = $('.shopping-cart-page'),  
19 - hasShowCartPresellTip = false;  
20 -  
21 -var navHammer,  
22 - advanceBuyHammer,  
23 - freebieHammer,  
24 - switchChose = false;  
25 -  
26 -var isRefreshByDelete = window.cookie('_yoho-cart-refreshByDelete') === 'true' ? true : false;  
27 -  
28 -var lowStockCount = 0;  
29 -  
30 -// cookie 参数  
31 -var actCkOpthn = {  
32 - path: '/cart/index'  
33 -};  
34 -  
35 -  
36 -loading.showLoadingMask();  
37 -  
38 -function needLoadRecommandForYou() {  
39 - return window.isCookiesEnabled() ?  
40 - $('.cart-goods').length <= 0 && !isRefreshByDelete :  
41 - $('.cart-goods').length <= 0;  
42 -}  
43 -  
44 -  
45 -if (needLoadRecommandForYou()) {  
46 - require('../product/recommend-for-you');  
47 -} else {  
48 - if (isRefreshByDelete) {  
49 - $('body').css('background-color', '#f0f0f0');  
50 - }  
51 - require('./good');  
52 -}  
53 -  
54 -$cartPage.css('display', 'block');  
55 -loading.hideLoadingMask();  
56 -  
57 -window.setCookie('_yoho-cart-refreshByDelete', false);  
58 -  
59 -ellipsis.init();  
60 -  
61 -function cartContentShow() {  
62 - return $cartContent.not('.hide');  
63 -}  
64 -  
65 -function mlellipsisHiddenNames() {  
66 - cartContentShow().find('.shopping-cart-good .name').each(function() {  
67 - this.mlellipsis(2);  
68 - });  
69 - switchChose = true;  
70 -}  
71 -  
72 -function hasChecked() {  
73 - return cartContentShow().find('.cart-goods .icon-cb-checked').length > 0 ? true : false;  
74 -}  
75 -  
76 -function shouldSelectGift() {  
77 - var $freebie = cartContentShow().find('.freebie');  
78 -  
79 - if ($freebie.length <= 0) {  
80 - return false;  
81 - }  
82 -  
83 - return true;  
84 -}  
85 -  
86 -function shouldLowStocks() {  
87 - var $lowStocks,  
88 - result = false;  
89 -  
90 - if (cartType === 'ordinary') {  
91 - $lowStocks = $('.common .low-stocks');  
92 - } else if (cartType === 'advance') {  
93 - $lowStocks = $('.presell .low-stocks');  
94 - }  
95 -  
96 - if ($lowStocks.length <= 0) {  
97 - return result;  
98 - }  
99 -  
100 - $lowStocks.each(function(idx, item) {  
101 - if ($(item).parent().parent().parent().siblings('.checkbox').hasClass('icon-cb-checked')) {  
102 - lowStockCount += 1;  
103 - result = true;  
104 - }  
105 - });  
106 -  
107 - return result;  
108 -}  
109 -  
110 -function showChooseGifDialog() {  
111 - dialog.showDialog({  
112 - dialogText: '您还未选择赠品,是否去选择赠品',  
113 - hasFooter: {  
114 - leftBtnText: '我不要赠品',  
115 - rightBtnText: '去选择'  
116 - }  
117 - }, function() {  
118 - window.location.href = cartContentShow().find('.freebie > a').attr('href');  
119 - }, function() {  
120 - var info = window.cookie('order-info');  
121 -  
122 - if (info) {  
123 - window.setCookie('order-info', '', actCkOpthn);  
124 - }  
125 - window.location.href = '/cart/index/orderEnsure?cartType=' + cartType;  
126 - });  
127 -}  
128 -  
129 -if (typeof window.cookie === 'function' && window.cookie('_hasShowCartPresellTip') === 'y') {  
130 - $('#presell-tip').removeClass('show').addClass('hide');  
131 - hasShowCartPresellTip = true;  
132 -} else {  
133 - $('#presell-tip').removeClass('hide').addClass('show');  
134 -}  
135 -  
136 -if (cartType === 'advance') {  
137 - $cartContent.toggleClass('hide');  
138 - $('#common-cart-nav').removeClass('active');  
139 - $('#presell-cart-nav').addClass('active');  
140 -  
141 - mlellipsisHiddenNames();  
142 - $(window).trigger('scroll');  
143 -}  
144 -  
145 -if ($cartNav.length > 0) {  
146 - navHammer = new Hammer(document.getElementsByClassName('cart-nav')[0]);  
147 - navHammer.on('tap', function(e) {  
148 - var $this = $(e.target).closest('li');  
149 -  
150 - if ($this.hasClass('active')) {  
151 - return;  
152 - }  
153 -  
154 - if (cartType === 'ordinary') {  
155 - cartType = 'advance';  
156 - $('#cartType').val('advance');  
157 - } else {  
158 - cartType = 'ordinary';  
159 - $('#cartType').val('ordinary');  
160 - }  
161 -  
162 - $this.siblings('.active').removeClass('active');  
163 - $this.addClass('active');  
164 -  
165 - // 切换普通商品和预售商品购物车显示  
166 - $cartContent.toggleClass('hide');  
167 -  
168 - if (switchChose === false) {  
169 - mlellipsisHiddenNames();  
170 - }  
171 -  
172 - // trigger lazyload  
173 - $(window).trigger('scroll');  
174 - });  
175 -  
176 - if (!hasShowCartPresellTip) {  
177 - setTimeout(function() {  
178 - $('#presell-tip').removeClass('show').addClass('hide');  
179 - window.setCookie('_hasShowCartPresellTip', 'y');  
180 - }, 3000);  
181 - }  
182 -}  
183 -  
184 -if ($('.advance-buy').length > 0) {  
185 - advanceBuyHammer = new Hammer(document.getElementsByClassName('advance-buy')[0]);  
186 - advanceBuyHammer.on('tap', function() {  
187 - window.location.href = '/cart/index/advanceBuy?cartType=' + cartType;  
188 - });  
189 -}  
190 -  
191 -if ($('.freebie').length > 0) {  
192 - freebieHammer = new Hammer(document.getElementsByClassName('freebie')[0]);  
193 - freebieHammer.on('tap', function() {  
194 - window.location.href = '/cart/index/gift?cartType=' + cartType;  
195 - });  
196 -}  
197 -  
198 -$('.btn-balance').on('touchend', function() {  
199 - var info = window.cookie('order-info');  
200 -  
201 - lowStockCount = 0;  
202 -  
203 - if (shouldLowStocks()) {  
204 - tip.show('所选商品中有' + lowStockCount + '种库存不足的商品');  
205 - return false;  
206 - }  
207 -  
208 - if (shouldSelectGift()) {  
209 - showChooseGifDialog();  
210 - return false;  
211 - }  
212 -  
213 - if (hasChecked()) {  
214 - if (info) {  
215 - window.setCookie('order-info', '', actCkOpthn);  
216 - }  
217 - window.location.href = '/cart/index/orderEnsure?cartType=' + cartType;  
218 - } else {  
219 - tip.show('请先勾选商品');  
220 - }  
221 -});  
222 -  
223 -$('.off-shell-goods').on('touchstart touchend', function(e) {  
224 - var $this = $(e.target).closest('span');  
225 -  
226 - if ($this.hasClass('icon-del')) {  
227 - return;  
228 - }  
229 -  
230 - tip.show('商品已下架');  
231 - return false;  
232 -});  
233 -  
234 -$('.chose').on('touchend', function() {  
235 - chosePanel.show();  
236 -});  
237 -  
238 -$cartNav.on('touchstart', 'li', function() {  
239 - $(this).addClass('bytouch');  
240 -}).on('touchend touchcancel', function() {  
241 - $cartNav.find('li').removeClass('bytouch');  
242 -});  
243 -  
244 -// 提前触发lazyload  
245 -$(window).scrollTop(1).scrollTop(0);  
1 -/**  
2 - * 购物车商品  
3 - * @author: xuqi<qi.xu@yoho.cn>  
4 - * @date: 2015/10/20  
5 - */  
6 -  
7 -var $ = require('jquery'),  
8 - ellipsis = require('mlellipsis'),  
9 - chosePanel = require('../cart/chose-panel'),  
10 - loading = require('../plugin/loading'),  
11 - lazyLoad = require('yoho.lazyload');  
12 -  
13 -var dialog = require('../me/dialog'),  
14 - tip = require('../plugin/tip');  
15 -  
16 -var $selectAllBtn = $('.balance .checkbox'),  
17 - requesting = false;  
18 -  
19 -// 上次编辑的商品skn  
20 -var previousEditSkn;  
21 -  
22 -ellipsis.init();  
23 -  
24 -lazyLoad({  
25 - try_again_css: 'order-failure'  
26 -});  
27 -  
28 -setTimeout(function() {  
29 - $('.shopping-cart-good .name').each(function() {  
30 - this.mlellipsis(2);  
31 - });  
32 -}, 0);  
33 -  
34 -function GoodInfo(properties) {  
35 - this.goods_type = properties.goods_type;  
36 - this.buy_number = properties.buy_number;  
37 - this.product_sku = properties.product_sku;  
38 - this.selected = properties.selected;  
39 -}  
40 -  
41 -// TIP:事件委托在.cart-goods,商品列表的容器统一需要有.cart-goods  
42 -$('.cart-goods').on('touchstart', '.checkbox', function() {  
43 - var $this = $(this),  
44 - $good = $this.closest('.shopping-cart-good'),  
45 - id = $good.data('id');  
46 -  
47 - var goodsList = [],  
48 - goodInfo = {};  
49 -  
50 - if (!$this.hasClass('icon-cb-checked') && $this.siblings('.info').find('.low-stocks').length > 0) {  
51 - tip.show('库存不足,无法购买');  
52 - return false;  
53 - }  
54 -  
55 - goodInfo.goods_type = $('#cartType').val();  
56 - goodInfo.selected = $this.hasClass('icon-cb-checked') ? 'N' : 'Y';  
57 - goodInfo.product_sku = id;  
58 - goodInfo.buy_number = $good.find('.count').eq(0).text().trim().replace('×', '');  
59 - goodsList.push(new GoodInfo(goodInfo));  
60 -  
61 - $.ajax({  
62 - type: 'post',  
63 - url: 'select',  
64 - data: {  
65 - skuList: JSON.stringify(goodsList)  
66 - }  
67 - }).then(function(data) {  
68 - if (data.code === 200) {  
69 - if ($this.hasClass('icon-cb-checked')) {  
70 - $this.removeClass('icon-cb-checked').addClass('icon-checkbox');  
71 - } else {  
72 - $this.removeClass('icon-checkbox').addClass('icon-cb-checked');  
73 - }  
74 - window.location.href = '/cart/index/index?cartType=' + $('#cartType').val();  
75 - } else if (data.code === 400) {  
76 - tip.show('网络异常');  
77 - }  
78 - }, function() {  
79 - tip.show('网络异常');  
80 - });  
81 -});  
82 -  
83 -/*  
84 - * 显示购物车编辑面板。  
85 - *  
86 - * @param {String} html. chose-pannel模板,由服务端返回  
87 - *  
88 - * @param {String} id. 商品skuid  
89 - *  
90 - * @param {Bool} isSelected. 所要编辑的商品是否被选中  
91 - *  
92 - * @param {Bool} isEditNum. 所要编辑的商品是否被选中  
93 - *  
94 - * @return false or undefined  
95 - *  
96 - */  
97 -function showEditPannelWithSku(html, id, isSelected, isEditNum) {  
98 - if (html.length < 2) {  
99 - tip.show('出错啦!');  
100 - return false;  
101 - }  
102 -  
103 - // 删掉页面上原有的pannel  
104 - chosePanel.remove();  
105 -  
106 - $(html).appendTo('#mainCart');  
107 -  
108 -  
109 - chosePanel.init();  
110 - chosePanel.setEditModeWithSknId(id, isSelected);  
111 -  
112 - if (!isEditNum) {  
113 - chosePanel.disableNumEdit();  
114 - }  
115 -  
116 - chosePanel.show();  
117 -}  
118 -  
119 -  
120 -$('.icon-edit').on('touchstart', function(e) {  
121 - var $this = $(this),  
122 - skn = $this.closest('.shopping-cart-good').data('skn');  
123 -  
124 - var $checkBox,  
125 - $tag;  
126 -  
127 - var id,  
128 - count,  
129 - canEditNum;  
130 -  
131 - if ($this.parents('.off-shell-goods').length) {  
132 - tip.show('商品已下架');  
133 - return false;  
134 - }  
135 -  
136 - // 如果点击的是上次编辑的商品,直接显示chose-pannel  
137 - if (skn === previousEditSkn) {  
138 - chosePanel.show();  
139 - return;  
140 - }  
141 -  
142 - previousEditSkn = skn;  
143 -  
144 -  
145 - $checkBox = $this.closest('.info').siblings('.checkbox');  
146 - $tag = $this.closest('.deps').siblings('.few-tag');  
147 -  
148 - id = $this.closest('.shopping-cart-good').data('id');  
149 - count = $this.data('count');  
150 -  
151 - // 加价购或者赠品不能编辑数量  
152 - canEditNum = $tag.hasClass('gift-tag') || $tag.hasClass('plus-tag') ? false : true;  
153 -  
154 - e.stopPropagation();  
155 -  
156 - loading.showLoadingMask();  
157 -  
158 -  
159 - $.ajax({  
160 - url: '/cart/index/goodinfo',  
161 - data: {  
162 - skn: skn,  
163 - buy_num: count  
164 - },  
165 - success: function(res) {  
166 - showEditPannelWithSku(res, id, $checkBox.hasClass('icon-cb-checked'), canEditNum);  
167 - },  
168 - error: function() {  
169 - tip.show('网络异常');  
170 - window.location.reload();  
171 - },  
172 - complete: function() {  
173 - loading.hideLoadingMask();  
174 - }  
175 - });  
176 -});  
177 -  
178 -$('.icon-del').on('touchstart', function(e) {  
179 - var $this = $(this);  
180 -  
181 - e.stopPropagation();  
182 -  
183 - dialog.showDialog({  
184 - dialogText: '您确定要从购物车中删除吗?',  
185 - hasFooter: {  
186 - leftBtnText: '取消',  
187 - rightBtnText: '确定'  
188 - }  
189 - }, function() {  
190 - var $shoppingCartGood = $this.closest('.shopping-cart-good'),  
191 - count = $this.data('count');  
192 -  
193 - $.ajax({  
194 - method: 'post',  
195 - url: '/cart/index/del',  
196 - data: {  
197 - sku: $shoppingCartGood.data('id'),  
198 - count: count,  
199 - promotionId: $shoppingCartGood.data('promotion')  
200 - }  
201 - }).then(function(data) {  
202 - if (data.code === 200) {  
203 - dialog.showDialog({  
204 - dialogText: '删除成功',  
205 - autoHide: true,  
206 - fast: true  
207 - });  
208 - window.setCookie('_yoho-cart-refreshByDelete', true);  
209 - window.location.href = '/cart/index/index?cartType=' + $('#cartType').val();  
210 - } else {  
211 - tip.show(data.message);  
212 - }  
213 - }, function() {  
214 - tip.show('网络异常');  
215 - window.location.reload();  
216 - });  
217 - });  
218 -});  
219 -  
220 -function requestUpdateAllGoodsCheckStatus(theGoods, successHandeler) {  
221 - if (requesting) {  
222 - return;  
223 - }  
224 - requesting = true;  
225 - $.ajax({  
226 - url: 'select',  
227 - type: 'post',  
228 - data: {  
229 - skuList: JSON.stringify(theGoods)  
230 - },  
231 - success: function(res) {  
232 - if (res.code === 200) {  
233 - successHandeler();  
234 - } else {  
235 - tip.show(res.message);  
236 - }  
237 - },  
238 - error: function() {  
239 - tip.show('网络异常');  
240 - },  
241 - complete: function() {  
242 - requesting = false;  
243 - }  
244 - });  
245 -}  
246 -  
247 -function didUpdateAllGoodsCheckStatus() {  
248 - var $checkedBoxs = $('.shopping-cart-good .icon-cb-checked'),  
249 - $uncheckedBoxs = $('.shopping-cart-good .icon-checkbox');  
250 -  
251 - var shouldSelectAll;  
252 -  
253 - if ($selectAllBtn.hasClass('icon-cb-checked')) {  
254 - $selectAllBtn.removeClass('icon-cb-checked').addClass('icon-checkbox');  
255 - shouldSelectAll = true;  
256 - } else {  
257 - $selectAllBtn.removeClass('icon-checkbox').addClass('icon-cb-checked');  
258 - shouldSelectAll = false;  
259 - }  
260 -  
261 - if (!shouldSelectAll) {  
262 - $uncheckedBoxs.each(function(idx, uncheckedBox) {  
263 - $(uncheckedBox).removeClass('icon-checkbox').addClass('icon-cb-checked');  
264 - });  
265 - } else {  
266 - $checkedBoxs.each(function(idx, checkedBox) {  
267 - $(checkedBox).removeClass('icon-cb-checked').addClass('icon-checkbox');  
268 - });  
269 - }  
270 - window.location.href = '/cart/index/index?cartType=' + $('#cartType').val();  
271 -}  
272 -  
273 -function bottomCheckBoxHandeler(isSelected, type, handlerAfterTouch) {  
274 - var goodInfo = {},  
275 - $goods = $('.cart-content:not(.hide) .shopping-cart-good'),  
276 - $good = null,  
277 - goodsList = [];  
278 -  
279 - goodInfo.goods_type = type;  
280 - goodInfo.selected = isSelected ? 'N' : 'Y';  
281 -  
282 - $goods.each(function(idx, good) {  
283 - $good = $(good);  
284 -  
285 - goodInfo.product_sku = $(good).data('id');  
286 - goodInfo.hasPromotion = $(good).data('promotion');  
287 - goodInfo.buy_number = $good.find('.count').eq(0).text().trim().replace('×', '');  
288 -  
289 - goodsList.push(new GoodInfo(goodInfo));  
290 - });  
291 -  
292 - requestUpdateAllGoodsCheckStatus(goodsList, handlerAfterTouch);  
293 -}  
294 -  
295 -// 全选按钮点击事件  
296 -$selectAllBtn.on('touchend', function() {  
297 - var cartType = $('#cartType').val(),  
298 - isSelect = $(this).hasClass('icon-cb-checked');  
299 -  
300 - if (cartType === 'ordinary') {  
301 - if (!isSelect && $('.common .low-stocks').length > 0) {  
302 - tip.show('所选商品中含有库存不足的商品');  
303 - return false;  
304 - }  
305 - } else if (cartType === 'advance') {  
306 - if (!isSelect && $('.presell .low-stocks').length > 0) {  
307 - tip.show('所选商品中含有库存不足的商品');  
308 - return false;  
309 - }  
310 - }  
311 -  
312 - bottomCheckBoxHandeler(isSelect, cartType, didUpdateAllGoodsCheckStatus);  
313 -});  
314 -  
315 -$('.down').on('touchend', function() {  
316 - chosePanel.show();  
317 -});