-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclapHandling.js
81 lines (68 loc) · 2.3 KB
/
clapHandling.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
var hashtags = function (text) {
return text.replace( new RegExp(/#\w+/g),
function myFunction (x) {
return "<a class='hashClick' href='#!'>" + x + "</a>";
}
);
};
var serverGrab = function(mapName) {
$.get('/allClaps', function(data) {
var claps = JSON.parse(data).sort(sortClaps);
var accessDOM = '';
var clapLoad = claps.length > 50 ? 50 : claps.length;
for(var i = 0 ; i < clapLoad; i++) {
accessDOM += addClap(claps[i]);
}
$("#claps").html(accessDOM);
$("#claps").fadeIn("slow");
});
};
var timeParser = function (time) {
return time.toLocaleDateString() + " " + time.getHours() + ":" + time.getMinutes();
};
var addClap = function (data) {
if(data.message.indexOf("#") > -1) {
data.message = hashtags(data.message);
}
var out = '<div id="' + data.time + '" class="clap">' +
'<p>' + data.message + '</p>';
out += data.username !== "###" ? '<p><b>var</b> <i>name</i> = "' + data.username + '";</p>' : '';
out += data.userId === userId ? '<button class="delButtons">x</button></br></br>' : '';
return out + timeParser(new Date(Number(data.time))) + '</p>' + '</div>';
};
var sortClaps = function (a, b) {
var aTime = Number(a.time);
var bTime = Number(b.time);
if (aTime < bTime) {
return 1;
}
if (aTime > bTime) {
return -1;
}
return 0;
};
var addCookie = function() {
var userId = "";
var chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for(var i = 0; i < 10; i++) {
userId += chars.charAt(Math.floor(Math.random() * chars.length));
}
return "userId=" + userId + ";"; //expires=Fri, 18 Dec 2015 12:00:00 UTC";
};
var needsUsername = function () {
var check = document.cookie;
return (check.indexOf("username=") < 0 || //check if a username cookie doesn't exist
check.split("username=").pop().split(";").shift() === "anonymous"); //or is our null placeholder
};
var cookieCheck = function() {
var check = document.cookie;
if(check.indexOf("username=") < 0) {
document.cookie = "username=anonymous";
}
if(check.indexOf("userId=") < 0) {
document.cookie = addCookie();
}
userId = document.cookie.split("userId=").pop().split(";").shift();
username = document.cookie.split("username=").pop().split(";").shift();
console.log("userId=" + userId, "username=" + username);
};