-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUICalculator.ts
91 lines (79 loc) · 3.56 KB
/
UICalculator.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/// <reference path="IUICalculator.ts" />
module Calculator {
export class UICalculator implements IUICalculator {
private element: HTMLElement;
private operation: IOperation;
private _operationIcons: { [name: string]: string } = {
"addition": "+",
"subtraction": "-",
"multiplication": "*",
"division": "/"
}
private _valFromMem: number;
private _curInput: number;
constructor(element: HTMLElement, operation: IOperation) {
this.element = element;
this.operation = operation;
this.createInputArea("CalcInput");
this.createButton("addition", "+", this.operationOnClick);
this.createButton("subtraction", "-", this.operationOnClick);
this.createButton("multiplication", "*", this.operationOnClick);
this.createButton("division", "/", this.operationOnClick);
}
createInputArea = (id: string): void => {
var inputEl = document.createElement('input');
inputEl.id = id;
this.element.appendChild(inputEl);
}
createButton = (id: string, text: string, callback): void => {
var buttonEl = document.createElement('button');
buttonEl.id = id;
buttonEl.name = id;
buttonEl.innerHTML = text;
buttonEl.addEventListener("click", callback, false);
this.element.appendChild(buttonEl);
}
calculateOperation = (firstOperand: number, secondOperand: number, operation: string): number => {
var result: number = 0;
switch (operation) {
case "addition":
result = this.operation.addition(firstOperand, secondOperand);
break;
case "subtraction":
result = this.operation.subtraction(firstOperand, secondOperand);
break;
case "multiplication":
result = this.operation.multiplication(firstOperand, secondOperand);
break;
case "division":
result = this.operation.division(firstOperand, secondOperand);
break;
}
return result;
}
operationOnClick = (e): void => {
this.getValues(e.target.name);
this.validateValues(e.target.name);
}
getValues = (operation: string): void => {
this._valFromMem = parseInt((<HTMLInputElement>document.getElementById("memory")).innerHTML);
this._curInput = parseInt((<HTMLInputElement>document.getElementById("CalcInput")).value);
}
validateValues = (operation: string): void => {
var inputElement = <HTMLInputElement>document.getElementById('CalcInput');
var memElement = <HTMLInputElement>document.getElementById("memory");
var symbolElement = <HTMLInputElement>document.getElementById("operationSymbol")
if (isNaN(this._curInput)) {
inputElement.innerHTML = this._operationIcons[operation];
} else if (!isNaN(this._valFromMem)) {
var result = this.calculateOperation(this._valFromMem, this._curInput, operation);
memElement.innerHTML = "" + result;
} else {
memElement.innerHTML = "" + this._curInput;
}
inputElement.value = "";
inputElement.focus();
symbolElement.innerHTML = this._operationIcons[operation];
}
}
}