2019-02-05 04:59:03 +01:00
|
|
|
/* Copyright: Ankitects Pty Ltd and contributors
|
|
|
|
* License: GNU AGPL, version 3 or later; http://www.gnu.org/licenses/agpl.html */
|
|
|
|
|
2017-07-28 08:19:06 +02:00
|
|
|
var ankiPlatform = "desktop";
|
|
|
|
var typeans;
|
2017-10-24 09:09:22 +02:00
|
|
|
var _updatingQA = false;
|
2017-07-29 06:24:45 +02:00
|
|
|
|
2017-08-10 12:45:42 +02:00
|
|
|
var qFade = 100;
|
|
|
|
var aFade = 0;
|
|
|
|
|
2017-08-28 06:54:22 +02:00
|
|
|
var onUpdateHook;
|
|
|
|
var onShownHook;
|
|
|
|
|
2020-11-15 20:57:07 +01:00
|
|
|
function _runHook(arr: () => Promise<any>[]): Promise<any[]> {
|
|
|
|
var promises = [];
|
|
|
|
|
2019-12-18 07:12:39 +01:00
|
|
|
for (var i = 0; i < arr.length; i++) {
|
2020-11-15 20:57:07 +01:00
|
|
|
promises.push(arr[i]());
|
2017-08-28 06:54:22 +02:00
|
|
|
}
|
2020-11-15 20:57:07 +01:00
|
|
|
|
|
|
|
return Promise.all(promises);
|
2017-08-28 06:54:22 +02:00
|
|
|
}
|
|
|
|
|
2017-08-10 12:45:42 +02:00
|
|
|
function _updateQA(html, fadeTime, onupdate, onshown) {
|
2017-10-24 09:09:22 +02:00
|
|
|
// if a request to update q/a comes in before the previous content
|
|
|
|
// has been loaded, wait a while and try again
|
|
|
|
if (_updatingQA) {
|
2020-06-24 13:54:38 +02:00
|
|
|
setTimeout(function () {
|
2019-12-18 07:12:39 +01:00
|
|
|
_updateQA(html, fadeTime, onupdate, onshown);
|
|
|
|
}, 50);
|
2017-10-24 09:09:22 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
_updatingQA = true;
|
|
|
|
|
2017-08-28 06:54:22 +02:00
|
|
|
onUpdateHook = [onupdate];
|
|
|
|
onShownHook = [onshown];
|
|
|
|
|
2017-08-06 04:01:30 +02:00
|
|
|
var qa = $("#qa");
|
2020-11-15 20:57:07 +01:00
|
|
|
|
|
|
|
// fade out current text
|
2020-12-09 14:17:34 +01:00
|
|
|
new Promise((resolve) => qa.fadeTo(fadeTime, 0, () => resolve()))
|
2017-08-06 04:01:30 +02:00
|
|
|
// update text
|
2020-11-15 20:57:07 +01:00
|
|
|
.then(() => {
|
|
|
|
try {
|
2020-11-15 21:21:04 +01:00
|
|
|
qa.html(html);
|
|
|
|
} catch (err) {
|
2020-11-15 20:57:07 +01:00
|
|
|
qa.html(
|
|
|
|
(
|
|
|
|
`Invalid HTML on card: ${String(err).substring(0, 2000)}\n` +
|
|
|
|
String(err.stack).substring(0, 2000)
|
|
|
|
).replace(/\n/g, "<br />")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => _runHook(onUpdateHook))
|
2020-11-15 21:21:04 +01:00
|
|
|
.then(() =>
|
|
|
|
// @ts-ignore wait for mathjax to ready
|
|
|
|
MathJax.startup.promise.then(() => {
|
2020-11-15 20:57:07 +01:00
|
|
|
// @ts-ignore clear MathJax buffer
|
|
|
|
MathJax.typesetClear();
|
2017-07-29 06:24:45 +02:00
|
|
|
|
2020-11-15 20:57:07 +01:00
|
|
|
// @ts-ignore typeset
|
|
|
|
return MathJax.typesetPromise(qa.slice(0, 1));
|
2020-11-15 21:21:04 +01:00
|
|
|
})
|
|
|
|
)
|
2020-11-15 20:57:07 +01:00
|
|
|
// and reveal when processing is done
|
2020-12-09 14:17:34 +01:00
|
|
|
.then(() => new Promise((resolve) => qa.fadeTo(fadeTime, 1, () => resolve())))
|
2020-11-15 20:57:07 +01:00
|
|
|
.then(() => _runHook(onShownHook))
|
2020-11-15 21:21:04 +01:00
|
|
|
.then(() => (_updatingQA = false));
|
2017-07-28 08:48:49 +02:00
|
|
|
}
|
2017-07-28 08:19:06 +02:00
|
|
|
|
2017-08-06 04:01:30 +02:00
|
|
|
function _showQuestion(q, bodyclass) {
|
2019-12-18 07:12:39 +01:00
|
|
|
_updateQA(
|
|
|
|
q,
|
|
|
|
qFade,
|
2020-06-24 13:54:38 +02:00
|
|
|
function () {
|
2019-12-18 07:12:39 +01:00
|
|
|
// return to top of window
|
|
|
|
window.scrollTo(0, 0);
|
2017-07-29 06:24:45 +02:00
|
|
|
|
2017-08-08 08:02:55 +02:00
|
|
|
document.body.className = bodyclass;
|
2019-12-18 07:12:39 +01:00
|
|
|
},
|
2020-06-24 13:54:38 +02:00
|
|
|
function () {
|
2019-12-18 07:12:39 +01:00
|
|
|
// focus typing area if visible
|
|
|
|
typeans = document.getElementById("typeans");
|
|
|
|
if (typeans) {
|
|
|
|
typeans.focus();
|
|
|
|
}
|
2017-08-08 08:02:55 +02:00
|
|
|
}
|
2019-12-18 07:12:39 +01:00
|
|
|
);
|
|
|
|
}
|
2017-08-08 08:02:55 +02:00
|
|
|
|
2019-12-18 07:12:39 +01:00
|
|
|
function _showAnswer(a, bodyclass) {
|
|
|
|
_updateQA(
|
|
|
|
a,
|
|
|
|
aFade,
|
2020-06-24 13:54:38 +02:00
|
|
|
function () {
|
2019-12-18 07:12:39 +01:00
|
|
|
if (bodyclass) {
|
|
|
|
// when previewing
|
|
|
|
document.body.className = bodyclass;
|
|
|
|
}
|
|
|
|
|
|
|
|
// scroll to answer?
|
|
|
|
var e = $("#answer");
|
|
|
|
if (e[0]) {
|
|
|
|
e[0].scrollIntoView();
|
|
|
|
}
|
|
|
|
},
|
2020-06-24 13:54:38 +02:00
|
|
|
function () {}
|
2019-12-18 07:12:39 +01:00
|
|
|
);
|
2017-07-29 06:24:45 +02:00
|
|
|
}
|
|
|
|
|
2019-12-18 04:32:32 +01:00
|
|
|
const _flagColours = {
|
2018-11-01 05:58:41 +01:00
|
|
|
1: "#ff6666",
|
|
|
|
2: "#ff9900",
|
|
|
|
3: "#77ff77",
|
2019-12-18 07:12:39 +01:00
|
|
|
4: "#77aaff",
|
2017-08-12 08:08:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
function _drawFlag(flag) {
|
|
|
|
var elem = $("#_flag");
|
|
|
|
if (flag === 0) {
|
|
|
|
elem.hide();
|
|
|
|
return;
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
2017-08-12 08:08:10 +02:00
|
|
|
elem.show();
|
|
|
|
elem.css("color", _flagColours[flag]);
|
2017-07-28 08:19:06 +02:00
|
|
|
}
|
|
|
|
|
2017-08-16 12:30:29 +02:00
|
|
|
function _drawMark(mark) {
|
|
|
|
var elem = $("#_mark");
|
|
|
|
if (!mark) {
|
|
|
|
elem.hide();
|
|
|
|
} else {
|
|
|
|
elem.show();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-28 08:19:06 +02:00
|
|
|
function _typeAnsPress() {
|
2019-12-18 04:32:32 +01:00
|
|
|
if ((window.event as KeyboardEvent).keyCode === 13) {
|
2017-07-28 08:19:06 +02:00
|
|
|
pycmd("ans");
|
|
|
|
}
|
|
|
|
}
|
2020-08-03 05:30:34 +02:00
|
|
|
|
|
|
|
function _emulateMobile(enabled: boolean) {
|
|
|
|
const list = document.documentElement.classList;
|
|
|
|
if (enabled) {
|
|
|
|
list.add("mobile");
|
|
|
|
} else {
|
|
|
|
list.remove("mobile");
|
|
|
|
}
|
|
|
|
}
|