core: support response.pjax

pull/468/head
undefined 2 years ago
parent 89fb04bfb3
commit f08ac664da

@ -451,27 +451,17 @@ export class ContestCodeHandler extends Handler {
export class ContestFilesHandler extends ContestDetailBaseHandler {
@param('tid', Types.ObjectID)
@param('pjax', Types.Boolean)
async get(domainId: string, tid: ObjectID, pjax = false) {
async get(domainId: string, tid: ObjectID) {
if (!this.user.own(this.tdoc)) this.checkPerm(PERM.PERM_EDIT_CONTEST);
const body = {
this.response.body = {
tdoc: this.tdoc,
tsdoc: this.tsdoc,
owner_udoc: await user.getById(domainId, this.tdoc.owner),
files: sortFiles(this.tdoc.files || []),
urlForFile: (filename: string) => this.url('contest_file_download', { tid, filename }),
};
if (pjax) {
this.response.body = {
fragments: (await Promise.all([
this.renderHTML('partials/files.html', body),
])).map((i) => ({ html: i })),
};
this.response.template = '';
} else {
this.response.pjax = 'partials/files.html';
this.response.template = 'contest_files.html';
this.response.body = body;
}
}
@param('tid', Types.ObjectID)

@ -284,7 +284,7 @@ const allPriv = Math.sum(Object.values(Priv));
class SystemUserPrivHandler extends SystemHandler {
@requireSudo
async get({ pjax }) {
async get() {
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 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,
Priv,
};
if (pjax) {
const html = await this.renderHTML('partials/manage_user_priv.html', this.response.body);
this.response.body = { fragments: [{ html }] };
} else this.response.template = 'manage_user_priv.html';
this.response.pjax = 'partials/manage_user_priv.html';
this.response.template = 'manage_user_priv.html';
}
@requireSudo

@ -34,24 +34,14 @@ class SwitchLanguageHandler extends Handler {
export class FilesHandler extends Handler {
noCheckPermView = true;
@param('pjax', Types.Boolean)
async get(domainId: string, pjax = false) {
async get() {
if (!this.user._files?.length) this.checkPriv(PRIV.PRIV_CREATE_FILE);
const body = {
this.response.body = {
files: sortFiles(this.user._files),
urlForFile: (filename: string) => this.url('fs_download', { uid: this.user._id, filename }),
};
if (pjax) {
this.response.body = {
fragments: (await Promise.all([
this.renderHTML('partials/files.html', body),
])).map((i) => ({ html: i })),
};
this.response.template = '';
} else {
this.response.pjax = 'partials/files.html';
this.response.template = 'home_files.html';
this.response.body = body;
}
}
@post('filename', Types.Name, true)

@ -92,8 +92,7 @@ class TrainingMainHandler extends Handler {
class TrainingDetailHandler extends Handler {
@param('tid', Types.ObjectID)
@param('uid', Types.PositiveInt, true)
@param('pjax', Types.Boolean, true)
async get(domainId: string, tid: ObjectID, uid: number, pjax: boolean) {
async get(domainId: string, tid: ObjectID, uid: number) {
const tdoc = await training.get(domainId, tid);
await bus.parallel('training/get', tdoc, this);
let targetUser = this.user._id;
@ -154,10 +153,8 @@ class TrainingDetailHandler extends Handler {
this.response.body = {
tdoc, tsdoc, pids, pdict, psdict, ndict, nsdict, udoc, udocs, dudict, selfPsdict,
};
if (pjax) {
const html = await this.renderHTML('partials/training_detail.html', this.response.body);
this.response.body = { fragments: [{ html }] };
} else this.response.template = 'training_detail.html';
this.response.pjax = 'partials/training_detail.html';
this.response.template = 'training_detail.html';
}
@param('tid', Types.ObjectID)

@ -35,6 +35,9 @@ export default (logger) => async (ctx: KoaContext, next) => {
response.body = new SystemError('Serialize failure', e.message);
}
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) {
const s = response.template.split('.');
let templateName = `${s[0]}.${args.domainId}.${s[1]}`;

@ -54,15 +54,16 @@ export interface HydroRequest {
websocket: boolean;
}
export interface HydroResponse {
body: any,
type: string,
status: number,
template?: string,
redirect?: string,
disposition?: string,
etag?: string,
attachment: (name: string, stream?: any) => void,
addHeader: (name: string, value: string) => void,
body: any;
type: string;
status: number;
template?: string;
pjax?: string;
redirect?: string;
disposition?: string;
etag?: string;
attachment: (name: string, stream?: any) => void;
addHeader: (name: string, value: string) => void;
}
interface HydroContext {
request: HydroRequest;

Loading…
Cancel
Save