-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathplexis.js
180 lines (159 loc) · 5.95 KB
/
plexis.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
// both client and server code here
// function getHashtag (message) {
// hashtag = message.split(/[#]+/).pop().toLowerCase()
// if !(message.split(/[#]+/).length === 1) {
// return hashtag;
// }
// else {
// return null;
// }
// }
// function getTopic (message) {
// words = message.split(" ");
// for (var i=0;i<words.length;i++) {
// if ($.inArray(words[i], topics)) { //unsure of how list of topics are being stored: using topics variable for now, should throw some error
// return words[i];
// foundTopic = true;
// break;
// }
// if !(foundTopic) {
// return null;
// }
// }
// }
if (Meteor.isClient) {
Template.conversation.branches = function() {
var messages = Messages.find({}, {sort: [["branchId", "asc"], ["timestamp", "asc"]]}).fetch();
var branches = []
var currentBranch = -1;
for (var i = 0; i < messages.length; i++) {
if (currentBranch !== messages[i].branchId) {
console.log('creating new branch!');
branch = {branchId: messages[i].branchId, messages: [messages[i]]};
branches.push(branch);
currentBranch = messages[i].branchId
} else {
console.log('pushing to existing branch with message: ' + messages[i].message);
branch.messages.push(messages[i]);
}
}
return branches;
};
Template.user.user = function() {
if(Meteor.user()) {
return Meteor.user().username;
}
};
Template.logout.events({
'click #sign-out': function() {
Meteor.logout();
return false;
}
});
Template.conversation.events({
'keyup .message': function(e) {
if (e.keyCode === 13) {
if (Meteor.user()) {
console.log('putting stuff in database in keyup .message');
Messages.insert({message: $(e.target).val(),
parentId: $(e.target).prev()[0].id,
branchId: $(e.target).parent().data('branchid'),
owner: Meteor.userId(),
//username: Meteor.user().emails[0].address.replace(/\@.*$/, ''),
username: Template.user.user(),
timestamp: new Date});
$('.message').val('');
} else {
alert('you must be logged in to do this!');
}
}
return false;
},
'click .submit': function(e) {
if (Meteor.user()) {
var $message = $(e.target).siblings('.message')
console.log('putting stuff in database in click .submit');
Messages.insert({message: $message.val(),
parentId: $message.prev()[0].id,
branchId: $(e.target).parent().data('branchid'),
owner: Meteor.userId(),
//username: Meteor.user().emails[0].address.replace(/\@.*$/, ''),
username: Template.user.user(),
timestamp: new Date});
$message.val('');
} else {
alert('you must be logged in to do this!')
}
return false;
},
'click .branch-link': function(e) {
// optimize this so we don't have to do another lookup
var messages = Messages.find({}, {sort: [["branchId", "desc"]]}).fetch();
var nextBranch = parseInt(messages[0].branchId) + 1;
Messages.insert({
message: $(e.target).siblings('.message-text').text(),
parentId: $(e.target).parent()[0].id,
branchId: nextBranch,
owner: Meteor.userId(), // 'true owner' of this duplicate is brancher, not necessarily orig message author
// owner: $(e.target).siblings('.owner').val() <-- this is optimal, but permissions don't allow it
username: $(e.target).siblings('.username').text(), // copy over username instead of do lookup
timestamp: new Date});
return false;
}
});
Template.register.events({
'click #create-account, keyup #account-email, keyup #account-name, keyup #account-password' : function(e, t) {
if (e.keyCode === 13 || e.type === 'click') {
var email = t.find('#account-email').value
, password = t.find('#account-password').value
, fullname = t.find('#account-name').value;
// Trim and validate the input
Accounts.createUser({email: email, password : password, username: fullname}, function(err){
if (err) {
console.log('gone pooped!');
// Inform the user that account creation failed
} else {
console.log('account made');
$('#account-email').val('');
$('#account-name').val('');
$('#account-password').val('');
// Success. Account has been created and the user
// has logged in successfully.
}
});
}
return false;
}
});
Template.login.events({
'click #login-button, keyup #login-email, keyup #login-password' : function(e, t){
// retrieve the input field values
if (e.keyCode === 13 || e.type === 'click') {
var email = t.find('#login-email').value
, password = t.find('#login-password').value;
// Trim and validate your fields here....
// If validation passes, supply the appropriate fields to the
// Meteor.loginWithPassword() function.
Meteor.loginWithPassword(email, password, function(err){
if (err) {
console.log('cool story bro, try again');
//Notifications.insert({text: 'Error Logging In!', type: 'login-error', ttl: 3});
}
// The user might not have been found, or their passwword
// could be incorrect. Inform the user that their
// login attempt has failed.
else {
console.log('you\'ve earn an Internet logged in');
//Notifications.insert({text: 'Login Success!', type: 'login-success', ttl: 1});
}
});
}
return false;
}
});
}
if (Meteor.isServer) {
Meteor.startup(function () {
// code to run on server at startup
});
}