PageRenderTime 62ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

/js/phono_chat.js

https://bitbucket.org/itoxable/chiron-gaming
JavaScript | 377 lines | 256 code | 61 blank | 60 comment | 28 complexity | e2848c743580d132cfd73285e145dfd6 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. /* Phono Chat */
  2. jQuery(document).ready(function() {
  3. var connStablished, calls = {}, chats = {}, mySessionInfo;
  4. var showingPlayers=false;
  5. function urlParam(name) {
  6. var results = new RegExp('[\\?&]' + name + '=([^&#]*)')
  7. .exec(window.location.href);
  8. if (!results) {
  9. return undefined;
  10. }
  11. return results[1] || undefined;
  12. }
  13. function createNewPhono() {
  14. //Clone a phono div
  15. var phonoCtr = (jQuery(".phono").size() + 1) - 1;
  16. var newPhonoID = "Phono" + phonoCtr;
  17. var newPhonoDiv = jQuery('.phono').first();
  18. // newPhonoDiv.attr("id",newPhonoID).appendTo('.phonoHldr').show();
  19. newPhonoDiv.find(".phonoID").text(newPhonoID+":");
  20. connStablished = jQuery.phono({
  21. apiKey: "eac2e3f059dff315a698ce822deae0e4",
  22. audio: {type:"none"},
  23. onReady: function(event) {
  24. newPhonoDiv.find(".sessionId").html(this.sessionId);
  25. newPhonoDiv.find(".phoneControl").show();
  26. //console.log("["+newPhonoID+"] Phono loaded");
  27. jQuery.ajax({
  28. type: "POST",
  29. url: "chatAPI.php",
  30. dataType: "json",
  31. data: {method: "connect", sId:this.sessionId},
  32. success: function(data) {
  33. mySessionInfo=data.myInfo;
  34. jQuery.each(mySessionInfo.friendsList, function(friendId, friendInfo){
  35. var thisPhono = newPhonoDiv.attr("id");
  36. friendElement = jQuery("<a></a>");
  37. friendElement.attr("id", friendId);
  38. friendElement.text(friendInfo.userName);
  39. friendElement.click(function(){
  40. openingChatSession(thisPhono, "chatSessionInfo", friendId, "");
  41. });
  42. newPhonoDiv.find(".phoneControl").append(friendElement);
  43. });
  44. $.each(mySessionInfo.messagesPending, function(userFrom, chatSessionFrom) {
  45. $.each(chatSessionFrom.messages, function(index, theMessage) {
  46. routeMessage(newPhonoID, "inbound", chatSessionFrom, theMessage);
  47. })
  48. delete chatSessionFrom.messages;
  49. });
  50. },
  51. });
  52. //if(!this.audio.permission()){
  53. // this.audio.showPermissionBox();
  54. //}
  55. },
  56. onUnready: function(event) {
  57. newPhonoDiv.find(".sessionId").text("disconnected");
  58. console.log("[" + newPhonoID + "] Phono disconnected");
  59. },
  60. onError: function(event) {
  61. newPhonoDiv.find(".sessionId").text(event.reason);
  62. console.log(event.reason);
  63. },
  64. messaging: {
  65. onMessage: function(event, message) {
  66. var fromSessionId = message.from.split("/");
  67. var messageWithExtraParam = message.body.split("$#_#$");
  68. chatSession = {
  69. fromName: messageWithExtraParam[0],
  70. fromUserId: messageWithExtraParam[1],
  71. sessionId: fromSessionId[0],
  72. };
  73. var realMessage = "";
  74. for (i=2; i < messageWithExtraParam.length; i++) {
  75. realMessage +=messageWithExtraParam[i];
  76. if (i+1<messageWithExtraParam.length) {
  77. realMessage += "$#_#$";
  78. }
  79. }
  80. routeMessage("MyChatSession","inbound",chatSession,realMessage);
  81. }
  82. }
  83. });
  84. }
  85. //Routes a message to the appropriate chat or creates a new chat
  86. function routeMessage(phonoID, type, chatSession, message) {
  87. prefix="";
  88. if (type == "inbound")
  89. prefix="from";
  90. else
  91. prefix="to";
  92. var theChat = "";
  93. $.each(chats, function(key, value) {
  94. if (value["toUserId"] == chatSession[prefix + "UserId"]
  95. || value["fromUserId"] == chatSession[prefix + "UserId"]) {
  96. theChat = key;
  97. var newMsg = chatMessage(chatSession[prefix + "Name"], message, type);
  98. var chatDiv = jQuery("#" + theChat);
  99. renderNewMessage(chatDiv, newMsg);
  100. chatDiv.find(".chatTxtInput").focus();
  101. return;
  102. }
  103. });
  104. //Did we find a chat?
  105. if (!theChat.length) {
  106. createNewChat(phonoID, type, chatSession, message);
  107. }
  108. }
  109. //Outputs an IM message
  110. function renderNewMessage(chat, msg) {
  111. chat.find(".chatBox").append(msg);
  112. chat.find(".chatBox").attr({
  113. scrollTop : chat.find(".chatBox").attr("scrollHeight")
  114. });
  115. }
  116. function createNewChat(phonoID, type, chatSession, message) {
  117. prefix="";
  118. if (type == "inbound")
  119. prefix="from";
  120. else
  121. prefix="to";
  122. //clone a chat box
  123. var phonoDiv = jQuery("#" + phonoID);
  124. var newChatID;
  125. if (jQuery("#Chat_" + chatSession[prefix + "UserId"]).length == 0) {
  126. newChatID = createChatDiv(phonoID, chatSession[prefix + "UserId"]);
  127. } else {
  128. newChatID = "Chat_" + chatSession[prefix + "UserId"];
  129. }
  130. var chatDiv = jQuery("#" + newChatID);
  131. chatDiv.find(".chatID").text(chatSession[prefix + "Name"]);
  132. if (message.length) {
  133. var newMsg = chatMessage(chatSession[prefix + "Name"], message, type);
  134. renderNewMessage(chatDiv, newMsg);
  135. }
  136. console.log("[" + phonoDiv.attr('id') + "] [" + newChatID
  137. + "] Chat started with " + chatSession.sessionId);
  138. // The Session Id is saved in the opened chats.
  139. chats[newChatID] = chatSession;
  140. }
  141. //Creates a new chat
  142. function openingChatSession(phonoID, type, to, message) {
  143. jQuery.ajax({
  144. type: "POST",
  145. url: "chatAPI.php",
  146. dataType: "json",
  147. data: {method: type, uId: to},
  148. success: function(data) {
  149. createNewChat(phonoID, "outbound", data.chatSession, message);
  150. }
  151. });
  152. }
  153. //Creates a new div to hold a chat. Returns the id of the new div
  154. function createChatDiv(phonoID, userId) {
  155. if (jQuery('.chats').length == 0) {
  156. var wrapper = jQuery('<div class="chats"/>');
  157. jQuery('body').append(wrapper);
  158. wrapper.click(function(event){
  159. event.stopPropagation();
  160. });
  161. } else {
  162. var wrapper = jQuery('.chats');
  163. }
  164. newChatID = "Chat_" + userId;
  165. singleEmptyChat = jQuery('<div id="'+ newChatID +'" class="chatHldr"/>');
  166. singleEmptyChatHeader = jQuery('<div class="chatHeader"><a class="closeChat" title="Remove" href="#"></a><span class="chatID"></span></div>');
  167. singleEmptyChatBox = jQuery('<div class="chatBox"></div>');
  168. singleEmptyChatInput = jQuery('<div class="chatInputHldr"><input class="chatTxtInput" type="text"><input class="sendMsg" type="button" value="send"></div>');
  169. jQuery('.sendMsg', singleEmptyChatInput).click(function(event){
  170. var thisPhono = $(this).closest(".phono");
  171. var thisChat = $(this).closest(".chatHldr");
  172. var msgText = thisChat.find(".chatTxtInput").val();
  173. var newMsg = chatMessage("You", msgText, "outbound");
  174. renderNewMessage(thisChat, newMsg);
  175. toSessionId=chats[thisChat.attr("id")].sessionId;
  176. msgTextWithParameters = mySessionInfo.myName +"$#_#$"+ mySessionInfo.myUserId +"$#_#$"+ msgText;
  177. connStablished.messaging.send(toSessionId, msgTextWithParameters);
  178. jQuery.ajax({
  179. type: "POST",
  180. url: "chatAPI.php",
  181. dataType: "json",
  182. data: {method: "outbound", toChatSession: chats[thisChat.attr("id")], message: msgText},
  183. success: function(data) {
  184. chats[thisChat.attr("id")] = data.chatSession;
  185. },
  186. });
  187. thisChat.find(".chatTxtInput").val("");
  188. console.log("[" + thisPhono.attr('id') + "] Sending message to: "
  189. + chats[thisChat.attr('id')] + " [" + msgText + "]");
  190. });
  191. singleEmptyChat.html(singleEmptyChatHeader);
  192. singleEmptyChat.append(singleEmptyChatBox);
  193. singleEmptyChat.append(singleEmptyChatInput);
  194. wrapper.append(singleEmptyChat);
  195. singleEmptyChatHeader.width(singleEmptyChat.width());
  196. return newChatID;
  197. }
  198. // Create and return a chat message bubble
  199. var chatMessage = function(from, body, type) {
  200. var result;
  201. if (type == 'inbound') {
  202. var borderStyle = "inbound";
  203. var fromStyle = "fromInbound";
  204. } else {
  205. var borderStyle = "outbound";
  206. var fromStyle = "fromOutbound";
  207. }
  208. result = jQuery("<div>").addClass("chatEntry").addClass(borderStyle);
  209. var fromHeader = jQuery("<div>").addClass("from").addClass(fromStyle).html(from)
  210. .appendTo(result);
  211. var msgBody = jQuery("<div>").addClass("body").html(body).appendTo(result);
  212. return result;
  213. }
  214. //jQuery('.closeChat').live('click', function() {
  215. // var thisChat = $(this).closest(".chatHldr");
  216. // var thisChatID = $(this).closest(".chatHldr").attr("id");
  217. // thisChat.slideUp(function(){
  218. // thisChat.remove();
  219. // });
  220. // delete chats[thisChatID];
  221. // console.log("[" + thisChatID + "] Chat closed");
  222. //});
  223. //
  224. //jQuery('.flashHelp a').live('click', function() {
  225. // var thisPhono = $(this).closest(".phono");
  226. // $(".flash-hldr").css("left", "250.5px");
  227. // thisPhono.find(".flashHelp").text("Try again");
  228. // return false;
  229. //});
  230. //
  231. //jQuery('.chat').live('click', function() {
  232. // var thisPhono = $(this).closest(".phono").attr("id");
  233. // var chatTo = $.trim(jQuery("#" + thisPhono).find(".chatTo").val());
  234. // //createNewChat(thisPhono, chatTo, "");
  235. // openingChatSession(thisPhono, "chatSessionInfo", chatTo, "");
  236. //});
  237. //
  238. //jQuery('.text').live('click', function() {
  239. // var thisPhono = $(this).closest(".phono");
  240. // var to = thisPhono.find(".jid").val();
  241. // var msg = thisPhono.find(".msgBody").val();
  242. // sendMessage(thisPhono.attr("id"), to, msg);
  243. // thisPhono.find(".msgBody").val("");
  244. //});
  245. //
  246. //jQuery(".logToggler").click(function() {
  247. // if (jQuery("#logConsole").css("height") == "25px") {
  248. // jQuery("#logConsole").css("height", "245px");
  249. // jQuery("body").css("margin-bottom", "285px");
  250. // $(this).text("Hide Log Viewer");
  251. // } else {
  252. // jQuery("#logConsole").css("height", "25px");
  253. // jQuery("body").css("margin-bottom", "25px");
  254. // $(this).text("Show Log Viewer");
  255. // }
  256. //
  257. // return false;
  258. //});
  259. jQuery(".players_button").click(function(event){
  260. event.stopPropagation();
  261. if(jQuery('.playersWrapper').is(':visible')){
  262. try{
  263. jQuery(".playersInner").getNiceScroll().remove();
  264. }catch(e){}
  265. jQuery('.playersWrapper').slideUp('fast',function() {
  266. $(this).remove();
  267. });
  268. return;
  269. }
  270. var wrapper = jQuery('<div class="playersWrapper"/>');
  271. playersPanel = jQuery('<div class="players_title" />');
  272. playersHeader = jQuery('<span />');
  273. playersHeader.css('margin-left', '10px');
  274. playersHeader.text('Players');
  275. playersPanel.append(playersHeader);
  276. playersInnerPanel = jQuery('<div class="playersInner">');
  277. if(mySessionInfo.friendsList.length > 4){
  278. playersInnerPanel.height('446px');
  279. }
  280. if(mySessionInfo.friendsList.length == 0){
  281. noPlayersListed = jQuery('<div class="player"><a href="javascript:;" class="player_inner"><div class="player_title"></div><div class="player_text">No Players</div><div style="font-weight: bold;" class="notification_date"></div></a></div>');
  282. playersInnerPanel.append(noPlayersListed);
  283. }
  284. else{
  285. jQuery.each(mySessionInfo.friendsList, function(friendId, friendInfo){
  286. singleFriend = jQuery("<div/>");
  287. singleFriend.addClass("player");
  288. singleFriendLink = jQuery("<a/>");
  289. singleFriendLink.addClass("player_inner");
  290. singleFriendInfo = jQuery("<div/>");
  291. singleFriendInfo.addClass("player_title");
  292. singleFriendInfo.attr("id", friendInfo.userId);
  293. singleFriendInfo.text(friendInfo.userName);
  294. singleFriendInfo.click(function(){
  295. openingChatSession("MyChatSession", "chatSessionInfo", friendId, "");
  296. });
  297. singleFriendLink.append(singleFriendInfo);
  298. singleFriend.append(singleFriendLink);
  299. playersInnerPanel.append(singleFriend);
  300. });
  301. }
  302. wrapper.html(playersPanel);
  303. wrapper.append(playersInnerPanel);
  304. jQuery('body').append(wrapper);
  305. wrapper.slideDown('fast',function() {
  306. jQuery('.players_title').width(wrapper.width());
  307. if(mySessionInfo.friendsList.length > 4){
  308. jQuery('.playersInner').niceScroll();
  309. }
  310. });
  311. wrapper.click(function(event){
  312. event.stopPropagation();
  313. });
  314. });
  315. createNewPhono();
  316. });