-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSalaryCalculator-React-2.html
148 lines (134 loc) · 3.7 KB
/
SalaryCalculator-React-2.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Salary Calculator</title>
<script src="react.js"></script>
<script src="react-dom.js"></script>
<script src="browser.min.js"></script>
<style type="text/css">
.field{
margin-bottom: 10px;
}
label{
display: block;
}
#divResult{
color : red;
font-size: 22pt;
font-weight: bold;
}
</style>
<script>
String.prototype.toInt = function(){
return parseInt(this,10);
};
//state and behavior of a calculator
//Model
function SalaryCalculator(){
var _data = {
basic : 0,
hra : 0,
da : 0,
tax : 0,
salary : 0
};
this.toJSON = function(){
return JSON.parse(JSON.stringify(_data));
};
this.get = function(attrName){
return _data[attrName];
};
this.set = function(attrName, value){
if (_data[attrName] === value) return;
_data[attrName] = value;
//trigger change notification
triggerChangeFor(attrName);
triggerChangeFor('all');
};
function triggerChangeFor(attrName){
var eventHandlerFns = _eventHandlers[attrName];
eventHandlerFns.forEach(function(eventHandlerFn){
if (typeof eventHandlerFn === 'function')
eventHandlerFn();
})
}
var _eventHandlers = {
basic : [],
hra : [],
da : [],
tax : [],
salary : [],
all : []
};
this.onChange = function(attrName, eventHandlerFn){
_eventHandlers[attrName].push(eventHandlerFn);
}
}
SalaryCalculator.prototype.calculate = function(){
var gross = this.get('basic') + this.get('hra') + this.get('da');
var net = gross * ((100-this.get('tax'))/100);
this.set('salary' , net);
}
</script>
<script type="text/template" id="calculatorTemplate">
</script>
</head>
<body>
<h1>Salary Calculator</h1>
<script type="text/babel">
var SalaryCalculatorView = React.createClass({
updateBasic : function(evt){
this.props.calculator.set('basic', evt.target.value.toInt())
},
updateHra : function(evt){
this.props.calculator.set('hra', evt.target.value.toInt())
},
updateDa : function(evt){
this.props.calculator.set('da', evt.target.value.toInt())
},
updateTax : function(evt){
this.props.calculator.set('tax', evt.target.value.toInt())
},
triggerCalculate : function(){
this.props.calculator.calculate();
},
render : function(){
var data = this.props.calculator.toJSON();
return (<div>
<div className="field">
<label>Basic :</label>
<input type="text" ref="txtBasic" value={data.basic} onChange={this.updateBasic}/>
</div>
<div className="field">
<label >HRA :</label>
<input type="text" ref="txtHra" value={data.hra} onChange={this.updateHra}/>
</div>
<div className="field">
<label >DA :</label>
<input type="text" ref="txtDa" value={data.da} onChange={this.updateDa}/>
</div>
<div className="field">
<label >Tax :</label>
<input type="range" ref="rangeTax" min="0" max="30" value={data.tax} onChange={this.updateTax}/>
<span id="spanTax">{data.tax}</span>
</div>
<div className="field">
<input type="button" value="Calculate" id="btnCalculate" onClick={this.triggerCalculate}/>
</div>
<div className="field">
<div id="divResult">{data.salary}</div>
</div>
</div>
);
}
});
window.calculator = new SalaryCalculator();
function render(){
ReactDOM.render(<SalaryCalculatorView calculator={calculator} />, document.body);
}
calculator.onChange('all', render);
render();
</script>
</body>
</html>