mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
ui: switch storage to indexeddb
This commit is contained in:
6 files changed
+102
-42
No files matched your search
@@ -1,17 +1,19 @@
|
||||
import $ from 'jquery';
|
||||
import { NamedPage } from 'vj/misc/Page';
|
||||
import { pjax } from 'vj/utils';
|
||||
import { openDB } from 'vj/utils/db';
|
||||
|
||||
const page = new NamedPage(['contest_scoreboard', 'homework_scoreboard'], () => {
|
||||
const page = new NamedPage(['contest_scoreboard', 'homework_scoreboard'], async () => {
|
||||
const db = await openDB;
|
||||
const { tdoc } = UiContext;
|
||||
const key = `scoreboard-star/${tdoc.domainId}/${tdoc.docId}`;
|
||||
const read = () => JSON.parse(localStorage.getItem(key) || '[]');
|
||||
const write = (data) => localStorage.setItem(key, JSON.stringify(data));
|
||||
const id = `${tdoc.domainId}/${tdoc.docId}`;
|
||||
const read = async () => (await db.get('scoreboard-star', id)).value || [];
|
||||
const write = (data) => db.put('scoreboard-star', { id, data });
|
||||
|
||||
$(`.star.user--${UserContext._id}`).closest('tr').addClass('star-highlight');
|
||||
read().forEach((uid) => $(`.star.user--${uid}`).addClass('activated').closest('tr').addClass('star-highlight'));
|
||||
$('.star').on('click', (e) => {
|
||||
const star = read();
|
||||
(await read()).forEach((uid) => $(`.star.user--${uid}`).addClass('activated').closest('tr').addClass('star-highlight'));
|
||||
$('.star').on('click', async (e) => {
|
||||
const star = await read();
|
||||
const $target = $(e.currentTarget);
|
||||
const uid = $target.data('uid');
|
||||
if (star.includes(uid)) {
|
||||
@@ -24,14 +26,14 @@ const page = new NamedPage(['contest_scoreboard', 'homework_scoreboard'], () =>
|
||||
write(star);
|
||||
});
|
||||
|
||||
$('.select.filter').on('change', (e) => {
|
||||
const val = $(e.target).val();
|
||||
async function update() {
|
||||
const val = $($('.select.filter')).val();
|
||||
if (val === 'all') {
|
||||
$('.data-table tbody tr').show();
|
||||
read().forEach((uid) => $(`.star.user--${uid}`).closest('tr').addClass('star-highlight'));
|
||||
(await read()).forEach((uid) => $(`.star.user--${uid}`).closest('tr').addClass('star-highlight'));
|
||||
} else if (val === 'star') {
|
||||
$('.data-table tbody tr').hide();
|
||||
read().forEach((uid) => $(`.star.user--${uid}`).closest('tr').show().removeClass('star-highlight'));
|
||||
(await read()).forEach((uid) => $(`.star.user--${uid}`).closest('tr').show().removeClass('star-highlight'));
|
||||
} else if (val === 'rank') {
|
||||
$('.data-table tbody tr').show();
|
||||
$('.rank--unrank').closest('tr').hide();
|
||||
@@ -41,13 +43,17 @@ const page = new NamedPage(['contest_scoreboard', 'homework_scoreboard'], () =>
|
||||
if (!uids?.length) return;
|
||||
uids.forEach((uid) => $(`.user--${uid}`).closest('tr').show());
|
||||
}
|
||||
});
|
||||
}
|
||||
$('.select.filter').on('change', update);
|
||||
|
||||
const beginAt = new Date(UiContext.tdoc.beginAt).getTime();
|
||||
const endAt = new Date(UiContext.tdoc.endAt).getTime();
|
||||
function updateScoreboard() {
|
||||
async function updateScoreboard() {
|
||||
const now = Date.now();
|
||||
if (beginAt <= now && now <= endAt) pjax.request({ url: UiContext.scoreboardUrl || '', push: false });
|
||||
if (beginAt <= now && now <= endAt) {
|
||||
await pjax.request({ url: UiContext.scoreboardUrl || '', push: false });
|
||||
await update();
|
||||
}
|
||||
}
|
||||
|
||||
setInterval(updateScoreboard, 180000);
|
||||
|
||||
Reference in new issue
Block a user