Chat.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. MWF.xApplication.Execution.Chat = new Class({
  2. Extends: MWF.widget.Common,
  3. Implements: [Options, Events],
  4. options: {
  5. "style" : "default",
  6. "documentId" : ""
  7. },
  8. initialize: function (dialogContainer, editorContainer, app, actions, lp, options) {
  9. this.setOptions(options);
  10. this.dialogContainer = $(dialogContainer);
  11. this.editorContainer = $(editorContainer);
  12. this.app = app;
  13. this.actions = actions;
  14. this.lp = lp;
  15. this.userName = layout.desktop.session.user.name;
  16. this.userName = layout.desktop.session.user.distinguishedName;
  17. this.path = "/x_component_Execution/$Chat/"+this.options.style+"/";
  18. this.actionSettingPath = MWF.defaultPath+"/widget/$SimpleEditor/"+this.options.style+"/ActionSetting.js";
  19. this.cssPath = this.path + "css.wcss";
  20. this._loadCss();
  21. this.orgActionsAlpha = MWF.Actions.get("x_organization_assemble_control");
  22. },
  23. load: function () {
  24. this._loadEmotionSetting();
  25. if( this.dialogContainer ){
  26. this.dialogNode = new Element( "div.dialogNode", {
  27. "styles" : this.css.dialogNode
  28. }).inject(this.dialogContainer);
  29. //this.dialogContainer.setStyle("height","400px");
  30. var _self = this;
  31. MWF.require("MWF.widget.ScrollBar", function () {
  32. this.scrollObj = new MWF.widget.ScrollBar(this.dialogContainer, {
  33. "indent": false,
  34. "style": "default",
  35. "where": "before",
  36. "distance": 60,
  37. "friction": 4,
  38. "axis": {"x": false, "y": true},
  39. "onScroll": function (y) {
  40. //var scrollSize = _self.dialogContainer.getScrollSize();
  41. //var clientSize = _self.dialogContainer.getSize();
  42. //var scrollHeight = scrollSize.y - clientSize.y;
  43. //
  44. //if (y + 200 > scrollHeight && _self.loadDialog) {
  45. // if (! _self.isItemsLoaded) _self.loadDialog();
  46. //}
  47. if( y == 0 ){
  48. if (! _self.isItemsLoaded) _self.loadDialog();
  49. }
  50. }
  51. });
  52. //this.scrollObj.scrollVNode.setStyles({"margin-top":"300px"})
  53. }.bind(this), false);
  54. this.items = [];
  55. this.isItemsLoaded = false;
  56. this.isItemLoadding = false;
  57. this.loadDialog( function(){
  58. this.scrollToLater();
  59. }.bind(this) );
  60. }
  61. if(this.editorContainer){
  62. this.loadEditor( this.editorContainer, "" );
  63. }
  64. },
  65. scrollToLater : function(){
  66. setTimeout( function(){
  67. var clientSize = this.scrollObj.node.getSize();
  68. if( !this.scrollObj.scrollVNode )this.scrollObj.checkScroll();
  69. if( this.scrollObj.scrollVNode ){
  70. var scrollVNodeSize = this.scrollObj.scrollVNode.getSize();
  71. var maxY = (clientSize.y.toFloat())-(scrollVNodeSize.y.toFloat());
  72. this.scrollObj.scroll( maxY, null );
  73. }
  74. }.bind(this), 500 )
  75. },
  76. _loadEmotionSetting : function(){
  77. if( this.emotionSetting )return;
  78. var r = new Request({
  79. url: this.actionSettingPath,
  80. async: false,
  81. method: "get",
  82. onSuccess: function(responseText, responseXML){
  83. this.emotionSetting = MWF.widget.SimpleEditor.Actions.setting.emotion;
  84. }.bind(this),
  85. onFailure: function(xhr){
  86. alert(xhr.responseText);
  87. }
  88. });
  89. r.send();
  90. },
  91. loadDialog: function( callback ){
  92. if (!this.isItemsLoaded) {
  93. if (!this.isItemLoadding) {
  94. this.isItemLoadding = true;
  95. this.getCurrentPageData(function (json) { //alert(JSON.stringify(json))
  96. var length = json.count; //|| json.data.length;
  97. if (length <= this.items.length) {
  98. this.isItemsLoaded = true;
  99. }
  100. json.data.each(function (d) { //alert(JSON.stringify(d))
  101. this.loadDialogItem( d );
  102. }.bind(this));
  103. this.isItemLoadding = false;
  104. if( callback )callback();
  105. }.bind(this), 10 )
  106. }
  107. }
  108. //this.dialogContainer.scrollTo(0, 400 );
  109. //alert(this.dialogContainer.getScrollSize().y)
  110. },
  111. loadDialogItem: function( d , position){
  112. this.items.push(d.id);
  113. var isCurrentUser = ( d.senderName == this.userName );
  114. //var isCurrentUser = false
  115. var msg_li = new Element("div.msg_li", {"styles": this.css.msg_li}).inject(this.dialogNode, position || "top");
  116. var msg_item = new Element("div", {"styles": this.css.msg_item}).inject(msg_li);
  117. var msg_person = new Element("div", {
  118. "styles": this.css[isCurrentUser ? "msg_person_right" : "msg_person_left"]
  119. }).inject(msg_item);
  120. var msg_face = new Element("img", {
  121. "styles": this.css.msg_face
  122. }).inject(msg_person);
  123. this.setUserFace(d.senderName, msg_face);
  124. if (!isCurrentUser) {
  125. var msg_name = new Element("div", {
  126. "styles": this.css.msg_person_name,
  127. "text": d.senderName.split("@")[0]
  128. }).inject(msg_person)
  129. }
  130. var msg_arrow_left = new Element("div", {
  131. "styles": this.css[isCurrentUser ? "msg_arrow_right" : "msg_arrow_left"]
  132. }).inject(msg_item);
  133. var msg_content_body = new Element("div", {
  134. "styles": this.css[isCurrentUser ? "msg_content_body_right" : "msg_content_body_left"]
  135. }).inject(msg_item);
  136. //if (isCurrentUser) {
  137. // var msg_del = new Element("span", {
  138. // "styles": this.css.msg_del,
  139. // "title": "点击删除"
  140. // }).inject(msg_content_body);
  141. // msg_del.addEvent("click", function () {
  142. // if (confirm("删除后无法恢复,确定要删除该信息?")) {
  143. // this.deleteSingleMessage(docid, seq, jQuery(this).parents(".msg_li"));
  144. // }
  145. // }.bind(this))
  146. //}
  147. var msg_content_area = new Element("div", {
  148. "styles": this.css.msg_content_area
  149. }).inject(msg_content_body);
  150. var msg_content_text = new Element("p", {
  151. "styles": this.css.msg_content_text,
  152. "html": this.parseEmotion( d.content )
  153. }).inject(msg_content_area);
  154. var msg_content_time = new Element("p", {
  155. "styles": this.css.msg_content_time,
  156. "text": d.createTime
  157. }).inject(msg_content_area);
  158. },
  159. parseEmotion : function( content ){
  160. return content.replace(/\[emotion=(.*?)\]/g, function( a,b ){
  161. return "<img imagename='"+b+"' style='cursor:pointer;border:0;padding:2px;' " +" class='MWF_editor_emotion' src='"+ this.emotionSetting.imagesPath + b + this.emotionSetting.fileExt +"'>";
  162. }.bind(this));
  163. },
  164. //listMessageData: function( callback ){
  165. // var json = {
  166. // "type": "success",
  167. // "data": [
  168. // {
  169. // "id": "53a508ec-7862-4036-a273-c15830cd3f86",
  170. // "createTime": "2016-04-19 15:38:50",
  171. // "updateTime": "2016-04-19 15:38:50",
  172. // "sequence": "2016041915385053a508ec-7862-4036-a273-c15830cd3f46",
  173. // "content": "飞哥,找时间和孟总对一下设计,看看情况。飞哥,找时间和孟总对一下设计,看看情况飞哥,找时间和孟总对一下设计,看看情况飞哥,找时间和孟总对一下设计,看看情况",
  174. // "person": "李义"
  175. // },
  176. // {
  177. // "id": "53a508ec-7862-4036-a273-c15830cd3f87",
  178. // "createTime": "2016-04-19 15:38:50",
  179. // "updateTime": "2016-04-19 15:38:50",
  180. // "sequence": "2016041915385053a508ec-7862-4036-a273-c15830cd3f46",
  181. // "content": "下周我们就开始写代码.下周我们就开始写代码.下周我们就开始写代码.下周我们就开始写代码.下周我们就开始写代码下周我们就开始写代码.",
  182. // "person": "金飞"
  183. // }
  184. // ],
  185. // "date": "2016-05-27 14:20:07",
  186. // "spent": 2,
  187. // "size": 2,
  188. // "count": 0,
  189. // "position": 0,
  190. // "message": ""
  191. // }
  192. //
  193. //},
  194. getCurrentPageData:function(callback,count){
  195. if (!count)count = 5;
  196. var id = (this.items && this.items.length) ? this.items[this.items.length - 1] : "(0)";
  197. var filter = {"workId":this.options.workId} || {};
  198. this.actions.getChatListNext(id, count, filter, function (json) { //alert("action="+JSON.stringify(json))
  199. if (callback) callback(json);
  200. }.bind(this),null,false)
  201. },
  202. setUserFace: function(userName, faceNode ){
  203. faceNode.set("src",this.orgActionsAlpha.getPersonIcon(userName));
  204. //this.orgActionsAlpha.getPersonIcon(userName, function(url){
  205. // faceNode.set("src",url);
  206. // //var json = { data : { icon : url } };
  207. // //this.userCache[ name ] = json;
  208. // //if( callback )callback( json );
  209. //}.bind(this), function(){ alert("err");
  210. // //var json = { data : { icon : "/x_component_ForumDocument/$Main/"+this.options.style+"/icon/noavatar_big.gif" } };
  211. // //this.userCache[ name ] = json;
  212. // //if( callback )callback( json );
  213. //}.bind(this));
  214. //this.getUserData( userName, function( userData ){
  215. // var icon;
  216. // if( userData.icon ){
  217. // icon = "data:image/png;base64,"+userData.icon;
  218. // }else{
  219. // icon = this.path+ ( userData.genderType=="f" ? "female.png" : "man.png");
  220. // }
  221. // faceNode.set("src", icon );
  222. //})
  223. },
  224. //getUserData : function( userName, callback ){
  225. // this.userData = this.userData || {};
  226. // if( this.userData[userName] ){
  227. // if(callback)callback(this.userData[userName])
  228. // }else{
  229. // this.actions.getPerson(function(json){
  230. // this.userData[userName] = json.data;
  231. // if(callback)callback(json.data)
  232. // }.bind(this),null,userName,false)
  233. // }
  234. //},
  235. sendMessage : function(content, callback){
  236. var d = {
  237. "workId" : this.options.workId,
  238. "createTime": new Date().format("db"),
  239. "content": content,
  240. "targetIdentity": this.app.identity,
  241. "senderName" : this.userName
  242. };
  243. this.actions.submitChat(d, function(json){
  244. }.bind(this),
  245. function(xhr,text,error){
  246. var errorText = error;
  247. if (xhr) errorMessage = xhr.responseText;
  248. var e = JSON.parse(errorMessage);
  249. if(e.message){
  250. this.app.notice( e.message,"error");
  251. }else{
  252. this.app.notice( errorText,"error");
  253. }
  254. }.bind(this),false);
  255. this.loadDialogItem(d, "bottom");
  256. if(callback)callback();
  257. },
  258. loadEditor: function ( container, data ) {
  259. MWF.require("MWF.widget.SimpleEditor", function () {
  260. this.editor = new MWF.widget.SimpleEditor({
  261. "style": "chatReceive", //使用的样式文件夹
  262. "hasHeadNode" : false,
  263. "hasTitleNode": false, //是否有标题区
  264. "editorDisabled": false, //编辑区是否能进行编辑
  265. "hasToolbar": true, //是否生成操作条
  266. "toolbarDisable": false, //操作条是否失效
  267. "hasSubmit": true, //是否形成提交按钮
  268. "submitDisable": false, //提交按钮是否失效
  269. "hasCustomArea": true, //是否有提示区
  270. "paragraphise": false, //回车是否形成段落
  271. "minHeight": 100, //最小高度
  272. "maxHeight": 100, //编辑区的最大高度
  273. "overFlow": "visible", //可选项为 visible, auto 和 max ,visible 高度随内容变化, auto 内容高度超过minHeight时滚动条,max 内容高度超过maxHeight时滚动条(ie6 和 文档模式为杂项时,失效)
  274. "width": "100%", //编辑器的宽度
  275. "action": "Emotion", //操作条上面有哪些操作,如果是all,表示使用 toolbarItems.json 里配置的所有操作,否则传入操作的 action ,用 空格隔开,比如 "Image | Emotion"
  276. "limit": 255, //输入长度限制,0表示无限制
  277. "onQueryLoad": function () {
  278. return true;
  279. },
  280. "onPostLoad": function ( editor ) {
  281. editor.setCustomInfo("");
  282. },
  283. "onSubmitForm": function ( editor ) {
  284. var content = editor.getContent(true);
  285. if( content.trim() != "<br>" && content.trim()!=""){
  286. this.sendMessage( content, function(){
  287. editor.setContent("");
  288. this.scrollToLater();
  289. }.bind(this));
  290. }else{
  291. this.app.notice("不能发送空消息","error");
  292. }
  293. }.bind(this)
  294. }, container, data||"", null, null);
  295. this.editor.load();
  296. }.bind(this));
  297. }
  298. });