widget-icon-btn.vue
7.79 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
<template>
<div class="icon-btn" :class="{'btn-selected': viewOption.selected}" :style="btnStyle">
<AuthComponent :auth="isAuth" class="click-wrap" @click="onClick"></AuthComponent>
<i class="iconfont" :class="iconClass" :style="iconStyle"></i>
<p v-if="viewText" class="icon-btn-text" :style="textStyle">
<span class="view-text">{{Number(viewText) ? viewText : ''}}</span>
<span class="placeholder-text">{{placText}}</span>
</p>
</div>
</template>
<script>
import {forEach, fill} from 'lodash';
import {createNamespacedHelpers} from 'vuex';
const {mapActions} = createNamespacedHelpers('user');
const {mapActions: articleMapActions} = createNamespacedHelpers('article');
import cookie from 'yoho-cookie';
const classMap = {
fav: {
default: 'icon-zan',
selected: 'icon-zan-fill',
boldDefault: 'icon-zan-bold'
},
star: {
default: 'icon-star',
selected: 'icon-star-fill'
},
share: {
default: 'icon-share'
},
msg: {
default: 'icon-msg'
}
};
const defaultOption = {
canSelect: true, // 是否支持选中
selected: false, // 初始选中状态(不受是否支持选中控制)
color: '#444', // btn字体颜色
selectedColor: '#d90025', // btn选中状态字体颜色(不设置默认与非选中一致)
iconBold: false, // icon粗体
iconFontSize: 48, // icon字号(单位px)
textSelectedColor: '#444', // text选中状态字体颜色(不设置默认与selectedColor一致)
textFontSize: 20, // text字号(单位px)
textAlign: 'top', // text位置, 默认normal(支持normal, top, bottom)
textZoom: 0.9, // text缩放
textAutoChange: true, // text自动增减,只支持number类型(受是否支持选中控制)
emitName: '' // 点击触发事件名称
};
export default {
name: 'WidgetIconBtn',
props: {
type: {
type: String,
default: 'fav'
},
text: {
type: [String, Number],
default() {
return '';
}
},
articleId: Number,
commentId: Number,
option: {
type: Object,
default() {
return defaultOption;
}
},
share: Boolean
},
data() {
return {
currentClass: {},
viewOption: {},
actionClass: '',
editText: null
};
},
created() {
this.setOptions();
this.setCurrentClass();
},
computed: {
syncFnName() {
if (this.articleId) {
if (this.type === 'fav') {
return 'praiseArticle';
} else if (this.type === 'star') {
return 'followArticle';
}
} else if (this.commentId && this.type === 'fav') {
return 'praiseComment';
}
},
isAuth() {
return !!this.syncFnName;
},
btnStyle() {
let color = this.viewOption.color || defaultOption.color;
return `color: ${this.viewOption.selected ? (this.viewOption.selectedColor || color) : color};`;
},
iconClass() {
if (this.actionClass) {
return this.actionClass;
}
return this.viewOption.selected ? this.currentClass.selected : this.currentClass.default;
},
iconStyle() {
return `font-size: ${this.pxToRem(this.viewOption.iconFontSize)};`;
},
textStyle() {
let style = `font-size: ${this.pxToRem(this.viewOption.textFontSize)};`;
let textAlign = this.viewOption.textAlign;
if (['top', 'bottom'].indexOf(textAlign) >= 0) {
style += ` vertical-align: ${textAlign};`;
}
let textZoom = this.viewOption.textZoom;
if (Number(textZoom) !== NaN) {
style += ` transform: scale(${textZoom}, ${textZoom});`;
}
if (this.viewOption.textSelectedColor) {
style += ` color: ${this.viewOption.textSelectedColor};`;
}
return style;
},
viewText() {
return `${(this.editText === null ? this.text : this.editText)}`;
},
placText() {
if (!isNaN(Number(this.text)) && this.viewText.length) {
return fill(Array(this.viewText.length), 0).join('');
} else {
return `${this.text}`;
}
}
},
watch: {
type() {
this.setCurrentClass();
},
option(val, oldVal) {
if (oldVal.selected !== val.selected) {
this.actionClass = '';
}
if (oldVal.iconBold !== val.iconBold) {
this.setCurrentClass();
}
this.setOptions();
}
},
methods: {
...mapActions(['followArticle', 'praiseArticle', 'praiseComment']),
...articleMapActions(['fetchArticleUpdate']),
pxToRem(px) {
const rootValue = 40;
if (typeof px !== 'number') {
px = parseInt(`0${px}`, 10);
}
if (px > 2) {
return (px / rootValue).toFixed(2) + 'rem';
} else {
return px + 'px';
}
},
setOptions() {
forEach(defaultOption, (value, key) => {
this.viewOption[key] = this.option.hasOwnProperty(key) ? this.option[key] : defaultOption[key];
});
this.viewOption = {...this.viewOption};
},
setCurrentClass() {
let currentClass = classMap[this.type] || classMap['fav'];
currentClass = {...currentClass};
currentClass.selected = currentClass.selected || currentClass.default;
if (this.option.iconBold) {
currentClass.boldDefault && (currentClass.default = currentClass.boldDefault);
currentClass.boldSelected && (currentClass.selected = currentClass.boldSelected);
}
this.currentClass = currentClass;
},
changeBtnStatus() {
this.viewOption.selected = !this.viewOption.selected;
if (this.viewOption.textAutoChange) {
if (!isNaN(Number(this.viewText))) {
this.editText = Number(this.viewText) + (this.viewOption.selected ? 1 : -1);
}
}
this.actionClass = this.viewOption.selected ? this.currentClass.selected : this.currentClass.default;
},
syncService(type, data) {
if (typeof this[type] === 'function') {
return this[type](data);
} else {
return Promise.resolve({code: 404});
}
},
onClick(evt) {
if (this.share) {
return this.$links.toDownloadApp();
}
if (this.syncing) {
return;
}
if (this.viewOption.canSelect) {
this.changeBtnStatus();
if (this.syncFnName) {
const backFn = (res) => {
this.syncing = false;
if (res.code !== 200) {
this.changeBtnStatus();
this.$createToast && this.$createToast({
txt: res.message || '服务器开小差了',
type: 'warn',
time: 1000
}).show();
} else {
if (['praiseArticle', 'followArticle'].some(s => s === this.syncFnName)) {
this.fetchArticleUpdate({articleId: this.articleId});
}
}
};
this.syncing = true;
this.syncService(this.syncFnName, {
articleId: this.articleId,
commentId: this.commentId,
status: this.viewOption.selected
}).then(backFn).catch(backFn);
}
}
this.viewOption.emitName && this.$emit(this.viewOption.emitName, evt);
}
},
};
</script>
<style type="scss">
.icon-btn {
display: inline-block;
line-height: 1;
vertical-align: middle;
position: relative;
> * {
display: inline-block;
vertical-align: middle;
}
.click-wrap {
width: 150%;
height: calc(100% + 30px);
position: absolute;
left: -25%;
top: -15px;
z-index: 1;
}
.icon-btn-text {
position: relative;
.view-text {
position: absolute;
}
.placeholder-text {
opacity: 0;
}
}
}
</style>