forked from ChallengeHunt/challengehunt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
332 lines (275 loc) · 10.3 KB
/
data.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
function initialize() {
if (!localStorage.data || !localStorage.hosts_data) {
getChallengeData();
}
else {
data = localStorage.getItem('data');
hosts_data = localStorage.getItem('hosts_data');
generateCards(data);
generateCardsArchive(data);
generateCardsPending(data);
loadDropDownWithHosts(hosts_data);
}
}
initialize();
// get all the challenge data from server
function getChallengeData() {
$.ajax({
type:'GET',
url: "http://challengehuntapp.appspot.com/v2",
beforeSend: function () {
// $("#target").loadingOverlay();
},
success: function (data) {
if(!((typeof localStorage["data"]) === 'undefined')) {
localStorage.removeItem('data');
}
localStorage.setItem('data', data);
generateCards(data);
generateCardsArchive(data);
generateCardsPending(data);
},
error: function(jq, status, message) {
}
});
}
// get all the hosts from the server
function getHosts() {
$.ajax({
type:'GET',
url: "http://challengehuntapp.appspot.com/hosts",
beforeSend: function () {
},
success: function (data) {
// $("#loader-select-menu").hide();
loadDropDownWithHosts(data);
},
error: function(jq, status, message) {
}
});
}
// creates a new card and applies all the style to it. returns the created card
function applyStyle(data, type) {
var newDiv = document.createElement('div');
newDiv.className = "cards grow";
if (type == "upcoming") {
var startDateTime = toTimeZone(data.start).split(",");
var startDate = dateFormatted(startDateTime[0], startDateTime[1]);
var startTime = timeFormatted(startDateTime[1]);
if (startDateTime.length == 1) {
startDateTime = toTimeZone(data.start).split(" ");
}
} else {
var endDateTime = toTimeZone(data.end).split(",");
if (endDateTime.length == 1) {
endDateTime = toTimeZone(data.end).split(" ");
}
}
// var startDate = dateFormatted(startDateTime[0], startDateTime[1]);
// var startTime = timeFormatted(startDateTime[1]);
var startDateTimeDiv = document.createElement('div');
var lengthOfContestname = data.contest_name.length;
var logoDiv = document.createElement('img');
if(imageExists("/img/"+data.host_name+".png")) {
logoDiv.setAttribute('src', '/img/'+ data.host_name +'.png');
}
// special case for hackercup
else if(data.host_name == "facebook.com/hackercup") {
logoDiv.setAttribute('src', '/img/hackercup.jpg');
}
// another case for codejam
else if(data.host_name == "google.com/codejam") {
logoDiv.setAttribute('src', '/img/codejam.png');
}
else {
logoDiv.setAttribute('src', '/img/default.jpg');
}
if (type == "upcoming") {
logoDiv.className = "logo-upcoming";
} else {
logoDiv.className = "logo";
}
newDiv.appendChild(logoDiv);
if (type == "upcoming") {
startDateTimeDiv.className = "time start-time";
startDateTimeDiv.innerHTML = "<i class='fa fa-play' style=' margin-right:5px;'></i>" + startDate + startTime ;
newDiv.appendChild(startDateTimeDiv);
}
if (type == "upcoming") {
// var startDateForCalender = dateForCalendar(startDateTime[0], startDateTime[1]) + 'T' + timeForCalendar(startDateTime[1]);
// var endDateForCalendar = dateForCalendar(endDateTime[0], startDateTime[1]) + 'T' + timeForCalendar(endDateTime[1]);
// var calendarTime = startDateForCalender + '/' + endDateForCalendar;
calendarLink = "https://www.google.com/calendar/render?action=TEMPLATE&text="+encodeURIComponent(data.contest_name)+"&location="+data.contest_url+"&pli=1&uid=&sf=true&output=xml#eventpage_6"
var calendarDiv = document.createElement('div');
calendarDiv.className = "calendar";
calendarDiv.innerHTML = "<center><a href='" + calendarLink + "' target='_blank'><i class='fa fa-bell-o'></i></a></center>";
newDiv.appendChild(calendarDiv);
}
if(lengthOfContestname < 28) {
var contestNameDiv = document.createElement('div');
contestNameDiv.className = "contest-name-short"
contestNameDiv.innerHTML = "<a href='"+data.contest_url +"' target='_blank'>" +data.contest_name+ "</a>";
newDiv.appendChild(contestNameDiv);
} else {
var contestNameDiv = document.createElement('div');
contestNameDiv.className = "contest-name-long";
contestNameDiv.innerHTML = "<a href='"+data.contest_url +"' target='_blank'>" +data.contest_name+ "</div>";
newDiv.appendChild(contestNameDiv);
}
var contestUrlDiv = document.createElement('div');
contestUrlDiv.className = "contest-url";
var contestUrlLink = document.createElement('a');
contestUrlLink.href = data.host_url;
contestUrlLink.target = "blank";
contestUrlLink.innerHTML = data.host_name ;
contestUrlDiv.appendChild(contestUrlLink);
newDiv.appendChild(contestUrlDiv);
var durationDiv = document.createElement('div');
durationDiv.className = "duration";
durationDiv.innerHTML = "Duration: " + data.duration;
newDiv.appendChild(durationDiv);
if (type == "active" || type == "archived") {
var endDate = dateFormatted(endDateTime[0], endDateTime[1]);
var endTime = timeFormatted(endDateTime[1]);
var endDateTimeDiv = document.createElement('div');
endDateTimeDiv.className = "time end-time"
endDateTimeDiv.innerHTML = "<i class='fa fa-stop' style=' margin-right:5px' ></i>"+ endDate + endTime ;
newDiv.appendChild(endDateTimeDiv);
}
return newDiv;
}
// generates card for active contest
function generateCards(data) {
var active_tabs = document.getElementById("active-contests");
active_tabs.innerHTML = "";
var active_contests = JSON.parse(data);
var active_contest_data = active_contests["active"];
var numberOfActiveContests = 0;
var prevContestName = "";
hosts = JSON.parse(localStorage.getItem('hosts'));
for (var i = 0; i < active_contest_data.length; i++) {
if(prevContestName != active_contest_data[i].contest_name){
active_card = applyStyle(active_contest_data[i], "active");
if ((typeof localStorage["hosts"]) === 'undefined') {
document.getElementById("active-contests").appendChild(active_card);
numberOfActiveContests += 1;
}
else if (hosts.hasOwnProperty(active_contest_data[i].host_name)) {
document.getElementById("active-contests").appendChild(active_card);
numberOfActiveContests += 1;
}
}
prevContestName = active_contest_data[i].contest_name;
}
chrome.browserAction.setBadgeText({text:numberOfActiveContests.toString()});
}
// generates card for upcoming contest
function generateCardsPending(data) {
var pending_tabs = document.getElementById("pending-contests");
pending_tabs.innerHTML = "";
var pending_contest_data = JSON.parse(data)["pending"];
hosts = JSON.parse(localStorage.getItem('hosts'));
var prevContestName = "";
for (var i = 0; i < pending_contest_data.length; i++) {
if(prevContestName != pending_contest_data[i].contest_name){
upcomingDiv = applyStyle(pending_contest_data[i], "upcoming");
if ((typeof localStorage["hosts"]) === 'undefined') {
document.getElementById("pending-contests").appendChild(upcomingDiv);
}
else if (hosts.hasOwnProperty(pending_contest_data[i].host_name)) {
document.getElementById("pending-contests").appendChild(upcomingDiv);
}
prevContestName = pending_contest_data[i].contest_name;
}
}
}
// generates card for archived contest
function generateCardsArchive(data) {
var archived_tabs = document.getElementById("archived-contests");
archived_tabs.innerHTML = "";
var archived_contest_data = JSON.parse(data)["archived"];
hosts = JSON.parse(localStorage.getItem('hosts'));
var prevContestName = "";
for (var i = 0; i < archived_contest_data.length; i++) {
if(prevContestName != archived_contest_data[i].contest_name){
archived_card = applyStyle(archived_contest_data[i], "archived");
if ((typeof localStorage["hosts"]) === 'undefined') {
document.getElementById("archived-contests").appendChild(archived_card);
}
else if (hosts.hasOwnProperty(archived_contest_data[i].host_name)) {
document.getElementById("archived-contests").appendChild(archived_card);
}
prevContestName = archived_contest_data[i].contest_name;
}
}
}
// select all the options from drop down which are already in the local storage
function loadDropDownWithHosts(data) {
var dropDown = document.getElementById("tokenize");
var hosts = (JSON.parse(data))["hosts"]
selected_hosts = JSON.parse(localStorage.getItem('hosts'));
for (var i = 0; i < hosts.length; i++) {
var newOption = document.createElement('option');
newOption.value = hosts[i];
if (!((typeof localStorage["hosts"]) === 'undefined') && selected_hosts.hasOwnProperty(hosts[i])) {
newOption.selected = true;
}
newOption.innerText = hosts[i];
dropDown.appendChild(newOption);
}
loadDropDown();
}
function loadDropDown() {
$('#tokenize').tokenize({
placeholder: "Filter by platform names...",
// displayDropdownOnFocus: true,
onAddToken: function(value, text){
if((typeof localStorage["hosts"]) === 'undefined') {
var hosts = {}
hosts[value] = true;
localStorage.setItem('hosts', JSON.stringify(hosts));
} else {
var hosts = JSON.parse(localStorage.getItem('hosts'));
if (hosts.hasOwnProperty(value)) {
} else {
hosts[value] = true;
localStorage.setItem('hosts', JSON.stringify(hosts));
}
}
if((typeof localStorage["data"]) === 'undefined') {
getChallengeData();
} else {
// if data already exists in localstorage, then call generateCards(data) else call getChallengeData()
var data = localStorage.getItem('data');
generateCards(data);
generateCardsArchive(data);
generateCardsPending(data);
}
},
onRemoveToken: function(value){
hosts = JSON.parse(localStorage.getItem('hosts'));
// check if hosts contain that value. should not be the case
if (!hosts.hasOwnProperty(value)) {
console.log("not there")
} else {
delete hosts[value];
var hostsLength = Object.keys(hosts).length;
// if all the hosts have been removed, delete the host key
if (hostsLength == 0) {
localStorage.removeItem('hosts');
} else {
localStorage.setItem('hosts', JSON.stringify(hosts));
}
}
// if data already exists in localstorage, then call generateCards(data) else call getChallengeData()
if((typeof localStorage["data"]) === 'undefined') {
getChallengeData();
} else {
var data = localStorage.getItem('data');
generateCards(data);
generateCardsArchive(data);
generateCardsPending(data);
}
},
});
}