/
script.js
345 lines (344 loc) · 12.5 KB
/
script.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
var sgt;
function GradeTable() {
/**
* Define all global variables here
*/
/**
* student_array - global array to hold student objects
* @type {Array}
*/
this.student_array = [];
var self = this;
this.dataPulled = false;
// this.url = "../../server/sgt.php";
this.url = "https://s-apis.learningfuze.com/sgt/";
this.hints = {
totals: {
calls: 0,
hits: 0,
errors: 0,
successes: 0,
hintsFound: 0
},
successes: {
count: [],
messages: [],
previousObject: {},
currentObject: {},
arrayOfCalls: []
},
errors: {
count: [],
messages: [],
previousObject: {},
currentObject: {},
arrayOfCalls: []
}
};
this.errors = {
messages: []
};
/**
* inputIds - id's of the elements that are used to add students
* @type {string[]}
*/
this.$studentName = $('#studentName');
this.$studentCourse = $('#course');
this.$studentGrade = $('#studentGrade');
/**
* addClicked - Event Handler when user clicks the add button
*/
this.addClicked = function () {
if (this.$studentName.val() !== "" && this.$studentCourse.val() !== "" && !isNaN(parseFloat( this.$studentGrade.val() )) ){
this.addStudent();
this.cancelClicked();
this.updateData();
}
};
/**
* cancelClicked - Event Handler when user clicks the cancel button, should clear out student form
*/
this.cancelClicked = function () {
this.clearAddStudentForm();
};
/**
* addStudent - creates a student objects based on input fields in the form and adds the object to global student array
*
* @return undefined
*/
this.addStudent = function () {
var studentName = this.$studentName.val();
var studentCourse = this.$studentCourse.val();
var studentGrade = this.$studentGrade.val();
var studentToBeAdded = {
name: studentName,
course: studentCourse,
grade: studentGrade
};
this.student_array.push(studentToBeAdded);
this.createStudentOnServer(studentToBeAdded);
};
/**
* clearAddStudentForm - clears out the form values based on inputIds variable
*/
this.clearAddStudentForm = function () {
this.$studentName.val("");
this.$studentCourse.val("");
this.$studentGrade.val("");
};
/**
* calculateAverage - loop through the global student array and calculate average grade and return that value
* @returns {number}
*/
this.calculateAverage = function () {
var $arrayOfAllElements = $('tbody').find('tr td:nth-child(3)');
var arrayOfAllGrades = [0]; //number
var highestValue = -1;
var lowestValue = 201;
for (var i=0; i < $arrayOfAllElements.length; i++){
arrayOfAllGrades[i] = Math.round($arrayOfAllElements[i].innerHTML);
if (arrayOfAllGrades[i] > highestValue) {
highestValue = arrayOfAllGrades[i]; //number
}
if (arrayOfAllGrades[i] < lowestValue) {
lowestValue = arrayOfAllGrades[i]; //number
}
}
var $arrayOfLowest = $arrayOfAllElements.filter(function(param){
return this.innerHTML == lowestValue;
});//string
var $arrayOfHighest = $arrayOfAllElements.filter(function(param){
return this.innerHTML == highestValue;
});//string
if ($arrayOfLowest[0] !== $arrayOfHighest[0]) {
$arrayOfLowest.addClass('bg-danger');
}
$arrayOfHighest.addClass('bg-success');
function sum(total,num){
if (isNaN(num))
return total;
return total+num;
}
var total = arrayOfAllGrades.reduce(sum); //number
return Math.round(total/arrayOfAllGrades.length);
};
/**
* updateData - centralized function to update the average and call student list update
*/
this.updateData = function () {
if (this.student_array === undefined){
return;
}
this.updateStudentList();
var average = this.calculateAverage();
$('.avgGrade').text(average);
};
/**
* updateStudentList - loops through global student array and appends each objects data into the student-list-container > list-body
*/
this.updateStudentList = function () {
$('tbody').empty();
for (var i = 0; i < this.student_array.length; i++) {
this.addStudentToDom(this.student_array[i]);
}
};
/**
* addStudentToDom - take in a student object, create html elements from the values and then append the elements
* into the .student_list tbody
* @param studentObj
*/
this.addStudentToDom = function (studentObject) {
var $newElement = $('' +
'<tr>' +
'<td>' + studentObject.name + '</td>' +
'<td>' + studentObject.course + '</td>' +
'<td>' + studentObject.grade + '</td>' +
'<td><button type="button" class="btn btn-danger btn-xs">Delete</button></td>' +
'</tr>');
(function(self){
$newElement.appendTo('.student-list tbody');
$newElement.find('button').click(function(){
var location = $newElement.index();
$newElement.remove();
self.deleteStudentFromServer(self.student_array[location]);
self.student_array.splice(location,1);
self.updateData();
});
})(this);
};
/**
* reset - resets the application to initial state. Global variables reset, DOM get reset to initial load state
*/
this.reset = function () {
this.student_array = [];
};
this.createStudentOnServer = function(object){
$.ajax({
dataType: 'json',
url: this.url+"create",
method: 'post',
data: {
api_key: 'yPaZqUuy8L',
"name": object.name,
"course": object.course,
"grade": object.grade
},
error: function (response) {
logAndCreateErrorAlert(response.errors);
},
success: function (response) {
if (response.success){
object.id = response.new_id;
var message = 'Student ID #'+object.id+' "'+object.name+'" was successfully created on the server.';
createSuccessAlert(message);
} else {
logAndCreateErrorAlert(response.errors);
}
}
})
};
this.deleteStudentFromServer = function(object){
$.ajax({
dataType: 'json',
url: this.url+'delete',
method: 'post',
data: {
api_key: 'yPaZqUuy8L',
"student_id": object.id,
"id": object.id
},
error: function (response) {
logAndCreateErrorAlert(response.errors);
},
success: function (response) {
if (response.success){
var message = 'Student ID #'+object.id+' "'+object.name+'" has been from removed from the server.';
createSuccessAlert(message);
} else {
logAndCreateErrorAlert(response.errors);
}
}
})
};
this.getDataFromServer = function(){
$.ajax({
dataType: 'json',
url: this.url+'get',
method: 'post',
data: {
api_key: 'yPaZqUuy8L'
},
error: function(response){
logAndCreateErrorAlert(response.error);
},
success: function(response){
if(response.success === false){
logAndCreateErrorAlert(response.error);
return;
}
if (self.dataPulled === false){
self.student_array = self.student_array.concat(response.data);
self.updateData();
self.dataPulled = true;
} else {
self.findHints();
}
}
})
};
function createSuccessAlert(successMsg){
var alert = $('<div class="alert alert-success alert-dismissable">' +
'<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>' +
'<strong>Success!</strong> <span id="successMsg">'+successMsg+'</span>');
$('#alerts').append(alert);
}
function logAndCreateErrorAlert(message){
if(!message) message = ["Please contact an administrator."];
self.errors.messages = self.errors.messages.concat(message);
var errorMsg = message.join(" | ");
var alert = $('<div class="alert alert-danger alert-dismissable">' +
'<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>' +
'<strong>Error:</strong> <span id="errorMsg">'+errorMsg+'</span>');
$('#alerts').append(alert);
}
this.findHints = function(){
$.ajax({
dataType: 'json',
url: 'https://s-apis.learningfuze.com/sgt/get',
method: 'post',
data: {
api_key: 'yPaZqUuy8L'
},
error: function(response){
self.hints.totals.calls++;
self.hints.totals.errors++;
console.log(response, this);
var existing = self.hints.errors.messages.indexOf(response.error[0]);
if (existing > -1) {
self.hints.errors.count[existing]++;
} else {
self.hints.errors.messages.push(response.error[0]);
self.hints.errors.count.push(1);
}
self.hints.errors.arrayOfCalls.push({
messages: response.error[0],
call: this,
object: response
});
self.hints.error.previousObject = self.error.currentObject;
self.hints.error.currentObject = response;
},
success: function(response){
self.hints.totals.calls++;
self.hints.totals.hits++;
if(response.success === false){
self.errors.messages = self.errors.messages.concat(response.error);
return;
}
self.hints.totals.successes++;
if (response.hint !== undefined) {
self.hints.totals.hintsFound++;
createHintAlert(response.hint);
console.log(response.hint);
var existing = self.hints.successes.messages.indexOf(response.hint);
if (existing > -1) {
self.hints.successes.count[existing]++;
} else {
self.hints.successes.messages.push(response.hint);
self.hints.successes.count.push(1);
}
self.hints.successes.arrayOfCalls.push({
messages: response.hint,
call: this,
object: response
});
}
self.hints.successes.previousObject = self.hints.successes.currentObject;
self.hints.successes.currentObject = response;
function createHintAlert(hintMsg){
var alert = $('<div class="alert alert-info alert-dismissable">' +
'<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>' +
'<strong>Hint:</strong> <span id="successMsg">'+hintMsg+'</span>');
$('#alerts').append(alert);
}
}
})
};
/**
* initialize - applies click handlers to the elements in the DOM
*/
this.initialize = function() {
$('#add').on('click',this.addClicked.bind(this));
$('#cancel').on('click',this.cancelClicked.bind(this));
$('#get').on('click',this.getDataFromServer.bind(this));
}
}
/**
* Listen for the document to load and reset the data to the initial state
*/
$(document).ready(function(){
sgt = new GradeTable();
sgt.initialize();
sgt.reset();
sgt.getDataFromServer();
});