From 4e00f43d677a1988517ce9a9ed10ee2d3d531e30 Mon Sep 17 00:00:00 2001 From: Menci Date: Tue, 11 Jul 2017 09:03:52 +0800 Subject: [PATCH] Workaround for submissions page refresh --- modules/submission.js | 50 +++++++++++++++++++++++--------------- views/submissions.ejs | 23 ++++++++++++++++++ views/submissions_item.ejs | 17 ------------- 3 files changed, 53 insertions(+), 37 deletions(-) diff --git a/modules/submission.js b/modules/submission.js index 8b2e1ce..af2a6b3 100644 --- a/modules/submission.js +++ b/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 { - let judge_state = await JudgeState.fromID(req.params.id); - if (!judge_state) throw new ErrorMessage('无此提交记录。'); + let ids = req.params.ids.split(','), rendered = {}; - 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); - judge_state.allowedSeeData = await judge_state.isAllowedSeeDataBy(res.locals.user); + await judge_state.loadRelationships(); - let contest; - if (judge_state.type === 1) { - contest = await Contest.fromID(judge_state.type_info); - contest.ended = await contest.isEnded(); + judge_state.allowedSeeCode = await judge_state.isAllowedSeeCodeBy(res.locals.user); + judge_state.allowedSeeData = await judge_state.isAllowedSeeDataBy(res.locals.user); - let problems_id = await contest.getProblems(); - judge_state.problem_id = problems_id.indexOf(judge_state.problem_id) + 1; - judge_state.problem.title = syzoj.utils.removeTitleTag(judge_state.problem.title); + let contest; + if (judge_state.type === 1) { + 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)) { - if (!['Compile Error', 'Waiting', 'Compiling'].includes(judge_state.status)) { - judge_state.status = 'Submitted'; + let problems_id = await contest.getProblems(); + judge_state.problem_id = problems_id.indexOf(judge_state.problem_id) + 1; + 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', { - contest: contest, - judge: judge_state - }); + res.send(rendered); } catch (e) { syzoj.log(e); res.render('error', { diff --git a/views/submissions.ejs b/views/submissions.ejs index 00c3ea0..ca06fa8 100644 --- a/views/submissions.ejs +++ b/views/submissions.ejs @@ -77,7 +77,9 @@ + <% let ids = []; %> <% for (let judge of judge_state) { %> + <% if (judge.pending) ids.push(judge.id); %> <% include submissions_item %> <% } %> @@ -93,5 +95,26 @@ $(function () { $('#select_language').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(); <% include footer %> diff --git a/views/submissions_item.ejs b/views/submissions_item.ejs index 8de86f6..20bc867 100644 --- a/views/submissions_item.ejs +++ b/views/submissions_item.ejs @@ -33,21 +33,4 @@ textFit(e, { maxFontSize: 14 }); <% } %> <%= judge.user.username %><% if (judge.user.nameplate) { %><%- judge.user.nameplate %><% } %> <%= syzoj.utils.formatDate(judge.submit_time) %> -<% if (isPending(judge.status)) { %> - -<% } %>