-
Notifications
You must be signed in to change notification settings - Fork 14
/
chat.js
70 lines (62 loc) · 1.66 KB
/
chat.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
var Rooms = new Meteor.Collection("rooms");
var Messages = new Meteor.Collection("messages");
if (Meteor.is_client) {
Template.rooms.events = {
"click #addRoom": function (){
var roomName = window.prompt("Name the room", "My room") || "Anonymous Room";
if(roomName) {
Rooms.insert({"name": roomName});
}
}
};
Template.main.currentRoom = function (){
return Session.get("room") || false;
};
Template.rooms.availableRooms = function (){
return Rooms.find({});
};
Template.roomItem.events = {
"click .enter": function (){
var name;
if(Session.get("name") === undefined) {
name = window.prompt("Your name", "Guest") || "Jerky";
Session.set("name", name);
}
Session.set("room", this._id);
},
"click .delete": function (){
Rooms.remove({_id:this._id});
}
};
Template.room.roomName = function (){
var room = Rooms.findOne({_id: Session.get("room")});
return room && room.name ;
};
Template.room.messages = function (){
return Messages.find({room: Session.get("room")});
};
Template.messageItem.authorClass = function (){
return Session.equals("name", this.author) ? ' mine' : '';
};
Template.room.events = {
"click #leave": function (){
if(!window.confirm("Leave this room?", "Do you really want to leave?")) { return; }
Session.set("room", undefined);
},
"submit": function (){
var $msg = $("#msg");
if ($msg.val()){
Messages.insert({
"room": Session.get("room"),
"author": Session.get("name"),
"text": $msg.val(),
"timestamp": (new Date()).toUTCString()
});
}
$msg.val("");
$msg.focus();
Meteor.flush()
$("#messages").scrollTop(99999);
}
};
}