/
mini.js
2343 lines (1863 loc) · 61.3 KB
/
mini.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
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Jappix - An open social platform
These are the Jappix Mini JS scripts for Jappix
-------------------------------------------------
License: dual-licensed under AGPL and MPLv2
Authors: Vanaryon, hunterjm, Camaran, regilero, Kloadut
Last revision: 18/06/12
*/
// Jappix Mini vars
var MINI_DISCONNECT = false;
var MINI_AUTOCONNECT = false;
var MINI_SHOWPANE = false;
var MINI_INITIALIZED = false;
var MINI_ANONYMOUS = false;
var MINI_ANIMATE = false;
var MINI_RANDNICK = false;
var MINI_NICKNAME = null;
var MINI_TITLE = null;
var MINI_DOMAIN = null;
var MINI_USER = null;
var MINI_PASSWORD = null;
var MINI_RECONNECT = 0;
var MINI_CHATS = [];
var MINI_GROUPCHATS = [];
var MINI_PASSWORDS = [];
var MINI_RESOURCE = JAPPIX_RESOURCE + ' Mini';
var MINI_ERROR_LINK = 'https://mini.jappix.com/issues';
// Setups connection handlers
function setupConMini(con) {
con.registerHandler('message', handleMessageMini);
con.registerHandler('presence', handlePresenceMini);
con.registerHandler('iq', handleIQMini);
con.registerHandler('onerror', handleErrorMini);
con.registerHandler('onconnect', connectedMini);
}
// Connects the user with the given logins
function connectMini(domain, user, password) {
try {
// We define the http binding parameters
oArgs = new Object();
if(HOST_BOSH_MINI)
oArgs.httpbase = HOST_BOSH_MINI;
else
oArgs.httpbase = HOST_BOSH;
// We create the new http-binding connection
con = new JSJaCHttpBindingConnection(oArgs);
// And we handle everything that happen
setupConMini(con);
// Generate a resource
var random_resource = getDB('jappix-mini', 'resource');
if(!random_resource)
random_resource = MINI_RESOURCE + ' (' + (new Date()).getTime() + ')';
// We retrieve what the user typed in the login inputs
oArgs = new Object();
oArgs.secure = true;
oArgs.xmllang = XML_LANG;
oArgs.resource = random_resource;
oArgs.domain = domain;
// Store the resource (for reconnection)
setDB('jappix-mini', 'resource', random_resource);
// Anonymous login?
if(MINI_ANONYMOUS) {
// Anonymous mode disabled?
if(!allowedAnonymous()) {
logThis('Not allowed to use anonymous mode.', 2);
// Notify this error
notifyErrorMini();
return false;
}
// Bad domain?
else if(lockHost() && (domain != HOST_ANONYMOUS)) {
logThis('Not allowed to connect to this anonymous domain: ' + domain, 2);
// Notify this error
notifyErrorMini();
return false;
}
oArgs.authtype = 'saslanon';
}
// Normal login
else {
// Bad domain?
if(lockHost() && (domain != HOST_MAIN)) {
logThis('Not allowed to connect to this main domain: ' + domain, 2);
// Notify this error
notifyErrorMini();
return false;
}
// No nickname?
if(!MINI_NICKNAME)
MINI_NICKNAME = user;
oArgs.username = user;
oArgs.pass = password;
}
// We connect !
con.connect(oArgs);
logThis('Jappix Mini is connecting...', 3);
}
catch(e) {
// Logs errors
logThis('Error while logging in: ' + e, 1);
// Reset Jappix Mini
disconnectedMini();
}
finally {
return false;
}
}
// When the user is connected
function connectedMini() {
// Update the roster
jQuery('#jappix_mini a.jm_pane.jm_button span.jm_counter').text('0');
// Do not get the roster if anonymous
if(MINI_ANONYMOUS)
initializeMini();
else
getRosterMini();
// For logger
if(MINI_RECONNECT)
logThis('Jappix Mini is now reconnected.', 3);
else
logThis('Jappix Mini is now connected.', 3);
// Reset reconnect var
MINI_RECONNECT = 0;
}
// When the user disconnects
function saveSessionMini() {
// Not connected?
if(!isConnected())
return;
// Save the actual Jappix Mini DOM
setDB('jappix-mini', 'dom', jQuery('#jappix_mini').html());
setDB('jappix-mini', 'nickname', MINI_NICKNAME);
// Save the scrollbar position
var scroll_position = '';
var scroll_hash = jQuery('#jappix_mini div.jm_conversation:has(a.jm_pane.jm_clicked)').attr('data-hash');
if(scroll_hash)
scroll_position = document.getElementById('received-' + scroll_hash).scrollTop + '';
setDB('jappix-mini', 'scroll', scroll_position);
// Save the session stamp
setDB('jappix-mini', 'stamp', getTimeStamp());
// Suspend connection
con.suspend(false);
logThis('Jappix Mini session save tool launched.', 3);
}
// Disconnects the connected user
function disconnectMini() {
// No connection?
if(!isConnected())
return false;
logThis('Jappix Mini is disconnecting...', 3);
// Change markers
MINI_DISCONNECT = true;
MINI_INITIALIZED = false;
// Add disconnection handler
con.registerHandler('ondisconnect', disconnectedMini);
// Disconnect the user
con.disconnect();
return false;
}
// When the user is disconnected
function disconnectedMini() {
// Remove the stored items
removeDB('jappix-mini', 'dom');
removeDB('jappix-mini', 'nickname');
removeDB('jappix-mini', 'scroll');
removeDB('jappix-mini', 'stamp');
// Connection error?
if(!MINI_DISCONNECT || MINI_INITIALIZED) {
// Browser error?
notifyErrorMini();
// Reset reconnect timer
jQuery('#jappix_mini').stopTime();
// Try to reconnect after a while
if(MINI_INITIALIZED && (MINI_RECONNECT < 5)) {
// Reconnect interval
var reconnect_interval = 10;
if(MINI_RECONNECT)
reconnect_interval = (5 + (5 * MINI_RECONNECT)) * 1000;
MINI_RECONNECT++;
// Set timer
jQuery('#jappix_mini').oneTime(reconnect_interval, function() {
launchMini(true, MINI_SHOWPANE, MINI_DOMAIN, MINI_USER, MINI_PASSWORD);
});
}
}
// Normal disconnection?
else
launchMini(false, MINI_SHOWPANE, MINI_DOMAIN, MINI_USER, MINI_PASSWORD);
// Reset markers
MINI_DISCONNECT = false;
MINI_INITIALIZED = false;
logThis('Jappix Mini is now disconnected.', 3);
}
// Handles the incoming messages
function handleMessageMini(msg) {
var type = msg.getType();
// This is a message Jappix can handle
if((type == 'chat') || (type == 'normal') || (type == 'groupchat') || !type) {
// Get the body
var body = trim(msg.getBody());
// Any subject?
var subject = trim(msg.getSubject());
if(subject)
body = subject;
if(body) {
// Get the values
var from = fullXID(getStanzaFrom(msg));
var xid = bareXID(from);
var use_xid = xid;
var hash = hex_md5(xid);
var nick = thisResource(from);
// Read the delay
var delay = readMessageDelay(msg.getNode());
var d_stamp;
// Manage this delay
if(delay) {
time = relativeDate(delay);
d_stamp = Date.jab2date(delay);
}
else {
time = getCompleteTime();
d_stamp = new Date();
}
// Get the stamp
var stamp = extractStamp(d_stamp);
// Is this a groupchat private message?
if(exists('#jappix_mini #chat-' + hash + '[data-type=groupchat]')) {
// Regenerate some stuffs
if((type == 'chat') || !type) {
xid = from;
hash = hex_md5(xid);
}
// XID to use for a groupchat
else
use_xid = from;
}
// Message type
var message_type = 'user-message';
// Grouphat values
if(type == 'groupchat') {
// Old message
if(msg.getChild('delay', NS_URN_DELAY) || msg.getChild('x', NS_DELAY))
message_type = 'old-message';
// System message?
if(!nick || subject) {
nick = '';
message_type = 'system-message';
}
}
// Chat values
else {
nick = jQuery('#jappix_mini a#friend-' + hash).text().revertHtmlEnc();
// No nickname?
if(!nick) {
// if the roster does not give us any nick the user may have send us a nickname to use with his first message
// @see http://xmpp.org/extensions/xep-0172.html
// we first check we do not have made this stuff before
var unknown_entry = jQuery("a.jm_unknown[data-xid="+xid+"]",jQuery("#jappix_mini"));
if (unknown_entry.length > 0) {
nick = unknown_entry.attr('data-nick');
} else {
msgnick = msg.getNick();
nick = getXIDNick(xid);
if (msgnick) {
if (nick != msgnick) {
// if there is a nickname in the message which differs from the jid-extracted nick then tell it to the user
nick = msgnick + ' (' + nick + ')';
}
}
//push that unknown guy in a temporary roster entry
var unknown_entry = jQuery('<a class="jm_unknown jm_offline" href="#"></a>')
.attr('data-nick',nick)
.attr('data-xid',xid);
unknown_entry.appendTo(jQuery(".jm_buddies",jQuery("#jappix_mini")));
}
}
}
// Define the target div
var target = '#jappix_mini #chat-' + hash;
// Create the chat if it does not exist
if(!exists(target) && (type != 'groupchat'))
chatMini(type, xid, nick, hash);
// Display the message
displayMessageMini(type, body, use_xid, nick, hash, time, stamp, message_type);
// Notify the user if not focused & the message is not a groupchat old one
if((!jQuery(target + ' a.jm_chat-tab').hasClass('jm_clicked') || !isFocused()) && (message_type == 'user-message'))
notifyMessageMini(hash);
logThis('Message received from: ' + from);
}
}
}
// Handles the incoming IQs
function handleIQMini(iq) {
// Define some variables
var iqFrom = fullXID(getStanzaFrom(iq));
var iqID = iq.getID();
var iqQueryXMLNS = iq.getQueryXMLNS();
var iqType = iq.getType();
var iqNode = iq.getNode();
// Build the response
var iqResponse = new JSJaCIQ();
iqResponse.setID(iqID);
iqResponse.setTo(iqFrom);
iqResponse.setType('result');
// Software version query
if((iqQueryXMLNS == NS_VERSION) && (iqType == 'get')) {
/* REF: http://xmpp.org/extensions/xep-0092.html */
var iqQuery = iqResponse.setQuery(NS_VERSION);
iqQuery.appendChild(iq.buildNode('name', {'xmlns': NS_VERSION}, 'Jappix Mini'));
iqQuery.appendChild(iq.buildNode('version', {'xmlns': NS_VERSION}, JAPPIX_VERSION));
iqQuery.appendChild(iq.buildNode('os', {'xmlns': NS_VERSION}, navigator.platform));
con.send(iqResponse);
logThis('Received software version query: ' + iqFrom);
}
// Roster push
else if((iqQueryXMLNS == NS_ROSTER) && (iqType == 'set')) {
// Display the friend
handleRosterMini(iq);
con.send(iqResponse);
logThis('Received a roster push.');
}
// Disco info query
else if((iqQueryXMLNS == NS_DISCO_INFO) && (iqType == 'get')) {
/* REF: http://xmpp.org/extensions/xep-0030.html */
var iqQuery = iqResponse.setQuery(NS_DISCO_INFO);
// We set the name of the client
iqQuery.appendChild(iq.appendNode('identity', {
'category': 'client',
'type': 'web',
'name': 'Jappix Mini',
'xmlns': NS_DISCO_INFO
}));
// We set all the supported features
var fArray = new Array(
NS_DISCO_INFO,
NS_VERSION,
NS_ROSTER,
NS_MUC,
NS_VERSION,
NS_URN_TIME
);
for(i in fArray)
iqQuery.appendChild(iq.buildNode('feature', {'var': fArray[i], 'xmlns': NS_DISCO_INFO}));
con.send(iqResponse);
logThis('Received a disco#infos query.');
}
// User time query
else if(jQuery(iqNode).find('time').size() && (iqType == 'get')) {
/* REF: http://xmpp.org/extensions/xep-0202.html */
var iqTime = iqResponse.appendNode('time', {'xmlns': NS_URN_TIME});
iqTime.appendChild(iq.buildNode('tzo', {'xmlns': NS_URN_TIME}, getDateTZO()));
iqTime.appendChild(iq.buildNode('utc', {'xmlns': NS_URN_TIME}, getXMPPTime('utc')));
con.send(iqResponse);
logThis('Received local time query: ' + iqFrom);
}
}
// Handles the incoming errors
function handleErrorMini(err) {
// First level error (connection error)
if(jQuery(err).is('error')) {
// Notify this error
disconnectedMini();
logThis('First level error received.', 1);
}
}
// Handles the incoming presences
function handlePresenceMini(pr) {
// Get the values
var from = fullXID(getStanzaFrom(pr));
var xid = bareXID(from);
var resource = thisResource(from);
var hash = hex_md5(xid);
var type = pr.getType();
var show = pr.getShow();
// Manage the received presence values
if((type == 'error') || (type == 'unavailable'))
show = 'unavailable';
else {
switch(show) {
case 'chat':
case 'away':
case 'xa':
case 'dnd':
break;
default:
show = 'available';
break;
}
}
// Is this a groupchat presence?
var groupchat_path = '#jappix_mini #chat-' + hash + '[data-type=groupchat]';
var is_groupchat = false;
if(exists(groupchat_path)) {
// Groupchat exists
is_groupchat = true;
// Groupchat buddy presence (not me)
if(resource != unescape(jQuery(groupchat_path).attr('data-nick'))) {
// Regenerate some stuffs
var groupchat = xid;
xid = from;
hash = hex_md5(xid);
// Remove this from the roster
if(show == 'unavailable')
removeBuddyMini(hash, groupchat);
// Add this to the roster
else
addBuddyMini(xid, hash, resource, groupchat);
}
}
// Friend path
var chat = '#jappix_mini #chat-' + hash;
var friend = '#jappix_mini a#friend-' + hash;
var send_input = chat + ' input.jm_send-messages';
// Is this friend online?
if(show == 'unavailable') {
// Offline marker
jQuery(friend).addClass('jm_offline').removeClass('jm_online jm_hover');
// Hide the friend just to be safe since the search uses .hide() and .show() which can override the CSS display attribute
jQuery(friend).hide();
// Disable the chat tools
if(is_groupchat) {
jQuery(chat).addClass('jm_disabled');
jQuery(send_input).blur().attr('disabled', true).attr('data-value', _e("Unavailable")).val(_e("Unavailable"));
}
}
else {
// Online marker
jQuery(friend).removeClass('jm_offline').addClass('jm_online');
// Check against search string
var search = jQuery('#jappix_mini div.jm_roster div.jm_search input.jm_searchbox').val();
var regex = new RegExp('((^)|( ))' + escapeRegex(search), 'gi');
var nick = unescape(jQuery(friend).data('nick'));
if(search && !nick.match(regex))
jQuery(friend).hide();
// Enable the chat input
if(is_groupchat) {
jQuery(chat).removeClass('jm_disabled');
jQuery(send_input).removeAttr('disabled').val('');
}
}
// Change the show presence of this buddy
jQuery(friend + ' span.jm_presence, ' + chat + ' span.jm_presence').attr('class', 'jm_presence jm_images jm_' + show);
// Update the presence counter
updateRosterMini();
logThis('Presence received from: ' + from);
}
// Handles the MUC main elements
function handleMUCMini(pr) {
// We get the xml content
var xml = pr.getNode();
var from = fullXID(getStanzaFrom(pr));
var room = bareXID(from);
var hash = hex_md5(room);
var resource = thisResource(from);
// Is it a valid server presence?
var valid = false;
if(!resource || (resource == unescape(jQuery('#jappix_mini #chat-' + hash + '[data-type=groupchat]').attr('data-nick'))))
valid = true;
// Password required?
if(valid && jQuery(xml).find('error[type=auth] not-authorized').size()) {
// Create a new prompt
openPromptMini(printf(_e("This room (%s) is protected with a password."), room));
// When prompt submitted
jQuery('#jappix_popup div.jm_prompt form').submit(function() {
try {
// Read the value
var password = closePromptMini();
// Any submitted chat to join?
if(password) {
// Send the password
presenceMini('', '', '', '', from, password, true, handleMUCMini);
// Focus on the pane again
switchPaneMini('chat-' + hash, hash);
}
}
catch(e) {}
finally {
return false;
}
});
return;
}
// Nickname conflict?
else if(valid && jQuery(xml).find('error[type=cancel] conflict').size()) {
// New nickname
var nickname = resource + '_';
// Send the new presence
presenceMini('', '', '', '', room + '/' + nickname, '', true, handleMUCMini);
// Update the nickname marker
jQuery('#jappix_mini #chat-' + hash).attr('data-nick', escape(nickname));
}
// Handle normal presence
else
handlePresenceMini(pr);
}
// Updates the user presence
function presenceMini(type, show, priority, status, to, password, limit_history, handler) {
var pr = new JSJaCPresence();
// Add the attributes
if(to)
pr.setTo(to);
if(type)
pr.setType(type);
if(show)
pr.setShow(show);
if(priority)
pr.setPriority(priority);
if(status)
pr.setStatus(status);
// Special presence elements
if(password || limit_history) {
var x = pr.appendNode('x', {'xmlns': NS_MUC});
// Any password?
if(password)
x.appendChild(pr.buildNode('password', {'xmlns': NS_MUC}, password));
// Any history limit?
if(limit_history)
x.appendChild(pr.buildNode('history', {'maxstanzas': 10, 'seconds': 86400, 'xmlns': NS_MUC}));
}
// Send the packet
if(handler)
con.send(pr, handler);
else
con.send(pr);
// No type?
if(!type)
type = 'available';
logThis('Presence sent: ' + type, 3);
}
// Sends a given message
function sendMessageMini(aForm) {
try {
var body = trim(aForm.body.value);
var xid = aForm.xid.value;
var type = aForm.type.value;
var hash = hex_md5(xid);
if(body && xid) {
// Send the message
var aMsg = new JSJaCMessage();
// if the roster does not give us any nick the user may have send us a nickname to use with his first message
// @see http://xmpp.org/extensions/xep-0172.html
var known_roster_entry = jQuery("a.jm_friend[data-xid="+xid+"]",jQuery("#jappix_mini"));
if (0==known_roster_entry.length) {
var subscription = known_roster_entry.attr('data-sub');
// the other may not know my nickname if we do not have both a roster entry, or if he doesn't have one
if ('both' != subscription && 'from' != subscription) {
// Adding our nickname in the message, hard to know if this is just the first one
aMsg.setNick(MINI_NICKNAME);
}
}
aMsg.setTo(xid);
aMsg.setType(type);
aMsg.setBody(body);
con.send(aMsg);
// Clear the input
aForm.body.value = '';
// Display the message we sent
if(type != 'groupchat')
displayMessageMini(type, body, getXID(), 'me', hash, getCompleteTime(), getTimeStamp(), 'user-message');
logThis('Message (' + type + ') sent to: ' + xid);
}
}
catch(e) {}
finally {
return false;
}
}
// Generates the asked smiley image
function smileyMini(image, text) {
return ' <img class="jm_smiley jm_smiley-' + image + ' jm_images" alt="' + encodeQuotes(text) + '" src="' + JAPPIX_STATIC + 'img/others/blank.gif' + '" /> ';
}
// Notifies incoming chat messages
function notifyMessageMini(hash) {
// Define the paths
var tab = '#jappix_mini #chat-' + hash + ' a.jm_chat-tab';
var notify = tab + ' span.jm_notify';
var notify_middle = notify + ' span.jm_notify_middle';
// Notification box not yet added?
if(!exists(notify))
jQuery(tab).append(
'<span class="jm_notify">' +
'<span class="jm_notify_left jm_images"></span>' +
'<span class="jm_notify_middle">0</span>' +
'<span class="jm_notify_right jm_images"></span>' +
'</span>'
);
// Increment the notification number
var number = parseInt(jQuery(notify_middle).text());
jQuery(notify_middle).text(number + 1);
// Update the notification counters
notifyCountersMini();
}
// Notifies the user from a session error
function notifyErrorMini() {
// Replace the Jappix Mini DOM content
jQuery('#jappix_mini').html(
'<div class="jm_starter">' +
'<a class="jm_pane jm_button jm_images" href="' + MINI_ERROR_LINK + '" target="_blank" title="' + _e("Click here to solve the error") + '">' +
'<span class="jm_counter jm_error jm_images">' + _e("Error") + '</span>' +
'</a>' +
'</div>'
);
}
// Updates the global counter with the new notifications
function notifyCountersMini() {
// Count the number of notifications
var number = 0;
jQuery('#jappix_mini span.jm_notify span.jm_notify_middle').each(function() {
number = number + parseInt(jQuery(this).text());
});
// Update the notification link counters
jQuery('#jappix_mini a.jm_switch').removeClass('jm_notifnav');
if(number) {
// Left?
if(jQuery('#jappix_mini div.jm_conversation:visible:first').prevAll().find('span.jm_notify').size())
jQuery('#jappix_mini a.jm_switch.jm_left').addClass('jm_notifnav');
// Right?
if(jQuery('#jappix_mini div.jm_conversation:visible:last').nextAll().find('span.jm_notify').size())
jQuery('#jappix_mini a.jm_switch.jm_right').addClass('jm_notifnav');
}
// No saved title? Abort!
if(MINI_TITLE == null)
return;
// Page title code
var title = MINI_TITLE;
// No new stuffs? Reset the title!
if(number)
title = '[' + number + '] ' + title;
// Apply the title
document.title = title;
return;
}
// Clears the notifications
function clearNotificationsMini(hash) {
// Not focused?
if(!isFocused())
return false;
// Remove the notifications counter
jQuery('#jappix_mini #chat-' + hash + ' span.jm_notify').remove();
// Update the global counters
notifyCountersMini();
return true;
}
// Updates the roster counter
function updateRosterMini() {
// Update online counter
jQuery('#jappix_mini a.jm_button span.jm_counter').text(jQuery('#jappix_mini a.jm_online').size());
}
// Updates the chat overflow
function updateOverflowMini() {
// Show hidden chats
jQuery('#jappix_mini div.jm_conversation:hidden').show();
// Process overflow
var number_visible = parseInt((jQuery(window).width() - 330) / 140);
var number_total = jQuery('#jappix_mini div.jm_conversation').size();
if(number_visible <= 0)
number_visible = 1;
// Must add the overflow switcher?
if(number_visible < number_total) {
// Create the overflow handler?
if(!jQuery('#jappix_mini a.jm_switch').size()) {
// Add the navigation links
jQuery('#jappix_mini div.jm_conversations').before(
'<a class="jm_switch jm_left jm_pane jm_images" href="#">' +
'<span class="jm_navigation jm_images"></span>' +
'</a>'
);
jQuery('#jappix_mini div.jm_conversations').after(
'<a class="jm_switch jm_right jm_pane jm_images" href="#">' +
'<span class="jm_navigation jm_images"></span>' +
'</a>'
);
// Add the click events
overflowEventsMini();
}
// Show first visible chats
var index_visible = number_visible - 1;
jQuery('#jappix_mini div.jm_conversation:gt(' + index_visible + '):visible').hide();
// Close the opened chat
if(jQuery('#jappix_mini div.jm_conversation:hidden a.jm_pane.jm_clicked').size())
switchPaneMini();
// Update navigation buttons
jQuery('#jappix_mini a.jm_switch').removeClass('jm_nonav');
if(!jQuery('#jappix_mini div.jm_conversation:visible:first').prev().size())
jQuery('#jappix_mini a.jm_switch.jm_left').addClass('jm_nonav');
if(!jQuery('#jappix_mini div.jm_conversation:visible:last').next().size())
jQuery('#jappix_mini a.jm_switch.jm_right').addClass('jm_nonav');
}
// Must remove the overflow switcher?
else {
jQuery('#jappix_mini a.jm_switch').remove();
jQuery('#jappix_mini div.jm_conversation:hidden').show();
}
}
// Click events on the chat overflow
function overflowEventsMini() {
jQuery('#jappix_mini a.jm_switch').click(function() {
// Nothing to do?
if(jQuery(this).hasClass('jm_nonav'))
return false;
var hide_this = show_this = '';
// Go left?
if(jQuery(this).is('.jm_left')) {
show_this = jQuery('#jappix_mini div.jm_conversation:visible:first').prev();
if(show_this.size())
hide_this = jQuery('#jappix_mini div.jm_conversation:visible:last');
}
// Go right?
else {
show_this = jQuery('#jappix_mini div.jm_conversation:visible:last').next();
if(show_this.size())
hide_this = jQuery('#jappix_mini div.jm_conversation:visible:first');
}
// Update overflow content
if(show_this && show_this.size()) {
// Hide
if(hide_this && hide_this.size()) {
hide_this.hide();
// Close the opened chat
if(hide_this.find('a.jm_pane').hasClass('jm_clicked'))
switchPaneMini();
}
// Show
show_this.show();
// Update navigation buttons
jQuery('#jappix_mini a.jm_switch').removeClass('jm_nonav');
if((jQuery(this).is('.jm_right') && !show_this.next().size()) || (jQuery(this).is('.jm_left') && !show_this.prev().size()))
jQuery(this).addClass('jm_nonav');
// Update notification counters
notifyCountersMini();
}
return false;
});
}
// Creates the Jappix Mini DOM content
function createMini(domain, user, password) {
// Try to restore the DOM
var dom = getDB('jappix-mini', 'dom');
var stamp = parseInt(getDB('jappix-mini', 'stamp'));
var suspended = false;
// Invalid stored DOM?
if(dom && isNaN(jQuery(dom).find('a.jm_pane.jm_button span.jm_counter').text()))
dom = null;
// Can resume a session?
con = new JSJaCHttpBindingConnection();
setupConMini(con);
// Old DOM?
if(dom && ((getTimeStamp() - stamp) < JSJACHBC_MAX_WAIT) && con.resume()) {
// Read the old nickname
MINI_NICKNAME = getDB('jappix-mini', 'nickname');
// Marker
suspended = true;
}
// New DOM?
else {
dom = '<div class="jm_position">' +
'<div class="jm_conversations"></div>' +
'<div class="jm_starter">' +
'<div class="jm_roster">' +
'<div class="jm_actions">' +
'<a class="jm_logo jm_images" href="https://mini.jappix.com/" target="_blank"></a>' +
'<a class="jm_one-action jm_join jm_images" title="' + _e("Join a chat") + '" href="#"></a>' +
'<a class="jm_one-action jm_status" title="' + _e("Status") + '" href="#">' +
'<span class="jm_presence jm_images jm_available"></span>' +
'</a>' +
'<div class="jm_status_picker">' +
'<a href="#" data-status="available">' +
'<span class="jm_presence jm_images jm_available"></span>' +
'<span class="jm_show_text">' + _e("Available") + '</span>' +
'</a>' +
'<a href="#" data-status="away">' +
'<span class="jm_presence jm_images jm_away"></span>' +
'<span class="jm_show_text">' + _e("Away") + '</span>' +
'</a>' +
'<a href="#" data-status="dnd">' +
'<span class="jm_presence jm_images jm_dnd"></span>' +
'<span class="jm_show_text">' + _e("Busy") + '</span>' +
'</a>' +
'<a href="#" data-status="unavailable">' +
'<span class="jm_presence jm_images jm_unavailable"></span>' +
'<span class="jm_show_text">' + _e("Offline") + '</span>' +
'</a>' +
'</div>' +
'</div>' +
'<div class="jm_buddies"></div>' +
'<div class="jm_search">' +
'<input type="text" class="jm_searchbox jm_images" placeholder="' + _e("Filter") + '" data-value="" />' +
'</div>' +
'</div>' +