Merge branch 'master' of git@github.com:kingdon/tues-crep
[tues-crep.git] / jquery.chat.js.txt
blob2185e8982e95fbb62333cfb18a5f551b866b54dd
1 /**\r
2  * @author vincent voyer\r
3  * vincent.voyer@gmail.com\r
4  */\r
5 (function($){\r
6         $.fn.chat = function(params){\r
7                 \r
8                 var params = $.extend({\r
9                         refresh:1.7\r
10                 },params);\r
11                 \r
12                 function chat(jElt) {\r
13                         //jElt correspond à <div id="container-principal">\r
14                         var chat=jElt.find('.chat div'); // div qui contiendra le texte du chat\r
15                         \r
16                         // gestion de l'entrée au clavier\r
17                         jElt.find('form').submit(function() {\r
18                                 var input=$(this).find(':input');\r
19                                 var message=input.val();\r
20                                 \r
21                                 if ($.trim(message).length > 0) { // il faut au moins avoir écrit quelque chose pour l'afficher\r
22                                         jElt.find('.ajaxStatus').show();\r
23                                         \r
24                                         input.val(''); /* on vide l'input */\r
25                                         input.blur();\r
26                                         input.attr("disabled", "disabled");\r
27                                         \r
28                                         $.post("/lab/chat/ecrire", {\r
29                                                 msg: message\r
30                                         }, function(data){\r
31                                                 input.removeAttr("disabled");\r
32                                                 input.focus();\r
33                                                 if (data) \r
34                                                         chat.append('<p>' + data.pseudo + ' &gt; <strong>' + data.msg + '</strong></p>');\r
35                                                 jElt.find('.ajaxStatus').hide();\r
36                                         }, 'json');\r
37                                 }\r
38                                 return false;\r
39                         });\r
40                         \r
41                         /* gestion de la récupération des nouveaux messages */\r
42                         var lireMessages = function(){\r
43                                 $.getJSON("/lab/chat/lire", function(data){\r
44                                         $.each(data, function(i,msg){\r
45                                                 chat.append('<p>'+msg.pseudo+' &gt; <strong>'+msg.msg+'</strong></p>');\r
46                                         });\r
47                                         setTimeout(lireMessages,params.refresh*1000);\r
48                                 });\r
49                         }\r
50                         lireMessages(); //on lance la récupération des messages\r
51                 }\r
52                 \r
53                 return this.each(function(){\r
54                         chat($(this));\r
55                 });             \r
56         };\r
57 })(jQuery)