core: support response.pjax

This commit is contained in:
undefined committed 2022-11-28 18:50:16 +08:00
1 parent 89fb04bfb3
commit f08ac664da
6 files changed
+27 -48

No files matched your search

+4 -14
View File
@@ -451,27 +451,17 @@ export class ContestCodeHandler extends Handler {
export class ContestFilesHandler extends ContestDetailBaseHandler { export class ContestFilesHandler extends ContestDetailBaseHandler {
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID)
@param('pjax', Types.Boolean) async get(domainId: string, tid: ObjectID) {
async get(domainId: string, tid: ObjectID, pjax = false) {
if (!this.user.own(this.tdoc)) this.checkPerm(PERM.PERM_EDIT_CONTEST); if (!this.user.own(this.tdoc)) this.checkPerm(PERM.PERM_EDIT_CONTEST);
const body = { this.response.body = {
tdoc: this.tdoc, tdoc: this.tdoc,
tsdoc: this.tsdoc, tsdoc: this.tsdoc,
owner_udoc: await user.getById(domainId, this.tdoc.owner), owner_udoc: await user.getById(domainId, this.tdoc.owner),
files: sortFiles(this.tdoc.files || []), files: sortFiles(this.tdoc.files || []),
urlForFile: (filename: string) => this.url('contest_file_download', { tid, filename }), urlForFile: (filename: string) => this.url('contest_file_download', { tid, filename }),
}; };
if (pjax) { this.response.pjax = 'partials/files.html';
this.response.body = { this.response.template = 'contest_files.html';
fragments: (await Promise.all([
this.renderHTML('partials/files.html', body),
])).map((i) => ({ html: i })),
};
this.response.template = '';
} else {
this.response.template = 'contest_files.html';
this.response.body = body;
}
} }
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID)
+3 -5
View File
@@ -284,7 +284,7 @@ const allPriv = Math.sum(Object.values(Priv));
class SystemUserPrivHandler extends SystemHandler { class SystemUserPrivHandler extends SystemHandler {
@requireSudo @requireSudo
async get({ pjax }) { async get() {
const defaultPriv = system.get('default.priv'); const defaultPriv = system.get('default.priv');
const udocs = await user.getMulti({ _id: { $gte: -1000, $ne: 1 }, priv: { $nin: [0, defaultPriv] } }).limit(1000).sort({ _id: 1 }).toArray(); const udocs = await user.getMulti({ _id: { $gte: -1000, $ne: 1 }, priv: { $nin: [0, defaultPriv] } }).limit(1000).sort({ _id: 1 }).toArray();
const banudocs = await user.getMulti({ _id: { $gte: -1000, $ne: 1 }, priv: 0 }).limit(1000).sort({ _id: 1 }).toArray(); const banudocs = await user.getMulti({ _id: { $gte: -1000, $ne: 1 }, priv: 0 }).limit(1000).sort({ _id: 1 }).toArray();
@@ -293,10 +293,8 @@ class SystemUserPrivHandler extends SystemHandler {
defaultPriv, defaultPriv,
Priv, Priv,
}; };
if (pjax) { this.response.pjax = 'partials/manage_user_priv.html';
const html = await this.renderHTML('partials/manage_user_priv.html', this.response.body); this.response.template = 'manage_user_priv.html';
this.response.body = { fragments: [{ html }] };
} else this.response.template = 'manage_user_priv.html';
} }
@requireSudo @requireSudo
+4 -14
View File
@@ -34,24 +34,14 @@ class SwitchLanguageHandler extends Handler {
export class FilesHandler extends Handler { export class FilesHandler extends Handler {
noCheckPermView = true; noCheckPermView = true;
@param('pjax', Types.Boolean) async get() {
async get(domainId: string, pjax = false) {
if (!this.user._files?.length) this.checkPriv(PRIV.PRIV_CREATE_FILE); if (!this.user._files?.length) this.checkPriv(PRIV.PRIV_CREATE_FILE);
const body = { this.response.body = {
files: sortFiles(this.user._files), files: sortFiles(this.user._files),
urlForFile: (filename: string) => this.url('fs_download', { uid: this.user._id, filename }), urlForFile: (filename: string) => this.url('fs_download', { uid: this.user._id, filename }),
}; };
if (pjax) { this.response.pjax = 'partials/files.html';
this.response.body = { this.response.template = 'home_files.html';
fragments: (await Promise.all([
this.renderHTML('partials/files.html', body),
])).map((i) => ({ html: i })),
};
this.response.template = '';
} else {
this.response.template = 'home_files.html';
this.response.body = body;
}
} }
@post('filename', Types.Name, true) @post('filename', Types.Name, true)
+3 -6
View File
@@ -92,8 +92,7 @@ class TrainingMainHandler extends Handler {
class TrainingDetailHandler extends Handler { class TrainingDetailHandler extends Handler {
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID)
@param('uid', Types.PositiveInt, true) @param('uid', Types.PositiveInt, true)
@param('pjax', Types.Boolean, true) async get(domainId: string, tid: ObjectID, uid: number) {
async get(domainId: string, tid: ObjectID, uid: number, pjax: boolean) {
const tdoc = await training.get(domainId, tid); const tdoc = await training.get(domainId, tid);
await bus.parallel('training/get', tdoc, this); await bus.parallel('training/get', tdoc, this);
let targetUser = this.user._id; let targetUser = this.user._id;
@@ -154,10 +153,8 @@ class TrainingDetailHandler extends Handler {
this.response.body = { this.response.body = {
tdoc, tsdoc, pids, pdict, psdict, ndict, nsdict, udoc, udocs, dudict, selfPsdict, tdoc, tsdoc, pids, pdict, psdict, ndict, nsdict, udoc, udocs, dudict, selfPsdict,
}; };
if (pjax) { this.response.pjax = 'partials/training_detail.html';
const html = await this.renderHTML('partials/training_detail.html', this.response.body); this.response.template = 'training_detail.html';
this.response.body = { fragments: [{ html }] };
} else this.response.template = 'training_detail.html';
} }
@param('tid', Types.ObjectID) @param('tid', Types.ObjectID)
@@ -35,6 +35,9 @@ export default (logger) => async (ctx: KoaContext, next) => {
response.body = new SystemError('Serialize failure', e.message); response.body = new SystemError('Serialize failure', e.message);
} }
response.type = 'application/json'; response.type = 'application/json';
} else if (response.pjax && args.pjax) {
const html = await ctx.renderHTML(response.pjax, response.body);
response.body = { fragments: [{ html }] };
} else if (response.template) { } else if (response.template) {
const s = response.template.split('.'); const s = response.template.split('.');
let templateName = `${s[0]}.${args.domainId}.${s[1]}`; let templateName = `${s[0]}.${args.domainId}.${s[1]}`;
+10 -9
View File
@@ -54,15 +54,16 @@ export interface HydroRequest {
websocket: boolean; websocket: boolean;
} }
export interface HydroResponse { export interface HydroResponse {
body: any, body: any;
type: string, type: string;
status: number, status: number;
template?: string, template?: string;
redirect?: string, pjax?: string;
disposition?: string, redirect?: string;
etag?: string, disposition?: string;
attachment: (name: string, stream?: any) => void, etag?: string;
addHeader: (name: string, value: string) => void, attachment: (name: string, stream?: any) => void;
addHeader: (name: string, value: string) => void;
} }
interface HydroContext { interface HydroContext {
request: HydroRequest; request: HydroRequest;