widget-icon-btn.vue
5.85 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
<template>
<div class="icon-btn" @click="onClick" :style="btnStyle">
<i class="iconfont" :class="iconClass" :style="iconStyle"></i>
<p v-if="text" class="icon-btn-text" :style="textStyle">
<span class="view-text">{{viewText}}</span>
<span class="placeholder-text">{{text}}</span>
</p>
</div>
</template>
<script>
import {pxToRem} from '../../../utils/helpers.js';
import {foreach} 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: [String, Number],
articleId: Number,
commentId: Number,
option: {
type: Object,
default() {
return defaultOption;
}
}
},
data() {
return {
btnSelected: false,
actionClass: '',
editText: null
}
},
created() {
foreach(defaultOption, (value, key) => {
if (!this.option.hasOwnProperty(key)) {
this.option[key] = value;
}
});
},
computed: {
btnStyle() {
let color = this.option.color || defaultOption.color;
return `color: ${this.btnSelected ? (this.option.selectedColor || color) : color};`;
},
iconClass() {
if (this.actionClass) {
return this.actionClass;
}
if (!this._icon) {
this._type = classMap[this.type] ? this.type : 'fav'
this._icon = classMap[this._type];
}
if (this.option.selected) {
this.btnSelected = true;
return this._icon.selected || this._icon.default;
}
return this._icon.default;
},
iconStyle() {
return `font-size: ${pxToRem(this.option.iconFontSize)};`;
},
textStyle() {
let style = `font-size: ${pxToRem(this.option.textFontSize)};`;
let textAlign = this.option.textAlign;
if (['top', 'bottom'].indexOf(textAlign) >= 0) {
style += ` vertical-align: ${textAlign};`;
}
let textZoom = this.option.textZoom;
if (Number(textZoom) !== NaN) {
style += ` transform: scale(${textZoom}, ${textZoom});`
}
if (this.option.textSelectedColor) {
style += ` color: ${this.option.textSelectedColor};`
}
return style;
},
viewText() {
return this.editText === null ? this.text : this.editText;
}
},
methods: {
...mapActions(['followArticle', 'praiseArticle', 'praiseComment']),
changeBtnStatus() {
this.btnSelected = !this.btnSelected;
if (this.option.textAutoChange) {
let _text = this.editText || this.text;
if (!isNaN(Number(this.viewText))) {
this.editText = Number(this.viewText) + (this.btnSelected ? 1 : -1);
}
}
if (this._icon.selected) {
this.actionClass = this.btnSelected ? this._icon.selected : this._icon.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.option.canSelect) {
this.changeBtnStatus();
const backFn = (res) => {
this.syncing = false;
if (res.code !== 200) {
this.changeBtnStatus();
}
if (res.code === 401) {
// TODO go_login
}
};
let syncFnName = '';
if (this.articleId) {
if (this._type === 'fav') {
syncFnName = 'praiseArticle';
} else if (this._type === 'star') {
syncFnName = 'praiseArticle';
}
} else if (this.commentId && this._type === 'fav') {
syncFnName = 'praiseComment';
}
if (syncFnName) {
this.syncing = true;
this.syncService(syncFnName, {
articleId: this.articleId,
status: this.btnSelected
}).then(backFn).catch(backFn);
}
}
this.option.emitName && this.$emit(this.option.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>