From 3ab5e7827c05af30a0eae34d46759b43eba8a8ef Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Wed, 3 Oct 2018 02:49:37 +0000 Subject: [PATCH] Tweaked {HTML, JavaScript, TypeScript} code to move repetitive event handler registration from HTML to script. --- javascript/qrcodegen-demo.js | 15 ++++++++++++++- javascript/qrcodegen-js-demo.html | 22 +++++++++++----------- typescript/qrcodegen-demo.ts | 15 ++++++++++++++- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/javascript/qrcodegen-demo.js b/javascript/qrcodegen-demo.js index 42366a1..9ca9490 100644 --- a/javascript/qrcodegen-demo.js +++ b/javascript/qrcodegen-demo.js @@ -24,6 +24,19 @@ "use strict"; +function initialize() { + var elems = document.querySelectorAll("input[type=number], textarea"); + for (var i = 0; i < elems.length; i++) { + if (elems[i].id.indexOf("version-") != 0) + elems[i].oninput = redrawQrCode; + } + elems = document.querySelectorAll("input[type=radio], input[type=checkbox]"); + for (var i = 0; i < elems.length; i++) + elems[i].onchange = redrawQrCode; + redrawQrCode(); +} + + function redrawQrCode() { // Show/hide rows based on bitmap/vector image output var bitmapOutput = document.getElementById("output-format-bitmap").checked; @@ -147,4 +160,4 @@ function handleVersionMinMax(which) { } -redrawQrCode(); +initialize(); diff --git a/javascript/qrcodegen-js-demo.html b/javascript/qrcodegen-js-demo.html index 29ca168..d534d7f 100644 --- a/javascript/qrcodegen-js-demo.html +++ b/javascript/qrcodegen-js-demo.html @@ -56,7 +56,7 @@ Text string: - + QR Code: @@ -71,26 +71,26 @@ Error correction: - - - - + + + + Output format: - - + + Border: - modules + modules Scale: - pixels per module + pixels per module Version range: @@ -101,11 +101,11 @@ Mask pattern: - (−1 for automatic, 0 to 7 for manual) + (−1 for automatic, 0 to 7 for manual) Boost ECC: - + Statistics: diff --git a/typescript/qrcodegen-demo.ts b/typescript/qrcodegen-demo.ts index ee8d987..c1fe6bd 100644 --- a/typescript/qrcodegen-demo.ts +++ b/typescript/qrcodegen-demo.ts @@ -28,6 +28,19 @@ const myWindow = window as any; + function initialize(): void { + let elems = document.querySelectorAll("input[type=number], textarea"); + for (let el of elems) { + if (!el.id.startsWith("version-")) + (el as any).oninput = myWindow.redrawQrCode; + } + elems = document.querySelectorAll("input[type=radio], input[type=checkbox]"); + for (let el of elems) + (el as HTMLInputElement).onchange = myWindow.redrawQrCode; + myWindow.redrawQrCode(); + } + + /*---- Functions called from HTML code ----*/ myWindow.redrawQrCode = function(): void { @@ -176,5 +189,5 @@ /*---- Initialization ----*/ - myWindow.redrawQrCode(); + initialize(); }