]> git.mxchange.org Git - friendica-addons.git/blob - jappixmini/jappix/js/chatstate.js
Merge pull request #439 from zeroadam/Issue3873
[friendica-addons.git] / jappixmini / jappix / js / chatstate.js
1 /*
2
3 Jappix - An open social platform
4 These are the chatstate JS script for Jappix
5
6 -------------------------------------------------
7
8 License: AGPL
9 Author: Vanaryon
10 Last revision: 25/08/11
11
12 */
13
14 // Sends a given chatstate to a given entity
15 function chatStateSend(state, xid, hash) {
16         var user_type = $('#' + hash).attr('data-type');
17         
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'))) {
20                 // Already sent?
21                 if(getDB('currentchatstate', xid) == state)
22                         return;
23                 
24                 // Write the state
25                 setDB('currentchatstate', xid, state);
26                 
27                 // New message stanza
28                 var aMsg = new JSJaCMessage();
29                 aMsg.setTo(xid);
30                 aMsg.setType(user_type);
31                 
32                 // Append the chatstate node
33                 aMsg.appendNode(state, {'xmlns': NS_CHATSTATES});
34                 
35                 // Send this!
36                 con.send(aMsg);
37         }
38 }
39
40 // Displays a given chatstate in a given chat
41 function displayChatState(state, hash, type) {
42         // Groupchat?
43         if(type == 'groupchat') {
44                 resetChatState(hash, type);
45                 
46                 // "gone" state not allowed
47                 if(state != 'gone')
48                         $('#page-engine .page-engine-chan .user.' + hash).addClass(state);
49         }
50         
51         // Chat
52         else {
53                 // We change the buddy name color in the page-switch
54                 resetChatState(hash, type);
55                 $('#page-switch .' + hash + ' .name').addClass(state);
56                 
57                 // We generate the chatstate text
58                 var text = '';
59                 
60                 switch(state) {
61                         // Active
62                         case 'active':
63                                 text = _e("Your friend is paying attention to the conversation.");
64                                 
65                                 break;
66                         
67                         // Composing
68                         case 'composing':
69                                 text = _e("Your friend is writing a message...");
70                                 
71                                 break;
72                         
73                         // Paused
74                         case 'paused':
75                                 text = _e("Your friend stopped writing a message.");
76                                 
77                                 break;
78                         
79                         // Inactive
80                         case 'inactive':
81                                 text = _e("Your friend is doing something else.");
82                                 
83                                 break;
84                         
85                         // Gone
86                         case 'gone':
87                                 text = _e("Your friend closed the chat.");
88                                 
89                                 break;
90                 }
91                 
92                 // We reset the previous state
93                 $('#' + hash + ' .chatstate').remove();
94                 
95                 // We create the chatstate
96                 $('#' + hash + ' .content').after('<div class="' + state + ' chatstate">' + text + '</div>');
97         }
98 }
99
100 // Resets the chatstate switcher marker
101 function resetChatState(hash, type) {
102         // Define the selector
103         var selector;
104         
105         if(type == 'groupchat')
106                 selector = $('#page-engine .page-engine-chan .user.' + hash);
107         else
108                 selector = $('#page-switch .' + hash + ' .name');
109         
110         // Reset!
111         selector.removeClass('active')
112         selector.removeClass('composing')
113         selector.removeClass('paused')
114         selector.removeClass('inactive')
115         selector.removeClass('gone');
116 }
117
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');
126                                 
127                                 // We send the friend a "composing" chatstate
128                                 chatStateSend('composing', xid, hash);
129                         }
130                         
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');
135                                 
136                                 // We send the friend an "active" chatstate
137                                 chatStateSend('active', xid, hash);
138                         }
139                 }
140         });
141         
142         target.change(function() {
143                 // Reset the composing database entry
144                 setDB('chatstate', xid, 'off');
145         });
146         
147         target.focus(function() {
148                 // Not needed
149                 if(target.is(':disabled'))
150                         return;
151                 
152                 // Nothing in the input, user is active
153                 if(!$(this).val())
154                         chatStateSend('active', xid, hash);
155                 
156                 // Something was written, user started writing again
157                 else
158                         chatStateSend('composing', xid, hash);
159         });
160         
161         target.blur(function() {
162                 // Not needed
163                 if(target.is(':disabled'))
164                         return;
165                 
166                 // Nothing in the input, user is inactive
167                 if(!$(this).val())
168                         chatStateSend('inactive', xid, hash);
169                 
170                 // Something was written, user paused
171                 else
172                         chatStateSend('paused', xid, hash);
173         });
174 }