diff --git a/gulpfile.js b/gulpfile.js index 00ddd9d08..e143df0fc 100644 --- a/gulpfile.js +++ b/gulpfile.js @@ -115,6 +115,7 @@ const refactoredSrc = [ "./src/js/test/keymap.js", "./src/js/test/live-wpm.js", "./src/js/test/caps-warning.js", + "./src/js/test/live-acc.js" ]; //legacy files diff --git a/src/js/commandline.js b/src/js/commandline.js index 7a066a28b..d1fe2c3bd 100644 --- a/src/js/commandline.js +++ b/src/js/commandline.js @@ -129,7 +129,7 @@ let commands = { id: "toggleShowLiveAcc", display: "Toggle live accuracy display", exec: () => { - toggleShowLiveAcc(); + LiveAcc.show(); }, }, { diff --git a/src/js/global-dependencies.js b/src/js/global-dependencies.js index bb80dda6a..ed440567d 100644 --- a/src/js/global-dependencies.js +++ b/src/js/global-dependencies.js @@ -44,3 +44,4 @@ import * as TestUI from "./test-ui"; import * as Keymap from "./keymap"; import * as LiveWpm from "./live-wpm"; import * as CapsWarning from "./caps-warning"; +import * as LiveAcc from './live-acc'; \ No newline at end of file diff --git a/src/js/script.js b/src/js/script.js index 1cfd2d278..5323c7357 100644 --- a/src/js/script.js +++ b/src/js/script.js @@ -1262,7 +1262,7 @@ function showResult(difficultyFailed = false) { Caret.hide(); LiveWpm.hide(); hideCrown(); - hideLiveAcc(); + LiveAcc.hide(); hideTimer(); Keymap.hide(); let stats = calculateStats(); @@ -2252,7 +2252,7 @@ function startTest() { showTimer(); $("#liveWpm").text("0"); LiveWpm.show(); - showLiveAcc(); + LiveAcc.show(); updateTimer(); clearTimeout(timer); @@ -2462,7 +2462,7 @@ function restartTest(withSameWordset = false, nosave = false, event) { Caret.hide(); testActive = false; LiveWpm.hide(); - hideLiveAcc(); + LiveAcc.hide(); hideTimer(); bailout = false; paceCaret = null; @@ -2807,70 +2807,6 @@ function liveWpmAndRaw() { }; } -function updateLiveAcc(acc) { - if (!testActive || !Config.showLiveAcc) { - hideLiveAcc(); - } else { - showLiveAcc(); - } - let number = Math.floor(acc); - if (Config.blindMode) { - number = 100; - } - document.querySelector("#miniTimerAndLiveWpm .acc").innerHTML = number + "%"; - document.querySelector("#liveAcc").innerHTML = number + "%"; -} - -function showLiveAcc() { - if (!Config.showLiveAcc) return; - if (!testActive) return; - if (Config.timerStyle === "mini") { - // $("#miniTimerAndLiveWpm .wpm").css("opacity", Config.timerOpacity); - if (!$("#miniTimerAndLiveWpm .acc").hasClass("hidden")) return; - $("#miniTimerAndLiveWpm .acc") - .removeClass("hidden") - .css("opacity", 0) - .animate( - { - opacity: Config.timerOpacity, - }, - 125 - ); - } else { - // $("#liveWpm").css("opacity", Config.timerOpacity); - if (!$("#liveAcc").hasClass("hidden")) return; - $("#liveAcc").removeClass("hidden").css("opacity", 0).animate( - { - opacity: Config.timerOpacity, - }, - 125 - ); - } -} - -function hideLiveAcc() { - // $("#liveWpm").css("opacity", 0); - // $("#miniTimerAndLiveWpm .wpm").css("opacity", 0); - $("#liveAcc").animate( - { - opacity: Config.timerOpacity, - }, - 125, - () => { - $("#liveAcc").addClass("hidden"); - } - ); - $("#miniTimerAndLiveWpm .acc").animate( - { - opacity: Config.timerOpacity, - }, - 125, - () => { - $("#miniTimerAndLiveWpm .acc").addClass("hidden"); - } - ); -} - function toggleResultWordsDisplay() { if (TestUI.resultVisible) { if ($("#resultWordsHistory").stop(true, true).hasClass("hidden")) { @@ -4015,7 +3951,7 @@ $(document).keydown(function (event) { } let acc = Misc.roundTo2(TestStats.calculateAccuracy()); - updateLiveAcc(acc); + LiveAcc.update(acc); }); function handleTab(event) { diff --git a/src/js/test/live-acc.js b/src/js/test/live-acc.js new file mode 100644 index 000000000..363125e87 --- /dev/null +++ b/src/js/test/live-acc.js @@ -0,0 +1,61 @@ +import Config from './config'; + +export function update(acc) { + let number = Math.floor(acc); + if (Config.blindMode) { + number = 100; + } + document.querySelector("#miniTimerAndLiveWpm .acc").innerHTML = number + "%"; + document.querySelector("#liveAcc").innerHTML = number + "%"; +} + + +export function show() { + if (!Config.showLiveAcc) return; + // if (!testActive) return; + if (Config.timerStyle === "mini") { + // $("#miniTimerAndLiveWpm .wpm").css("opacity", Config.timerOpacity); + if (!$("#miniTimerAndLiveWpm .acc").hasClass("hidden")) return; + $("#miniTimerAndLiveWpm .acc") + .removeClass("hidden") + .css("opacity", 0) + .animate( + { + opacity: Config.timerOpacity, + }, + 125 + ); + } else { + // $("#liveWpm").css("opacity", Config.timerOpacity); + if (!$("#liveAcc").hasClass("hidden")) return; + $("#liveAcc").removeClass("hidden").css("opacity", 0).animate( + { + opacity: Config.timerOpacity, + }, + 125 + ); + } +} + +export function hide() { + // $("#liveWpm").css("opacity", 0); + // $("#miniTimerAndLiveWpm .wpm").css("opacity", 0); + $("#liveAcc").animate( + { + opacity: Config.timerOpacity, + }, + 125, + () => { + $("#liveAcc").addClass("hidden"); + } + ); + $("#miniTimerAndLiveWpm .acc").animate( + { + opacity: Config.timerOpacity, + }, + 125, + () => { + $("#miniTimerAndLiveWpm .acc").addClass("hidden"); + } + ); +} diff --git a/src/js/userconfig.js b/src/js/userconfig.js index ed27c321b..811becc22 100644 --- a/src/js/userconfig.js +++ b/src/js/userconfig.js @@ -609,11 +609,21 @@ function setShowLiveAcc(live, nosave) { live = false; } ConfigSet.showLiveAcc(live); + if (live) { + LiveAcc.show(); + } else { + LiveAcc.hide(); + } if (!nosave) saveConfigToCookie(); } -function toggleShowLiveAcc() { +function toggleLiveAcc() { ConfigSet.showLiveAcc(!Config.showLiveAcc); + if (Config.showLiveAcc) { + LiveAcc.show(); + } else { + LiveAcc.hide(); + } saveConfigToCookie(); }