-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-08.html
186 lines (163 loc) · 3.95 KB
/
index-08.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
<!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 SalaryCalculatorView(calculator){
//Presentation
var _template = `
<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>
`;
var $root = this.$root = $('<div></div>');
this.render = function(){
$root.html(_template);
};
//Behaviors
//Reacting to user actions
$root.on('change', '#txtBasic', function(){
calculator.set('basic', $(this).val().toInt());
});
$root.on('change', '#txtHra', function(){
calculator.set('hra', $(this).val().toInt());
});
$root.on('change', '#txtDa', function(){
calculator.set('da', $(this).val().toInt());
});
$root.on('change', '#rangeTax', function(){
calculator.set('tax', $(this).val().toInt());
});
$root.on('click', '#btnCalculate', function(){
calculator.calculate();
});
//Reacting to model changes
calculator.subscribe('salary', function(){
$('#tdSalary', $root).html(calculator.get('salary'));
});
calculator.subscribe('basic', function(){
$('#tdBasic', $root).html(calculator.get('basic'));
});
calculator.subscribe('hra', function(){
$('#tdHra', $root).html(calculator.get('hra'));
});
calculator.subscribe('da', function(){
$('#tdDa', $root).html(calculator.get('da'));
});
calculator.subscribe('tax', function(){
$('#tdTax', $root).html(calculator.get('tax'));
});
}
/*$(function(){
window.calculator = new SalaryCalculator();
});*/
</script>
</head>
<body>
<h1>Salary Calculator</h1>
<hr>
</body>
</html>