Main.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. MWF.xApplication.Chat.options.multitask = false;
  2. MWF.xApplication.Chat.options.executable = false;
  3. MWF.xApplication.Chat.Main = new Class({
  4. Extends: MWF.xApplication.Common.Main,
  5. Implements: [Options, Events],
  6. options: {
  7. "style": "default",
  8. "name": "Chat",
  9. "icon": "icon.png",
  10. "width": "800",
  11. "height": "500",
  12. "title": MWF.xApplication.Chat.LP.title,
  13. "id": "",
  14. "owner": "",
  15. "desktopReload": false
  16. },
  17. onQueryLoad: function(){
  18. this.lp = MWF.xApplication.Chat.LP;
  19. },
  20. loadApplication: function(callback){
  21. this.dialogues = {};
  22. this.node = new Element("div", {"styles": this.css.content}).inject(this.content);
  23. this.dialogueTabAreaNode = new Element("div", {"styles": this.css.dialogueTabAreaNode}).inject(this.node);
  24. this.chatContentAreaNode = new Element("div", {"styles": this.css.chatContentAreaNode}).inject(this.node);
  25. this.chatTitleNode = new Element("div", {"styles": this.css.chatTitleNode}).inject(this.chatContentAreaNode);
  26. this.chatTitleAddMemberAction = new Element("div", {"styles": this.css.chatTitleAddMemberAction}).inject(this.chatTitleNode);
  27. //this.chatTitleAddMemberActionButton = new Element("div", {"styles": this.css.chatTitleAddMemberActionButton, "text": this.lp.add}).inject(this.chatTitleAddMemberAction);
  28. //this.chatTitleDelMemberActionButton = new Element("div", {"styles": this.css.chatTitleAddMemberActionButton, "text": this.lp.del}).inject(this.chatTitleAddMemberAction);
  29. this.chatTitleMemberNode = new Element("div", {"styles": this.css.chatTitleMemberNode}).inject(this.chatTitleNode);
  30. this.chatAreaNode = new Element("div", {"styles": this.css.chatAreaNode}).inject(this.chatContentAreaNode);
  31. this.chatInputAreaNode = new Element("div", {"styles": this.css.chatInputAreaNode}).inject(this.chatContentAreaNode);
  32. this.chatInputNode = new Element("div", {"styles": this.css.chatInputNode}).inject(this.chatInputAreaNode);
  33. this.chatTextAreaNode = new Element("textarea", {"styles": this.css.chatTextAreaNode}).inject(this.chatInputNode);
  34. this.chatInputActionNode = new Element("div", {"styles": this.css.chatInputActionNode}).inject(this.chatInputAreaNode);
  35. this.chatSendActionNode = new Element("div", {"styles": this.css.chatSendActionNode, "text": this.lp.send}).inject(this.chatInputActionNode);
  36. this.setChatAreaHeight();
  37. this.addEvent("resize", this.setChatAreaHeight);
  38. this.setEvent();
  39. },
  40. setEvent: function(){
  41. this.chatTextAreaNode.addEvent("keydown", function(e){
  42. if (e.control && (e.code==13)){
  43. this.sendMessage();
  44. this.chatTextAreaNode.focus();
  45. }
  46. }.bind(this));
  47. this.chatSendActionNode.addEvent("click", this.sendMessage.bind(this));
  48. //this.chatTitleAddMemberActionButton.addEvent("click", this.addFriend.bind(this));
  49. //this.chatTitleDelMemberActionButton.addEvent("click", this.delFriend.bind(this));
  50. },
  51. //addFriend: function(){
  52. // if (this.current){
  53. // var panel = this.desktop.top.userPanel;
  54. // var chatData = panel.chatData;
  55. // if (!chatData) chatData = {};
  56. // if (!chatData.onlineList) chatData.onlineList = [];
  57. //
  58. // var user = this.current.members[0];
  59. // if (chatData.onlineList.indexOf(user.name)==-1){
  60. // if (layout.desktop.session.user.name!=user.name) panel.users["online"+user.name] = new MWF.xDesktop.UserPanel.User(panel.userListOnlineAreaNode, panel, user.name);
  61. // chatData.onlineList.push(user.name);
  62. // panel.chatData = chatData;
  63. // MWF.UD.putData("chat", chatData);
  64. // this.current.setCurrent();
  65. // }
  66. // }
  67. //},
  68. //delFriend: function(){
  69. // if (this.current){
  70. // var panel = this.desktop.top.userPanel;
  71. // var chatData = panel.chatData;
  72. // if (!chatData) chatData = {};
  73. // if (!chatData.onlineList) chatData.onlineList = [];
  74. //
  75. // var user = this.current.members[0];
  76. // if (chatData.onlineList.indexOf(user.name)!=-1){
  77. // var userItem = panel.users["online"+user.name];
  78. // if (userItem) userItem.destroy();
  79. //
  80. // chatData.onlineList.erase(user.name);
  81. // panel.chatData = chatData;
  82. // MWF.UD.putData("chat", chatData);
  83. //
  84. // this.current.setCurrent();
  85. // }
  86. // }
  87. //},
  88. //addToCathList: function(){
  89. // if (this.current){
  90. // var panel = this.desktop.top.userPanel;
  91. // var chatData = panel.chatData;
  92. // if (!chatData) chatData = {};
  93. // if (!chatData.chatList) chatData.chatList = [];
  94. //
  95. // var user = this.current.members[0];
  96. // if (chatData.chatList.indexOf(user.name)==-1){
  97. // if (layout.desktop.session.user.name!=user.name) panel.users["chat"+user.name] = new MWF.xDesktop.UserPanel.User(panel.userListChatAreaNode, panel, user.name);
  98. // chatData.chatList.unshift(user.name);
  99. // panel.chatData = chatData;
  100. // MWF.UD.putData("chat", chatData);
  101. // this.current.setCurrent();
  102. // }
  103. // }
  104. //},
  105. sendMessage: function(){
  106. if (this.current){
  107. var text = this.chatTextAreaNode.get("value");
  108. if (text){
  109. //message = {
  110. // "messageType": "chat",
  111. // "personList": this.current.toNames,
  112. // "text": text
  113. //}
  114. message = {
  115. "text": text,
  116. "type": "text",
  117. "from": this.current.owner.name,
  118. "person": this.current.toNames[0],
  119. "category": "dialog"
  120. };
  121. this.desktop.socket.send(message);
  122. this.current.showMessage(message);
  123. }
  124. this.chatTextAreaNode.set("value", "");
  125. //this.addToCathList();
  126. }
  127. },
  128. setChatAreaHeight: function(){
  129. var size = this.chatContentAreaNode.getSize();
  130. var titleSize = this.chatTitleNode.getSize();
  131. var inputSize = this.chatInputAreaNode.getSize();
  132. var y = size.y - titleSize.y - inputSize.y;
  133. this.chatAreaNode.setStyle("height", ""+y+"px");
  134. },
  135. addDialogueBack: function(owner, members){
  136. var dialogue = new MWF.xApplication.Chat.Dialogue(owner, members, this);
  137. var key1 = owner.name+members[0].name;
  138. var key2 = members[0].name+owner.name;
  139. this.dialogues[key1] = dialogue;
  140. this.dialogues[key2] = dialogue;
  141. return dialogue;
  142. },
  143. addDialogue: function(owner, members){
  144. var dialogue = new MWF.xApplication.Chat.Dialogue(owner, members, this);
  145. var key1 = owner.name+members[0].name;
  146. var key2 = members[0].name+owner.name;
  147. this.dialogues[key1] = dialogue;
  148. this.dialogues[key2] = dialogue;
  149. dialogue.setCurrent();
  150. return dialogue;
  151. }
  152. });
  153. MWF.xApplication.Chat.Dialogue = new Class({
  154. initialize: function(owner, members, chat){
  155. this.chat = chat;
  156. this.owner = owner;
  157. this.members = members;
  158. this.css = this.chat.css;
  159. this.unreadCount = 0;
  160. this.messageDate = null;
  161. this.load();
  162. },
  163. load: function(){
  164. this.createTab();
  165. this.createContent();
  166. },
  167. createContent: function(){
  168. this.chatContentScrollNode = new Element("div", {"styles": this.css.chatContentScrollNode}).inject(this.chat.chatAreaNode);
  169. this.chatContentNode = new Element("div", {"styles": this.css.chatContentNode}).inject(this.chatContentScrollNode);
  170. this.scroll = new Fx.Scroll(this.chatContentScrollNode);
  171. MWF.require("MWF.widget.ScrollBar", function(){
  172. new MWF.widget.ScrollBar(this.chatContentScrollNode, {
  173. "style":"xDesktop_Message", "where": "before", "indent": false, "distance": 50, "friction": 6, "axis": {"x": false, "y": true}
  174. });
  175. }.bind(this));
  176. },
  177. createTab: function(){
  178. this.tabNode = new Element("div", {"styles": this.css.chatTabNode}).inject(this.chat.dialogueTabAreaNode);
  179. var icon = this.members[0].icon;
  180. if (this.members.length>1){
  181. icon = this.chat.path+this.chat.options.style+"/group.png";
  182. }else{
  183. if (!icon){
  184. if (this.members[0].genderType=="f"){
  185. icon = "/x_component_Chat/$Main/"+this.chat.options.style+"/female.png";
  186. }else{
  187. icon = "/x_component_Chat/$Main/"+this.chat.options.style+"/man.png";
  188. }
  189. }else{
  190. icon = "data:image/png;base64,"+icon;
  191. }
  192. }
  193. if (this.members[0].onlineStatus=="offline"){
  194. icon = MWF.grayscale(icon);
  195. }
  196. this.tabIconNode = new Element("div", {"styles": this.css.tabIconNode}).inject(this.tabNode);
  197. this.tabIconImgNode = new Element("img", {"styles": this.css.tabIconImgNode}).inject(this.tabIconNode);
  198. this.tabIconImgNode.set("src", icon);
  199. this.tabCloseAction = new Element("div", {"styles": this.css.tabCloseAction}).inject(this.tabNode);
  200. this.tabTextNode = new Element("div", {"styles": this.css.tabTextNode}).inject(this.tabNode);
  201. var names = [];
  202. this.members.each(function(m){
  203. names.push(m.name);
  204. });
  205. this.toNames = names;
  206. this.title = names.join(",");
  207. this.tabTextNode.set("text", this.title);
  208. this.tabNode.addEvents({
  209. "mouseover": function(){
  210. if (this.chat.current != this) this.tabNode.setStyles(this.css.chatTabNode_over);
  211. this.tabCloseAction.setStyle("display", "block");
  212. }.bind(this),
  213. "mouseout": function(){
  214. if (this.chat.current != this) this.tabNode.setStyles(this.css.chatTabNode);
  215. this.tabCloseAction.setStyle("display", "none");
  216. }.bind(this),
  217. "click": function(){this.setCurrent();}.bind(this)
  218. });
  219. this.tabCloseAction.addEvents({
  220. //"mouseover": function(){
  221. // this.tabCloseAction.setStyles(this.css.tabCloseAction_over);
  222. //}.bind(this),
  223. //"mouseout": function(){
  224. // this.tabCloseAction.setStyles(this.css.tabCloseAction);
  225. //}.bind(this),
  226. "click": function(e){
  227. this.close();
  228. e.stopPropagation();
  229. }.bind(this)
  230. });
  231. },
  232. close: function(){
  233. },
  234. setCurrent: function(){
  235. if (this.chat.current) this.chat.current.setUncurrent();
  236. this.tabNode.setStyles(this.css.chatTabNode_current);
  237. this.chatContentScrollNode.setStyle("display", "block");
  238. this.chat.chatTitleMemberNode.set("text", this.title);
  239. this.chat.setTitle(this.title);
  240. //this.chat.chatTitleAddMemberActionButton.setStyle("display", "block");
  241. //this.chat.chatTitleDelMemberActionButton.setStyle("display", "none");
  242. //if (this.chat.desktop.top.userPanel.chatData){
  243. // if (this.chat.desktop.top.userPanel.chatData.onlineList){
  244. // if (this.chat.desktop.top.userPanel.chatData.onlineList.indexOf(this.members[0].name)!=-1){
  245. // this.chat.chatTitleAddMemberActionButton.setStyle("display", "none");
  246. // this.chat.chatTitleDelMemberActionButton.setStyle("display", "block");
  247. // }
  248. // }
  249. //}
  250. this.chat.current = this;
  251. this.checkUnread();
  252. this.clearUnread();
  253. },
  254. checkUnread: function(){
  255. if (layout.desktop.widgets["IMIMWidget"]){
  256. var unShowMessage = layout.desktop.widgets["IMIMWidget"].unShowMessage;
  257. var key = this.members[0].name+this.owner.name;
  258. if (unShowMessage[key]){
  259. if (unShowMessage[key].length){
  260. unShowMessage[key].each(function(msg){
  261. this.showMessage(msg, msg.from);
  262. }.bind(this));
  263. }
  264. delete unShowMessage[key];
  265. layout.desktop.widgets["IMIMWidget"].setUnread();
  266. if (layout.desktop.apps["IM"]) layout.desktop.apps["IM"].checkUnread(this.members[0].name);
  267. }
  268. }
  269. },
  270. setUncurrent: function(){
  271. this.chatContentScrollNode.setStyle("display", "none");
  272. this.tabNode.setStyles(this.css.chatTabNode);
  273. this.chat.current = null;
  274. },
  275. showMessage: function(msg, from){
  276. var messageDate = new Date();
  277. if (!this.messageDate || ((messageDate.getTime()-this.messageDate.getTime()) > 120000)){
  278. var timeText = messageDate.format("%Y-%m-%d %H:%M");
  279. var timeNode = new Element("div", {"styles": this.css.messageTimeNode, "text": timeText}).inject(this.chatContentNode);
  280. this.messageDate = messageDate;
  281. }
  282. var messageNode = new Element("div", {"styles": this.css.messageNode}).inject(this.chatContentNode);
  283. var icon = "";
  284. var iconcss = "";
  285. var textcss = "";
  286. if (from){
  287. for (var i=0; i<this.members.length; i++){
  288. if (this.members[i].name==from) break;
  289. }
  290. if (this.members[i]){
  291. icon = this.getIcon(this.members[i]);
  292. iconCss = this.css.messageIconGetNode;
  293. textAreaCss = this.css.messageTextAreaGetNode;
  294. textCss = this.css.messageTextGetNode;
  295. }
  296. }else{
  297. icon = this.getIcon(this.owner);
  298. iconCss = this.css.messageIconSendNode;
  299. textAreaCss = this.css.messageTextAreaSendNode;
  300. textCss = this.css.messageTextSendNode;
  301. }
  302. var iconNode = new Element("div", {"styles": iconCss}).inject(messageNode);
  303. var iconImgNode = new Element("img", {"styles": this.css.messageIconImgNode, "src": icon}).inject(iconNode);
  304. var textAreaNode = new Element("div", {"styles": textAreaCss}).inject(messageNode);
  305. var text = msg.text.replace(/[\n+]/g, "<br/>");
  306. var textNode = new Element("div", {"styles": textCss, "html": text}).inject(textAreaNode);
  307. this.scroll.toElement(messageNode);
  308. //this.chatContentNode
  309. },
  310. getIcon: function(data){
  311. var icon = "";
  312. if (data.icon){
  313. icon = "data:image/png;base64,"+data.icon;
  314. }else{
  315. if (data.genderType=="f"){
  316. icon = "/x_component_Chat/$Main/"+this.chat.options.style+"/female.png";
  317. }else{
  318. icon = "/x_component_Chat/$Main/"+this.chat.options.style+"/man.png";
  319. }
  320. }
  321. return icon;
  322. },
  323. addUnreadMessage: function(data){
  324. if (!this.unreadNode) this.unreadNode = new Element("div", {"styles": this.css.userListUnreadNode, "text": "0"}).inject(this.tabNode, "bottom");
  325. var i = this.unreadNode.get("text").toInt()+1;
  326. this.unreadNode.set("text", i);
  327. //this.node.inject(this.container, "top");
  328. },
  329. clearUnread: function(){
  330. if (this.unreadNode){
  331. this.unreadNode.destroy();
  332. this.unreadNode = null;
  333. }
  334. }
  335. });