Main.js 16 KB

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