-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathclient.js
More file actions
147 lines (134 loc) · 4.01 KB
/
client.js
File metadata and controls
147 lines (134 loc) · 4.01 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
// utility functions
util = {
urlRE: /https?:\/\/([-\w\.]+)+(:\d+)?(\/([^\s]*(\?\S+)?)?)?/g,
// html sanitizer
toStaticHTML: function (inputHtml) {
inputHtml = inputHtml.toString();
return inputHtml.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">");
},
//pads n with zeros on the left,
//digits is minimum length of output
//zeroPad(3, 5); returns "005"
//zeroPad(2, 500); returns "500"
zeroPad: function (digits, n) {
n = n.toString();
while (n.length < digits)
n = '0' + n;
return n;
},
//it is almost 8 o'clock PM here
//timeString(new Date); returns "19:49"
timeString: function (date) {
var minutes = date.getMinutes().toString();
var hours = date.getHours().toString();
return this.zeroPad(2, hours) + ":" + this.zeroPad(2, minutes);
},
dateString: function (d) {
var h = this.zeroPad(2, d.getHours());
var m = this.zeroPad(2, d.getMinutes());
var s = this.zeroPad(2, d.getSeconds());
var yy = d.getFullYear();
var mm = this.zeroPad(2, d.getMonth() + 1);
var dd = this.zeroPad(2, d.getDate());
return yy + "-" + mm + "-" + dd + " " + h + ":" + m + ":" + s
},
//does the argument only contain whitespace?
isBlank: function (text) {
var blank = /^\s*$/;
return (text.match(blank) !== null);
}
};
function updateRSS(rss) {
var bytes = parseInt(rss);
if (bytes) {
var megabytes = bytes / (1024 * 1024);
megabytes = Math.round(megabytes * 10) / 10;
$("#rss").text(megabytes.toString());
}
}
//submit a new message to the server
function send() {
jQuery.get("/send", {
to: $("#to").attr("value"),
subject: $("#subject").attr("value"),
text: $("#text").val()
}, function (data) {
}, "json");
}
//Transition the page to the state that prompts the user for a nickname
function showConnect() {
$("#connect").show();
$("#loading").hide();
$("#body").hide();
}
//transition the page to the loading screen
function showLoad() {
$("#connect").hide();
$("#loading").show();
$("#body").hide();
}
//transition the page to the main chat view, putting the cursor in the textfield
function showForm() {
$("#connect").hide();
$("#loading").hide();
$("#body").show();
}
//get a list of the users presently in the room, and add it to the stream
function status() {
jQuery.get("/status", {}, function (data, status) {
starttime = new Date(data.starttime);
rss = data.rss;
updateRSS(rss);
$("#uptime").text(starttime);
}, "json");
}
$(document).ready(function () {
$("#submit").click(function () {
showLoad();
$.ajax({
type: "GET" // XXX should be POST
, dataType: "json"
, url: "/join"
, data: {email: $("#email").attr("value")}
, error: function (data) {
console.error(data);
alert(data.response);
showConnect();
}
, success: function (data){
console.log(data);
showForm();
}
});
return false;
});
$("#send").click(function () {
showLoad();
$.ajax({
type: "GET" // XXX should be POST
, dataType: "json"
, url: "/send"
, data: {
to: $("#to").attr("value"),
subject: $("#subject").attr("value"),
text: $("#text").val()
}
, error: function (data) {
console.error(data.response);
alert(data.response);
showForm();
}
, success: function (data){
console.log(data);
alert(data.response);
showForm();
$("#text").val("");
}
});
return false;
});
showConnect();
status();
});