From ba6b533b00a3304c7a11c1ea7e7e4ae25e13d197 Mon Sep 17 00:00:00 2001 From: JDonadio Date: Mon, 16 Oct 2017 15:01:18 -0300 Subject: [PATCH] add key event listener --- src/pages/send/amount/amount.ts | 36 +++++++++++++++++++++++---------- src/pages/send/send.ts | 10 +++++---- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/pages/send/amount/amount.ts b/src/pages/send/amount/amount.ts index 4c7cc1ef8..62ff7cb11 100644 --- a/src/pages/send/amount/amount.ts +++ b/src/pages/send/amount/amount.ts @@ -1,4 +1,4 @@ -import { Component } from '@angular/core'; +import { Component, HostListener } from '@angular/core'; import { NavController, NavParams } from 'ionic-angular'; import * as _ from 'lodash'; import { ConfirmPage } from '../confirm/confirm'; @@ -21,18 +21,35 @@ export class AmountPage { private SMALL_FONT_SIZE_LIMIT = 10; private availableUnits: Array = []; private unitIndex: number = 0; + private reNr: RegExp = /^[1234567890\.]$/; + private reOp: RegExp = /^[\*\+\-\/]$/; constructor(public navCtrl: NavController, public navParams: NavParams) { - this.amount = ""; + this.amount = ''; this.allowSend = false; } - + ionViewDidLoad() { - console.log('ionViewDidLoad AmountPage'); - console.log("this.navParams.data", this.navParams.data); + console.log('Params', this.navParams.data); this.address = this.navParams.data.address; this.sending = this.navParams.data.sending; } + + @HostListener('document:keydown', ['$event']) handleKeyboardEvent(event: KeyboardEvent) { + if (!event.key) return; + if (event.which === 8) { + event.preventDefault(); + this.removeDigit(); + } + + if (event.key.match(this.reNr)) { + this.pushDigit(event.key); + } else if (event.key.match(this.reOp)) { + this.pushOperator(event.key); + } else if (event.keyCode === 86) { + // if (event.ctrlKey || event.metaKey) processClipboard(); + } else if (event.keyCode === 13) this.finish(); + } pushDigit(digit: string) { if (this.amount && this.amount.length >= this.LENGTH_EXPRESSION_LIMIT) return; @@ -41,7 +58,6 @@ export class AmountPage { //if (this.availableUnits[this.unitIndex].isFiat && this.amount.indexOf('.') > -1 && this.amount[this.amount.indexOf('.') + 2]) return; this.amount = (this.amount + digit).replace('..', '.'); - console.log("this.amount", this.amount); this.checkFontSize(); this.processAmount(); }; @@ -57,7 +73,7 @@ export class AmountPage { this.amount = this._pushOperator(this.amount, operator); }; - _pushOperator(val: string, operator: string) { + private _pushOperator(val: string, operator: string) { if (!this.isOperator(_.last(val))) { return val + operator; } else { @@ -66,12 +82,12 @@ export class AmountPage { }; isOperator(val: string) { - var regex = /[\/\-\+\x\*]/; + const regex = /[\/\-\+\x\*]/; return regex.test(val); }; isExpression(val: string) { - var regex = /^\.?\d+(\.?\d+)?([\/\-\+\*x]\d?\.?\d+)+$/; + const regex = /^\.?\d+(\.?\d+)?([\/\-\+\*x]\d?\.?\d+)+$/; return regex.test(val); }; @@ -122,7 +138,6 @@ export class AmountPage { return 0; } if (!_.isFinite(result)) return 0; - console.log("evaluate result: ", result); return result; }; @@ -152,5 +167,4 @@ export class AmountPage { this.navCtrl.push(CustomAmountPage, {address: this.address, amount: this.globalResult}); } } - } diff --git a/src/pages/send/send.ts b/src/pages/send/send.ts index 698970f85..fe88ec0e0 100644 --- a/src/pages/send/send.ts +++ b/src/pages/send/send.ts @@ -1,5 +1,5 @@ import { Component } from '@angular/core'; -import { NavController, NavParams } from 'ionic-angular'; +import { NavController } from 'ionic-angular'; import * as _ from 'lodash'; import { AmountPage } from './amount/amount'; @@ -8,11 +8,13 @@ import { AmountPage } from './amount/amount'; templateUrl: 'send.html', }) export class SendPage { + public search: string; - constructor(public navCtrl: NavController, public navParams: NavParams) { + constructor(public navCtrl: NavController) { } - + ionViewDidLoad() { + this.search = ''; console.log('ionViewDidLoad SendPage'); } @@ -23,7 +25,7 @@ export class SendPage { findContact(search: string) { // TODO: Improve this function console.log("Send search string", search); - if (search.length === 34 && _.startsWith(search, '1')) { + if (search.length === 34) { this.navCtrl.push(AmountPage, {address: search, sending: true}); } }