-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
269 lines (233 loc) · 6.25 KB
/
scripts.js
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
/**
* Created by Shawn on 8/15/15.
*/
// form selectors
var $form = $('#Info_frm');
var $first = $('#FirstName');
var $last = $('#LastName');
var $number = $('#EmployeeNum');
var $title = $('#Title');
var $review = $('#Review');
var $salary = $('#Salary');
// selectors, objects for handling the lists
var $employUl = $('#Employees_ul');
var employeesArray = new Array();
var $remove = $('<button>');
$remove.text("X");
$remove.addClass("js-remove");
var totalSal = 0;
var randomTitles = ["Clerk", "Assistant", "Accountant", "Systems Engineer", "Tech Support", "Janitor",
"EVP", "Salesman", "Customer Service"];
$(document).ready(function()
{
showSalary();
$('#Add_btn').on('click', function (e)
{
// get employee number for checking and adding employee, setting exists to false to check if it already exists
var num = parseInt($('#EmployeeNum').val());
var exists = false;
// check to make sure employee hasn't already been added
for(var i = 0;i < employeesArray.length; i++)
{
if(employeesArray[i].number == num)
{
alert("Employee has already been added.");
exists = true;
}
}
// employee hasn't been added, continue
if(exists == false)
{
// add new employee object to array
employeesArray.push(new Employee($first.val(), $last.val(), $number.val(),
$title.val(), $review.val(), $salary.val()));
var i = employeesArray.length - 1;
console.log(employeesArray[i]);
// add this employees salary to the total
addSalary(employeesArray[employeesArray.length - 1].salary);
// call function to create and add ul/li to employees list
addToList();
}
e.preventDefault();
});
$('#Random_btn').click(function (e)
{
var random = new Employee();
employeesArray.push(random);
addToList();
addSalary(employeesArray[employeesArray.length - 1].salary);
e.preventDefault();
});
$($employUl).on('mouseenter', '.js-employee li:first-child', function (e)
{
//console.log("middle enter", $(this).text());
$(this).next().removeClass('hidden');
e.preventDefault();
});
$($employUl).on('mouseleave', '.js-employee li:first-child', function (e)
{
//console.log("middle leave", $(this).text());
$(this).next().addClass('hidden');
e.preventDefault();
});
$($employUl).on("click", ".js-remove", function(e)
{
var $ul = $(this).closest(".js-employee");
var id = $ul.data('id');
// iterate the array of employee objects to find that employee and remove it
employeesArray.forEach(function (element, index)
{
if(element.number == id)
{
alert(employeesArray[index].getForwardName() +" has been terminated.");
removeSalary(employeesArray[index].salary);
employeesArray.splice(index, 1);
return true;
}
});
// remove the parent ul for employee from list
$ul.remove();
e.preventDefault();
});
});
var Employee = function(first, last, num, title, review, salary)
{
this.getName = function ()
{
return this.lastName +", "+ this.firstName;
};
this.getTitledInfo = function ()
{
return "Number: "+ this.number.toLocaleString() +" Title: "+ this.title +" Review: "+ this.review +" Salary: $"+ this.salary.toLocaleString();
};
this.getAllInfo = function ()
{
return this.lastName +", "+ this.firstName +" Number: "+ this.number.toLocaleString() +" Title: "+
this.title +" Review: "+ this.review +" Salary: $"+ this.salary.toLocaleString();
};
this.randomize = function ()
{
this.firstName = chance.first();
this.lastName = chance.last();
// assume the employee number already exists, until proven otherwise
var exists = true;
while(exists == true)
{
var num = chance.integer({min: 1, max: 999});
var insideExists = false;
// loop through array to find if employee number exists. Breaks if found to start process over.
for(var i = 0;i < employeesArray.length; i++)
{
if(employeesArray[i].number == num)
{
insideExists = true;
break;
}
}
// if wasn't found in array, accept the number as not having been taken
if(insideExists == false)
{
this.number = num;
exists = false;
}
}
this.title = randomTitles[chance.integer({min: 0, max: 8})];
this.review = chance.integer({min: 1, max: 5});
this.salary = chance.integer({min: 20000, max: 200000});
return true;
};
this.getForwardName = function()
{
return this.firstName +" "+ this.lastName;
};
// if first name is set to 0 (int), then create random employee
if(first != undefined)
{
this.firstName = first;
this.lastName = last;
if(isNaN(num))
{
this.number = parseInt(removeNonNumeric(num));
}else
{
this.number = num;
}
this.title = title;
this.review = review;
if(isNaN(salary))
{
this.salary = parseInt(removeNonNumeric(salary));
}else
{
this.salary = salary;
}
}else
{
this.randomize();
}
};
function addSalary(sal)
{
totalSal += sal;
showSalary();
}
function removeSalary(sal)
{
totalSal -= sal;
showSalary();
}
function showSalary()
{
$('#TotalSalary_spn').text('$'+ totalSal.toLocaleString());
}
// for adding new entries to ul, assumes last one entered is desired
function addToList()
{
var i = employeesArray.length - 1;
var $ul = $('<ul>',
{
'data-id': employeesArray[i].number,
'class': 'js-employee rating'+ employeesArray[i].review
});
var name = employeesArray[i].getName();
var $li1 = $('<li>',
{
'class': 'js-empName'
});
$li1.text(name);
$remove.clone().appendTo($li1);
var $li2 = $('<li>', {
'class': 'hidden info'
});
$li2.text(employeesArray[i].getTitledInfo());
$ul.append($li1);
$ul.append($li2);
// needed to make sure it stops checking once it's been inserted, or to add it at end if it's alphabetically the last employee
var inserted = false;
if($employUl.find('.js-employee').length == 0)
{// insert if no employees are listed
$employUl.append($ul);
inserted = true;
}else
{
// iterate employee UL's, see where it belongs alphabetically
$employUl.find('.js-empName').each(function()
{
if(name < $(this).text() == true && inserted == false)
{
inserted = true;
$ul.insertBefore($(this).closest('.js-employee'));
return true;
}
});
// insert as last since it hasn't been inserted yet
if(inserted == false)
{
$employUl.append($ul);
}
}
}
function removeNonNumeric(str){
var numericString = str.replace(/[^0-9]/g, '');
return numericString;
}