-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
151 lines (133 loc) · 3.92 KB
/
ui.js
File metadata and controls
151 lines (133 loc) · 3.92 KB
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
namespace("Seed.Game.UI");
Seed.Game.UI = (function() {
var _private = {
startId : 1,
answerFieldCount : 1
}
return {
loadTemplate : function(path, data) {
return new EJS({
url : path
}).render(data);
},
init : function() {
this.addQuizAnswerField();
this.loadIdentifications();
$("#addAnotherQuiz").bind("click", {
context : this
}, function(event) {
event.data.context.addQuizAnswerField(event);
});
$("#evaluateButton").bind("click", {
context : this
}, this.submitQuizes);
this.notifier = Seed.Game.Notifier;
},
loadIdentifications : function() {
this.idMap = {};
var idMap = this.idMap;
Seed.Game.API.get(Seed.Game.Server.API.TeamMember, "IDs fetched", function(data) {
var parsed = [];
for(var i = 0, len = data.data.length; i < len; ++i) {
parsed[i] = data.data[i].name;
idMap[data.data[i].name] = data.data[i]._id;
}
$("#userAutocomplete").autocomplete({
source : parsed,
select : function(event, item) {
$("#idPlaceholder").html(Seed.Game.UI.idMap[item.item.value]);
$("#userId").val(Seed.Game.UI.idMap[item.item.value]);
}
});
});
},
addQuizAnswerField : function(event) {
if(_private.answerFieldCount >= 3) {
this.notifier.sticky().notice("Attention! Only the first three will be evaluated by the backend");
}
var html = this.loadTemplate("javascript/templates/" + "quizAnswersUIMarkup.ejs", {
quizId : "quiz" + _private.startId
});
$("#quizContent").append(html);
$("#quiz" + _private.startId).children(".removeField").bind("click", {
answerFieldId : "quiz" + _private.startId,
context : this
}, function(event) {
event.data.context.removeQuizAnswerField(event.data.answerFieldId);
});
_private.startId++;
_private.answerFieldCount++;
},
removeQuizAnswerField : function(id) {
_private.answerFieldCount--;
$("#" + id).remove();
$("#" + id).unbind("click");
},
submitQuizes : function(event) {
var self = event.data.context;
var validFlag = true, payLoad = {
data : {
identificaton : "",
quizes : [
// {type : "Cloud", answers : "abcdfs"}
]
}
}, userId = null;
$.each($("#quizContent").children(), function(index, item) {
var givenAnswers = $(item).children(".answers").val().removeWhiteSpace(), givenType = $(item).children(".types").val();
$(item).removeClass("warning");
/*
* START OF VALIDATION SECTOR
*/
// answers validation, we don't want to flood the sever with shit
if(givenAnswers.length !== 8) {
self.notifier.warning("8 answers must be given, only {0} available".format(givenAnswers.length));
validFlag = false;
}
if(givenAnswers.match(/[^a-e]/) /*e means empty answer*/) {
self.notifier.warning("Answers must be in the range of a to d");
validFlag = false;
}
// paint it with red border
if(validFlag === false) {
$(item).addClass("warning");
return false;
}
var quiz = {
type : givenType,
answers : givenAnswers
}
payLoad.data.quizes.push(quiz);
});
userId = $("#userId").val();
if(parseInt(userId) === -1) {
validFlag = false;
self.notifier.warning("No name selected");
}
// do not call ajax
if(validFlag === false) {
return;
}
/*
* END OF VALIDATION SECTOR
*/
payLoad.data.identificaton = userId;
$.ajax({
type : "POST",
url : Seed.Game.Server.getEvalURL(),
data : payLoad,
success : function(data) {
console.log(data);
data = JSON.parse(data);
var points = data.data.score + data.data.bonus, dataPayload = {
points : points
};
Seed.Game.API.helperMethod(Seed.Game.Server.API.TeamMember, data.data.identification, Seed.Game.Server.API.Helper.ADD_POINTS, dataPayload, "Points added");
},
error : function(jqXHR, textStatus, errorThrown) {
console.log(textStatus + ' ' + errorThrown);
}
});
}
}
})();