-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathprimabylon.js
278 lines (202 loc) · 7.69 KB
/
primabylon.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Global-Part
var Messages = new Meteor.Collection("messages");
var Translations = new Meteor.Collection("translations");
var nowTranslated = false;
var toTranslate = false;
var diffDate;
// helper function
function addslashes(str) {
return (str + '').replace(/([\\'])/g, "\\$1").replace(/\0/g, "\\0");
}
// helper time format (time object)
function timeformat(obj) {
var currentHours = obj.getUTCHours();
var currentMinutes = obj.getUTCMinutes();
var currentSeconds = obj.getUTCSeconds();
currentHours = (currentHours < 10 ? "0": "") + currentHours;
currentMinutes = (currentMinutes < 10 ? "0": "") + currentMinutes;
currentSeconds = (currentSeconds < 10 ? "0": "") + currentSeconds;
return time = currentHours + ":" + currentMinutes + ":" + currentSeconds;
}
// Client-Part
if (Meteor.isClient) {
Meteor.startup(function() {
Session.set("username", undefined);
Session.set("userlang", undefined);
Meteor.call('getServerDate', function(error, serverdate) { //removeCall
if (error)
console.log(error);
// Difference between server and client date + latence time
diffDate = new Date(serverdate) - new Date();
});
});
// Yahoo Query Language Wrapper for jQuery
$.YQL = function(query, callback) {
if (!query || !callback) {
throw new Error('$.YQL(): Parameters may be undefined');
}
// Whole Query for JSON output
var url = "http://query.yahooapis.com/v1/public/yql?q=" + query + "&format=json&env=store://datatables.org/alltableswithkeys&callback=?";
$.getJSON(url, callback);
};
// global helper: is the Session set?
Handlebars.registerHelper('chatUser', function() {
if (Session.get("username") && Session.get("userlang")) {
return true;
}
});
// global helper: Current User Language?
Handlebars.registerHelper('userLang', function() {
return Session.get("userlang");
});
////////////////////////////////////////////////////////////////////////////////////
Template.line.MessageList = function() {
// main routine - searching messages to translate them in current user language - the code screams for optimization!
Messages.find({}, {
sort: {
created: 1
}
}).forEach(function(message) {
if (message.userlang != Session.get("userlang")) {
toTranslate = true;
for (lang in message.languages) {
if (message.languages[lang] == Session.get("userlang")) {
toTranslate = false;
nowTranslated = false;
break;
}
}
if (toTranslate) {
// selecting DOM elements at Google Translate, since the Google API is not free, this approach will help us out
var yq = encodeURIComponent("select json.json.json from google.translate where q='" + addslashes(message.text) + "' and source='" + message.userlang + "' and target='" + Session.get("userlang") + "' limit 1");
$.YQL(yq, function(data) {
var post = data.query.results.json.json.json.json;
if (post) {
nowTranslated = true;
Translations.insert({
text: post,
user: message.user,
userlang: Session.get("userlang"),
origin: message.userlang,
created: message.created,
time: message.time
});
}
});
}
}
});
// Making sure all translations were saved
Meteor.flush();
return Translations.find({
userlang: Session.get("userlang")
}, {
sort: {
created: 1
}
});
};
Template.line.rendered = function() {
// Dirty code - updating global message collection while rendering the translation
if (nowTranslated) {
Messages.update({
languages: {
$nin: [Session.get("userlang")]
}
}, {
$push: {
languages: Session.get("userlang")
}
}, {
multi: true
});
}
};
Template.chat.rendered = function() {
var element = this.find("#chatbox");
if(element.clientHeight === element.scrollHeight) {
this.scrolledFlush = true;
}
else if(this.scrolledFlush) {
element.scrollTop = element.scrollHeight - element.clientHeight;
}
else if(typeof this.scrolledFlush == "undefined") { //first time rendered
element.scrollTop = element.scrollHeight - element.clientHeight;
}
};
Template.chat.events({
'scroll #chatbox': function(event, template) {
var e = event.target;
if(e.scrollTop === e.scrollHeight - e.clientHeight) {
template.scrolledFlush = true;
} else {
template.scrolledFlush = false;
}
}
});
Template.hello.events({
// Event handling the enter button
"keydown #mymessage": function(event) {
var code = (event.keyCode ? event.keyCode: event.which);
if (code == 13) {
var text = $('#mymessage').val();
curdate = new Date();
curdate = new Date(curdate.getTime() + diffDate); // Added diff from serverdate
curtime = timeformat(curdate);
Messages.insert({
text: text,
user: Session.get("username"),
userlang: Session.get("userlang"),
created: curdate,
time: curtime,
languages: [Session.get("userlang")]
});
Translations.insert({
text: text,
user: Session.get("username"),
userlang: Session.get("userlang"),
origin: Session.get("userlang"),
created: curdate,
time: curtime
});
Meteor.flush();
$('#mymessage').val('');
}
},
// Start chatting after choosing username/language
"click #btn-sess": function() {
var username = $('input:text[name=username]').val();
var userlang = $('#userlang').val();
if (username && userlang) {
Session.set("username", username);
Session.set("userlang", userlang);
}
}
});
}
// Server-Part
if (Meteor.isServer) {
Meteor.startup(function() {
Messages.remove({});
Translations.remove({});
// Observe message count, reset system after saving more than 50 messages
var countMsg = 0;
var query = Messages.find({});
var handle = query.observe({
added: function() {
countMsg++;
if (countMsg > 50) {
Meteor.flush();
Messages.remove({});
Translations.remove({});
countMsg = 0;
}
}
});
});
Meteor.methods({
getServerDate: function() {
return new Date();
}
});
}