Skip to content

Commit 2f41723

Browse files
author
Oleksandr Shvetsov
committed
Updated chat sample
1 parent 23edee3 commit 2f41723

File tree

14 files changed

+655
-357
lines changed

14 files changed

+655
-357
lines changed

samples/chat/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Overview
2+
QuickBlox JS Sample Chat
3+
4+
This is a code sample for [QuickBlox](http://quickblox.com/) platform. It is a great way for developers using QuickBlox platform to learn how to integrate private and group chat, add text and image attachments sending into your application.
5+
6+
# Credentials
7+
8+
Welcome to QuickBlox [Credentials](https://quickblox.com/developers/5_Minute_Guide), where you can get your credentials in just 5 minutes! All you need is to:
9+
10+
1. Register a free QuickBlox account and add your App there.
11+
2. Update credentials in your application code.[Chat](https://quickblox.com/developers/5_Minute_Guide#Update_authentication_credentials_3)
12+
13+
# Chat Sample
14+
15+
This Sample demonstrates how to work with [Chat](https://quickblox.com/developers/Web_XMPP_Chat_Sample#Guide:_Getting_Started_with_Chat_API) QuickBlox module.
16+
17+
It allows:
18+
19+
1. Authenticate with Quickblox Chat and REST.
20+
2. Receive and display list of dialogs.
21+
3. Modify dialog by adding occupants.
22+
4. Real-time chat messaging and attachment's handling.
23+
24+
# Documentation
25+
26+
Original sample description & setup guide - [JS Chat Sample](https://quickblox.com/developers/Web_XMPP_Chat_Sample#Guide:_Getting_Started_with_Chat_API)

samples/chat/css/dashboard.css

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,20 @@
593593
padding: 7px 24px;
594594
}
595595

596+
.group_chat__filter {
597+
border-bottom: 1px solid #DADFE1;
598+
display: flex;
599+
padding: 13px 8px;
600+
}
601+
.group_chat__filter > input {
602+
flex: 1 1 100%;
603+
font-size: 16px;
604+
line-height: 19px;
605+
border: none;
606+
margin: 0 10px 0 0;
607+
padding: 0 10px;
608+
}
609+
596610
.user__item {
597611
padding: 8px 24px;
598612
display: flex;

samples/chat/css/dialogs.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,3 +250,8 @@
250250
.dialog_unread_counter.hidden {
251251
display: none;
252252
}
253+
254+
.center {
255+
width: 100%;
256+
text-align: center;
257+
}

samples/chat/css/login.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,14 @@
8787
color: #17D04B;
8888
}
8989

90+
.login_form__row.error input {
91+
border-bottom: 2px solid red !important;
92+
}
93+
94+
.login_form__row.error label {
95+
color: red !important;
96+
}
97+
9098
.m-login__button {
9199
width: 100%;
92100
}

samples/chat/index.html

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
1212
<link rel="stylesheet" href="./css/style.css">
1313

14-
<script src="https://unpkg.com/navigo@4.3.6/lib/navigo.min.js" defer></script>
14+
<script src="https://unpkg.com/navigo@7.1.2/lib/navigo.min.js" defer></script>
1515
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore.js" defer></script>
1616
<script src="../../quickblox.min.js" defer></script>
1717
</head>
@@ -28,20 +28,26 @@
2828
<img src="./img/qb-logo.svg" alt="QuickBlox">]
2929
</a>
3030
<h1>Quickblox Chat Sample</h1>
31-
<h3>Please enter your username and user group.
32-
Users within the same group will be able
33-
to communicate, create chats with each other
34-
</h3>
31+
<h3>Please enter your Login and User name</h3>
3532
</div>
3633

3734
<form name="loginForm" class="login__form">
3835
<div class="login_form__row">
39-
<label for="userName">User name</label>
40-
<input type="text" id="userName" name="userName"/>
36+
<label for="userLogin">Login</label>
37+
<input
38+
type="text"
39+
id="userLogin"
40+
name="userLogin"
41+
minlength="3"
42+
maxlength="15"
43+
pattern='^[a-zA-Z]{1}[a-zA-Z0-9]+$'
44+
title="Field should contain alphanumeric characters only in a range 3-15. The first character must be a letter."
45+
required
46+
/>
4147
</div>
4248
<div class="login_form__row">
43-
<label for="userGroup">User group</label>
44-
<input type="text" id="userGroup" name="userGroup"/>
49+
<label for="userName">User name</label>
50+
<input type="text" id="userName" name="userName" minlength="3" maxlength="15" required />
4551
</div>
4652
<div class="login__button_wrap">
4753
<button type="submit" name="login_submit" class="btn m-login__button j-login__button" disabled>
@@ -72,7 +78,6 @@ <h3>Please enter your username and user group.
7278
<img src="./img/qb-logo.svg" alt="QuickBlox">
7379
</a>
7480
<div class="dashboard__status_wrap">
75-
<h2 class="dashboard__title"><%- user.user_tags %></h2>
7681
<p class="dashboard__status j-dashboard_status">
7782
Logged in as <%- user.name %>
7883
</p>
@@ -186,6 +191,9 @@ <h1 class="dialog__title j-dialog__title"><%- title %></h1>
186191
</div>
187192
<div class="notifications j-notifications hidden"></div>
188193
<div class="content__inner j-content__inner">
194+
<div class="group_chat__filter">
195+
<input type="text" autocomplete="off" autocorrect="off" placeholder="Filter by User name" />
196+
</div>
189197
<p class="update__chat__description"><span class="update__chat_counter j-update__chat_counter">0</span>&nbsp;user selected:</p>
190198
<div class="update_chat__user_list j-update_chat__user_list">
191199
</div>
@@ -210,6 +218,14 @@ <h1 class="dialog__title j-dialog__title"><%- title %></h1>
210218
</div>
211219
</script>
212220

221+
<script type="text/template" id="tpl_date_message">
222+
<div class="message__wrap" id="<%=month+'-'+date.getDate()%>" >
223+
<div class="center">
224+
<%= month+ '/' + date.getDate() %>
225+
</div>
226+
</div>
227+
</script>
228+
213229
<script type="text/template" id="tpl_message">
214230
<div class="message__wrap" id="<%= message.id %>" data-status="<%= message.status %>">
215231
<span class="message__avatar m-user__img_<%= sender ? sender.color : 'not_loaded' %>">
@@ -244,6 +260,9 @@ <h1 class="dialog__title j-dialog__title"><%- title %></h1>
244260
<script type="text/template" id="tpl_notificationMessage">
245261
<div class="message__wrap" id="<%= id %>">
246262
<p class="m-notification_message"><%= text %></p>
263+
<div class="message__timestamp">
264+
<%= date_sent %>
265+
</div>
247266
</div>
248267
</script>
249268

@@ -256,6 +275,9 @@ <h1 class="group_chat__title">New Group Chat</h1>
256275
</div>
257276
<div class="notifications j-notifications hidden"></div>
258277
<div class="content__inner j-content__inner">
278+
<div class="group_chat__filter">
279+
<input type="text" autocomplete="off" autocorrect="off" placeholder="Filter by User name" />
280+
</div>
259281
<p class="group__chat__description">Select participants:</p>
260282
<div class="group_chat__user_list j-group_chat__user_list">
261283
</div>

samples/chat/js/QBconfig.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
var QBconfig = {
22
credentials: {
3-
appId: 72448,
4-
authKey: 'f4HYBYdeqTZ7KNb',
5-
authSecret: 'ZC7dK39bOjVc-Z8'
3+
appId: '',
4+
authKey: '',
5+
authSecret: ''
66
},
77
appConfig: {
88
chatProtocol: {
@@ -39,6 +39,7 @@ var CONSTANTS = {
3939
},
4040
NOTIFICATION_TYPES: {
4141
NEW_DIALOG: '1',
42-
UPDATE_DIALOG: '2'
42+
UPDATE_DIALOG: '2',
43+
LEAVE_DIALOG: '3'
4344
}
4445
};

samples/chat/js/app.js

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,22 +57,17 @@ App.prototype.renderDashboard = function (activeTabName) {
5757
listeners.setListeners();
5858

5959
logoutBtn.addEventListener('click', function () {
60-
QB.users.delete(app.user.id, function(err, user){
61-
if (err) {
62-
console.error('Can\'t delete user by id: '+app.user.id+' ', err);
63-
}
6460

65-
loginModule.isLogin = false;
66-
app.isDashboardLoaded = false;
61+
loginModule.isLogin = false;
62+
app.isDashboardLoaded = false;
6763

68-
localStorage.removeItem('user');
69-
helpers.clearCache();
64+
localStorage.removeItem('user');
65+
helpers.clearCache();
7066

71-
QB.chat.disconnect();
72-
QB.destroySession();
73-
74-
router.navigate('#!/login');
75-
});
67+
QB.chat.disconnect();
68+
QB.destroySession(() => null);
69+
70+
router.navigate('#!/login');
7671
});
7772

7873
this.tabSelectInit();
@@ -90,7 +85,7 @@ App.prototype.loadWelcomeTpl = function () {
9085
App.prototype.tabSelectInit = function () {
9186
var self = this,
9287
tabs = document.querySelectorAll('.j-sidebar__tab_link');
93-
88+
9489
_.each(tabs, function (item) {
9590
item.addEventListener('click', function (e) {
9691
e.preventDefault();
@@ -99,7 +94,16 @@ App.prototype.tabSelectInit = function () {
9994
}
10095

10196
var tab = e.currentTarget;
102-
self.loadChatList(tab);
97+
self.loadChatList(tab).then(function(){
98+
var activeTab = document.querySelector('.j-sidebar__tab_link.active'),
99+
tabType = activeTab.dataset.type,
100+
dialogType = dialogModule._cache[dialogModule.dialogId].type === 1 ? 'public' : 'chat',
101+
isActiveTab = tabType === dialogType;
102+
103+
if(isActiveTab && dialogModule._cache[dialogModule.dialogId].type === CONSTANTS.DIALOG_TYPES.CHAT ) {
104+
dialogModule.renderMessages(dialogModule.dialogId);
105+
}
106+
});
103107
});
104108
});
105109
};
@@ -153,17 +157,10 @@ App.prototype.buildCreateDialogTpl = function () {
153157
if (document.forms.create_dialog.create_dialog_submit.disabled) return false;
154158

155159
document.forms.create_dialog.create_dialog_submit.disabled = true;
156-
157-
var users = self.userListConteiner.querySelectorAll('.selected'),
158-
type = users.length > 2 ? 2 : 3,
159-
name = document.forms.create_dialog.dialog_name.value,
160-
occupants_ids = [];
161-
162-
_.each(users, function (user) {
163-
if (+user.id !== self.user.id) {
164-
occupants_ids.push(user.id);
165-
}
166-
});
160+
161+
var occupants_ids = userModule.selectedUserIds,
162+
type = occupants_ids.length > 1 ? 2 : 3,
163+
name = document.forms.create_dialog.dialog_name.value;
167164

168165
if (!name && type === 2) {
169166
var userNames = [];
@@ -197,6 +194,7 @@ App.prototype.buildCreateDialogTpl = function () {
197194
document.forms.create_dialog.dialog_name.value = titleText.slice(0, 40);
198195
}
199196
});
197+
200198
userModule.initGettingUsers();
201199
};
202200

0 commit comments

Comments
 (0)