-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex-07.html
168 lines (153 loc) · 3.53 KB
/
index-07.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Salary Calculator</title>
<style>
body{
margin-left: 50px;
}
.field{
margin-bottom: 10px;
}
label{
display: block;
}
#tdSalary{
color : red;
font-size: 20pt;
}
</style>
<script src="jquery-3.3.1.min.js"></script>
<script>
String.prototype.toInt = function() {
return parseInt(this, 10);
};
//Model
function SalaryCalculator(){
var data = {
basic : 0,
hra : 0,
da : 0,
tax : 0,
salary : 0
};
this.get = function(attrName){
return data[attrName];
};
this.set = function(attrName, value){
if (data[attrName] === value) return;
data[attrName] = value;
//trigger change notification for 'attrName'
triggerChange(attrName);
};
var subscriptions = {
basic : [],
hra : [],
da : [],
tax : [],
salary : []
};
this.subscribe = function(attrName, subscriptionFn){
subscriptions[attrName].push(subscriptionFn);
}
function triggerChange(attrName){
var subscriptionFns = subscriptions[attrName];
subscriptionFns.forEach(function(subscriptionFn){
if (typeof subscriptionFn === 'function')
subscriptionFn();
});
}
}
//behavior
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);
};
//View
$(function(){
window.calculator = new SalaryCalculator();
//Reacting to user actions
$('#txtBasic').change(function(){
calculator.set('basic', $('#txtBasic').val().toInt());
});
$('#txtHra').change(function(){
calculator.set('hra', $('#txtHra').val().toInt());
});
$('#txtDa').change(function(){
calculator.set('da', $('#txtDa').val().toInt());
});
$('#rangeTax').change(function(){
calculator.set('tax', $('#rangeTax').val().toInt());
});
$('#btnCalculate').click(function(){
calculator.calculate();
});
//Reacting to model changes
calculator.subscribe('salary', function(){
$('#tdSalary').html(calculator.get('salary'));
});
calculator.subscribe('basic', function(){
$('#tdBasic').html(calculator.get('basic'));
});
calculator.subscribe('hra', function(){
$('#tdHra').html(calculator.get('hra'));
});
calculator.subscribe('da', function(){
$('#tdDa').html(calculator.get('da'));
});
calculator.subscribe('tax', function(){
$('#tdTax').html(calculator.get('tax'));
});
});
</script>
</head>
<body>
<h1>Salary Calculator</h1>
<hr>
<section>
<div class="field">
<label for="">Basic :</label>
<input type="number" id="txtBasic">
</div>
<div class="field">
<label for="">HRA :</label>
<input type="number" id="txtHra">
</div>
<div class="field">
<label for="">DA :</label>
<input type="number" id="txtDa">
</div>
<div class="field">
<label for="">Tax :</label>
<input type="range" id="rangeTax" min="0" max="30" value="0">
</div>
<div class="field">
<input type="button" value="Calculate" id="btnCalculate">
</div>
<div class="field">
<table>
<thead>
<tr>
<th>Basic</th>
<th>HRA</th>
<th>DA</th>
<th>Tax</th>
<th>Salary</th>
</tr>
</thead>
<tbody>
<tr>
<td id="tdBasic">[Basic]</td>
<td id="tdHra">[Hra]</td>
<td id="tdDa">[Da]</td>
<td id="tdTax">[Tax]</td>
<td id="tdSalary">[Salary]</td>
</tr>
</tbody>
</table>
</div>
</section>
</body>
</html>