-
Notifications
You must be signed in to change notification settings - Fork 0
/
candy.js
183 lines (163 loc) · 6.53 KB
/
candy.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
/** File: candy.js
* Candy - Chats are not dead yet.
*
* Authors:
* - Antoni Bertran <[email protected]>
* Based on chatstates
* https://github.com/strophe/strophejs-plugins/blob/master/chatstates/strophe.chatstates.js
*
* Copyright:
* (c) 2014 3&Punt Solucions Informàtiques Sl. All rights reserved.
*/
var CandyShop = (function(self) {
return self;
}(CandyShop || {}));
/** Class: CandyShop.UserTyping
* Send the notification when the user is typing
*/
CandyShop.UserTyping = (function(self, Candy, $) {
/** Object: _options
* Options:
*/
var _options = {
};
var _connection = null;
/**
*
* @type Boolean
*/
var _is_composing = false;
/** Function: init
* Initialize the ChatRecall plugin
*
* Parameters:
* (Object) options - An options packet to apply to this plugin
*/
self.init = function(options) {
// apply the supplied options to the defaults specified
$.extend(true, _options, options);
self.applyTranslations();
$(Candy).on('candy:view.message.after-show', CandyShop.UserTyping.onKeyUp);
Strophe.addConnectionPlugin('chatstates',
{
init: function(connection)
{
this._connection = connection;
Strophe.addNamespace('CHATSTATES', 'http://jabber.org/protocol/chatstates');
},
statusChanged: function(status)
{
if (status === Strophe.Status.CONNECTED
|| status === Strophe.Status.ATTACHED)
{
this._connection.addHandler(this._notificationReceived.bind(this),
Strophe.NS.CHATSTATES, "message");
}
},
addActive: function(message)
{
return message.c('active', {xmlns: Strophe.NS.CHATSTATES}).up();
},
_notificationReceived: function(message)
{
var composing = $(message).find('composing'),
paused = $(message).find('paused'),
// active = $(message).find('active'),
// jid = $(message).attr('from')
current_user = $(message).attr('current_user');
if (composing.length > 0)
{
if (Candy.Core.getUser().getNick() !== current_user) {
CandyShop.UserTyping.showMessage(current_user + ' ' + $.i18n._('candyshopUserTypingUserIsTyping') + '...', true);
}
}
if (paused.length > 0)
{
CandyShop.UserTyping.showMessage('', false);
}
/*if (active.length > 0)
{
// $(document).trigger('active.chatstates', jid);
}*/
return true;
},
/*sendActive: function(jid, type)
{
this._sendNotification(jid, type, 'active');
},*/
sendComposing: function(jid, type)
{
this._sendNotification(jid, type, 'composing');
},
sendPaused: function(jid, type)
{
this._sendNotification(jid, type, 'paused');
},
_sendNotification: function(jid, type, notification)
{
if (!type)
type = 'chat';
this._connection.send($msg(
{
to: jid,
type: type,
current_user: Candy.Core.getUser().getNick()
})
.c(notification, {xmlns: Strophe.NS.CHATSTATES}));
}
});
}
/**
* Shows the message of user is typing
* @param {type} message
* @param {type} show_it
* @returns void
*/
self.showMessage = function(message, show_it) {
var $messagePane = $(Candy.View.Pane.Room.getPane(Candy.View.getCurrent().roomJid));
$messagePane.find('.candy-usertyping-message-container').hide();
$messagePane.find('.candy-usertyping-message-container').remove();
if (show_it) {
var html = '<li class="candy-usertyping-message-container"><div class="infomessage">' + message + '</div></li>';
Candy.View.Pane.Room.appendToMessagePane(Candy.View.getCurrent().roomJid, html);
Candy.View.Pane.Room.scrollToBottom(Candy.View.getCurrent().roomJid);
}
}
/**
* Triggers the event on key up
* @returns void
*/
self.onKeyUp = function() {
$(".field[name='message']").keyup(function(e) {
if (e.which === 13 || $(".field[name='message']").val().length===0) {
if (_is_composing) {
_is_composing = false;
Candy.Core.getConnection().chatstates.sendPaused(Candy.View.getCurrent().roomJid, 'groupchat');
}
} else {
if (!_is_composing) {
_is_composing = true;
Candy.Core.getConnection().chatstates.sendComposing(Candy.View.getCurrent().roomJid, 'groupchat');
setTimeout(CandyShop.UserTyping.disableComposing, 5000);
}
}
});
}
/**
* Disable the composing message after 5 seconds
* @returns {undefined}
*/
self.disableComposing = function() {
_is_composing = false;
CandyShop.UserTyping.showMessage('', false);
}
/** Function: applyTranslations
* Apply translations to this plugin
*/
self.applyTranslations = function() {
Candy.View.Translation.en.candyshopUserTypingUserIsTyping = 'is typing';
Candy.View.Translation.es.candyshopUserTypingUserIsTyping = 'está escribiendo';
Candy.View.Translation.ca.candyshopUserTypingUserIsTyping = 'està escribint';
};
return self;
}(CandyShop.UserTyping || {}, Candy, jQuery));