Browse Source

Workaround for submissions page refresh

pull/6/head
Menci 7 years ago
parent
commit
4e00f43d67
  1. 50
      modules/submission.js
  2. 23
      views/submissions.ejs
  3. 17
      views/submissions_item.ejs

50
modules/submission.js

@ -87,36 +87,46 @@ app.get('/submissions', async (req, res) => {
} }
}); });
app.get('/submissions/:id/ajax', async (req, res) => { app.get('/submissions/:ids/ajax', async (req, res) => {
try { try {
let judge_state = await JudgeState.fromID(req.params.id); let ids = req.params.ids.split(','), rendered = {};
if (!judge_state) throw new ErrorMessage('无此提交记录。');
await judge_state.loadRelationships(); for (let id of ids) {
let judge_state = await JudgeState.fromID(id);
if (!judge_state) throw new ErrorMessage('无此提交记录。');
judge_state.allowedSeeCode = await judge_state.isAllowedSeeCodeBy(res.locals.user); await judge_state.loadRelationships();
judge_state.allowedSeeData = await judge_state.isAllowedSeeDataBy(res.locals.user);
let contest; judge_state.allowedSeeCode = await judge_state.isAllowedSeeCodeBy(res.locals.user);
if (judge_state.type === 1) { judge_state.allowedSeeData = await judge_state.isAllowedSeeDataBy(res.locals.user);
contest = await Contest.fromID(judge_state.type_info);
contest.ended = await contest.isEnded();
let problems_id = await contest.getProblems(); let contest;
judge_state.problem_id = problems_id.indexOf(judge_state.problem_id) + 1; if (judge_state.type === 1) {
judge_state.problem.title = syzoj.utils.removeTitleTag(judge_state.problem.title); contest = await Contest.fromID(judge_state.type_info);
contest.ended = await contest.isEnded();
if (contest.type === 'noi' && !contest.ended && !await judge_state.problem.isAllowedEditBy(res.locals.user)) { let problems_id = await contest.getProblems();
if (!['Compile Error', 'Waiting', 'Compiling'].includes(judge_state.status)) { judge_state.problem_id = problems_id.indexOf(judge_state.problem_id) + 1;
judge_state.status = 'Submitted'; judge_state.problem.title = syzoj.utils.removeTitleTag(judge_state.problem.title);
if (contest.type === 'noi' && !contest.ended && !await judge_state.problem.isAllowedEditBy(res.locals.user)) {
if (!['Compile Error', 'Waiting', 'Compiling'].includes(judge_state.status)) {
judge_state.status = 'Submitted';
}
} }
} }
let o = { pending: judge_state.pending, html: null, status: judge_state.status };
o.html = await require('util').promisify(app.render).bind(app)('submissions_item', {
contest: contest,
judge: judge_state
});
rendered[id] = o;
} }
res.render('submissions_item', { res.send(rendered);
contest: contest,
judge: judge_state
});
} catch (e) { } catch (e) {
syzoj.log(e); syzoj.log(e);
res.render('error', { res.render('error', {

23
views/submissions.ejs

@ -77,7 +77,9 @@
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<% let ids = []; %>
<% for (let judge of judge_state) { %> <% for (let judge of judge_state) { %>
<% if (judge.pending) ids.push(judge.id); %>
<tr id="submissions_<%= judge.id %>"><% include submissions_item %></tr> <tr id="submissions_<%= judge.id %>"><% include submissions_item %></tr>
<% } %> <% } %>
</tbody> </tbody>
@ -93,5 +95,26 @@ $(function () {
$('#select_language').dropdown(); $('#select_language').dropdown();
$('#select_status').dropdown(); $('#select_status').dropdown();
}); });
var ids = <%= JSON.stringify(ids) %>;
function update() {
setTimeout(function () {
$.get('/submissions/' + ids.join(',') + '/ajax', function (data) {
var newIDs = [];
for (var id in data) {
if (data[id].pending) newIDs.push(id);
var e = $('#submissions_' + id);
if (e.find('span.status').text().trim() !== data[id].status) e.html(data[id].html);
}
if (newIDs.length > 0) {
ids = newIDs;
update();
}
});
}, 500);
}
update();
</script> </script>
<% include footer %> <% include footer %>

17
views/submissions_item.ejs

@ -33,21 +33,4 @@ textFit(e, { maxFontSize: 14 });
<% } %> <% } %>
<td><a href="<%= syzoj.utils.makeUrl(['user', judge.user_id]) %>"><%= judge.user.username %></a><% if (judge.user.nameplate) { %><%- judge.user.nameplate %><% } %></td> <td><a href="<%= syzoj.utils.makeUrl(['user', judge.user_id]) %>"><%= judge.user.username %></a><% if (judge.user.nameplate) { %><%- judge.user.nameplate %><% } %></td>
<td><%= syzoj.utils.formatDate(judge.submit_time) %> <td><%= syzoj.utils.formatDate(judge.submit_time) %>
<% if (isPending(judge.status)) { %>
<script>
function update_judge_<%= judge.id %>() {
setTimeout(function () {
<%
let url = syzoj.utils.makeUrl(['submissions', judge.id, 'ajax']);
%>
$.get('<%= url %>', function (data) {
var e = $('#submissions_<%= judge.id %>');
if (e.html() != data) e.html(data);
else update_judge_<%= judge.id %>();
});
}, 500);
}
update_judge_<%= judge.id %>();
</script>
<% } %>
</td> </td>

Loading…
Cancel
Save