widget-icon-btn.vue
6.35 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
<template>
<AuthComponent :auth="isAuth" class="icon-btn" @click="onClick" :style="btnStyle">
<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>
</AuthComponent>
</template>
<script>
import {forEach, fill} from 'lodash';
import {createNamespacedHelpers} from 'vuex';
const {mapActions} = createNamespacedHelpers('user');
const classMap = {
fav: {
default: 'icon-zan',
selected: 'icon-zan-fill'
},
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选中状态字体颜色(不设置默认与非选中一致)
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;
}
}
},
data() {
return {
viewOption: {},
btnSelected: false,
actionClass: '',
editText: null
};
},
created() {
forEach(defaultOption, (value, key) => {
this.viewOption[key] = this.option.hasOwnProperty(key) ? this.option[key] : defaultOption[key];
});
this.viewOption = {...this.viewOption};
},
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.btnSelected ? (this.viewOption.selectedColor || color) : color};`;
},
iconClass() {
if (this.actionClass) {
return this.actionClass;
}
if (classMap[this.type]) {
const currentClass = classMap[this.type];
if (this.viewOption.selected) {
this.btnSelected = true;
return currentClass.selected || currentClass.default;
}
return 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}`;
}
}
},
methods: {
...mapActions(['followArticle', 'praiseArticle', 'praiseComment']),
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';
}
},
changeBtnStatus() {
this.btnSelected = !this.btnSelected;
if (this.viewOption.textAutoChange) {
if (!isNaN(Number(this.viewText))) {
this.editText = Number(this.viewText) + (this.btnSelected ? 1 : -1);
}
}
if (classMap[this.type]) {
this.actionClass = this.btnSelected ? classMap[this.type].selected : classMap[this.type].default;
}
},
syncService(type, data) {
if (typeof this[type] === 'function') {
return this[type](data);
} else {
return Promise.resolve({code: 404});
}
},
onClick(evt) {
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();
}
};
this.syncing = true;
this.syncService(this.syncFnName, {
articleId: this.articleId,
commentId: this.commentId,
status: this.btnSelected
}).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;
> * {
display: inline-block;
vertical-align: middle;
}
.icon-btn-text {
position: relative;
.view-text {
position: absolute;
}
.placeholder-text {
opacity: 0;
}
}
}
</style>