1
- // no need for jquery: it's a really big library: bad for performace
1
+ class History {
2
+ historyShow ( state , deleteAll ) {
3
+ const target = document . getElementById ( "history" ) ;
4
+ var p = document . createElement ( "p" ) ;
5
+ p . innerHTML = state . join ( "" ) ;
6
+ deleteAll ? target . innerHTML = "" : target . appendChild ( p ) ;
7
+ }
8
+
9
+ historyPush ( state ) {
10
+ this . history . push ( state ) ;
11
+ this . redoUndoKeyPressed = this . history . length ;
12
+ this . historyShow ( state ) ;
13
+ }
14
+
15
+ deleteHistory ( ) {
16
+ this . history = [ ] ;
17
+ this . historyShow ( [ ] , "delete" ) ;
18
+ this . redoUndoKeyPressed = this . history . length ;
19
+ }
2
20
3
- const $ = ( selector ) => {
4
- return document . querySelector ( selector ) ;
21
+ RedoOrUndo ( cmd ) {
22
+ cmd === "undo" && this . redoUndoKeyPressed ? this . goAhead ( "undo" ) : false ;
23
+ cmd === "redo" && this . redoUndoKeyPressed < this . history . length - 1 ? this . goAhead ( "redo" ) : true ;
24
+ }
25
+
26
+ goAhead ( cmd ) {
27
+ cmd === "undo" ? this . redoUndoKeyPressed -- : this . redoUndoKeyPressed ++ ;
28
+ this . setState ( {
29
+ input : this . history [ this . redoUndoKeyPressed ]
30
+ } )
31
+ }
5
32
}
6
33
7
34
8
35
9
36
10
- class Calculator {
37
+
38
+
39
+
40
+
41
+ class Calculator extends History {
11
42
constructor ( ) {
12
- this . state = {
13
- monitor : [ ] ,
14
- output : ""
15
- } ;
16
- this . output = document . getElementById ( "steps" ) ;
17
- this . input = document . getElementById ( "calBody" ) ;
18
- this . bound = this . bound . bind ( this ) ;
43
+ super ( ) ;
44
+ this . state = [ ] ;
45
+ this . history = [ ] ;
46
+ this . redoUndoKeyPressed = 0 ;
47
+ this . instantiate ( ) ;
48
+ }
49
+
50
+ instantiate ( ) {
19
51
this . getUserInput ( ) ;
20
- this . leaveZero ( ) ;
52
+ this . render ( ) ;
53
+ this . history . push ( ) ;
21
54
}
22
55
23
- render ( ) {
24
- this . output . innerHTML = this . state . output ;
56
+ render ( total ) {
57
+ var noData = this . state . join ( "" ) ;
58
+ noData === "" ? noData = 0 : true ;
59
+ document . getElementById ( "steps" ) . innerHTML = total ? total : noData ;
25
60
}
26
61
27
- getUserInput ( ) {
28
- this . input . addEventListener ( 'click' , ( e ) => {
29
- var id = e . target . id ;
30
- if ( id !== "calBody" ) {
31
- const ctrkeys = [ "deleteAll" , "backOne" , "total" ] ;
32
- ctrkeys . includes ( id ) ? this . commandStation ( id ) : this . regularClick ( id ) ;
33
- }
34
- } ) ;
62
+ regularClick ( id ) {
63
+ this . setState ( {
64
+ input : this . state . concat ( id )
65
+ } )
35
66
}
36
67
68
+ backOne ( ) {
69
+ this . state . pop ( ) ;
70
+ this . render ( ) ;
71
+ }
37
72
73
+ deleteAll ( ) {
74
+ this . setState ( {
75
+ input : [ ]
76
+ } ) ;
77
+ this . redoUndoKeyPressed = this . history . length ;
78
+ }
79
+
80
+ setState ( obj , total ) {
81
+ this . state = obj . input ;
82
+ this . render ( total ) ;
83
+ }
38
84
39
85
commandStation ( id ) {
40
86
switch ( id ) {
@@ -43,79 +89,46 @@ class Calculator {
43
89
break ;
44
90
case "backOne" :
45
91
this . backOne ( ) ;
92
+ break ;
46
93
case "total" :
47
- this . total ( ) ;
94
+ this . getTotal ( "total" ) ;
95
+ break ;
96
+ case "undo" :
97
+ this . RedoOrUndo ( "undo" ) ;
98
+ break ;
99
+ case "redo" :
100
+ this . RedoOrUndo ( "redo" ) ;
101
+ break ;
48
102
default :
49
103
break ;
50
104
}
51
105
}
52
106
53
- regularClick ( id ) {
54
- this . state . monitor . push ( id ) ;
55
- this . state . output = this . state . monitor . join ( "" ) ;
56
- this . render ( ) ;
57
- console . log ( this . state )
58
- }
59
-
60
- deleteAll ( ) {
61
- this . state = {
62
- monitor : [ ] ,
63
- output : ""
64
- }
65
- this . render ( ) ;
66
- this . leaveZero ( ) ;
67
- }
68
-
69
- backOne ( ) {
70
- var monitor = [ ] ;
71
- var state = this . state ;
72
-
73
- for ( var i = 0 ; i < state . monitor . length - 1 ; i ++ ) {
74
- monitor [ i ] = state . monitor [ i ] . toString ( ) . split ( "" )
75
- }
76
-
77
- this . state = {
78
- monitor : monitor ,
79
- output : monitor . join ( "" )
80
- }
81
-
82
- if ( state . output === "" ) {
83
- state . output = "0" ;
84
- }
85
- this . render ( ) ;
86
- }
87
-
88
- total ( ) {
89
- // give something wrong and see how it works
107
+ getTotal ( cmd ) {
90
108
try {
91
- var total = eval ( this . state . monitor . join ( "" ) ) ;
109
+ var total = eval ( this . state . join ( "" ) ) ;
110
+ this . historyPush ( this . state ) ;
92
111
} catch ( e ) {
93
- total = e ;
112
+ var total = e ;
94
113
}
95
-
96
- this . state = {
97
- output : total ,
98
- monitor : total . toString ( ) . split ( "" )
99
- } ;
100
- this . render ( ) ;
114
+ this . setState ( {
115
+ input : [ ] . concat ( total . toString ( ) . split ( "" ) )
116
+ } , total ) ;
101
117
}
102
118
103
-
104
-
105
- bound ( ) {
106
- this . deleteAll = this . deleteAll . bind ( this ) ;
107
- this . backOne = this . backOne . bind ( this ) ;
108
- this . total = this . total . bind ( this ) ;
109
- this . commandStation = this . commandStation . bind ( this ) ;
110
- this . getUserInput = this . getUserInput . bind ( this ) ;
111
- this . render = this . render . bind ( this ) ;
112
- }
113
- leaveZero ( ) {
114
- this . output . innerHTML = "0" ;
119
+ getUserInput ( ) {
120
+ document . getElementById ( "deleteHistory" ) . addEventListener ( "click" , ( e ) => {
121
+ this . deleteHistory ( ) ;
122
+ } ) ;
123
+ document . getElementById ( "calBody" ) . addEventListener ( 'click' , ( e ) => {
124
+ var id = e . target . id ;
125
+ if ( id !== "calBody" ) {
126
+ const ctrkeys = [ "deleteAll" , "backOne" , "total" , "undo" , "redo" ] ;
127
+ ctrkeys . includes ( id ) ? this . commandStation ( id ) : this . regularClick ( id ) ;
128
+ }
129
+ } ) ;
115
130
}
116
-
117
131
}
118
132
119
133
120
-
121
134
const StartCalc = new Calculator ( ) ;
0 commit comments