feature-selector.vue
13.6 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
<template>
<div class="feature-selector" :class="{ 'slide-in': isVisible }">
<div class="header">
<div id="image-box" class="image-box">
<img :src="selection.thumbnail | resize 45 60"/>
</div>
<div class="text-box">
<h3 class="line-clamp-2">{{entity.productName}}</h3>
<h4>{{entity.formatSalesPrice !== '0' ? entity.formatSalesPrice : entity.formatMarketPrice}}</h4>
</div>
</div>
<hr>
<div>
<section>
<h4>颜色</h4>
<feature-options name="color" :options="colors" :selection="selection.color"></feature-options>
</section>
<section>
<h4>尺码</h4>
<feature-options name="size" :options="sizes" :selection="selection.size"></feature-options>
</section>
<button @click="addToCart()"
class="button button-solid add-to-cart">{{buttonText || '加入购物袋'}}
</button>
</div>
</div>
</template>
<style>
.feature-selector {
background: #fff;
width: 100%;
min-height: 608px;
bottom: 0;
position: fixed;
padding: 20px 30px;
z-index: 1001;
transform: translate3d(0, 100%, 0);
transition: all 0.1s ease-in-out;
.header {
height: 120px;
h3 {
margin: 0;
font-weight: 300;
}
h4 {
color: #b0b0b0;
font-weight: 200;
font-size: 30px;
margin-top: 32px;
margin-bottom: 0;
}
.image-box {
width: 90px;
height: 120px;
display: inline-block;
opacity: 1;
}
.text-box {
display: inline-block;
margin-left: 24px;
max-width: 512px;
}
}
hr {
border: none;
border-top: 1px solid #f0f0f0;
margin-top: 30px;
margin-bottom: 20px;
}
ul {
list-style: none;
border: none;
margin-left: 18px;
margin-bottom: 0;
padding: 0;
max-width: 600px;
}
li {
display: inline-block;
}
section {
margin-top: 30px;
h4 {
float: left;
margin-top: 20px;
font-size: 25px;
line-height: 88px;
display: inline-block;
}
}
.add-to-cart {
width: 100%;
margin-top: 50px;
font-size: 27px;
}
&.slide-in {
transform: translate3d(0, 0, 0);
}
}
.rotate-animation-target {
animation: rotate-animation 2000ms linear both;
}
/* Generated with Bounce.js. Edit at http://bouncejs.com#%7Bs%3A%5B%7BT%3A%22t%22%2Ce%3A%22b%22%2Cd%3A2000%2CD%3A0%2Cf%3A%7Bx%3A0%2Cy%3A0%7D%2Ct%3A%7Bx%3A10%2Cy%3A290%7D%2Cs%3A3%2Cb%3A4%7D%2C%7BT%3A%22r%22%2Ce%3A%22b%22%2Cd%3A2000%2CD%3A0%2Cf%3A0%2Ct%3A360%2Cs%3A3%2Cb%3A4%7D%2C%7BT%3A%22c%22%2Ce%3A%22b%22%2Cd%3A2000%2CD%3A0%2Cf%3A%7Bx%3A1%2Cy%3A1%7D%2Ct%3A%7Bx%3A0.01%2Cy%3A0.01%7D%2Cs%3A3%2Cb%3A4%7D%5D%7D */
@keyframes rotate-animation {
0% {
-webkit-transform: matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
transform: matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
}
4.3% {
-webkit-transform: matrix3d(-0.489, -0.044, 0, 0, 0.044, -0.489, 0, 0, 0, 0, 1, 0, 5.143, 149.138, 0, 1);
transform: matrix3d(-0.489, -0.044, 0, 0, 0.044, -0.489, 0, 0, 0, 0, 1, 0, 5.143, 149.138, 0, 1);
}
8.61% {
-webkit-transform: matrix3d(0.094, -0.09, 0, 0, 0.09, 0.094, 0, 0, 0, 0, 1, 0, 8.786, 254.79, 0, 1);
transform: matrix3d(0.094, -0.09, 0, 0, 0.09, 0.094, 0, 0, 0, 0, 1, 0, 8.786, 254.79, 0, 1);
}
12.91% {
-webkit-transform: matrix3d(-0.04, -0.013, 0, 0, 0.013, -0.04, 0, 0, 0, 0, 1, 0, 10.523, 305.16, 0, 1);
transform: matrix3d(-0.04, -0.013, 0, 0, 0.013, -0.04, 0, 0, 0, 0, 1, 0, 10.523, 305.16, 0, 1);
}
17.22% {
-webkit-transform: matrix3d(-0.069, -0.046, 0, 0, 0.046, -0.069, 0, 0, 0, 0, 1, 0, 10.933, 317.067, 0, 1);
transform: matrix3d(-0.069, -0.046, 0, 0, 0.046, -0.069, 0, 0, 0, 0, 1, 0, 10.933, 317.067, 0, 1);
}
28.33% {
-webkit-transform: matrix3d(-0.01, -0.001, 0, 0, 0.001, -0.01, 0, 0, 0, 0, 1, 0, 10.206, 295.982, 0, 1);
transform: matrix3d(-0.01, -0.001, 0, 0, 0.001, -0.01, 0, 0, 0, 0, 1, 0, 10.206, 295.982, 0, 1);
}
39.44% {
-webkit-transform: matrix3d(0.016, -0.001, 0, 0, 0.001, 0.016, 0, 0, 0, 0, 1, 0, 9.938, 288.193, 0, 1);
transform: matrix3d(0.016, -0.001, 0, 0, 0.001, 0.016, 0, 0, 0, 0, 1, 0, 9.938, 288.193, 0, 1);
}
61.66% {
-webkit-transform: matrix3d(0.01, 0, 0, 0, 0, 0.01, 0, 0, 0, 0, 1, 0, 10.004, 290.121, 0, 1);
transform: matrix3d(0.01, 0, 0, 0, 0, 0.01, 0, 0, 0, 0, 1, 0, 10.004, 290.121, 0, 1);
}
83.98% {
-webkit-transform: matrix3d(0.01, 0, 0, 0, 0, 0.01, 0, 0, 0, 0, 1, 0, 10, 289.992, 0, 1);
transform: matrix3d(0.01, 0, 0, 0, 0, 0.01, 0, 0, 0, 0, 1, 0, 10, 289.992, 0, 1);
}
100% {
-webkit-transform: matrix3d(0.01, 0, 0, 0, 0, 0.01, 0, 0, 0, 0, 1, 0, 10, 290, 0, 1);
transform: matrix3d(0.01, 0, 0, 0, 0, 0.01, 0, 0, 0, 0, 1, 0, 10, 290, 0, 1);
}
}
</style>
<script>
/**
* 颜色尺码选择组件
*
* @author: Aiden Xu<aiden.xu@yoho.cn>
* @date: 2016/07/20
*/
const $ = require('jquery');
const tip = require('common/tip');
const Overlay = require('common/overlay');
/**
* 颜色尺码选择组件
**
* @event feature.close 关闭事件
* @event feature:color.select 选择颜色
* @event feature:size.select 选择尺码
*/
module.exports = {
init() {
},
data() {
return {
colors: [],
sizes: [],
colorSizes: {},
thumbnails: {},
selection: {
color: null,
size: null,
thumbnail: ''
}
};
},
props: {
/** 是否可见 */
isVisible: Boolean,
/** 实体对象 */
entity: Object,
/**
* 加入购物车回调函数
*
* @param result 后端返回的数据
*/
onAddToCart: Function,
buttonText: String
},
watch: {
isVisible() {
if (this.isVisible) {
this.overlay.show();
} else {
this.overlay.hide();
this.$parent.$emit('feature.close');
}
},
entity() {
const thumbnails = {};
const selection = {};
const colorSizes = {};
const stocks = {};
const self = this;
// 更新颜色
this.colors = this.entity.goodsList.filter((goods)=> {
// 确保商品启用
return goods.status !== 0;
}).map((goods)=> {
// 缩略图
thumbnails[goods.colorId] = goods.colorImage;
// 生成colorId 与 size的 映射
colorSizes[goods.colorId] = goods.sizeList.map((size)=> {
if (!stocks[goods.colorId]) {
stocks[goods.colorId] = 0;
}
// 默认选中有库存的第一个颜色尺码
if (size.storageNumber > 0) {
if (!selection.color) {
self.selection.color = selection.color = {
text: goods.colorName,
value: goods.colorId,
disabled: false
};
}
if (!selection.size && size.storageNumber > 0) {
self.selection.size = selection.size = {
text: size.sizeName,
value: size.productSku,
goodsId: goods.goodsId,
disabled: false
};
}
}
// 计算所有尺码的库存
stocks[goods.colorId] += size.storageNumber;
return {
text: size.sizeName,
value: size.productSku,
disabled: size.storageNumber === 0,
goodsId: goods.goodsId
};
});
return {
text: goods.colorName,
value: goods.colorId,
disabled: stocks[goods.colorId] === 0 // 是否售完
};
});
if (!selection.color) {
this.selection.color = selection.color = this.colors[0];
}
this.sizes = colorSizes[selection.color.value];
this.colorSizes = colorSizes;
this.thumbnails = thumbnails;
// 选择默认值
this.$emit('feature:color.select', selection.color);
this.$emit('feature:size.select', selection.size);
}
},
components: {
featureOptions: require('./feature-options.vue')
},
created() {
const self = this;
this.overlay = new Overlay({
disableScrolling: false,
onClose: function() {
self.isVisible = false;
}
});
// 选择颜色
this.$on('feature:color.select', (opt)=> {
let setSelectedSize = function(color, size) {
const sizes = self.colorSizes[color.value];
if (!size) {
let canSelectSizes = sizes.filter(item => {
return item.disabled === false;
});
return canSelectSizes.length ? canSelectSizes[0] : null;
} else {
// 切换颜色后选择匹配的尺码
if (sizes && sizes.length > 0) {
const oldSizes = sizes.filter((item) => {
return item.value === size.value;
});
if (oldSizes && oldSizes.length > 0) {
const newSizes = self.colorSizes[opt.value];
const matchedSize = newSizes.filter((item) => {
return !item.disabled && item.text === oldSizes[0].text;
});
if (matchedSize && matchedSize.length > 0) {
return matchedSize[0];
}
}
}
}
};
const selection = {
color: opt,
size: setSelectedSize(self.selection.color, self.selection.size),
thumbnail: this.thumbnails[opt.value]
};
this.sizes = this.colorSizes[opt.value];
Object.assign(this.selection, selection);
});
// 选择尺码
this.$on('feature:size.select', (opt)=> {
const selection = {
size: opt
};
Object.assign(this.selection, selection);
});
},
methods: {
/**
* 将当前选择添加到购物车
*/
addToCart() {
// console.log(`${this.selection.color}:${this.selection.size}`);
if (!this.selection.color) {
tip('请选择颜色');
return;
}
if (!this.selection.size) {
tip('请选择尺码');
return;
}
const param = Object.assign({goodsId: this.entity.productId}, this.selection);
this.onAddToCart(param, this);
},
/**
* 播放加入购物车动画
*/
playAnimation() {
const imageBox = $('#image-box');
const pos = imageBox.offset();
const clone = imageBox.clone();
clone[0].addEventListener('webkitAnimationEnd', ()=> {
clone.remove();
});
clone.addClass('rotate-animation-target').appendTo('body').css({
top: pos.top,
left: pos.left,
position: 'absolute',
'z-index': 1002
});
}
}
};
</script>