53e29c027630f3fc40da66bb42d12be0734ae449
[hermes] / www / js / jquery.juitter.js
1 /*\r
2 JUITTER 1.0.0 - 22/07/2009 - http://juitter.com\r
3 BY RODRIGO FANTE - http://rodrigofante.com\r
4 \r
5 ** jQuery 1.2.* or higher required\r
6 \r
7 Juitter is distributed under the MIT License\r
8 Read more about the MIT License --> http://www.opensource.org/licenses/mit-license.php\r
9 \r
10 This script is just a beta test version, download and use it at your own risk.\r
11 The Juitter developer shall have no responsability for data loss or damage of any kind by using this script.\r
12 */\r
13 (function($) {\r
14         var conf = {},\r
15                 // JUITTER DEFAULT CONFIGURATION ========================\r
16                 // YOU CAN CHANGE THE DYNAMIC VARS ON CALLING THE start method, see the system.js for more information about it.\r
17 \r
18                 numMSG = 20; // set the number of messages to be show\r
19                 containerDiv="tweets", // //Set a place holder DIV which will receive the list of tweets example <div id="juitterContainer"></div>\r
20                 loadMSG="Loading messages...", // Loading message, if you want to show an image, fill it with "image/gif" and go to the next variable to set which image you want to use on \r
21                 imgName="loader.gif", // Loading image, to enable it, go to the loadMSG var above and change it to "image/gif"\r
22                 readMore="Read it on Twitter", // read more message to be show after the tweet content\r
23                 nameUser="image" // insert "image" to show avatar of "text" to show the name of the user that sent the tweet \r
24                 live:"live-20", //optional, disabled by default, the number after "live-" indicates the time in seconds to wait before request the Twitter API for updates, I do not recommend to use less than 60 seconds.\r
25                 // end of configuration\r
26         \r
27                 // some global vars\r
28                 aURL="";msgNb=1;\r
29                 var mode,param,time,lang,contDiv,loadMSG,gifName,numMSG,readMore,fromID,ultID,filterWords;\r
30                 var running=false;\r
31                 // Twitter API Urls\r
32                 apifMultipleUSER = "http://search.twitter.com/search.json?from%3A";\r
33                 apifUSER = "http://search.twitter.com/search.json?q=from%3A";\r
34                 apitMultipleUSER = "http://search.twitter.com/search.json?to%3A";\r
35                 apitUSER = "http://search.twitter.com/search.json?q=to%3A";\r
36                 apiSEARCH = "http://search.twitter.com/search.json?q=";\r
37         $.Juitter = {\r
38                 registerVar: function(opt){\r
39                         mode=opt.searchType;\r
40                         param=opt.searchObject;\r
41                         timer=opt.live;\r
42                         lang=opt.lang?opt.lang:"";\r
43                         contDiv=opt.placeHolder?opt.placeHolder:containerDiv;\r
44                         loadMSG=opt.loadMSG?opt.loadMSG:loadMSG;\r
45                         gifName=opt.imgName?opt.imgName:imgName;\r
46                         numMSG=opt.total?opt.total:numMSG;\r
47                         readMore=opt.readMore?opt.readMore:readMore;\r
48                         fromID=opt.nameUser?opt.nameUser:nameUser;\r
49                         filterWords=opt.filter;\r
50                         openLink=opt.openExternalLinks?"target='_self'":"";\r
51                 },\r
52                 start: function(opt) {          \r
53                         ultID=0;\r
54                         if($("#"+contDiv)){     \r
55                                 this.registerVar(opt);\r
56                                 // show the load message\r
57                                 this.loading();\r
58                                 // create the URL  to be request at the Twitter API\r
59                                 aURL = this.createURL();\r
60                                 // query the twitter API and create the tweets list\r
61                                 this.conectaTwitter(1);         \r
62                                 // if live mode is enabled, schedule the next twitter API query\r
63                                 if(timer!=undefined&&!running) this.temporizador();\r
64                         }   \r
65                 },\r
66                 update: function(){\r
67                         this.conectaTwitter(2);         \r
68                         if(timer!=undefined) this.temporizador();\r
69                 },\r
70                 loading: function(){\r
71                         if(loadMSG=="image/gif"){\r
72                                 $("<img></img>")\r
73                                         .attr('src', gifName)\r
74                                         .appendTo("#"+contDiv); \r
75                         } else $("#"+contDiv).html(loadMSG);\r
76                 },\r
77                 createURL: function(){\r
78                         var url = "";\r
79                         jlg=lang.length>0?"&lang="+lang:jlg=""; \r
80                         var seachMult = param.search(/,/);\r
81                         if(seachMult>0) param = "&ors="+param.replace(/,/g,"+");\r
82                         if(mode=="fromUser" && seachMult<=0) url=apifUSER+param;\r
83                         else if(mode=="fromUser" && seachMult>=0) url=apifMultipleUSER+param;\r
84                         else if(mode=="toUser" && seachMult<=0) url=apitUSER+param;\r
85                         else if(mode=="toUser" && seachMult>=0) url=apitMultipleUSER+param;\r
86                         else if(mode=="searchWord") url=apiSEARCH+param+jlg;\r
87                         url += "&rpp="+numMSG;          \r
88                         return url;\r
89                 },\r
90                 delRegister: function(){\r
91                         // remove the oldest entry on the tweets list\r
92                         if(msgNb>=numMSG){\r
93                                 $(".twittLI").each(\r
94                                         function(o,elemLI){\r
95                                                 if(o>=numMSG) $(this).hide("slow");                                                                                                       \r
96                                         }\r
97                                 );\r
98                         }       \r
99                 },\r
100                 conectaTwitter: function(e){\r
101                         // query the twitter api and create the tweets list\r
102                         $.ajax({\r
103                                 url: aURL,\r
104                                 type: 'GET',\r
105                                 dataType: 'jsonp',\r
106                                 timeout: 1000,\r
107                                 error: function(){ $("#"+contDiv).html("fail#"); },\r
108                                 success: function(json){\r
109                                         if(e==1) $("#"+contDiv).html("");                               \r
110                                         $.each(json.results,function(i,item) {\r
111                                                 if(e==1 || (i<numMSG && item.id>ultID)){\r
112                                                         if(i==0){\r
113                                                                 tultID = item.id;\r
114                                                                 $("<ul></ul>")\r
115                                                                         .attr('id', 'twittList'+ultID)\r
116                                                                         .attr('class','twittList rounded')\r
117                                                                         .prependTo("#"+contDiv);  \r
118                                                         }\r
119                                                         if (item.text != "undefined") {\r
120                                                                 var link =  "http://twitter.com/"+item.from_user+"/status/"+item.id;  \r
121                                                                 \r
122                                                                 var tweet = $.Juitter.filter(item.text);\r
123                                                                 \r
124                                                                 if(fromID=="image") mHTML="<a href='http://www.twitter.com/"+item.from_user+"'><img src='"+item.profile_image_url+"' alt='"+item.from_user+"' class='juitterAvatar' /></a> "+$.Juitter.textFormat(tweet)+" <a href='" + link + "' class='jRM' "+openLink+"><span class='time'>&raquo; "+item.created_at+"</span></a>";\r
125                                                                 else mHTML="<a href='http://www.twitter.com/"+item.from_user+"'>@"+item.from_user+":</a> "+$.Juitter.textFormat(tweet)+" <a href='" + link + "' "+openLink+"><span class='time'>&raquo; "+item.created_at+"</span></a>";\r
126                                                                 \r
127                                                                 $("<li></li>") \r
128                                                                         .html(mHTML)  \r
129                                                                         .attr('id', 'twittLI'+msgNb)\r
130                                                                         .attr('class', 'twittLI')\r
131                                                                         .appendTo("#twittList"+ultID);\r
132 \r
133                                                                 $('#twittLI'+msgNb).hide();\r
134                                                                 $('#twittLI'+msgNb).show("slow");\r
135                                                                 \r
136                                                                 // remove old entries\r
137                                                                 $.Juitter.delRegister();\r
138                                                                 msgNb++;                                                                \r
139                                                         }\r
140                                                 }\r
141                                         });     \r
142                                         ultID=tultID;\r
143                                 }\r
144                         });\r
145                 },      \r
146                 filter: function(s){\r
147                         if(filterWords){\r
148                                 searchWords = filterWords.split(",");                           \r
149                                 if(searchWords.length>0){\r
150                                         cleanHTML=s;\r
151                                         $.each(searchWords,function(i,item){    \r
152                                                 sW = item.split("->").length>0 ? item.split("->")[0] : item;\r
153                                                 rW = item.split("->").length>0 ? item.split("->")[1] : "";                                      \r
154                                                 regExp=eval('/'+sW+'/gi');                                      \r
155                                                 cleanHTML = cleanHTML.replace(regExp, rW);                                                      \r
156                                         });\r
157                                 } else cleanHTML = s;                   \r
158                                 return cleanHTML;\r
159                         } else return s;\r
160                 },\r
161                 textFormat: function(texto){\r
162                         //make links\r
163                         var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;\r
164                         texto = texto.replace(exp,"<a href='$1' class='extLink' "+openLink+">$1</a>"); \r
165                         var exp = /[\@]+([A-Za-z0-9-_]+)/ig;\r
166                         texto = texto.replace(exp,"<a href='http://twitter.com/$1' class='profileLink'>@$1</a>"); \r
167                         var exp = /[\#]+([A-Za-z0-9-_]+)/ig;\r
168                         texto = texto.replace(exp,"<a href='http://juitter.com/#$1' onclick='$.Juitter.start({searchType:\"searchWord\",searchObject:\"$1\"});return false;' class='hashLink'>#$1</a>"); \r
169                         // make it bold\r
170                         if(mode=="searchWord"){\r
171                                 tempParam = param.replace(/&ors=/,"");\r
172                                 arrParam = tempParam.split("+");\r
173                                 $.each(arrParam,function(i,item){                                       \r
174                                         regExp=eval('/'+item+'/gi');\r
175                                         newString = new String(''+item+'');\r
176                                         texto = texto.replace(regExp, newString);                                         \r
177                                 });                             \r
178                         }\r
179                         return texto;\r
180                 },\r
181                 temporizador: function(){\r
182                         // live mode timer\r
183                         running=true;\r
184                         aTim = timer.split("-");\r
185                         if(aTim[0]=="live" && aTim[1].length>0){\r
186                                 tempo = aTim[1]*1000;\r
187                                 setTimeout("$.Juitter.update()",tempo);\r
188                         }\r
189                 }\r
190         };      \r
191 })(jQuery);