-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.html
More file actions
288 lines (267 loc) · 9.03 KB
/
chat.html
File metadata and controls
288 lines (267 loc) · 9.03 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
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
279
280
281
282
283
284
285
286
287
288
<html>
<head>
<script src="https://cdn.firebase.com/js/client/2.2.1/firebase.js"></script>
<script src='https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js'></script>
<script src='/javascripts/google_auth.js'></script>
<script src='javascripts/microsoftTranslator.js'></script>
<script src='javascripts/translationRouter.js'></script>
<script src='javascripts/jquery.phoenix.min.js'></script>
<script src='javascripts/clientNaverTranslator.js'></script>
<link rel="stylesheet" type="text/css" href="css/chat.css">
</head>
<body>
<!-- CHAT MARKUP -->
<div class="example-chat l-demo-container">
<header>
<div class='example-chat-toolbar'>
<h2>chat with translation</h2>
</div>
<div class='example-chat-username-field'>
<!--<label for="nameInput">name:</label>-->
<input type='text' id='nameInput' placeholder='enter a username...' class='phoenix' >
<!--<input type='button' id='logOut' value='log out'>-->
</div>
</header>
<ul id='example-messages' class="example-chat-messages"></ul>
<footer>
<input type='text' id='messageInput' placeholder='Type a message...'>
<div class='lang-selector'>
<div id='from-lang'>
<label>I am typing in: </label>
<select name="my-lang" class='phoenix'>
<option value="en" selected>en</option>
<option value="es">es</option>
<option value="de">de</option>
<option value="ko">ko</option>
<option value="zh-CHS">zh-CHS</option>
</select>
</div>
<div id='to-lang'>
<form class='phoenix'>
<label>I want to see translations in:</label>
<br>
<label>en</label>
<input type="checkbox" class='phoenix' name="en" value="en">
<label>es</label>
<input type="checkbox" class='phoenix' name="es" value="es">
<label>de</label>
<input type="checkbox" class='phoenix' name="de" value="de">
<label>ko</label>
<input type="checkbox" class='phoenix' name="ko" value="ko">
<label>zh-CHS</label>
<input type="checkbox" class='phoenix' name="zh-CHS" value="zh-CHS">
</form>
</div>
</div>
</footer>
</div>
<div class='notes'>
<h3>Latest changes:</h3>
<ul>
<li>new visual design</li>
<li>translator token requests use promises to avoid translation without a token</li>
<li>translator modularized</li>
<li>Added Naver for Korean translation</li>
</ul>
</div>
<!-- auth connector -->
<script>
var nameField = $('#nameInput');
var logOutButton = $('#logOut');
var rootRef = new Firebase("https://suminchat.firebaseio.com");
var onAuthCallback = function(authData) {
googleAuth.authDataCallback(authData);
if (authData) {
if (($('#nameInput').val().length < 1) ) {
updateUserName(googleAuth.userName);
};
rootRef.offAuth(onAuthCallback);
setupMessageMonitor();
};
};
// this seems to work but the below version is simpler
/*
setTimeout(function() {
var auth = rootRef.getAuth();
if (auth == null) {
rootRef.onAuth(onAuthCallback);
} else {
onAuthCallback(auth);
}
}, 1000);
*/
//$(document).ready(setTimeout(function() { rootRef.onAuth(onAuthCallback); }, 200));
// we'll do this onLoad
logOutButton.click(function() {
rootRef.unauth(); // not working so great
});
function updateUserName(name) {
nameField.val(name);
};
</script>
<!-- checkbox handlers -->
<script>
var supportedLanguageCodes = [ 'en', 'es', 'de', 'ko', 'zh-CHS' ];
function checkBoxFor(lang) { return($('input[value='+lang+']')); };
function isARequestedDisplayLanguage(lang) {
if (checkBoxFor(lang).is(':checked')) {
return(true);
} else {
return(false);
}
};
// handle to checkbox - displaying translations based upon checked boxes
$(document).ready(function() {
supportedLanguageCodes.forEach(function(langKey) {
var langClass = '.'+langKey;
var langCheckBox = checkBoxFor(langKey);
langCheckBox.click(function() {
if (langCheckBox.is(':checked')) {
$(langClass).show(1, scrollToBottom);
scrollToBottom();
} else {
$(langClass).slideUp('slow');
}
})
});//forEach
});
$(document).ready(function() {
$('.phoenix').phoenix();
});
function scrollToBottom() {
messageList[0].scrollTop = messageList[0].scrollHeight;
};
</script>
<!-- CHAT JAVACRIPT -->
<script>
var chatRoom = location.host.match(/localhost/) ? 'demo2' : 'demo1';
var messagesRef = new Firebase('https://suminchat.firebaseio.com/').child(chatRoom);
// REGISTER DOM ELEMENTS
var messageField = $('#messageInput');
var nameField = $('#nameInput');
var messageList = $('#example-messages');
// LISTEN FOR KEYPRESS EVENT
messageField.keypress(function (e) {
if (e.keyCode == 13) {
//FIELD VALUES
var username = nameField.val();
var message = messageField.val();
var currentLang = $('[name=my-lang] option:selected').attr('value');
//SAVE DATA TO FIREBASE AND EMPTY FIELD
var newMessageRef = messagesRef.push({
name:username,
text:message,
userID:googleAuth.userID,
translations: null,
specifiedLang: currentLang
});
//kick off translation for this message
handleTranslation(newMessageRef, message, currentLang);
messageField.val('');
}
});
function setupMessageMonitor() {
// Add a callback that is triggered for each chat message.
messagesRef.limitToLast(30).on('child_added', function (snapshot) {
//GET DATA
var data = snapshot.val();
var username = data.name || "anonymous";
var message = data.text;
var uID = data.userID || 'none';
var mID = snapshot.key(); //
//CREATE ELEMENTS MESSAGE & SANITIZE TEXT
if (uID == googleAuth.userID) {
var messageElement = $("<li class='me'>");
}
else {
var messageElement = $("<li>");
};
messageElement.addClass("userID-" + uID);
messageElement.attr('id', "messageID" + mID);
var nameElement = $("<strong class='example-chat-username'></strong>")
nameElement.text(username);
messageElement.text(message).prepend(nameElement);
// inserting translations
var translationHTML = generateTranslationElements(data.translations)
messageElement.append(translationHTML);
//put the message on the page
messageList.append(messageElement);
//SCROLL TO BOTTOM OF MESSAGE LIST
messageList[0].scrollTop = messageList[0].scrollHeight;
});
// ADD NEW MESSAGE TRANSLATIONS
// this most likely is only going to occur on a translation update
// loading of new messages should be in child added
messagesRef.limitToLast(10).on('child_changed', function(snapshot) {
//$('.notes').append('<p>received update</p>');
var data = snapshot.val();
var mID = snapshot.key();
//find the particular message
messageElement = $('#messageID'+mID);
// inserting translations
if (data.translations && messageElement) {
var translationHTML = generateTranslationElements(data.translations)
//messageElement.children('.translations').remove();
//translationHTML.hide();
messageElement.children('.translations').replaceWith(translationHTML);
messageElement.children('.translations').fadeIn(400);
//SCROLL TO BOTTOM OF MESSAGE LIST
messageList[0].scrollTop = messageList[0].scrollHeight;
};
});
};
function generateTranslationElements(translations) {
var elem = $("<p class='translations'>");
if (translations) {
keys = Object.keys(translations);
keys.forEach(function(key) {
var translatedMessage = translations[key];
if (translatedMessage) {
var newElem = $('<p class="translation ' + key +'">' + key + ': ' + translatedMessage + '</p>');
elem.append(newElem);
if (isARequestedDisplayLanguage(key)) {
newElem.show();
};
};
});
};
return(elem);
// end translations
};
</script>
<script>
//translator integration
var __last_msg = {};
var textInput = $('#textInput');
onload = function ()
{
console.log('do auth');
setTimeout(function() { rootRef.onAuth(onAuthCallback); }, 500);
}
// request a new callback function and pass its name and the text to the translator
function handleTranslation(ref, message, fromLang) {
// TODO: currently we try to translate to all supported languages, this is wasteful
// but would require a fair bit of re-work to fix.
supportedLanguageCodes.forEach(function(langCode) {
//build a new function to be called as the callBack by ms translator
var opts = { firebaseRef: ref, to: langCode } // this needs to be unique for each call
Translator.translate(message,
fromLang,
langCode,
updateFBWithTranslation,
opts);
});
}
// update the firebase Reference with the translated message
function updateFBWithTranslation(translatedMessage, opts) {
var ref = opts.firebaseRef;
var lang = opts.to;
var keySet = {};
console.log(translatedMessage, opts);
keySet[lang] = translatedMessage;
// ref.update({translations: keySet});
ref.child('translations').update(keySet);
};
</script>
</body>
</html>