3 Jappix - An open social platform
4 These are the chatstate JS script for Jappix
6 -------------------------------------------------
10 Last revision: 25/08/11
14 // Sends a given chatstate to a given entity
15 function chatStateSend(state, xid, hash) {
16 var user_type = $('#' + hash).attr('data-type');
18 // If the friend client supports chatstates and is online
19 if((user_type == 'groupchat') || ((user_type == 'chat') && $('#' + hash + ' .message-area').attr('data-chatstates') && !exists('#page-switch .' + hash + ' .unavailable'))) {
21 if(getDB('currentchatstate', xid) == state)
25 setDB('currentchatstate', xid, state);
28 var aMsg = new JSJaCMessage();
30 aMsg.setType(user_type);
32 // Append the chatstate node
33 aMsg.appendNode(state, {'xmlns': NS_CHATSTATES});
40 // Displays a given chatstate in a given chat
41 function displayChatState(state, hash, type) {
43 if(type == 'groupchat') {
44 resetChatState(hash, type);
46 // "gone" state not allowed
48 $('#page-engine .page-engine-chan .user.' + hash).addClass(state);
53 // We change the buddy name color in the page-switch
54 resetChatState(hash, type);
55 $('#page-switch .' + hash + ' .name').addClass(state);
57 // We generate the chatstate text
63 text = _e("Your friend is paying attention to the conversation.");
69 text = _e("Your friend is writing a message...");
75 text = _e("Your friend stopped writing a message.");
81 text = _e("Your friend is doing something else.");
87 text = _e("Your friend closed the chat.");
92 // We reset the previous state
93 $('#' + hash + ' .chatstate').remove();
95 // We create the chatstate
96 $('#' + hash + ' .content').after('<div class="' + state + ' chatstate">' + text + '</div>');
100 // Resets the chatstate switcher marker
101 function resetChatState(hash, type) {
102 // Define the selector
105 if(type == 'groupchat')
106 selector = $('#page-engine .page-engine-chan .user.' + hash);
108 selector = $('#page-switch .' + hash + ' .name');
111 selector.removeClass('active')
112 selector.removeClass('composing')
113 selector.removeClass('paused')
114 selector.removeClass('inactive')
115 selector.removeClass('gone');
118 // Adds the chatstate events
119 function eventsChatState(target, xid, hash) {
120 target.keyup(function(e) {
121 if(e.keyCode != 13) {
122 // Composing a message
123 if($(this).val() && (getDB('chatstate', xid) != 'on')) {
124 // We change the state detect input
125 setDB('chatstate', xid, 'on');
127 // We send the friend a "composing" chatstate
128 chatStateSend('composing', xid, hash);
131 // Stopped composing a message
132 else if(!$(this).val() && (getDB('chatstate', xid) == 'on')) {
133 // We change the state detect input
134 setDB('chatstate', xid, 'off');
136 // We send the friend an "active" chatstate
137 chatStateSend('active', xid, hash);
142 target.change(function() {
143 // Reset the composing database entry
144 setDB('chatstate', xid, 'off');
147 target.focus(function() {
149 if(target.is(':disabled'))
152 // Nothing in the input, user is active
154 chatStateSend('active', xid, hash);
156 // Something was written, user started writing again
158 chatStateSend('composing', xid, hash);
161 target.blur(function() {
163 if(target.is(':disabled'))
166 // Nothing in the input, user is inactive
168 chatStateSend('inactive', xid, hash);
170 // Something was written, user paused
172 chatStateSend('paused', xid, hash);