-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbuilder.html
259 lines (175 loc) · 7.75 KB
/
chatbuilder.html
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
<!DOCTYPE html>
<html>
<head>
<link href="chatbuilder.css" rel="stylesheet" type="text/css">
<meta charset="utf-8">
<title>Chatbuilder</title>
<script src="http://chatbuilder.hackreactor.com/ChatBuilder.js"></script>
</head>
<body>
<div class="mask"></div>
<div class="container">
<h2>Greg Domorski's Fixed ChatBuilder App</h2>
<h3 class="new-msgs"></h3>
<input placeholder="Type your message here" class="draft" type="text"/>
<button class="send">Send</button>
<button onclick="changeUserName()">Change Username </button>
<button onclick="resizeText(1)"> Increase Text Size</button>
<button onclick="resizeText(-1)"> Decrease Text Size</button>
<button class='open'>Register Your Username</button>
<ul class="messages"> </ul>
</div>
<form class="pop-up-box">
<h3> Register Your Username </h3>
<label>First Name:<label>
<input type="text"/> <p>
<label>Last Name:<label>
<input type="text"/> <p>
<label>Desired Username:<label>
<input type="text"/> <p>
<label>Email:<label>
<input type="text"/> <p>
<button type='button' class="close">Register</button>
</form>
<script>
//moved the script to the bottom of the page so it has time to load all the HTML
/*
* Nice, you found the HTML source code for ChatBuilder! This document kicks everything off when you load it
* in your browser, and is a starting point for the whole app. It's pretty incomplete though--you should
* make your own version of it that works better!
*
* You can't edit the code here until it's in a file on your hard drive, so copy this whole page of source
* code and paste it into a plain text editor like Sublime Text 2 (http://www.sublimetext.com/). Save it as
* a `.html` file, and open that file with Google Chrome. You can now edit it however you like, and refresh
* the page to see your modifications take effect on how the application runs.
*
* Fair warning: one or more of the steps ahead could require a good amount of research to understand all
* the terms and technologies mentioned. Googling words you haven't heard before is a great idea. Just be
* careful not to spend too long in 'research mode' without making any forward progress on your real goal of
* completing the app!
*
* When you've got this code saved as a local file, uncomment the line of JavaScript code below and open
* the new file in Google Chrome. Your next instructions will be waiting for you in the JavaScript console.
* If you already know the Chrome JS development tools pretty well, feel free to skip this opening tutorial
* by calling the `.start()` function on `Chat.guide` instead of `.intro()`
*
* NOTE: Please do not publish or share any of the code associated with this challenge. We've worked really
* hard to build this material and publishing solutions diminishes its effectiveness.
*/
delete Chat.display;
delete Chat.fetch;
delete Chat.send;
var counter = 0;
var msgLength = 0;
var newUserName;
$(document).ready(function(){
getMessages();
$('.container').fadeIn(2000);
$('input').keydown(function(event){
if (event.keyCode === 13) {
Chat.send($('.draft').val());
};
});
$('.open').click(function(){
$('.pop-up-box').fadeIn(1800);
$('.mask').fadeIn(400);
});
$('.close').click(function(){
$('.pop-up-box').fadeOut(1800);
$('.mask').fadeOut(1800);
});
});
Chat.display = function (msg){
// add that li to the messages list
var msgArr = msg.text.split(':');
var str = msg.text;
if(str.includes(Chat.username)){
$('.messages').prepend('<li> <span class="my-username">'+ msgArr[0] +'</span> <span class="my-username">' + msgArr[1] + '</span></li>');
} else{
$('.messages').prepend('<li> <span class="user-name">'+ msgArr[0] +'</span>' + msgArr[1] + '</li>');
/* use dot to reference classes, # references an ID nothing, references the tag name, append adds something to the front while prepend adds something the the end. li refers to a list element. We are getting the text from an object. prepend adds messages to the top, append adds messages to the bottom, string represnetation of an element for prepend */
}
};
Chat.fetch = function (fn){ //this function goes to the server returns a array of messages, when it came back from the server, run a callback function
var url = Chat.resourceAddress + '?order=createdAt' //the resource address can be found when you console.log(Chat)
$.get(url, function( data ) { //find the location from the network panel, job of this url is to send messages
$('.messages').html(''); //clearing all the messages
fn(data.results); //adds them to the page, results can be found in the response window of the url
// alert( "Load was performed." );
});
}
Chat.send = function (str){
// $.ajax ({
// method: "POST",
// url: 'https://api.parse.com/1/classes/chats',
// data: { text: 'str', username: 'Dan'}
// })
$.post(Chat.resourceAddress, JSON.stringify({text: Chat.username + ": " + str, username: Chat.username})
); //second parameter is the format the server is expecting
$('input').val('')
}
var getMessages = function () {
Chat.fetch(function(msgs){ ////fetch gets the messages
msgLength = msgs.length;
msgs.forEach(function(msg){ //forEach method shows each message on a different line
Chat.display(msg); //display gets the messages on the screen
});
});
};
window.setInterval(function(){
getMessages();
counter += msgLength; //shothand way of writing counter = counter + msgs.length;
// var messageElement = $('.new-msgs');
// var text = counter + ' New Messages' //.text is a getter
// messageElement.text(text); //sets the text = to variable text, which is a string
if(counter > 50){
// messageElement.show();
}
}, 3000); //get messages every three seconds.
// Hook up the button and the input with jQuery
$('.send').click(function() { //send references the button class
Chat.send($('.draft').val()); //gets the text inside the draft element, .val gets the value;
});
function changeUserName(){
newUserName = prompt('What do you want to change your username to?');
if(newUserName){
Chat.username = newUserName
alert("Your username has been changed to " + Chat.username )
}else{
alert("Your username will stay at " + Chat.username )
}
}
function resizeText(multiplier) {
if (document.body.style.fontSize == "") {
document.body.style.fontSize = "1.0em";
}
document.body.style.fontSize = parseFloat(document.body.style.fontSize) + (multiplier * 0.2) + "em";
}
// all our messages
// 1. rob1
// 2. rob2
// 1. ourmessage
// 2. rob1
// 3. rob2
// 4. rob3
// 5. rob4
// 6. rob5
// server list of messages
// 1. rob1
// 2. rob2
// 3. rob3
// 4. ourmessage
// 5. rob1
// 6. rob3
// our page list of messages
// // 1. rob3
// // 2. rob2
// // 3. rob1
// 1. ourmessage
// 2. rob3
// 3. rob2
//text = "Robochat: Androids dream of"
//username = "Robochat"
</script>
</body>
</html>