monkeytype/src/js/ui.js
Jack 239e3e61d6 lots of little code refactoring #495
relocated smaller functions and most events
divided code into small modules (layout emulator, support popup, version popup)
2021-03-29 00:19:17 +01:00

195 lines
4.9 KiB
JavaScript

import Config, * as UpdateConfig from "./config";
import * as Notifications from "./notification-center";
import * as Leaderboards from "./leaderboards";
import * as ManualRestart from "./manual-restart-tracker";
import * as Misc from "./misc";
import * as Caret from "./caret";
import * as TestLogic from "./test-logic";
import * as CustomText from "./custom-text";
import * as CommandlineLists from "./commandline-lists";
import * as Commandline from "./commandline";
export let pageTransition = false;
export function setPageTransition(val) {
pageTransition = val;
}
export function updateKeytips() {
if (Config.swapEscAndTab) {
$(".pageSettings .tip").html(`
tip: You can also change all these settings quickly using the
command line (
<key>tab</key>
)`);
$("#bottom .keyTips").html(`
<key>esc</key> - restart test<br>
<key>tab</key> - command line`);
} else {
$(".pageSettings .tip").html(`
tip: You can also change all these settings quickly using the
command line (
<key>esc</key>
)`);
$("#bottom .keyTips").html(`
<key>tab</key> - restart test<br>
<key>esc</key> - command line`);
}
}
export function swapElements(
el1,
el2,
totalDuration,
callback = function () {
return;
},
middleCallback = function () {
return;
}
) {
if (
(el1.hasClass("hidden") && !el2.hasClass("hidden")) ||
(!el1.hasClass("hidden") && el2.hasClass("hidden"))
) {
//one of them is hidden and the other is visible
if (el1.hasClass("hidden")) {
callback();
return false;
}
$(el1)
.removeClass("hidden")
.css("opacity", 1)
.animate(
{
opacity: 0,
},
totalDuration / 2,
() => {
middleCallback();
$(el1).addClass("hidden");
$(el2)
.removeClass("hidden")
.css("opacity", 0)
.animate(
{
opacity: 1,
},
totalDuration / 2,
() => {
callback();
}
);
}
);
} else if (el1.hasClass("hidden") && el2.hasClass("hidden")) {
//both are hidden, only fade in the second
$(el2)
.removeClass("hidden")
.css("opacity", 0)
.animate(
{
opacity: 1,
},
totalDuration,
() => {
callback();
}
);
} else {
callback();
}
}
if (firebase.app().options.projectId === "monkey-type-dev-67af4") {
$("#top .logo .bottom").text("monkey-dev");
$("head title").text("Monkey Dev");
$("body").append(
`<div class="devIndicator tr">DEV</div><div class="devIndicator bl">DEV</div>`
);
}
if (window.location.hostname === "localhost") {
window.onerror = function (error) {
Notifications.add(error, -1);
};
$("#top .logo .top").text("localhost");
$("head title").text($("head title").text() + " (localhost)");
firebase.functions().useFunctionsEmulator("http://localhost:5001");
$("body").append(
`<div class="devIndicator tl">local</div><div class="devIndicator br">local</div>`
);
}
//stop space scrolling
window.addEventListener("keydown", function (e) {
if (e.keyCode == 32 && e.target == document.body) {
e.preventDefault();
}
});
$(".merchBanner a").click((event) => {
$(".merchBanner").remove();
Misc.setCookie("merchbannerclosed", true, 365);
});
$(".merchBanner .fas").click((event) => {
$(".merchBanner").remove();
Misc.setCookie("merchbannerclosed", true, 365);
Notifications.add(
"Won't remind you anymore. Thanks for continued support <3",
0,
5
);
});
$(".scrollToTopButton").click((event) => {
window.scrollTo(0, 0);
});
$(document).on("click", "#bottom .leftright .right .current-theme", (e) => {
if (e.shiftKey) {
UpdateConfig.toggleCustomTheme();
} else {
// if (Config.customTheme) {
// toggleCustomTheme();
// }
CommandlineLists.setCurrent(CommandlineLists.themeCommands);
Commandline.show();
}
});
$(document.body).on("click", ".pageAbout .aboutEnableAds", () => {
CommandlineLists.pushCurrent(CommandlineLists.commandsEnableAds);
Commandline.show();
});
window.addEventListener("beforeunload", (event) => {
// Cancel the event as stated by the standard.
if (
(Config.mode === "words" && Config.words < 1000) ||
(Config.mode === "time" && Config.time < 3600) ||
Config.mode === "quote" ||
(Config.mode === "custom" &&
CustomText.isWordRandom &&
CustomText.word < 1000) ||
(Config.mode === "custom" &&
CustomText.isTimeRandom &&
CustomText.time < 1000) ||
(Config.mode === "custom" &&
!CustomText.isWordRandom &&
CustomText.text.length < 1000)
) {
//ignore
} else {
if (TestLogic.active) {
event.preventDefault();
// Chrome requires returnValue to be set.
event.returnValue = "";
}
}
});
$(window).resize(() => {
Caret.updatePosition();
});