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
|
/*
Jappix - An open social platform
These are the chatstate JS script for Jappix
-------------------------------------------------
License: AGPL
Author: Valérian Saliou
*/
// Bundle
var ChatState = (function () {
/**
* Alias of this
* @private
*/
var self = {};
/**
* Sends a given chatstate to a given entity
* @public
* @param {string} state
* @param {string} xid
* @param {string} hash
* @return {undefined}
*/
self.send = function(state, xid, hash) {
try {
var user_type = $('#' + hash).attr('data-type');
// If the friend client supports chatstates and is online
if((user_type == 'groupchat') || ((user_type == 'chat') && $('#' + hash + ' .message-area').attr('data-chatstates') && !Common.exists('#page-switch .' + hash + ' .unavailable'))) {
// Already sent?
if(DataStore.getDB(Connection.desktop_hash, 'currentchatstate', xid) == state) {
return;
}
// Write the state
DataStore.setDB(Connection.desktop_hash, 'currentchatstate', xid, state);
// New message stanza
var aMsg = new JSJaCMessage();
aMsg.setTo(xid);
aMsg.setType(user_type);
// Append the chatstate node
aMsg.appendNode(state, {
'xmlns': NS_CHATSTATES
});
// Send this!
con.send(aMsg);
}
} catch(e) {
Console.error('ChatState.send', e);
}
};
/**
* Displays a given chatstate in a given chat
* @public
* @param {string} state
* @param {string} hash
* @param {string} type
* @return {undefined}
*/
self.display = function(state, hash, type) {
try {
// Groupchat?
if(type == 'groupchat') {
self.reset(hash, type);
// "gone" state not allowed
if(state != 'gone') {
$('#page-engine .page-engine-chan .user.' + hash).addClass(state);
}
}
// Chat
else {
// We change the buddy name color in the page-switch
self.reset(hash, type);
$('#page-switch .' + hash + ' .name').addClass(state);
// We generate the chatstate text
var text = '';
switch(state) {
// Active
case 'active':
text = Common._e("Your friend is paying attention to the conversation.");
break;
// Composing
case 'composing':
text = Common._e("Your friend is writing a message...");
break;
// Paused
case 'paused':
text = Common._e("Your friend stopped writing a message.");
break;
// Inactive
case 'inactive':
text = Common._e("Your friend is doing something else.");
break;
// Gone
case 'gone':
text = Common._e("Your friend closed the chat.");
break;
}
// We reset the previous state
$('#' + hash + ' .chatstate').remove();
// We create the chatstate
$('#' + hash + ' .content').after(
'<div class="' + state + ' chatstate">' + text + '</div>'
);
}
} catch(e) {
Console.error('ChatState.display', e);
}
};
/**
* Resets the chatstate switcher marker
* @public
* @param {string} hash
* @param {string} type
* @return {undefined}
*/
self.reset = function(hash, type) {
try {
// Define the selector
var selector;
if(type == 'groupchat') {
selector = $('#page-engine .page-engine-chan .user.' + hash);
} else {
selector = $('#page-switch .' + hash + ' .name');
}
// Reset!
selector.removeClass('active composing paused inactive gone');
} catch(e) {
Console.error('ChatState.reset', e);
}
};
/**
* Adds the chatstate events
* @public
* @param {object} target
* @param {string} xid
* @param {string} hash
* @param {string} type
* @return {undefined}
*/
self.events = function(target, xid, hash, type) {
try {
target.keyup(function(e) {
if(e.keyCode != 13) {
// Composing a message
if($(this).val() && (DataStore.getDB(Connection.desktop_hash, 'chatstate', xid) != 'on')) {
// We change the state detect input
DataStore.setDB(Connection.desktop_hash, 'chatstate', xid, 'on');
// We send the friend a "composing" chatstate
self.send('composing', xid, hash);
}
// Flushed the message which was being composed
else if(!$(this).val() && (DataStore.getDB(Connection.desktop_hash, 'chatstate', xid) == 'on')) {
// We change the state detect input
DataStore.setDB(Connection.desktop_hash, 'chatstate', xid, 'off');
// We send the friend an "active" chatstate
self.send('active', xid, hash);
}
}
});
target.change(function() {
// Reset the composing database entry
DataStore.setDB(Connection.desktop_hash, 'chatstate', xid, 'off');
});
target.focus(function() {
// Not needed
if(target.is(':disabled')) {
return;
}
// Something was written, user started writing again
if($(this).val()) {
self.send('composing', xid, hash);
}
// Chat only: Nothing in the input, user is active
else if(type == 'chat') {
self.send('active', xid, hash);
}
});
target.blur(function() {
// Not needed
if(target.is(':disabled')) {
return;
}
// Something was written, user paused
if($(this).val()) {
self.send('paused', xid, hash);
} else if(type == 'chat') {
self.send('inactive', xid, hash);
}
});
} catch(e) {
Console.error('ChatState.events', e);
}
};
/**
* Return class scope
*/
return self;
})();
|