PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/mod/elggchat/views/default/elggchat/js.php

https://github.com/sarriaroman/PuntoUBP
PHP | 423 lines | 306 code | 70 blank | 47 comment | 69 complexity | c96c6829a70f60c3e72e308488386bdc MD5 | raw file
  1. <?php
  2. /**
  3. * ElggChat - Pure Elgg-based chat/IM
  4. *
  5. * All the javascript/JQuery functions are in this file
  6. *
  7. * @package elggchat
  8. * @author ColdTrick IT Solutions
  9. * @copyright Coldtrick IT Solutions 2009
  10. * @link http://www.coldtrick.com/
  11. * @version 0.4
  12. */
  13. /*<a href='javascript:inviteFriends(" + i + ")'><?php echo strtolower(elgg_echo("elggchat:chat:invite")); ?></a>*/
  14. require_once(dirname(dirname(dirname(dirname(dirname(dirname(__FILE__)))))) . "/engine/start.php");
  15. global $CONFIG;
  16. $basesec = get_plugin_setting("chatUpdateInterval","elggchat");
  17. if(!$basesec) $basesec = 5;
  18. $maxsecs = get_plugin_setting("maxChatUpdateInterval","elggchat");
  19. if(!$maxsecs) $maxsecs = 30;
  20. $sound = get_plugin_setting("enableSounds","elggchat");
  21. if(empty($sound)) $sound = "no";
  22. $flash = get_plugin_setting("enableFlashing","elggchat");
  23. if(empty($flash)) $flash = "no";
  24. header('Content-type: text/javascript');
  25. ?>
  26. var basesec = <?php echo $basesec;?>;
  27. var maxsecs = <?php echo $maxsecs;?>;
  28. var delay = 1000;
  29. var secs;
  30. var processing = false;
  31. var pollingPause = false;
  32. var lastTimeDataReceived = new Date().getTime();
  33. function InitializeTimer(){
  34. // Set the length of the timer, in seconds
  35. secs = basesec;
  36. tick();
  37. <?php if($flash == "yes"){?>
  38. blink_new();
  39. <?php }?>
  40. }
  41. function blink_new(){
  42. $(".elggchat_session_new_messages").toggleClass("elggchat_session_new_messages_blink");
  43. self.setTimeout("blink_new()", 1000);
  44. }
  45. function tick(){
  46. if(!pollingPause){
  47. if(!processing){
  48. if (secs == 0){
  49. checkForSessions();
  50. } else {
  51. secs = secs - 1;
  52. }
  53. } else {
  54. resetTimer();
  55. }
  56. self.setTimeout("tick()", delay);
  57. }
  58. }
  59. function resetTimer(){
  60. // if needed apply multiplier
  61. var currentTimeStamp = new Date().getTime();
  62. var timeDiff = (currentTimeStamp - lastTimeDataReceived) / 1000;
  63. var interval = Math.ceil((Math.sqrt(Math.pow(basesec * 10 / 2, 2) + (2 * basesec * 10 * timeDiff)) - (basesec * 10 / 2)) / (basesec * 10));
  64. // reset secs
  65. secs = basesec * interval;
  66. if(secs > maxsecs){
  67. secs = maxsecs;
  68. }
  69. }
  70. function inviteFriends(sessionid){
  71. var currentChatWindow = $("#" + sessionid + " .chatmembersfunctions_invite");
  72. if(currentChatWindow.css("display") != "block"){
  73. currentChatWindow.html("");
  74. $("#elggchat_friends_picker .chatmemberinfo").each(function(){
  75. var friend = $(this).find("a");
  76. if(!($("#" + sessionid + " .chatmember a[rel='" + friend.attr('rel') + "']").length > 0)){
  77. newFriend = "<a href='javascript:addFriend(" + sessionid + ", " + friend.attr('rel') + ")'>";
  78. newFriend += friend.html();
  79. newFriend += "</a><br />";
  80. currentChatWindow.append(newFriend);
  81. }
  82. });
  83. }
  84. currentChatWindow.slideToggle();
  85. }
  86. function addFriend(sessionid, friend){
  87. $.post("<?php echo $CONFIG->wwwroot; ?>action/elggchat/invite?chatsession=" + sessionid + "&friend=" + friend, function(){
  88. $("#" + sessionid + " .chatmembersfunctions_invite").toggle();
  89. checkForSessions();
  90. $("#" + sessionid + " input[name='chatmessage']").focus();
  91. });
  92. }
  93. function leaveSession(sessionid){
  94. if(confirm("<?php echo elgg_echo('elggchat:chat:leave:confirm');?>")){
  95. eraseCookie("elggchat_session_" + sessionid);
  96. var current = readCookie("elggchat");
  97. if(current == sessionid){
  98. eraseCookie("elggchat");
  99. }
  100. $.post("<?php echo $CONFIG->wwwroot; ?>action/elggchat/leave?chatsession=" + sessionid, function(){
  101. $("#" + sessionid).remove();
  102. checkForSessions();
  103. });
  104. }
  105. }
  106. function elggchat_toolbar_resize(){
  107. $("#elggchat_toolbar_left").css("width", $(window).width() - $("#toggle_elggchat_toolbar").width());
  108. }
  109. /*function toggleChatToolbar(speed){
  110. $('#elggchat_toolbar_left').toggle(speed);
  111. $('#toggle_elggchat_toolbar').toggleClass('minimizedToolbar');
  112. if($('#toggle_elggchat_toolbar').hasClass('minimizedToolbar')){
  113. createCookie("elggchat_toolbar_minimized", "true");
  114. pollingPause = true;
  115. $('#toggle_elggchat_toolbar').attr("title", "<?php echo elgg_echo("elggchat:toolbar:maximize");?>");
  116. } else {
  117. pollingPause = false;
  118. checkForSessions();
  119. tick();
  120. eraseCookie("elggchat_toolbar_minimized");
  121. $('#toggle_elggchat_toolbar').attr("title", "<?php echo elgg_echo("elggchat:toolbar:minimize");?>");
  122. }
  123. }*/
  124. function startSession(friendGUID){
  125. $.post('<?php echo $CONFIG->wwwroot;?>action/elggchat/create?invite=' + friendGUID, function(data){
  126. if(data){
  127. checkForSessions();
  128. openSession(data);
  129. }
  130. });
  131. }
  132. function toggleFriendsPicker(){
  133. $("#elggchat_friends_picker").slideToggle();
  134. }
  135. function scroll_to_bottom(sessionid){
  136. var chat_window = $("#" + sessionid +" .chatsessiondata");
  137. var scrHeight = chat_window.find(".chatmessages").attr("scrollHeight");
  138. chat_window.find(".chatmessages").attr("scrollTop", scrHeight);
  139. }
  140. function notify_new_message(){
  141. <?php if($sound == "yes"){?>
  142. $.sound.play("<?php echo $CONFIG->wwwroot; ?>mod/elggchat/js/sound/new_message.wav");
  143. <?php }?>
  144. }
  145. function checkForSessions(firsttime){
  146. if (typeof firsttime == "undefined") {
  147. firsttime = false;
  148. }
  149. // Starting the work, so stop the timer
  150. processing = true;
  151. $.getJSON("<?php echo $CONFIG->wwwroot; ?>action/elggchat/poll", function(data){
  152. if(typeof(data.sessions) != "undefined"){
  153. var current = readCookie("elggchat");
  154. $.each(data.sessions, function(i, session){
  155. var sessionExists = false;
  156. $("#" + i).each(function(){
  157. sessionExists = true;
  158. });
  159. if( (i != current || sessionExists == false) ){
  160. var newSession = "";
  161. newSession += "<div class='elggchat_session_leave' onclick='leaveSession(" + i + ")' title='<?php echo elgg_echo("elggchat:chat:leave");?>'></div><a href='javascript:openSession(" + i + ")'>" + session.name + "</a>";
  162. newSession += "<div class='chatsessiondatacontainer'>";
  163. newSession += "<div class='chatsessiondata'>";
  164. newSession += "<div class='chatmembers'><table>";
  165. if(typeof(session.members) != "undefined"){
  166. $.each(session.members, function(memNum, member){
  167. newSession += member;
  168. });
  169. }
  170. newSession += "</table></div>";
  171. newSession += "<div class='chatmembersfunctions' style='background-color:white;'>";
  172. newSession += "</div><div class='chatmembersfunctions_invite'></div>";
  173. newSession += "<div class='chatmessages'>";
  174. if(typeof(session.messages) != "undefined"){
  175. $.each(session.messages, function(msgNum, msg){
  176. newSession += msg;
  177. });
  178. }
  179. newSession += "</div>";
  180. newSession += "<div class='elggchatinput'>";
  181. newSession += "<form>";
  182. newSession += "<input name='chatsession' type='hidden' value='" + i + "'></input>";
  183. newSession += "<input name='chatmessage' type='text' autocomplete='off'></input>";
  184. newSession += "</form>";
  185. newSession += "</div>";
  186. newSession += "</div>";
  187. newSession += "</div>";
  188. if(sessionExists){
  189. $("#" + i).html(newSession);
  190. } else {
  191. newSession = "<div class='session' id='" + i + "'>" + newSession + "</div>";
  192. $("#elggchat_sessions").append(newSession);
  193. }
  194. } else {
  195. $("#" + i + ">a").html(session.name);
  196. var membersData = "";
  197. if(typeof(session.members) != "undefined"){
  198. $.each(session.members, function(memNum, member){
  199. membersData += member;
  200. });
  201. }
  202. $("#" + i + " .chatmembers").html("<table>" + membersData + "</table>");
  203. var messageData = "";
  204. var cookie = readCookie("elggchat_session_" + i);
  205. var lastKnownMsgId = 0;
  206. if(cookie > 0){
  207. var lastKnownMsgId = parseInt(readCookie("elggchat_session_" + i));
  208. }
  209. if(typeof(session.messages) != "undefined"){
  210. $.each(session.messages, function(msgNum, msg){
  211. if(msgNum > lastKnownMsgId || lastKnownMsgId == NaN){
  212. messageData += msg;
  213. lastTimeDataReceived = new Date().getTime();
  214. }
  215. });
  216. }
  217. $("#" + i + " .chatmessages").append(messageData);
  218. }
  219. });
  220. // search for new data
  221. $(".session").each(function(){
  222. var sessionid = $(this).attr("id");
  223. var lastKnownMsgId = parseInt(readCookie("elggchat_session_" + sessionid));
  224. var newestMsgId = parseInt($("#" + sessionid + " .chatmessages div:last").attr("id"));
  225. if(newestMsgId > lastKnownMsgId || !lastKnownMsgId){
  226. if($(this).find(".chatsessiondatacontainer").css("display") != "block" && newestMsgId){
  227. if(!($("#" + sessionid).hasClass("elggchat_session_new_messages")) && !firsttime){
  228. notify_new_message();
  229. }
  230. $("#" + sessionid).addClass("elggchat_session_new_messages");
  231. lastTimeDataReceived = new Date().getTime();
  232. }
  233. }
  234. });
  235. // register submit events on message input
  236. $(".elggchatinput form").unbind("submit");
  237. $(".elggchatinput form").bind("submit", function(){
  238. var input = $.trim($(this).find("input[name='chatmessage']").val());
  239. if(input != ""){
  240. $.post("<?php echo $CONFIG->wwwroot;?>action/elggchat/post_message", $(this).serialize(), function(data){
  241. checkForSessions();
  242. });
  243. }
  244. // empty input field
  245. $(this).find("input[name='chatmessage']").val("");
  246. return false;
  247. });
  248. if(current){
  249. if($("#" + current + " .chatsessiondatacontainer").css("display") != "block"){
  250. openSession(current);
  251. }
  252. var cookie = readCookie("elggchat_session_" + current);
  253. if(cookie > 0){
  254. var lastKnownMsgId = parseInt(cookie);
  255. } else {
  256. var lastKnownMsgId = 0;
  257. }
  258. var newestMsgId = parseInt($("#" + current + " .chatmessages div:last").attr("id"));
  259. if(newestMsgId > lastKnownMsgId){
  260. scroll_to_bottom(current);
  261. createCookie("elggchat_session_" + current, newestMsgId);
  262. }
  263. }
  264. }
  265. // build friendspicker
  266. try {
  267. $("#elggchat_friends a").html("<?php echo elgg_echo("elggchat:friendspicker:info");?> (" + data.friends.online.length + ")");
  268. } catch( e ) {
  269. $("#elggchat_friends a").html("<?php echo elgg_echo("elggchat:friendspicker:info");?> (0)");
  270. }
  271. if(typeof(data.friends) != "undefined"){
  272. $("#elggchat_friends_picker").html("");
  273. var tableDataOnline = "";
  274. var numOnline = data.friends.online.length;
  275. $.each(data.friends.online, function(i, friend){
  276. tableDataOnline += friend;
  277. });
  278. var tableDataOffline = "";
  279. var numOffline = data.friends.offline.length;
  280. $.each(data.friends.offline, function(i, friend){
  281. tableDataOffline += friend;
  282. });
  283. $("#elggchat_friends_picker").append("<h3 class='settings'><?php echo elgg_echo('elggchat:friendspicker:online');?> (" + numOnline + ")</h3><table>" + tableDataOnline + "</table><h3 class='settings'><?php echo elgg_echo('elggchat:friendspicker:offline');?> (" + numOffline + ")</h3><table>" + tableDataOffline + "</table>");
  284. $("#elggchat_friends_picker a").each(function(){
  285. $(this).attr("href","javascript:startSession(" + this.rel + "); toggleFriendsPicker();");
  286. });
  287. }
  288. // Done with all the work
  289. resetTimer();
  290. processing = false;
  291. });
  292. }
  293. function openSession(id){
  294. $("#"+ id).removeClass("elggchat_session_new_messages");
  295. var current = $("#" + id + " .chatsessiondatacontainer").css("display");
  296. eraseCookie("elggchat");
  297. $("#elggchat_sessions .chatsessiondatacontainer").hide();
  298. if(current != "block"){
  299. createCookie("elggchat", id);
  300. var last = $("#" + id + " .chatmessages div:last").attr("id");
  301. createCookie("elggchat_session_" + id, last);
  302. $("#" + id + " .chatsessiondatacontainer").toggle();
  303. }
  304. scroll_to_bottom(id);
  305. $("#" + id + " input[name='chatmessage']").focus();
  306. }
  307. /* Cookie Functions */
  308. function createCookie(name, value, days) {
  309. if (days) {
  310. var date = new Date();
  311. date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  312. var expires = "Expires=" + date.toGMTString() + "; ";
  313. } else {
  314. var expires = "";
  315. }
  316. document.cookie = name + "=" + value + "; " + expires + "Path=/;";
  317. }
  318. function readCookie(name) {
  319. var nameEQ = name + "=";
  320. var ca = document.cookie.split(';');
  321. for(var i = 0; i < ca.length; i++) {
  322. var c = ca[i];
  323. while (c.charAt(0) == ' '){
  324. c = c.substring(1, c.length);
  325. }
  326. if (c.indexOf(nameEQ) == 0){
  327. return c.substring(nameEQ.length, c.length);
  328. }
  329. }
  330. return null;
  331. }
  332. function eraseCookie(name) {
  333. createCookie(name, "", -1);
  334. }
  335. var $j = jQuery.noConflict();
  336. $j(document).ready(function(){
  337. /*if(readCookie("elggchat_toolbar_minimized")){
  338. toggleChatToolbar(0);
  339. }
  340. */
  341. $j(window).resize(function(){
  342. elggchat_toolbar_resize();
  343. });
  344. elggchat_toolbar_resize();
  345. InitializeTimer();
  346. checkForSessions(true);
  347. });
  348. /*$j(document).click(function(){
  349. if( $j('#latest_river').is(':visible') ) {
  350. $j('#latest_river').hide('slow');
  351. }
  352. });*/