From 517736ae1d3813fb3bc7e32c36cd90906d530b91 Mon Sep 17 00:00:00 2001 From: Marvin Borner Date: Sun, 27 Jan 2019 18:25:08 +0100 Subject: Cleaned up code --- public/scripts/input_pin.js | 57 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 public/scripts/input_pin.js (limited to 'public/scripts/input_pin.js') diff --git a/public/scripts/input_pin.js b/public/scripts/input_pin.js new file mode 100644 index 0000000..b496e95 --- /dev/null +++ b/public/scripts/input_pin.js @@ -0,0 +1,57 @@ +const $ = require('jquery'); +let pin = []; + +/** + * Initializes actions of the pin input field + * @param callback + */ +function init(callback) { + let tryCount = 0; + + $('#1').focus().on('input', (el) => { + pin.push($(el.target).val()); + $(el.target).val('*'); + $('#' + (parseInt($(el.target).attr('id')) + 1).toString()).focus(); + }); + + $('#2').on('input', (el) => { + pin.push($(el.target).val()); + $(el.target).val('*'); + $('#' + (parseInt($(el.target).attr('id')) + 1).toString()).focus(); + }); + + $('#3').on('input', (el) => { + pin.push($(el.target).val()); + $(el.target).val('*'); + $('#' + (parseInt($(el.target).attr('id')) + 1).toString()).focus(); + }); + + $('#4').on('input', (el) => { + pin.push($(el.target).val()); + $(el.target).val('*'); + tryCount++; + callback(pin.join(''), tryCount) + }); +} + +/** + * Displays error message and clears input + */ +function isWrong(message) { + $('#pin_message').text(message); + for (let i = 1; i < 5; i++) $(`#${i}`).val(''); + $('#1').focus(); + pin = []; +} + +/** + * Display generation message + */ +function needsGeneration() { + // TODO: Add loading animation + $('#pin_message').text('Generating keys...'); +} + +exports.init = init; +exports.failure = isWrong; +exports.generate = needsGeneration; -- cgit v1.2.3