that holds the elements
+ */
+const makeControls = () => {
+ const wrapper = document.createElement( 'div' );
+ wrapper.setAttribute( 'id', 'qr-generator-controls' );
+
+ const targetLabel = document.createElement( 'label' );
+ targetLabel.setAttribute( 'for', 'qr-generator--target' );
+ targetLabel.innerText = 'Target URL:';
+ wrapper.append( targetLabel );
+
+ const targetElem = document.createElement( 'input' );
+ targetElem.setAttribute( 'type', 'text' );
+ targetElem.setAttribute( 'id', 'qr-generator--target' );
+
+ wrapper.append( targetElem );
+
+ const generateBtn = document.createElement( 'button' );
+ generateBtn.setAttribute( 'id', 'qr-generator--btn' );
+ generateBtn.innerText = 'Generate';
+
+ const emitGeneration = () => {
+ const newEvent = new CustomEvent(
+ 'do-generate',
+ {
+ detail: {
+ url: targetElem.value
+ }
+ }
+ );
+ wrapper.dispatchEvent( newEvent );
+ };
+
+ generateBtn.addEventListener( 'click', emitGeneration );
+
+ // Listen to the enter key
+ targetElem.addEventListener(
+ 'keypress' ,
+ ( e ) => {
+ if ( e.key === 'Enter' ) {
+ emitGeneration();
+ }
+ }
+ );
+
+ wrapper.append( generateBtn );
+
+ return wrapper;
+};
+
+const onLoad = () => {
+ // Popup to avoid accidental reloads
+ window.onbeforeunload = () => false;
+
+ const target = document.getElementById( 'tool-target' );
+
+ const controls = makeControls();
+ target.append( controls );
+
+ const outputElem = document.createElement( 'div' );
+ outputElem.setAttribute( 'id', 'qr-generator-output' );
+ target.append( outputElem );
+
+ const downloadNote = document.createElement( 'p' );
+ downloadNote.innerText = 'To download, right click on the image and save the image.';
+ downloadNote.innerText += 'You can submit a new URL to generate a new QR code.'
+
+ const downloadNoteHolder = document.createElement( 'div' );
+ target.append( downloadNoteHolder );
+
+ controls.addEventListener(
+ 'do-generate',
+ ( e ) => {
+ // console.log( e );
+ // So that a new URL clears the existing image while waiting
+ outputElem.replaceChildren();
+ outputElem.replaceChildren( makeQRImg( e.detail.url ) )
+ if ( downloadNoteHolder.children.length === 0 ) {
+ downloadNoteHolder.append( downloadNote );
+ }
+ }
+ );
+};
+
+document.addEventListener( 'DOMContentLoaded', onLoad );
diff --git a/src/Pages/ToolPage.php b/src/Pages/ToolPage.php
index 19f2e3f..61a2c05 100644
--- a/src/Pages/ToolPage.php
+++ b/src/Pages/ToolPage.php
@@ -12,6 +12,7 @@ class ToolPage extends BasePage {
private const TOOL_MAPPING = [
'countdown' => true,
'dvorak' => true,
+ 'qr-generator' => true,
];
public function __construct( array $params ) {
diff --git a/tests/data/tools-index.html b/tests/data/tools-index.html
index 10a841a..2cc3bf1 100644
--- a/tests/data/tools-index.html
+++ b/tests/data/tools-index.html
@@ -1,3 +1,3 @@
-
Known tools These are just some JavaScript utilities for Daniel, though anyone who wants to
+
Tools Known tools These are just some JavaScript utilities for Daniel, though anyone who wants to
is obviously free to use them.
\ No newline at end of file
diff --git a/tests/data/tools-missing.html b/tests/data/tools-missing.html
index 0e63ce3..8470a54 100644
--- a/tests/data/tools-missing.html
+++ b/tests/data/tools-missing.html
@@ -1,3 +1,3 @@
-
Tools Error The requested tool `missing` is not recognized
Known tools These are just some JavaScript utilities for Daniel, though anyone who wants to
+
Tools Error The requested tool `missing` is not recognized
Known tools These are just some JavaScript utilities for Daniel, though anyone who wants to
is obviously free to use them.
\ No newline at end of file