index.page.js
4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/**
* 购物车商品
* @author: feng.chen<feng.chen@yoho.cn>
* @date: 2016/12/22
*/
'use strict';
let cookie = require('yoho-cookie');
require('cart/index.page.css');
require('common');
let $ = require('yoho-jquery'),
hbsTemplate = require('cart/index/index.hbs'),
lazyLoad = require('yoho-jquery-lazyload'),
loading = require('plugin/loading');
let indexObj = {
cartData: {},
editMode: false,
posting: false,
init() {
let self = this;
self.setCartReferrer();
$('.nav-btn').on('click', () => {
self.navBtnClick();
});
$('.nav-back').on('click', () => {
self.navBackClick();
});
let promise = self.refreshPage();
if (promise) {
promise.then(() => {
require('./index/recommend-for-you').init(self);
});
}
},
setCartReferrer() {
let referrer = document.referrer || window.queryString.referrer || '';
let localUrl = window.location.href.replace(/\/?\?.*$/, '').replace(/https?:\/\//, '');
let referrerPath = referrer.replace(/\/?\?.*$/, '').replace(/https?:\/\//, '');
if (referrer && localUrl !== referrerPath &&
referrer.indexOf('orderEnsure') === -1 && referrer.indexOf('/gift') === -1 &&
referrer.indexOf('/advanceBuy') === -1 && referrer.indexOf('/product/index/index?promotion_id=') === -1) {
cookie.set('_cartReferrer', referrer);
}
},
navBtnClick() {
let self = this;
$('.main-wrap').toggleClass('edit');
if ($('.main-wrap').hasClass('edit')) {
self.editMode = true;
$('.chk.edit').removeClass('checked');
} else {
self.editMode = false;
}
},
navBackClick() {
let referrer = cookie.get('_cartReferrer');
if (referrer && referrer !== window.location.href) {
cookie.remove('_cartReferrer');
if (referrer === document.referrer || referrer === window.queryString.referrer) {
window.history.go(-1);
} else {
window.location.href = referrer;
}
} else {
window.location.href = '/?go=1';
}
},
getCartData() {
return $.ajax({
url: '/cart/index/new/data',
type: 'POST'
});
},
refreshPage(data) {
let self = this;
if (!data) {
loading.showLoading();
return self.getCartData().then(cartData => {
return self.renderPage(cartData.data, true).then(() => {
loading.hideLoading();
});
});
}
return self.renderPage(data);
},
renderPage(data, no_try) {
let self = this;
if ((!data || (!data.commonCart && !data.preSellCart)) && no_try !== true) {
return self.refreshPage();
}
let cartType = cookie.get('_cartType') || 'ordinary';
// 普通购物车空,则显示预售购物车
if (data.commonGoodsCount === 0 && data.ordinarySoldOut === 0) {
data.cartNav = false;
data.cartType = 'advance';
self.cartData = data.preSellCart;
} else if (data.presellGoodsCount === 0 && data.advanceSoldOut === 0) { // 预售购物车空,则显示普通购物车
data.cartNav = false;
data.cartType = 'ordinary';
self.cartData = data.commonCart;
} else { // 以上两个购物车中都有数据, 默认显示普通购物车
data.cartNav = true;
data.cartType = cartType || 'ordinary';
self.cartData = data.commonCart;
}
cookie.set('_cartType', data.cartType);
data.ordinaryCart = data.cartType === 'ordinary';
data.advanceCart = !data.ordinaryCart;
if (data.isEmptyCart) {
$('.nav-btn').hide();
} else {
$('.nav-btn').show();
}
$('.cart-box').html(hbsTemplate(data));
require('./index/cart').init(self);
require('./index/good').init(self);
$('.btn-edit').on('click', () => {
self.navBtnClick();
});
lazyLoad($('img.lazy'));
return Promise.resolve();
}
};
$(() => {
indexObj.init();
});