-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.html
More file actions
294 lines (261 loc) · 9.46 KB
/
Copy pathindex.html
File metadata and controls
294 lines (261 loc) · 9.46 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
<!DOCTYPE html>
<html lang="zh-Hans">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="shortcut icon" href="../assets/images/favicon.ico" type="image/x-icon"/>
<link rel="stylesheet" href="../assets/main.css">
<title>前端晚练课</title>
</head>
<body>
<div id="app" v-cloak>
<header>
<h1 class="app-name">
<img src="../assets/images/title_exerciser-ranking-list.png" draggable="false" :alt="title">
<span>{{ stageRange.join('-') }}期</span>
</h1>
</header>
<ol class="ranking-list">
<li v-for="rank in rankings" v-if="rank.attends.length">
<div class="ranking-list-order"></div>
<div class="ranking-list-main">
<img :src="getAvatar(rank.user)">
<a :href="getGitHubPage(rank.user)" target="_blank" rel="noopener noreferrer">
{{ rank.user }}
</a>
</div>
<div class="ranking-list-count">
<a class="count-item" href="javascript:;"
:class="{active: checkActive(rank.user)}"
:title="category['stage']"
@click="showUserData($event, rank.user)"
>
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-count"></use>
</svg>
<b>{{ rank.attends.length }}</b>
</a>
<span class="count-item" :title="category.excellent">
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-excellent"></use>
</svg>
<b>{{ rank.excellentNum }}</b>
</span>
<span class="count-item" :title="category['thumbs']">
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-thumbs"></use>
</svg>
<b>{{ rank.thumbsNum }}</b>
</span>
</div>
</li>
</ol>
<div class="modal" v-show="modal.isShow"
:style="{top: modal.posY + 'px', left: modal.posX + 'px'}"
>
<div class="modal-head">
<h3 class="modal-title">{{ modal.title }}</h3>
<a class="modal-close-btn" href="javascript:;"
@click="closeModal"
>
<svg class="icon" aria-hidden="true">
<use xlink:href="#icon-close"></use>
</svg>
</a>
</div>
<div class="modal-body">
<table class="table">
<thead>
<tr>
<th>期数</th>
<th>{{ category.stage }}</th>
<th>{{ category.excellent }}</th>
<th>{{ category.thumbs }}</th>
</tr>
</thead>
<tbody>
<tr v-for="stageInfo in countStages">
<td>{{ stageInfo.idx }}</td>
<template v-if="stageInfo.data">
<td>
<i class="highlight">√</i>
</td>
<td>
<i class="highlight" v-if="stageInfo.data.excellent">√</i>
<i v-else>-</i>
</td>
<td>{{ stageInfo.data.thumbs || 0 }}</td>
</template>
<template v-else>
<td v-for="n in 3">-</td>
</template>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!-- <script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.js" integrity="sha256-ufGElb3TnOtzl5E4c/qQnZFGP+FYEZj5kbSEdJNrw0A=" crossorigin="anonymous"></script> -->
<script src="https://cdn.jsdelivr.net/npm/vue@2.6.10/dist/vue.min.js" integrity="sha256-chlNFSVx3TdcQ2Xlw7SvnbLAavAQLO0Y/LBiWX04viY=" crossorigin="anonymous"></script>
<script src="//at.alicdn.com/t/font_1171464_4vo2bprjxsk.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios@0.18.0/dist/axios.min.js" integrity="sha256-mpnrJ5DpEZZkwkE1ZgkEQQJW/46CSEh/STrZKOB/qoM=" crossorigin="anonymous"></script>
<script src="../assets/js/multi-sort.js"></script>
<script src="../assets/js/closest.js"></script>
<script src="../assets/js/get-url-param.js"></script>
<script>
var ONE_SESSION = 100; // 一届为100期 (1 session = 100 stage)
new Vue({
el: '#app',
data: {
title: '试炼者排行榜',
category: {
stage: '参与期数',
excellent: '优秀回答',
thumbs: '点赞数'
},
session: 1, // 第几届(日常更新)
rankings: [], // 格式化后的排行数据
modal: {
isShow: false,
title: '',
data: null,
posX: 0,
posY: 0
}
},
computed: {
// 期数范围
stageRange: function() {
var start = (this.session - 1) * ONE_SESSION + 1;
var end = this.session * ONE_SESSION;
return [start, end];
},
// 统计当前用户参与场期的数据
countStages: function() {
var start = this.stageRange[0];
var details = this.modal.data;
var result = [];
if (details && details.length) {
for (var i = 0; i < ONE_SESSION; i++) {
var temp = { idx: i + start };
for (var j = 0; j < details.length; j++) {
if (details[j].stage === temp.idx) {
temp.data = details[j];
break;
}
}
result.push(temp);
}
}
return result;
}
},
created: function() {
this.session = this.verifyUrlParam('session', 1);
// 浏览器标题:xx排行榜 (m-n期) - 前端晚练课
document.title = this.title +
' (' + this.stageRange.join('-') + '期)' +
' - ' + document.title;
this.reqData(this.session) // 获取 json 数据
.catch(
// 获取不到文件时(一般由于 session 期数越界),缺省加载第一个
(function() {
this.session = 1;
this.reqData(this.session);
}).bind(this)
);
// 点击空白处关闭弹框
window.addEventListener('click', (function(e) {
var el = e.target;
if (this.modal.isShow) {
// 排除点击触发按钮和弹出框的情况
if (!el.closest('a.count-item') && !el.closest('.modal')) {
this.closeModal();
}
}
}).bind(this), false);
},
methods: {
// 拉取数据
reqData: function(sessionNum) {
var that = this;
return axios.get('session' + sessionNum + '.json?' + (+new Date()))
.then(function(res) { return res.data })
.then(function(data) {
data = data.map(function(item) {
// 参与期数
item.attendNum = item.attends.length;
// 优秀回答数量
item.excellentNum = item.attends.reduce(function(total, attend) {
return total += attend.excellent ? 1 : 0
}, 0);
// 点赞数量
item.thumbsNum = item.attends.reduce(function(total, attend) {
return total += attend.thumbs || 0
}, 0);
return item;
});
// 按优秀回答数、点赞数、参与期数多重排序(降序)
data.sort(
multiSort(['excellentNum', 'thumbsNum', 'attendNum'], true)
)
that.rankings = data;
});
},
// url 参数校验
verifyUrlParam: function(name, defValue) {
var value = getUrlParam(name);
if (name === 'session') {
if (!(value && (value | 0) === Number(value))) { // 期数不存在或非整数的期数
value = defValue;
}
}
return value;
},
// 获取用户的 github 头像
getAvatar: function(username) {
return 'https://avatars.githubusercontent.com/' + username + '?s=52&v=4';
},
// 获取用户的 github 主页地址
getGitHubPage: function(username) {
return 'https://github.com/' + username;
},
checkActive: function(username) {
return this.modal.title === username;
},
// 展示当前用户的参与场期统计数据
showUserData: function(e, username) {
var node = e.target.closest('.count-item');
var modalHt = 444;
for (var i = 0; i < this.rankings.length; i++) {
if (this.rankings[i].user === username) {
this.modal.data = this.rankings[i].attends;
break;
}
}
var pos = node.getBoundingClientRect();
var posY = pos.top + pos.height + pageYOffset;
// 溢出视区下方,则自上方弹出
if (posY + modalHt > document.documentElement.clientHeight) {
posY = pos.top - modalHt + pageYOffset;
// 上方也溢出,则下方弹出(撑出滚动条)
if (posY < 0) {
posY = pos.top + pos.height + pageYOffset
}
}
this.modal.posX = pos.left + pos.width / 2;
this.modal.posY = posY;
this.modal.title = username;
this.modal.isShow = true;
},
closeModal: function() {
this.modal.isShow = false;
this.modal.title = '';
this.modal.data = null;
}
}
});
</script>
</body>
</html>