Main.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. debugger;
  107. if (this.current){
  108. var text = this.chatTextAreaNode.get("value");
  109. if (text){
  110. debugger;
  111. //message = {
  112. // "messageType": "chat",
  113. // "personList": this.current.toNames,
  114. // "text": text
  115. //}
  116. message = {
  117. "text": text,
  118. "type": "text",
  119. "from": this.current.owner.name,
  120. "person": this.current.toNames[0],
  121. "category": "dialog"
  122. };
  123. this.desktop.socket.send(message);
  124. this.current.showMessage(message);
  125. }
  126. this.chatTextAreaNode.set("value", "");
  127. //this.addToCathList();
  128. }
  129. },
  130. setChatAreaHeight: function(){
  131. var size = this.chatContentAreaNode.getSize();
  132. var titleSize = this.chatTitleNode.getSize();
  133. var inputSize = this.chatInputAreaNode.getSize();
  134. var y = size.y - titleSize.y - inputSize.y;
  135. this.chatAreaNode.setStyle("height", ""+y+"px");
  136. },
  137. addDialogueBack: function(owner, members){
  138. var dialogue = new MWF.xApplication.Chat.Dialogue(owner, members, this);
  139. var key1 = owner.name+members[0].name;
  140. var key2 = members[0].name+owner.name;
  141. this.dialogues[key1] = dialogue;
  142. this.dialogues[key2] = dialogue;
  143. return dialogue;
  144. },
  145. addDialogue: function(owner, members){
  146. var dialogue = new MWF.xApplication.Chat.Dialogue(owner, members, this);
  147. var key1 = owner.name+members[0].name;
  148. var key2 = members[0].name+owner.name;
  149. this.dialogues[key1] = dialogue;
  150. this.dialogues[key2] = dialogue;
  151. dialogue.setCurrent();
  152. return dialogue;
  153. }
  154. });
  155. MWF.xApplication.Chat.Dialogue = new Class({
  156. initialize: function(owner, members, chat){
  157. this.chat = chat;
  158. this.owner = owner;
  159. this.members = members;
  160. this.css = this.chat.css;
  161. this.unreadCount = 0;
  162. this.messageDate = null;
  163. this.load();
  164. },
  165. load: function(){
  166. this.createTab();
  167. this.createContent();
  168. },
  169. createContent: function(){
  170. this.chatContentScrollNode = new Element("div", {"styles": this.css.chatContentScrollNode}).inject(this.chat.chatAreaNode);
  171. this.chatContentNode = new Element("div", {"styles": this.css.chatContentNode}).inject(this.chatContentScrollNode);
  172. this.scroll = new Fx.Scroll(this.chatContentScrollNode);
  173. MWF.require("MWF.widget.ScrollBar", function(){
  174. new MWF.widget.ScrollBar(this.chatContentScrollNode, {
  175. "style":"xDesktop_Message", "where": "before", "indent": false, "distance": 50, "friction": 6, "axis": {"x": false, "y": true}
  176. });
  177. }.bind(this));
  178. },
  179. createTab: function(){
  180. this.tabNode = new Element("div", {"styles": this.css.chatTabNode}).inject(this.chat.dialogueTabAreaNode);
  181. var icon = this.members[0].icon;
  182. if (this.members.length>1){
  183. icon = this.chat.path+this.chat.options.style+"/group.png";
  184. }else{
  185. if (!icon){
  186. if (this.members[0].genderType=="f"){
  187. icon = "/x_component_Chat/$Main/"+this.chat.options.style+"/female.png";
  188. }else{
  189. icon = "/x_component_Chat/$Main/"+this.chat.options.style+"/man.png";
  190. }
  191. }else{
  192. icon = "data:image/png;base64,"+icon;
  193. }
  194. }
  195. if (this.members[0].onlineStatus=="offline"){
  196. icon = MWF.grayscale(icon);
  197. }
  198. this.tabIconNode = new Element("div", {"styles": this.css.tabIconNode}).inject(this.tabNode);
  199. this.tabIconImgNode = new Element("img", {"styles": this.css.tabIconImgNode}).inject(this.tabIconNode);
  200. this.tabIconImgNode.set("src", icon);
  201. this.tabCloseAction = new Element("div", {"styles": this.css.tabCloseAction}).inject(this.tabNode);
  202. this.tabTextNode = new Element("div", {"styles": this.css.tabTextNode}).inject(this.tabNode);
  203. var names = [];
  204. this.members.each(function(m){
  205. names.push(m.name);
  206. });
  207. this.toNames = names;
  208. this.title = names.join(",");
  209. this.tabTextNode.set("text", this.title);
  210. this.tabNode.addEvents({
  211. "mouseover": function(){
  212. if (this.chat.current != this) this.tabNode.setStyles(this.css.chatTabNode_over);
  213. this.tabCloseAction.setStyle("display", "block");
  214. }.bind(this),
  215. "mouseout": function(){
  216. if (this.chat.current != this) this.tabNode.setStyles(this.css.chatTabNode);
  217. this.tabCloseAction.setStyle("display", "none");
  218. }.bind(this),
  219. "click": function(){this.setCurrent();}.bind(this)
  220. });
  221. this.tabCloseAction.addEvents({
  222. //"mouseover": function(){
  223. // this.tabCloseAction.setStyles(this.css.tabCloseAction_over);
  224. //}.bind(this),
  225. //"mouseout": function(){
  226. // this.tabCloseAction.setStyles(this.css.tabCloseAction);
  227. //}.bind(this),
  228. "click": function(e){
  229. this.close();
  230. e.stopPropagation();
  231. }.bind(this)
  232. });
  233. },
  234. close: function(){
  235. },
  236. setCurrent: function(){
  237. if (this.chat.current) this.chat.current.setUncurrent();
  238. this.tabNode.setStyles(this.css.chatTabNode_current);
  239. this.chatContentScrollNode.setStyle("display", "block");
  240. this.chat.chatTitleMemberNode.set("text", this.title);
  241. this.chat.setTitle(this.title);
  242. //this.chat.chatTitleAddMemberActionButton.setStyle("display", "block");
  243. //this.chat.chatTitleDelMemberActionButton.setStyle("display", "none");
  244. //if (this.chat.desktop.top.userPanel.chatData){
  245. // if (this.chat.desktop.top.userPanel.chatData.onlineList){
  246. // if (this.chat.desktop.top.userPanel.chatData.onlineList.indexOf(this.members[0].name)!=-1){
  247. // this.chat.chatTitleAddMemberActionButton.setStyle("display", "none");
  248. // this.chat.chatTitleDelMemberActionButton.setStyle("display", "block");
  249. // }
  250. // }
  251. //}
  252. this.chat.current = this;
  253. this.checkUnread();
  254. this.clearUnread();
  255. },
  256. checkUnread: function(){
  257. if (layout.desktop.widgets["IMIMWidget"]){
  258. var unShowMessage = layout.desktop.widgets["IMIMWidget"].unShowMessage;
  259. var key = this.members[0].name+this.owner.name;
  260. if (unShowMessage[key]){
  261. if (unShowMessage[key].length){
  262. unShowMessage[key].each(function(msg){
  263. this.showMessage(msg, msg.from);
  264. }.bind(this));
  265. }
  266. delete unShowMessage[key];
  267. layout.desktop.widgets["IMIMWidget"].setUnread();
  268. if (layout.desktop.apps["IM"]) layout.desktop.apps["IM"].checkUnread(this.members[0].name);
  269. }
  270. }
  271. },
  272. setUncurrent: function(){
  273. this.chatContentScrollNode.setStyle("display", "none");
  274. this.tabNode.setStyles(this.css.chatTabNode);
  275. this.chat.current = null;
  276. },
  277. showMessage: function(msg, from){
  278. var messageDate = new Date();
  279. if (!this.messageDate || ((messageDate.getTime()-this.messageDate.getTime()) > 120000)){
  280. var timeText = messageDate.format("%Y-%m-%d %H:%M");
  281. var timeNode = new Element("div", {"styles": this.css.messageTimeNode, "text": timeText}).inject(this.chatContentNode);
  282. this.messageDate = messageDate;
  283. }
  284. var messageNode = new Element("div", {"styles": this.css.messageNode}).inject(this.chatContentNode);
  285. var icon = "";
  286. var iconcss = "";
  287. var textcss = "";
  288. if (from){
  289. for (var i=0; i<this.members.length; i++){
  290. if (this.members[i].name==from) break;
  291. }
  292. if (this.members[i]){
  293. icon = this.getIcon(this.members[i]);
  294. iconCss = this.css.messageIconGetNode;
  295. textAreaCss = this.css.messageTextAreaGetNode;
  296. textCss = this.css.messageTextGetNode;
  297. }
  298. }else{
  299. icon = this.getIcon(this.owner);
  300. iconCss = this.css.messageIconSendNode;
  301. textAreaCss = this.css.messageTextAreaSendNode;
  302. textCss = this.css.messageTextSendNode;
  303. }
  304. var iconNode = new Element("div", {"styles": iconCss}).inject(messageNode);
  305. var iconImgNode = new Element("img", {"styles": this.css.messageIconImgNode, "src": icon}).inject(iconNode);
  306. var textAreaNode = new Element("div", {"styles": textAreaCss}).inject(messageNode);
  307. var text = msg.text.replace(/[\n+]/g, "<br/>");
  308. var textNode = new Element("div", {"styles": textCss, "html": text}).inject(textAreaNode);
  309. this.scroll.toElement(messageNode);
  310. //this.chatContentNode
  311. },
  312. getIcon: function(data){
  313. var icon = "";
  314. if (data.icon){
  315. icon = "data:image/png;base64,"+data.icon;
  316. }else{
  317. if (data.genderType=="f"){
  318. icon = "/x_component_Chat/$Main/"+this.chat.options.style+"/female.png";
  319. }else{
  320. icon = "/x_component_Chat/$Main/"+this.chat.options.style+"/man.png";
  321. }
  322. }
  323. return icon;
  324. },
  325. addUnreadMessage: function(data){
  326. if (!this.unreadNode) this.unreadNode = new Element("div", {"styles": this.css.userListUnreadNode, "text": "0"}).inject(this.tabNode, "bottom");
  327. var i = this.unreadNode.get("text").toInt()+1;
  328. this.unreadNode.set("text", i);
  329. //this.node.inject(this.container, "top");
  330. },
  331. clearUnread: function(){
  332. if (this.unreadNode){
  333. this.unreadNode.destroy();
  334. this.unreadNode = null;
  335. }
  336. }
  337. });