mutations.js
5.3 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
import * as Types from './types';
import {getArticleImageSize} from 'utils/image-handler';
import { get } from 'lodash';
import Vue from 'vue';
function articlefield(type, thumb) {
if (type === 'article') {
return `article${thumb ? 'Thumb' : ''}List`;
} else if (type === 'topic') {
return `article${thumb ? 'Thumb' : ''}ListByTopic`;
} else if (type === 'userArticle') {
return `articleUser${thumb ? 'Thumb' : ''}List`;
}
return '';
}
function updateArticleState(state, item) {
Vue.set(state.articleStates, item.articleId, {
hasAttention: item.hasAttention,
hasFavor: item.hasFavor,
hasPraise: item.hasPraise,
commentCount: item.commentCount,
favoriteCount: item.favoriteCount,
praiseCount: item.praiseCount,
comments: item.comments
});
}
function setArticleList(state, data, type, thumb) {
data.forEach((item, index) => {
get(item, 'productList', []).forEach(product => {
product.favorite = false;
});
item.blockIndex = 1;
item.lazy = index >= 1;
item.isExpand = false;
item.introHeight = 0;
if (!thumb) {
updateArticleState(state, item);
} else {
item.comments = [];
item.hasAttention = 'N';
item.hasFavor = 'N';
item.hasPraise = 'N';
item.commentCount = 0;
item.favoriteCount = 0;
item.praiseCount = 0;
}
});
state[articlefield(type, thumb)] = state[articlefield(type, thumb)].concat(data);
state[articlefield(type, thumb)].forEach((item, index) => {
const imageBlocks = get(item, 'blockList', []).filter(block => block.templateKey === 'image');
imageBlocks.forEach((img, inx) => {
let {width, height} = getArticleImageSize({
width: img.width,
height: img.height,
MIN_SCALE: inx === 0 ? (3 / 4) : void 0
});
img.width = parseInt(width, 10);
img.height = parseInt(height, 10);
});
item.index = index;
});
}
export default {
[Types.FETCH_ARTICLE_LIST_REQUEST](state, {refresh}) {
state.fetchArticleList = true;
if (refresh) {
state.articleList = [];
}
},
[Types.FETCH_ARTICLE_LIST_SUCCESS](state, {data, thumb}) {
state.fetchArticleList = false;
setArticleList(state, data, 'article', thumb);
},
[Types.FETCH_ARTICLE_LIST_FAILD](state) {
state.fetchArticleList = false;
},
[Types.FETCH_ARTICLE_LIST_USER_REQUEST](state, {refresh}) {
state.fetchArticleUserList = true;
if (refresh) {
state.articleUserList = [];
}
},
[Types.FETCH_ARTICLE_LIST_USER_SUCCESS](state, {data, thumb}) {
state.fetchArticleUserList = false;
setArticleList(state, data, 'userArticle', thumb);
},
[Types.FETCH_ARTICLE_LIST_USER_FAILD](state) {
state.fetchArticleUserList = false;
},
[Types.FETCH_ARTICLE_TOPIC_REQUEST](state, {page}) {
state.fetchArticleListByTopic = true;
if (page === 1) {
state.articleLastedTimeByTopic = 0;
state.articleListByTopic = [];
}
},
[Types.FETCH_ARTICLE_TOPIC_SUCCESS](state, {data}) {
state.fetchArticleListByTopic = false;
state.articleLastedTimeByTopic = data.lastedTime;
setArticleList(state, data, 'topic');
},
[Types.FETCH_ARTICLE_TOPIC_FAILD](state) {
state.fetchArticleListByTopic = false;
},
[Types.FETCH_ARTICLE_PRODUCT_SUCCESS](state, {articles, favs, articleProductType}) {
articles.forEach(article => {
if (article.articleProductType === articleProductType) {
get(article, 'productList', []).forEach(product => {
const find = favs.find(f => f.id === product.productId);
if (find) {
product.favorite = find.favorite;
}
});
}
});
},
[Types.FETCH_GUANG_SUCCESS](state, data) {
state.articleDetail = data;
},
[Types.CHANGE_AUTHOR_FOLLOW](state, {authorUid, follow, type}) {
state[articlefield(type)].forEach(article => {
if (article.authorUid === authorUid) {
article.hasAttention = follow ? 'Y' : 'N';
}
});
},
[Types.CHANGE_ARTICLE_LIST_SLIDE](state, {articleId, index, type}) {
state[articlefield(type)].forEach(article => {
if (article.articleId === articleId) {
article.blockIndex = index;
return;
}
});
},
[Types.CHANGE_ARTICLE_LIST_PRELAZY](state, {articleId, type}) {
let inx;
state[articlefield(type)].forEach((article, index) => {
if (article.articleId === articleId) {
inx = index;
article.lazy = false;
} else if (index - inx > 3) {
return;
} else {
article.lazy = false;
}
});
},
[Types.UPDATE_ARTICLE_STATE](state, {data}) {
updateArticleState(state, data);
},
[Types.ASYNC_ARTICLE_COMMENT](state, {articleId, type}) {
state[articlefield(type)].forEach(article => {
if (article.articleId === articleId) {
article.comments = state.articleStates[articleId].comments;
}
});
},
[Types.CHANGE_ARTICLE_LIST_INTRO](state, {articleId, isExpand, type}) {
const find = state[articlefield(type)].find(article => article.articleId === articleId);
if (find) {
find.isExpand = isExpand;
}
},
[Types.CHANGE_ARTICLE_LIST_INTRO_HEIGHT](state, {articleId, introHeight, type}) {
const find = state[articlefield(type)].find(article => article.articleId === articleId);
if (find) {
find.introHeight = introHeight || find.introHeight;
}
}
};