prepare lichess.announce for client side use

pull/6291/head
Niklas Fiekas 2020-04-04 12:44:15 +02:00
parent a393ff27d8
commit 498f7551a9
2 changed files with 15 additions and 6 deletions

View File

@ -33,6 +33,7 @@ interface Lichess {
redirect(o: string | { url: string, cookie: Cookie }): void;
reload(): void;
escapeHtml(str: string): string;
announce(d: LichessAnnouncement): void;
// standalones/trans.js
trans(i18n: { [key: string]: string | undefined }): Trans
@ -151,6 +152,11 @@ interface LichessStorageEvent {
value?: string;
}
interface LichessAnnouncement {
msg?: string;
date?: string;
}
interface Window {
lichess: Lichess

View File

@ -43,21 +43,24 @@
lichess.announce = (() => {
let timeout;
const kill = () => $('#announce').remove();
const kill = () => {
if (timeout) clearTimeout(timeout);
timeout = undefined;
$('#announce').remove();
};
const set = (d) => {
if (!d) return;
kill();
if (timeout) clearTimeout(timeout);
if (d.msg) {
$('body').append(
'<div id="announce" class="announce">' +
d.msg +
'<time class="timeago" datetime="' + d.date + '"></time>' +
lichess.escapeHtml(d.msg) +
(d.date ? '<time class="timeago" datetime="' + d.date + '"></time>' : '') +
'<div class="actions"><a class="close">X</a></div>' +
'</div>'
).find('#announce .close').click(kill);
timeout = setTimeout(kill, new Date(d.date) - Date.now());
lichess.pubsub.emit('content_loaded');
timeout = setTimeout(kill, d.date ? new Date(d.date) - Date.now() : 5000);
if (d.date) lichess.pubsub.emit('content_loaded');
}
};
set($('body').data('announce'));