-
Notifications
You must be signed in to change notification settings - Fork 0
/
simply-chat.js
137 lines (107 loc) · 4.05 KB
/
simply-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
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
Chat = new Mongo.Collection("chat"); //store chat mesages in mongoDB chat collection
if (Meteor.isClient) {
Meteor.subscribe("chat");
Meteor.subscribe("userState");
//==================================================================================BODY HELPERS START
Template.body.helpers({
chats: function () {
if (Meteor.users.findOne(Meteor.userId())) { //return all messages
return Chat.find({});
}
},
onlineUsers: function () {
if (Meteor.users.findOne(Meteor.userId())) {
return Meteor.users.find({"status.online": true, username: { $ne: Meteor.user().username }});
}
},
whoisTyping: function () {
if (Meteor.users.findOne(Meteor.userId())) {
return Meteor.users.find({"status.online": true, "typing": "typing", username: { $ne: Meteor.user().username } }); //get users typing status only if their online
}
}
});
//==================================================================================BODY HELPERS END
//==================================================================================BODY EVENTS START
Template.body.events({
//submit message if input text not empty
"submit .new-msg": function (event) {
var text = event.target.text.value;
if(text.trim() !== ""){
Meteor.call("isTyping", '');
Meteor.call("newMsg", text);
}
console.log(event);
// Clear form
event.target.text.value = "";
// Prevent default form submit
return false;
},
//monitor as user is typing and keep track if user is typing or not
"keyup .new-msg input[type=text]": function (event) {
if(event.target.value.trim() !== "") {
Meteor.call("isTyping", 'typing');
}
else{
Meteor.call("isTyping", '');
}
}
});
//==================================================================================BODY EVENTS START
//==================================================================================CHAT EVENTS START
Template.chat.events({
"click .delete": function () {
Meteor.call("deleteTask", this._id);
}
});
Template.chat.rendered = function () {
$(".chat-box").animate({ scrollTop: $('.chat-box')[0].scrollHeight}, 5); //automatic slide down when a new message is received
}
//==================================================================================CHAT EVENTS END
//==================================================================================CHAT HELPERS START
Template.chat.helpers({
isOwner: function () {
return this.owner === Meteor.userId();
},
//compare logged in user with chat author
equals: function(param1, param2) {
return (param1 === param2);
}
});
//==================================================================================CHAT HELPERS END
Accounts.ui.config({
passwordSignupFields: "USERNAME_ONLY"
});
}
//==================================================================================DEFINE METHODS HANDLING DB FUNCTIONALITIES
Meteor.methods({
newMsg: function (text) {
if (! Meteor.userId()) {
throw new Meteor.Error("not-authorized"); // Make sure the user is logged in before inserting a task
}
Chat.insert({
text: text,
createdAt: moment().tz('America/Belize').format('ha z'), //currently only saves time as local time
owner: Meteor.userId(),
username: Meteor.user().username
});
},
isTyping: function (text) {
Meteor.users.update(Meteor.userId(), { $set: { typing: text } }); //update users typing status when typing
},
/* animateChatbox: function(){
$(".chat-box").animate({ scrollTop: $('.chat-box')[0].scrollHeight}, 5);
}, :: caused $ is not defined error when called jquery animation through this function*/
deleteTask: function (taskId) {
var task = Chat.findOne(taskId); //get the task and remove
Chat.remove(taskId);
}
});
//==================================================================================SERVER SIDE, PUBLISH NECESSARY COLLECTIONS
if (Meteor.isServer) {
Meteor.publish("chat", function () {
return Chat.find({});
});
Meteor.publish("userState", function (event) {
return Meteor.users.find({});
});
}