-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
1103 lines (1027 loc) · 41.7 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!-- Copyright © 2023 - present TheeCalculator, All rights reserved. -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="
default-src 'self';
style-src 'self' 'unsafe-inline';
script-src 'self' blob: 'unsafe-eval' filesystem: 'unsafe-inline';
">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8;">
<meta name="referrer" content="origin">
<meta name="theme-color" content="#529cbd">
<meta name="author" content="Thee Calculator Team">
<meta name="twitter:site" content="@the">
<meta name="mobile-web-app-capable" content="yes">
<meta name="twitter:card" content="summary_large_image">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-title" content="Calculator">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link href="./img/logo.png?23032531" rel="icon" type="image/png" sizes="32x32">
<link href="./img/logo.png?23032531" rel="icon" type="image/png" sizes="128x128">
<link href="./img/logo.png?23032531" rel="icon" type="image/png" sizes="180x180">
<link href="./img/logo.png?23032531" rel="icon" type="image/png" sizes="192x192">
<link href="./img/logo.png?23032531" rel="shortcut icon" type="image/png" sizes="196x196">
<link href="./img/logo.png?23032531" rel="shortcut icon" type="image/png" sizes="512x512">
<link href="./img/logo.png?23032531" rel="apple-touch-icon" type="image/png" sizes="120x120">
<link href="./img/logo.png?23032531" rel="apple-touch-icon" type="image/png" sizes="152x152">
<link href="./img/logo.png?23032531" rel="apple-touch-icon" type="image/png" sizes="180x180">
<link href="./img/logo.png?23032531" rel="apple-touch-icon" type="image/png" sizes="512x512">
<meta property="og:type" content="website">
<meta property="og:locale" content="en_GB">
<meta property="og:image:width" content="1400">
<meta property="og:image:height" content="700">
<meta property="og:site_name" content="Thee Calculator | for everyday needs">
<meta content="Thee Calculator | for everyday needs" property="og:title">
<meta content="Thee Calculator | for everyday needs" property="twitter:title">
<meta
content="Thee Calculator is for everyday needs. It helps you make basic calculations with a unique consistent experience on all your devices."
name="description">
<meta
content="Thee Calculator is for everyday needs. It helps you make basic calculations with a unique consistent experience on all your devices."
property="og:description">
<meta
content="Thee Calculator is for everyday needs. It helps you make basic calculations with a unique consistent experience on all your devices."
property="twitter:description">
<meta content="./img/cover.jpg" property="og:image">
<meta content="./img/cover.jpg" itemprop="thumbnailUrl">
<meta content="./img/cover.jpg" itemprop="image">
<meta content="./img/cover.jpg" property="twitter:image">
<meta content="Screenshot of Thee Calculator" property="twitter:image:alt">
<meta property="og:publish_date" name="publish_date" content="2023-02-26T12:21:11+01:00">
<meta property="og:modified_date" name="modified_date" content="2023-03-25T00:03:01+01:00">
<meta property="article:published_time" content="2023-02-26T12:21:11+01:00">
<meta property="article:modified_time" content="2023-03-25T00:03:01+01:00">
<meta property="og:url" content="https://theecal.com/">
<link href="./img/cover.jpg" rel="image_src">
<link rel="canonical" href="https://theecal.com/">
<link rel="manifest" href="./manifest.json?24062201">
<title>Thee Calculator | for everyday needs</title>
</head>
<style>
/* Copyright © 2023-present TheeCalculator, All rights reserved. */
:root {
--clear: transparent;
--white: white;
--black: black;
--dark: #141414;
--light_blue: #43caff1a;
--sea_blue: #529cbd;
--light_green: #9dffa22e;
--light_red: #ffbabb61;
--light_grey: #f1f1f1;
--grey: #c1c1c1;
--shadow: #00000033;
--max_width: 600px;
--large_radius: 32px;
--small_radius: 17px;
}
html {
font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
font-size: 32px;
color: var(--black);
}
body {
display: flex;
flex-direction: column;
align-items: center;
max-width: 100vw;
margin: 0;
transition: 0.5s ease-in-out;
}
p {
margin: 0;
}
a {
color: black;
}
h1,
h2 {
margin: 0;
font-weight: 700;
margin-bottom: 14px;
}
h1 {
font-size: 30px;
}
h2 {
font-size: 20px;
}
button {
border: none;
cursor: pointer;
padding: 0;
background-color: var(--light_blue);
font-size: 32px;
font-weight: 700;
outline: none;
}
.outline {
display: flex;
flex-direction: column;
align-items: center;
justify-content: flex-end;
width: 90vw;
padding: 30px 0;
height: calc(100vh - 60px);
min-height: 560px;
margin: 0;
}
.display_area {
overflow: hidden;
position: relative;
display: flex;
flex-direction: column;
justify-content: flex-end;
align-items: center;
height: 100%;
max-height: 100%;
min-height: 200px;
width: 100%;
margin-bottom: 8px;
border-radius: var(--large_radius);
max-width: var(--max_width);
background-color: var(--light_grey);
}
.logo {
width: 48px;
height: 48px;
margin: 5px 0;
}
.icon_bu {
width: 32px;
height: 32px;
padding: 16px;
opacity: 0.3;
width: 100%;
}
.icon_wrap {
display: none;
overflow: hidden;
position: absolute;
width: 64px;
height: 64px;
z-index: 3;
}
.bu_cover {
position: absolute;
z-index: 3;
width: 100%;
height: 100%;
background-color: var(--clear);
}
.copy_wrap {
bottom: 0;
right: 0;
}
.save_wrap {
bottom: 160px;
right: 0;
}
.info_wrap {
display: flex;
top: 0;
left: 0;
}
.app_info {
display: none;
padding: 0 24px 40px 24px;
font-size: 16px;
}
.info_txt {
margin-bottom: 14px;
font-weight: 300;
}
.history_clear {
top: 0;
right: 0;
}
.history_content {
position: relative;
display: flex;
flex-direction: column;
overflow: auto;
width: 100%;
max-height: 100%;
margin-top: 56px;
box-shadow: inset 0px -8px 8px -8px var(--shadow);
}
.history_content::-webkit-scrollbar {
width: 10px;
background-color: var(--clear);
}
.history_content::-webkit-scrollbar-thumb {
background-color: var(--grey);
}
.history_content::-webkit-scrollbar-track {
background-color: var(--clear);
}
.history_result {
width: 100%;
max-width: 100%;
height: 100%;
min-height: 50px;
font-size: 24px;
font-weight: 300;
padding: 0 56px 0 24px;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
text-align: left;
border-radius: 0;
background-color: var(--clear);
cursor: pointer;
}
.calc_section {
display: flex;
align-items: flex-end;
flex-direction: column;
justify-content: space-between;
text-align: right;
min-height: 160px;
width: 100%;
z-index: 0;
}
.calc_equation {
font-size: 10vh;
font-weight: 700;
width: calc(100% - 20px);
text-align: left;
padding: 0 10px;
height: 80px;
display: flex;
justify-content: flex-end;
}
.calc_result {
font-size: 40px;
width: 100%;
max-width: calc(100% - 85px);
margin: 0 65px 5px 20px;
background-color: var(--clear);
border: none;
text-align: right;
outline: none;
overflow: hidden;
}
.grid_bu {
display: grid;
grid-template-columns: repeat(4, 1fr);
column-gap: 8px;
row-gap: 8px;
width: 100%;
max-width: var(--max_width);
}
.num_bu {
position: absolute;
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 56px;
z-index: 1;
background-color: var(--clear);
}
.num_bu_wrap {
position: relative;
overflow: hidden;
border-radius: var(--large_radius);
background-color: var(--light_blue);
}
.num_bu_wrap:active {
border-radius: var(--small_radius);
filter: none !important;
}
.num_bu_2 {
grid-column: span 2;
}
.num_bu_4 {
grid-column: span 4;
}
.img_icon {
width: 48px;
height: 48px;
z-index: 0;
}
@media only screen and (max-width: 500px) {
.img_icon {
width: 32px;
height: 32px;
}
.num_bu {
height: 40px;
}
}
.margin_top_2 {
margin-top: 2vw;
}
/* General */
.expand {
width: 100%;
}
/* Colours */
.bg_l_red {
background-color: var(--light_red);
}
.bg_blue {
background-color: var(--sea_blue);
}
.bg_l_green {
background-color: var(--light_green);
}
.bg_l_grey {
background-color: var(--light_grey);
}
.bg_grey {
background-color: var(--grey);
}
.bg_dark {
background-color: var(--dark);
}
.t_white {
color: var(--white);
}
.t_black {
color: var(--black);
}
.shadow_top_bottom {
box-shadow:
inset 0px 8px 8px -8px var(--shadow),
inset 0px -8px 8px -8px var(--shadow);
}
.shadow_inside {
box-shadow: inset 0 0 4px var(--shadow);
}
.bold {
font-weight: 700;
}
.dark {
filter: invert(1);
}
</style>
<body>
<div class="outline">
<div class="display_area shadow_inside">
<div class="icon_wrap info_wrap">
<img class="icon_bu" src="./img/info.svg" alt="Information icon">
<button class="bu_cover" title="Learn more"></button>
</div>
<div class="icon_wrap history_clear">
<img class="icon_bu" src="./img/delete.svg" alt="Bin icon">
<button class="bu_cover" title="Delete history"></button>
</div>
<div class="icon_wrap save_wrap">
<img class="icon_bu" src="./img/save_off.svg" alt="Disk icon outline">
<button class="bu_cover" title="History save: off"></button>
</div>
<div class="history_content">
<div class="app_info" style="display:none;">
<img class="logo" src="./img/logo.svg?23032531" alt="T shaped as a divide sign"
title="Thee Calculator logo">
<br>
<h1>Thee Calculator</h1>
<p class="info_txt">
Thee Calculator is for everyday needs. It helps you make basic calculations with a unique
consistent experience on all your devices.
</p>
<p class="info_txt">
Thee Calculator (TheeCal) software source code is open for transparency. You can access the
source
code by checking "Sources" in your browser's developer tools or on
<a href="https://github.com/prhasn/TheeCal" target="_blank">GitHub</a>
</p>
<p class="info_txt">
Only icons are used without any letters to make Thee calculator usable for most people. Users'
feedback is continuously being used to improve the user experience. Feel free to give us
feedback
<a href="https://twitter.com/TheeCalc" target="_blank">@TheeCalc</a>
or email us <a href="mailto:[email protected]">[email protected]</a>
</p>
<br>
<h2>Privacy Policy</h2>
<p class="info_txt">
Thee Calculator does not use cookies. However, third-party service providers might use cookies
to
improve
the website performance and efficiency, e.g. Cloudflare. You can check
<a href="https://www.cloudflare.com/privacypolicy/" target="_blank">
Cloudflare's privacy policy here
</a>
</p>
<hr>
<p class="info_txt">version 1.1</p>
<p class="info_txt" style="font-weight: 500;">
TheeCal™ and TheeCalculator™ are trademarks of Thee Calculator.
Copyright © 2023 - present Thee Calculator. All rights reserved.
</p>
</div>
<button class="history_result" title="Previous equation"></button>
</div>
<div class="calc_section">
<p class="calc_equation"></p>
<p class="calc_result" title="Results value">0</p>
</div>
<div class="icon_wrap copy_wrap">
<img class="icon_bu" src="./img/copy.svg" alt="Two overlapping rectangles">
<button class="bu_cover" title="Copy the result"></button>
</div>
</div>
<div class="grid_bu">
<div class="num_bu num_bu_wrap bg_l_red">
<button id="bu_clear" class="num_bu" title="Clear values"></button>
<img class="img_icon" src="./img/refresh.svg" alt="Arrow forming a clockwise circle">
</div>
<div class="num_bu num_bu_wrap bg_l_red">
<button id="bu_back" class="num_bu" title="Backspace"></button>
<img class="img_icon" src="./img/backspace.svg"
alt="X character inside a rectangular arrow pointing left">
</div>
<div class="num_bu num_bu_wrap bg_l_green">
<button id="bu_brackets" class="num_bu" title="Add brackets"></button>
<img class="img_icon" src="./img/brackets.svg" alt="Brackets characters ()">
</div>
<div class="num_bu num_bu_wrap bg_l_green">
<button id="bu_percentage" class="num_bu" value="%" title="Percentage sign"></button>
%
</div>
<div class="num_bu num_bu_wrap">
<button id="bu_7" class="num_bu" value="7" title="Number 7"></button>
7
</div>
<div class="num_bu num_bu_wrap">
<button id="bu_8" class="num_bu" value="8" title="Number 8"></button>
8
</div>
<div class="num_bu num_bu_wrap">
<button id="bu_9" class="num_bu" value="9" title="Number 9"></button>
9
</div>
<div class="num_bu num_bu_wrap bg_l_green">
<button id="bu_divide" class="num_bu" value="÷" title="Divide sign"></button>
÷
</div>
<div class="num_bu num_bu_wrap">
<button id="bu_4" class="num_bu" value="4" title="Number 4"></button>
4
</div>
<div class="num_bu num_bu_wrap">
<button id="bu_5" class="num_bu" value="5" title="Number 5"></button>
5
</div>
<div class="num_bu num_bu_wrap">
<button id="bu_6" class="num_bu" value="6" title="Number 6"></button>
6
</div>
<div class="num_bu num_bu_wrap bg_l_green">
<button id="bu_multiply" class="num_bu" value="×" title="Multiply sign"></button>
×
</div>
<div class="num_bu num_bu_wrap">
<button id="bu_1" class="num_bu" value="1" title="Number 1"></button>
1
</div>
<div class="num_bu num_bu_wrap">
<button id="bu_2" class="num_bu" value="2" title="Number 2"></button>
2
</div>
<div class="num_bu num_bu_wrap">
<button id="bu_3" class="num_bu" value="3" title="Number 3"></button>
3
</div>
<div class="num_bu num_bu_wrap bg_l_green">
<button id="bu_minus" class="num_bu" value="-" title="Minus sign"></button>
-
</div>
<div class="num_bu num_bu_wrap num_bu_2">
<button id="bu_0" class="num_bu" value="0" title="Number 0"></button>
0
</div>
<div class="num_bu num_bu_wrap">
<button id="bu_dot" class="num_bu" value="." title="Decimal point"></button>
.
</div>
<div class="num_bu num_bu_wrap bg_l_green">
<button id="bu_plus" class="num_bu" value="+" title="Plus sign"></button>
+
</div>
<div class="num_bu num_bu_wrap num_bu_4 bg_blue" style="color:var(--white);">
<button id="bu_equal" class="num_bu expand" value="=" title="Equal sign"></button>
=
</div>
</div>
</div>
</body>
<script>
/* Copyright © 2023-present TheeCalculator (TheeCal), All rights reserved.
@preserve */
"use-strict"
const
/** TheeCalculator initiation */
start = () => {
let
/** Equation done */
equalDone = false,
/** History record array */
history = [],
/** History display entries */
resultsItems;
const
/** Vibrate */
vibrate = () => navigator?.vibrate?.(25),
/** Select element */
s = c => document.querySelector(c),
/** Select element array */
sA = c => document.querySelectorAll(c),
/** Flex display */
flex = e => e.style.display = `flex`,
/** Block display */
block = e => e.style.display = `block`,
/** Hide elements */
hide = e => e.style.display = `none`,
/** Check elements class list */
classCheck = (/** Element */ e, /** Class */ c) => e.classList.contains(c) ? false : true,
/** Disable button */
disableBu = e => e.parentElement.classList.add(`shadow_inside`),
/** Disable buttons array */
disableAll = a => a.forEach(e => disableBu(e)),
/** Enable button */
enableBu = e => e.parentElement.classList.remove(`shadow_inside`),
/** Enable buttons array */
enableAll = a => a.forEach(e => enableBu(e)),
/** Check button enable status */
enabledCheck = e => classCheck(e.parentElement, `shadow_inside`),
/** Comma addition */
commaNum = (num) => {
if (isNaN(num)) return num
const numSplit = (num + ``).split(".");
numSplit[0] = numSplit[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
return numSplit.join(".");
},
/** Comma removal */
commaNone = (calcString) => calcString.replaceAll(`,`, ``),
/** Expression buttons */
expressions = [`÷`, `×`, `-`, `+`],
/** History list */
hContent = s(`.history_content`),
/** App info */
appInfo = s(`.app_info`),
/** Calculator input section */
calcSec = s(`.calc_section`),
/** Main input display */
calcEqu = s(`.calc_equation`),
/** Results display */
calcResult = s(`.calc_result`),
/** Button elements */
bu = {
/** App info button */
info: s(`.info_wrap`),
/** Clear history button */
corner: s(`.history_clear`),
/** Toggle history save status */
save: s(`.save_wrap`),
/** Copy button */
copy: s(`.copy_wrap`),
/** Backspace button */
back: s(`#bu_back`),
/** Clear all button */
clear: s(`#bu_clear`),
/** Brackets button */
brackets: s(`#bu_brackets`),
/** Dot button */
dot: s(`#bu_dot`),
/** Numpad buttons */
numpad: sA(`.num_bu`),
/** Numpad icons */
icons: sA(`.img_icon`),
/** Numbers buttons object */
numbers: {
/** Number button 9 */
bu9: s(`#bu_9`),
/** Number button 8 */
bu8: s(`#bu_8`),
/** Number button 7 */
bu7: s(`#bu_7`),
/** Number button 6 */
bu6: s(`#bu_6`),
/** Number button 5 */
bu5: s(`#bu_5`),
/** Number button 4 */
bu4: s(`#bu_4`),
/** Number button 3 */
bu3: s(`#bu_3`),
/** Number button 2 */
bu2: s(`#bu_2`),
/** Number button 1 */
bu1: s(`#bu_1`),
/** Number button 0 */
bu0: s(`#bu_0`),
/** Percentage Button */
buPer: s(`#bu_percentage`),
},
/** Expression buttons */
exp: {
/** Divide button */
buDivide: s(`#bu_divide`),
/** Multiply button */
buMultiply: s(`#bu_multiply`),
/** Minus button */
buMinus: s(`#bu_minus`),
/** Plus button */
buPlus: s(`#bu_plus`),
},
/** Equal button */
equal: s(`#bu_equal`)
},
/** Save mode toggle */
saveToggle = () => {
const h = localStorage.history;
bu.save.children[1].title = `History save: ` + (h ? `on` : `off`);
bu.save.children[0].src = `./img/` + (h ? `save_on` : `save_off`) + `.svg`;
bu.save.children[0].alt = `Disk icon ${h ? `filled` : `outline`}`;
},
/** Content shadows update */
hShadow = () => {
const
h = hContent.scrollTop,
l = hContent.classList;
!h || h > (hContent.scrollHeight - hContent.clientHeight) ?
l.remove(`shadow_top_bottom`) : l.add(`shadow_top_bottom`)
},
/** History general update */
hUpdate = () => {
const
l = resultsItems.length,
t = history.length;
if (t > l) for (let i = l; i < t; i++)
hContent.appendChild(resultsItems[0].cloneNode(1));
else if (t < l) for (let i = (t || 1); i < l; i++)
resultsItems[i].remove();
resultsItems = sA(`.history_result`);
for (let i = 0; i < history.length; i++)
resultsItems[i].innerText = history[i];
history.length ? (
resultsItems.forEach(i => i.onclick = () => {
calcResult.innerText = ``;
calcEqu.innerText = i.innerText.split(` = `)[0];
updateUI();
vibrate();
}),
flex(bu.corner),
flex(bu.save)
) : (
resultsItems[0].innerText = ``,
hide(bu.corner),
hide(bu.save)
);
// Scroll to bottom
hContent.scrollTo(0, hContent.scrollHeight);
// Update scroll shadows
hShadow();
saveToggle();
localStorage.history && (localStorage.history = JSON.stringify(history));
},
/** Clear all inputs */
clearAll = () => {
calcEqu.innerText = `0`;
calcResult.innerText = ``;
updateUI();
},
/** Brackets opened */
bracketOpen = value => (value.match(/[(]/g) || []).length >
(value.match(/[)]/g) || []).length,
/** Clear results */
resultsRevert = () => equalDone && (
calcEqu.innerText = commaNone(calcResult.innerText).split(` =`)[0],
calcResult.innerText = ``,
equalDone = false
),
/** Live results calculation */
liveResults = () => {
/** Input value */
const equation = commaNone(calcEqu.innerText);
// Sanitise input
for (let i = 0; i < equation.length; i++)
if (!equation[i].match(/\d|\+|\-|\÷|\×|\%|\(|\)|\./g)) return
// Evaluate input
try {
/**
* Eval options:
* new Function("return " + calc)()
* window.eval[0](calc)
* [eval][0](calc)
*/
const
filter = equation
.replaceAll(/\×/g, `*`)
.replaceAll(/\÷/g, `/`)
.replaceAll(/\%/g, `*0.01`),
final = Number([eval][0](filter));
(final || final == 0) && (calcResult.innerText = commaNum(final));
return { equation, final }
} catch { return {}; };
},
/** Update all buttons status */
updateUI = v => {
// analyse value
let value = commaNone(calcEqu.innerText);
v != undefined && (
value != `0` && (!equalDone || expressions.indexOf(v) > -1) ? (
!equalDone ? resultsRevert()
: (
calcEqu.innerText = calcResult.innerText,
equalDone = false
),
calcEqu.innerText += v
) : (
equalDone = false,
calcResult.innerText = ``,
calcEqu.innerText = v
)
);
value == `` && (calcEqu.innerText = `0`);
value = commaNone(calcEqu.innerText);
// enable/disable buttons
const
length = value.length,
last = value[length - 1],
ratio = calcEqu.clientWidth / length;
if (last == `%` || last == `)`) { // Disable numbers pad
for (const b in bu.numbers) disableBu(bu.numbers[b]);
disableBu(bu.dot);
} else { // Enable numbers pad
for (const b in bu.numbers) enableBu(bu.numbers[b]);
enableBu(bu.dot);
};
value == `0` ? (
hide(bu.copy),
disableAll([bu.back, bu.clear, bu.equal])
) : (
calcResult.innerText == `0` ? hide(bu.copy) : flex(bu.copy),
enableAll([bu.back, bu.clear, bu.equal])
);
value == `0` || expressions.indexOf(last) > -1 || last == `.` || last == `(` ?
disableBu(bu.equal) : enableBu(bu.equal);
// check numbers
if (Number(last) || last == `%` || value == `0`) {
if (value == `0`) {
disableBu(bu.numbers.buPer);
for (const e in bu.exp) disableBu(bu.exp[e]);
} else {
enableBu(bu.numbers.buPer);
for (const e in bu.exp) enableBu(bu.exp[e]);
};
bracketOpen(value) || value == `0` ? enableBu(bu.brackets) : disableBu(bu.brackets);
} else {
disableBu(bu.numbers.buPer);
bracketOpen(value) ? disableBu(bu.brackets) : enableBu(bu.brackets);
};
// Update font size
calcEqu.style.fontSize = (
ratio > 60 ? 100
: ratio > 45 ? 80
: ratio > 30 ? 60
: 20
) + `px`;
// Live results
liveResults();
},
/** History setup */
hSetup = () => {
bu.corner.children[1].onclick = () => {
history = [];
hUpdate();
vibrate();
};
hContent.onscroll = () => hShadow();
resultsItems = sA(`.history_result`);
localStorage.history && (history = JSON.parse(localStorage.history));
hUpdate();
},
/** Dark mode check */
isDark = () => {
const
defaultSet = matchMedia(`(prefers-color-scheme: dark)`),
setting = Number(localStorage.dark);
return setting === 1 || setting == 0 ? (
defaultSet.onchange = null,
setting
) : defaultSet ? (
defaultSet.onchange = () => darkMode(),
defaultSet.matches ? 1 : 0
) : 0;
},
/** Dark mode styling */
darkMode = () => {
const
dark = isDark(),
bodyClasses = s(`body`).classList;
dark ? bodyClasses.add(`bg_dark`)
: bodyClasses.remove(`bg_dark`)
bu.numpad.forEach(b => {
const classes = b.classList;
dark ? classes.add(`t_white`)
: classes.remove(`t_white`);
});
bu.icons.forEach(b => {
const classes = b.classList;
dark ? classes.add(`dark`)
: classes.remove(`dark`);
});
},
/** Setup buttons */
buSetup = () => {
bu.save.children[1].onclick = () => {
localStorage.history ? localStorage.removeItem(`history`)
: localStorage.history = JSON.stringify(history);
saveToggle();
vibrate();
};
bu.copy.children[1].onclick = () => {
navigator.clipboard.writeText(commaNone(calcResult.innerText));
vibrate();
};
bu.back.onclick = () => {
if (enabledCheck(bu.back)) {
resultsRevert();
const value = commaNone(calcEqu.innerText);
value != `0` && (calcEqu.innerText = value.slice(0, value.length - 1));
updateUI();
vibrate();
};
};
bu.clear.onclick = () => {
if (enabledCheck(bu.clear)) {
clearAll();
vibrate();
};
};
bu.brackets.onclick = () => {
if (enabledCheck(bu.brackets)) {
const value = commaNone(calcEqu.innerText);
value == `0` ? calcEqu.innerText = `(`
: calcEqu.innerText += bracketOpen(value) ? `)` : `(`;
updateUI();
vibrate();
};
};
for (const b in bu.numbers) {
const button = bu.numbers[b];
button.onclick = () => {
if (enabledCheck(button)) {
updateUI(button.value);
vibrate();
};
}
};
bu.dot.onclick = () => {
if (enabledCheck(bu.dot)) {
const value = calcEqu.innerText;
for (let i = value.length - 1; i > -1; i--) {
const v = value[i];
if (equalDone) break
else if (v.match(/\./g)) return
else if (expressions.indexOf(v) > -1 || v == `%`) break
};
updateUI(`.`);
vibrate();
};
};
// Expression buttons initial setup
for (const b in bu.exp) {
const button = bu.exp[b];
button.onclick = () => {
if (enabledCheck(button)) {
const
value = calcEqu.innerText,
lastNum = value.length - 1;
expressions.indexOf(value[lastNum]) > -1 && (
calcEqu.innerText = value.slice(0, lastNum)
);
updateUI(button.value);
vibrate();
};
};
};
// Equal button setup
bu.equal.onclick = () => {
const results = liveResults();
if (
enabledCheck(bu.equal)
&& (results.final || results.final == 0)
) {
equalDone = true;
history.push(results.equation + ` = ` + commaNum(results.final));
hUpdate();
vibrate();
};
};
// Info button setup
const updateCornerIcon = (
del,
fun
) => {
const dark = isDark();
bu.corner.children[0].src = `./img/${del ? `delete` : dark ? `light` : `dark`}.svg`;
bu.corner.children[0].alt = `${del ? `Bin` : `${dark ? `light` : `dark`} star`} icon`;
bu.corner.children[1].title = del ? `Delete history` : `Switch to ${dark ? `light` : `dark`} mode`;
if (fun) bu.corner.children[1].onclick = fun;
};