From 4581de11324a94d3911d0bcf31338e33e70ae254 Mon Sep 17 00:00:00 2001 From: ReznoRMichael Date: Mon, 12 Jul 2021 22:23:29 +0200 Subject: [PATCH] move page functions code to page-functions.js --- src/js/HKCheckCompletion.js | 47 ------------------------------------- src/js/page-functions.js | 47 +++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 47 deletions(-) diff --git a/src/js/HKCheckCompletion.js b/src/js/HKCheckCompletion.js index 4a77fc4..8901a01 100644 --- a/src/js/HKCheckCompletion.js +++ b/src/js/HKCheckCompletion.js @@ -1764,43 +1764,6 @@ function ResetCompletion(db) { } } -/** - * Focuses, selects and copies to clipboard contents inside a clicked element. Includes optional tooltip update after the copying is done. - * @param {MouseEvent} mouseEvent from the clicked element (AddEventListener) - * @param {string} tooltipId Element ID of the tooltip to update - * @param {string} tooltipFill Updated contents of the tooltip - */ -function SelectCopyInputText(mouseEvent, tooltipId = "", tooltipFill = "") { - - const element = document.getElementById(mouseEvent.target.id); - - // this prevents the un-selected effect after clicking the second time (clears all selection first) - if (window.getSelection) { - window.getSelection().removeAllRanges(); - } - - element.focus(); // best to focus the element first before selecting - element.select(); - element.setSelectionRange(0, 99999); // for mobile devices - - // Copy the text inside the text field to clipboard - document.execCommand("copy"); - - // optional tooltip showing - if (tooltipFill.length && tooltipId.length) FillInnerHTML(tooltipId, tooltipFill); -} - -/** - * Fills the innerHTML of a given HTML Element with provided contents - * @param {string} elementId Element ID to update - * @param {string} textFill Updated contents (innerHTML) - */ -function FillInnerHTML(elementId, textFill) { - - const element = document.getElementById(elementId); - element.innerHTML = textFill; -} - /* ----------------------- Event Listeners -------------------------- */ // Populate HTML at load (before img and css) @@ -1808,16 +1771,6 @@ document.addEventListener("DOMContentLoaded", () => { InitializeHTMLPopulation(HK); }); -// Does an action when the save file location input text is clicked once (auto select & copy to clipboard) -document.getElementById("save-location-input").addEventListener("click", (e) => { - SelectCopyInputText(e, "save-location-input-tooltip", "Copied save file location to clipboard"); -}, false); - -// Choose File input field -document.getElementById("save-location-input").addEventListener("mouseout", () => { - FillInnerHTML("save-location-input-tooltip", "Click once to copy to clipboard"); -}, false); - // Analyze Text button document.getElementById("save-area-read").addEventListener("click", () => { HKReadTextArea("save-area"); diff --git a/src/js/page-functions.js b/src/js/page-functions.js index c502f7a..66f240e 100644 --- a/src/js/page-functions.js +++ b/src/js/page-functions.js @@ -994,6 +994,43 @@ function StorageAvailable(type) { } } +/** + * Fills the innerHTML of a given HTML Element with provided contents + * @param {string} elementId Element ID to update + * @param {string} textFill Updated contents (innerHTML) + */ + function FillInnerHTML(elementId, textFill) { + + const element = document.getElementById(elementId); + element.innerHTML = textFill; +} + +/** + * Focuses, selects and copies to clipboard contents inside a clicked element. Includes optional tooltip update after the copying is done. + * @param {MouseEvent} mouseEvent from the clicked element (AddEventListener) + * @param {string} tooltipId Element ID of the tooltip to update + * @param {string} tooltipFill Updated contents of the tooltip + */ + function SelectCopyInputText(mouseEvent, tooltipId = "", tooltipFill = "") { + + const element = document.getElementById(mouseEvent.target.id); + + // this prevents the un-selected effect after clicking the second time (clears all selection first) + if (window.getSelection) { + window.getSelection().removeAllRanges(); + } + + element.focus(); // best to focus the element first before selecting + element.select(); + element.setSelectionRange(0, 99999); // for mobile devices + + // Copy the text inside the text field to clipboard + document.execCommand("copy"); + + // optional tooltip showing + if (tooltipFill.length && tooltipId.length) FillInnerHTML(tooltipId, tooltipFill); +} + /* ----------------------- Event Listeners -------------------------- */ document.addEventListener("scroll", () => { @@ -1010,6 +1047,16 @@ SCROLL_BUTTON.addEventListener("click", () => { ScrollToElement(ROOT); }); +// Auto select & copy to clipboard when the save file location input text is clicked once +document.getElementById("save-location-input").addEventListener("click", (e) => { + SelectCopyInputText(e, "save-location-input-tooltip", "Copied save file location to clipboard"); +}, false); + +// Switch text back to the default on mouse out +document.getElementById("save-location-input").addEventListener("mouseout", () => { + FillInnerHTML("save-location-input-tooltip", "Click once to copy to clipboard"); +}, false); + // Checkboxes functions document.getElementById("checkbox-hints").addEventListener("click", CheckboxHintsToggle, false); document.getElementById("checkbox-spoilers").addEventListener("click", CheckboxSpoilersToggle, false);