-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
232 lines (174 loc) · 5.73 KB
/
index.html
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
<!DOCTYPE html><html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Math Power! Calculator using external AWS Lambda function and AWS API Gateway</title>
<style type="text/css">
input, select, button, #useAWSLambdaLabel {
margin-left: 0.3rem;
margin-right: 0.3rem;
padding: 0.3rem;
border-radius: 0.3rem;
}
button {
width: 7.5rem;
height: 2.4rem;
}
select:hover, button:hover, #useAWSLambdaLabel:hover {
background-color: blue;
color: white;
}
button:focus {
background-color: darkgreen;
color: white;
}
button:hover {
cursor: pointer;
}
.form-item {
margin-bottom: 1.2rem;
}
.small-input {
max-width: 3.6rem;
}
#result {
max-width: 4.8rem;
}
.inline-elements {
display: flex;
align-items: center;
}
.inline-elements > label {
line-height: 1.5;
text-align: right;
padding-right: 1.2rem !important;
}
#httpMethod {
margin-left: 0.3rem;
}
.log-entry {
margin-bottom: 0.6rem;
}
</style>
<script defer type="text/javascript" src="./getUrlForApi.js"></script>
<script defer type="text/javascript" src="./calc.js"></script>
<script type="text/javascript">
window.addEventListener( "DOMContentLoaded", () => {
const calculateButton = document.getElementById("calculate");
const num1Input = document.getElementById("num1");
const num2Input = document.getElementById("num2");
const operatorSelect = document.getElementById("operator");
const resultInput = document.getElementById("result");
const useAWSLambdaCheckbox = document.getElementById("useAWSLambda");
const httpMethodSelect = document.getElementById("httpMethod");
const logDiv = document.getElementById("log");
const calculateInternal = (num1, operator, num2) => {
num1 = parseFloat(num1); // because +v or Number(v) both return 0 instead of NaN...
num2 = parseFloat(num2); // ...when v = null or [] or " ".
switch (operator) {
case "plus":
return num1 + num2;
case "minus":
return num1 - num2;
case "times":
return num1 * num2;
case "divided by":
return num1 / num2;
case "to the power of":
return Math.pow(num1, num2);
default:
return "[noop]";
};
};
const calculate = async (num1, operator, num2, fromApi, httpMethod) => {
const calc = fromApi ? calculateFromApi : calculateInternal;
try {
const calculated = await calc(num1, operator, num2, httpMethod);
const returned = fromApi
? parseFloat(calculated) // instead of +v or Number(v) -- both return 0 instead of NaN when v = null or [] or " ".
: calculated;
return returned;
} catch(e) {
return e.message;
};
};
const now = dt => (dt = new Date(dt ?? new Date), dt.toLocaleTimeString() + " " + dt.toDateString());
const logResults = (logValues, descending) => {
const logLine = [];
for (let value of logValues) {
logLine.push(String(value) + (logLine.length ? "" : ":"));
};
const logEntry = document.createElement("div");
logEntry.classList.add("log-entry");
logEntry.textContent = logLine.join(" ");
descending ? logDiv.insertAdjacentElement("afterbegin", logEntry) : logDiv.appendChild(logEntry);
};
const handleCalculateClick = async () => {
let num1 = num1Input.value.trim();
let num2 = num2Input.value.trim();
const operator = operatorSelect.value;
const useAWSLambda = useAWSLambdaCheckbox.checked;
const httpMethod = httpMethodSelect.value;
let values = [
num1.length ? num1 : num1 = undefined,
operator,
num2.length ?num2: num2 = undefined
];
let result = await calculate(...values, useAWSLambda, httpMethod);
logResults([
now(),
`[${useAWSLambda ? httpMethod.toUpperCase().replace(/[[\]]/g, "") : "internal"}]`,
num1,
operator,
num2,
"= " + result
]);
resultInput.value = result;
resultInput.select();
};
const focusOnCalculateButton = () => {
calculateButton.focus();
};
calculateButton.addEventListener("click", handleCalculateClick);
useAWSLambdaCheckbox.addEventListener("change", focusOnCalculateButton);
} );
</script>
</head>
<body>
<h1>Math Power! Calculator using external AWS Lambda function and AWS API Gateway</h1>
<div id="app">
<div class="form-item">
<input autofocus type="number" id="num1" class="small-input">
<select id="operator">
<option value="[choose operator]">[choose operator]</option>
<option value="plus">plus</option>
<option value="minus">minus</option>
<option value="times">times</option>
<option value="divided by">divided by</option>
<option value="to the power of">to the power of</option>
</select>
<input type="number" id="num2" class="small-input">
</div>
<div class="form-item">
<button type="button" id="calculate">Calculate</button>
<input type="text" id="result" readonly>
</div>
<div class="form-item inline-elements">
<label id="useAWSLambdaLabel">
<input type="checkbox" id="useAWSLambda">
Use external function<br />
from AWS Lambda<br />
via AWS API Gateway
</label>
<select id="httpMethod">
<option value="[choose method]">[choose method]</option>
<option value="get" selected>GET</option>
<option value="post">POST</option>
<option value="put">PUT</option>
<option value="patch">PATCH</option>
<option value="delete">DELETE</option>
</select>
</div>
<div id="log" contenteditable=""></div>
</div>
</body></html>