-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
313 lines (270 loc) · 10.9 KB
/
index.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
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<link href="https://fonts.googleapis.com/css?family=Limelight|Flamenco|Federo|Yesteryear|Josefin Sans|Spinnaker|Sansita One|Handlee|Droid Sans|Oswald:400,300,700" media="screen" rel="stylesheet" type="text/css" />
<link href="stylesheets/bootstrap.css" media="screen" rel="stylesheet" type="text/css" />
<link href="stylesheets/bootstrap-responsive.css" media="screen" rel="stylesheet" type="text/css" />
<link href="stylesheets/common.css" media="screen" rel="stylesheet" type="text/css" />
<link href="stylesheets/fontawesome.css" media="screen" rel="stylesheet" type="text/css" />
<link href="stylesheets/project.css" media="screen" rel="stylesheet" type="text/css" />
<link href="stylesheets/index.css" media="screen" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/json2.js"></script>
<script type="text/javascript" src="js/underscore.js"></script>
<script type="text/javascript" src="js/backbone.js"></script>
<script type="text/javascript" src="js/backbone.marionette.js"></script>
<script type="text/javascript" src="js/backbone.localStorage.js"></script>
<style type="text/css">
#message-list
{
width:250px;
height:300px;
overflow-y: scroll;
}
#message-input
{
width:200px;
}
</style>
<title>Chat</title>
</head>
<body>
<div class="container-fluid">
<div class="well well-1 well-2 well-3">
<h3 class="heading">Chat Now</h3>
<div class="row-fluid">
<span id="main-region" class="span12">
<div id="message-list" class="well well-8">
<div class="row-fluid">
<span class="span12"></span>
</div>
</div>
<div id="action-list" class="row-fluid">
<span class="span12">
<div class="well well-1 well-2 well-5">
<input placeholder="" id="message-input" class="span12" name=""/>
</div>
</span>
<span class="span6">
<div class="well well-1 well-2 well-5">
<button id="message-send" class="btn">SEND MESSAGE</button>
</div>
</span>
</div>
</span>
</div>
</div>
</div>
<script type="text/javascript">
var messageCache = [{ // Create a local variable to store messages
user: "Admin", // Add the initial entry where the user is named Admin
message: "Hello, world!", // Add the initial entry with message "Hello, World!"
datetime:showdate()
}];
$(document).ready(function()
{
var ChatManager = new Marionette.Application(); //Create a local variable ChatManager to store Application type.
//Declare regions for various areas
ChatManager.addRegions({
mainRegion:"#main-region",
messageRegion:"#message-list",
actionRegion:"#action-list"
});
//Created model for Message
ChatManager.Message=Backbone.Model.extend({
defaults: {
user:"Admin",
message:"Hello, world!",
datetime:showdate()
}
});
//Created a collection object with model
ChatManager.MessageCollection=Backbone.Collection.extend({
model:ChatManager.Message,
localStorage: new Backbone.LocalStorage("ChatManager-Message")
});
//Created Message Collection object to store collection of messages.
var messages=new ChatManager.MessageCollection();
//Created local variable to store the message.
var cachedMessage="";
//ItemView created for actions
ChatManager.ActionView=Marionette.ItemView.extend({
template:"#action-list",
events:{
"click #message-send":"addguestmessage", //Event for button click.
"keypress #message-input":"checkenterkey" //Event for input to add message on enter key.
},
addguestmessage:function(){
//Create a message object, This will be used to add Guest message into the chat.
var message=new ChatManager.Message({
user:"Guest",
message:$('#message-input').val(),
datetime:showdate()
});
ChatManager.addMessage(message);
//Create a message object, This will be used to add Admin message into the chat.
message=new ChatManager.Message({
user:"Admin",
message:cachedMessage,
datetime:showdate()
});
//Show Admin message after 1 sec.
var t=setTimeout(function(){ChatManager.addMessage(message)}, 1000); s
},
checkenterkey:function(e){
if (!e) e = window.event;
var keyCode = e.keyCode || e.which;
if (keyCode == '13')
{
//Create a message object, This will be used to add Guest message into the chat.
var message=new ChatManager.Message({
user:"Guest",
message:$('#message-input').val(),
datetime:showdate()
});
ChatManager.addMessage(message);
//Add Admin message into the chat.
message=new ChatManager.Message({
user:"Admin",
message:cachedMessage,
datetime:showdate()
});
//Show Admin message after 1 sec.
var t=setTimeout(function(){ChatManager.addMessage(message);}, 1000);
}
}
});
//Created ItemView for admin template
ChatManager.MessagesItemView=Marionette.ItemView.extend({
template:"#message-template-admin"
});
//Created ItemView for guest template
ChatManager.MessagesItemViewGuest=Marionette.ItemView.extend({
template:"#message-template-guest"
});
//Collection View created and onRender to add animation affect on addition of new message.
ChatManager.MessagesView=Marionette.CollectionView.extend({
itemView:ChatManager.MessagesItemView,
onRender: function() {
(this.$el).find('div:last').hide().fadeIn(1700); //Fade In effect for last message.
}
});
//Event for initialize after event for adding collection and adding views in the regions.
ChatManager.on("initialize:after",function(){
//Clear Localstorage
//localStorage.removeItem("ChatManager-Message");
//localStorage.clear();
//Default message to be displayed in the chat.
var message=new ChatManager.Message({
user:"Admin",
message:"Hello, world!",
datetime:showdate()
});
messages.add(message);
//Crated a collection object, This will store the collection from the local Storage.
var c=new Backbone.Collection();
//Created a new local variable ls which will store the local storage data collection.
var ls = new Backbone.LocalStorage("ChatManager-Message");
c=ls.findAll();
//Loop to add all the local storage objects in the messages collection.
for(i=0;i<c.length;i++)
{
var msg=new ChatManager.Message({
user:c[i].user,
message:c[i].message,
datetime:c[i].datetime
});
messages.add(msg);
}
//Assign the collection messages to View.
var messageListView=new ChatManager.MessagesView({
collection:messages
});
//Created View for action area.
var actionView=new ChatManager.ActionView();
ChatManager.messageRegion.show(messageListView);
ChatManager.actionRegion.show(actionView);
//To maintain scroll position of chat list.
var div = $('div');
$('#message-list').scrollTop(div.get(0).scrollHeight);
});
//Created addMessage to add message to collection object and display the collection in the region.
ChatManager.addMessage=function(message)
{
messages.add(message);//Add message object to the collection messages.
message.save(); //Save to localstorage
cachedMessage=$('#message-input').val();//Store the value of input into the local variable
var messageListView=new ChatManager.MessagesView({
collection:messages,
template:"#message-template-guest"
});
ChatManager.messageRegion.show(messageListView);//Show the messageRegion value.
$('#message-input').val("");//Clear the value of input after the message has been added.
//To maintain scroll position of chat list.
var div = $('div');
$('#message-list').scrollTop(div.get(0).scrollHeight);
}
ChatManager.start();
});
//Created showdate to return the date in the custom format.
function showdate()
{
var d = new Date();
minutes = d.getMinutes().toString().length == 1 ? '0'+d.getMinutes() : d.getMinutes();
seconds = d.getSeconds().toString().length == 1 ? '0'+d.getSeconds() : d.getSeconds();
hours = d.getHours().toString().length == 1 ? '0'+d.getHours() : d.getHours();
ampm = d.getHours() >= 12 ? 'pm' : 'am';
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
days = ['Sun','Mon','Tue','Wed','Thu','Fri','Sat'];
return days[d.getDay()]+' '+months[d.getMonth()]+' '+d.getDate()+' '+d.getFullYear()+' '+hours+':'+minutes+':'+seconds+' '+ampm;
}
</script>
<script type="text/template" id="message-template-admin">
<div class="row-fluid">
<span class="span12">
<div class="well well-1 well-2 well-4 well-5">
<div class="row-fluid">
<span class="span12">
<div class="well pull-left well-2 well-3 well-4 well-6">
<p><%= user %>:</p> <%= message %>
</div>
</span>
</div>
<div class="row-fluid">
<span class="span12">
<div class="well pull-left well-1 well-2 well-4 well-5">
<p>Sent at <%= datetime %></p>
</div>
</span>
</div>
</div>
</span>
</div>
</script>
<script type="text/template" id="message-template-guest">
<div class="row-fluid">
<span class="span12">
<div class="well well-1 well-2 well-4 well-5">
<div class="row-fluid">
<span class="span12">
<div class="well pull-right well-2 well-3 well-4 well-7">
<p><%= user %>:</p> <%= message %>
</div>
</span>
</div>
<div class="row-fluid">
<span class="span12">
<div class="well pull-right well-1 well-2 well-4 well-5">
<p>Sent at <%= datetime %></p>
</div>
</span>
</div>
</div>
</span>
</div>
</div>
</script>
</body>
</html>