Message.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. MWF.xDesktop.Message = new Class({
  2. Implements: [Options, Events],
  3. options: {
  4. "style": "default"
  5. },
  6. initialize: function(desktop, options){
  7. this.setOptions(options);
  8. this.desktop = desktop;
  9. this.container = this.desktop.desktopNode;
  10. this.actionNode = this.desktop.top.messageActionNode;
  11. this.css = this.desktop.css;
  12. this.items = [];
  13. this.isShow = false;
  14. this.isMorph = false;
  15. this.unread = 0;
  16. },
  17. load: function(){
  18. this.maskNode = new Element("iframe", {"styles": this.css.messageContainerMaskNode});
  19. this.maskNode.setStyles({
  20. "border": "0px",
  21. "margin": "0px",
  22. "padding": "0px"
  23. });
  24. this.maskNode.inject(this.container);
  25. this.node = new Element("div", {"styles": this.css.messageContainerNode});
  26. this.node.inject(this.container);
  27. this.scrollNode = new Element("div", {"styles": this.css.messageContainersScrollNode}).inject(this.node);
  28. this.contentNode = new Element("div", {"styles": this.css.messageContainerContentNode}).inject(this.scrollNode);
  29. this.operationNode = new Element("div", {"styles": this.css.messageContainersOperationNode}).inject(this.node);
  30. this.clearAction = new Element("div", {"styles": this.css.messageContainersClearActionNode, "text": MWF.LP.desktop.clearMessage}).inject(this.operationNode);
  31. this.clearAction.addEvents({
  32. "mouseover": function(){this.clearAction.setStyles(this.css.messageContainersClearActionNode_over);}.bind(this),
  33. "mouseout": function(){this.clearAction.setStyles(this.css.messageContainersClearActionNode);}.bind(this),
  34. "mousedown": function(){this.clearAction.setStyles(this.css.messageContainersClearActionNode_down);}.bind(this),
  35. "mouseup": function(){this.clearAction.setStyles(this.css.messageContainersClearActionNode_over);}.bind(this),
  36. "click": function(){this.clearMessage();}.bind(this)
  37. });
  38. MWF.require("MWF.widget.ScrollBar", function(){
  39. new MWF.widget.ScrollBar(this.scrollNode, {
  40. "style":"xDesktop_Message", "where": "before", "indent": false, "distance": 100, "friction": 6, "axis": {"x": false, "y": true}
  41. });
  42. }.bind(this));
  43. this.node.addEvent("click", function(e){
  44. e.stopPropagation();
  45. e.preventDefault();
  46. });
  47. this.setPosition();
  48. this.hideMessage = function(){
  49. this.hide();
  50. }.bind(this);
  51. },
  52. clearMessage: function(){
  53. var clearItems = [];
  54. this.items.each(function(item){
  55. if (item.status!="progress") clearItems.push(item);
  56. }.bind(this));
  57. while (clearItems.length){
  58. clearItems[0].close();
  59. clearItems.erase(clearItems[0]);
  60. }
  61. },
  62. setPosition: function(){
  63. var size = this.container.getSize();
  64. var position = this.container.getPosition();
  65. this.maskNode.setStyle("height", ""+size.y+"px");
  66. this.node.setStyle("height", ""+size.y+"px");
  67. var y = size.y - 40;
  68. this.scrollNode.setStyle("height", ""+y+"px");
  69. this.maskNode.position({
  70. relativeTo: this.container,
  71. position: "rightTop",
  72. edge: (this.isShow) ? "rightTop" : "leftTop"
  73. // offset: {"x": 0, "y": -100}
  74. });
  75. this.node.position({
  76. relativeTo: this.container,
  77. position: "rightTop",
  78. edge: (this.isShow) ? "rightTop" : "leftTop"
  79. // offset: {"x": 0, "y": -100}
  80. });
  81. },
  82. show: function(){
  83. var index = MWF.xDesktop.zIndexPool.zIndex
  84. this.css.messageContainerMaskNode["z-index"] = index;
  85. this.css.messageContainerNode["z-index"] = index;
  86. if (!this.isMorph){
  87. this.isMorph = true;
  88. this.setPosition();
  89. if (!this.morph){
  90. this.maskMorph = new Fx.Morph(this.maskNode, {
  91. duration: "200",
  92. transition: Fx.Transitions.Sine.easeOut
  93. });
  94. this.morph = new Fx.Morph(this.node, {
  95. duration: "200",
  96. transition: Fx.Transitions.Sine.easeOut
  97. });
  98. }
  99. this.maskNode.setStyle("display", "block");
  100. this.node.setStyle("display", "block");
  101. var position = this.node.getPosition();
  102. var size = this.node.getSize();
  103. var left = position.x-size.x;
  104. this.maskMorph.start({"left": ""+left+"px", "z-index":index});
  105. this.morph.start({"left": ""+left+"px", "z-index":index}).chain(function(){
  106. this.isShow = true;
  107. this.isMorph = false;
  108. this.desktop.node.addEvent("click", this.hideMessage);
  109. }.bind(this));
  110. }
  111. },
  112. hide: function(){
  113. if (!this.isMorph){
  114. this.isMorph = true;
  115. if (!this.morph){
  116. this.maskMorph = new Fx.Morph(this.maskNode, {
  117. duration: "200",
  118. transition: Fx.Transitions.Sine.easeOut
  119. });
  120. this.morph = new Fx.Morph(this.node, {
  121. duration: "200",
  122. transition: Fx.Transitions.Sine.easeOut
  123. });
  124. }
  125. var position = this.node.getPosition();
  126. var size = this.node.getSize();
  127. var left = position.x+size.x;
  128. this.maskMorph.start({"left": ""+left+"px"}).chain(function(){
  129. this.maskNode.setStyle("display", "none");
  130. }.bind(this));
  131. this.morph.start({"left": ""+left+"px"}).chain(function(){
  132. this.node.setStyle("display", "none");
  133. this.isShow = false;
  134. this.isMorph = false;
  135. this.desktop.node.removeEvent("click", this.hideMessage);
  136. }.bind(this));
  137. }
  138. },
  139. resize: function(){
  140. this.setPosition();
  141. },
  142. addMessage: function(msg, time){
  143. var showTime = (time) ? (new Date()).parse(time) : new Date();
  144. var item = new MWF.xDesktop.Message.Item(this,msg, showTime);
  145. this.items.push(item);
  146. this.addUnread();
  147. return item;
  148. },
  149. addTooltip: function(msg, time){
  150. var showTime = (time) ? (new Date()).parse(time) : new Date();
  151. var tooltop = new MWF.xDesktop.Message.Tooltip(this,msg,showTime);
  152. return tooltop;
  153. },
  154. getUnread: function(){
  155. //获取未读消息列表和数量
  156. return this.unread || 0;
  157. },
  158. setUnread: function(){
  159. //this.actionNode
  160. if (this.unread>0){
  161. if (!this.unreadNode){
  162. this.unreadNode = new Element("div", {"styles": this.css.messageUnreadCountNode}).inject(this.actionNode);
  163. }
  164. this.unreadNode.set("text", this.unread);
  165. }else{
  166. if (this.unreadNode){
  167. this.unreadNode.destroy();
  168. this.unreadNode = null;
  169. delete this.unreadNode;
  170. }
  171. }
  172. },
  173. addUnread: function(count){
  174. var addCount = count || 1;
  175. this.unread = this.unread+addCount;
  176. this.setUnread();
  177. }
  178. });
  179. MWF.xDesktop.Message.Item = new Class({
  180. Implements: [Events],
  181. initialize: function(message, msg, showTime){
  182. this.message = message;
  183. this.container = this.message.contentNode;
  184. this.css = this.message.css;
  185. this.msg = msg;
  186. this.showTime = showTime;
  187. // msg = {
  188. // "subject": "",
  189. // "content": "",
  190. // "type": "",
  191. // "action": {
  192. //
  193. // }
  194. // };
  195. this.load();
  196. },
  197. load: function(){
  198. this.node = new Element("div", {"styles": this.css.messageItemNode});
  199. this.topNode = new Element("div", {"styles": this.css.messageItemTopNode}).inject(this.node);
  200. this.closeNode = new Element("div", {"styles": this.css.messageItemCloseNode}).inject(this.topNode);
  201. this.subjectNode = new Element("div", {"styles": this.css.messageItemSubjectNode}).inject(this.topNode);
  202. this.contentNode = new Element("div", {"styles": this.css.messageItemContentNode}).inject(this.node);
  203. this.bottomNode = new Element("div", {"styles": this.css.messageItemBottomNode}).inject(this.node);
  204. this.dateNode = new Element("div", {"styles": this.css.messageItemDateNode}).inject(this.bottomNode);
  205. this.actionsNode = new Element("div", {"styles": this.css.messageItemActionsNode}).inject(this.bottomNode);
  206. this.subjectNode.set({"text": this.msg.subject, "title": this.msg.subject});
  207. this.contentNode.set({"html": this.msg.content});
  208. this.contentNode.set({"title": this.contentNode.get("text")});
  209. this.dateNode.set("text", this.showTime.format("db"));
  210. this.node.inject(this.container, "top");
  211. this.setEvent();
  212. },
  213. setEvent: function(){
  214. this.closeNode.addEvents({
  215. "click": function(e){
  216. this.close(null, e);
  217. }.bind(this)
  218. });
  219. },
  220. close: function(callback, e){
  221. //flag = true;
  222. //this.fireEvent("close", [flag, e]);
  223. //alert(flag);
  224. //if (flag){
  225. this.closeItem(callback, e);
  226. //}
  227. },
  228. closeItem: function(callback){
  229. var morph = new Fx.Morph(this.node, {
  230. duration: "200"
  231. // transition: Fx.Transitions.Sine.easeOut
  232. });
  233. var size = this.node.getSize();
  234. this.node.setStyle("height", ""+size.y+"px");
  235. this.message.items.erase(this);
  236. morph.start({
  237. "opacity": 0,
  238. "height": "0px"
  239. }).chain(function(){
  240. this.message.addUnread(-1);
  241. this.node.destroy();
  242. if (callback) callback();
  243. delete this;
  244. }.bind(this));
  245. }
  246. });
  247. MWF.xDesktop.Message.Item.tooltips = [];
  248. MWF.xDesktop.Message.tooltipNode = null;
  249. MWF.xDesktop.Message.Tooltip = new Class({
  250. Extends: MWF.xDesktop.Message.Item,
  251. setEvent: function(){
  252. if (!MWF.xDesktop.Message.tooltipNode){
  253. MWF.xDesktop.Message.tooltipNode = new Element("div", {
  254. "styles": this.css.messageTooltipAreaNode
  255. }).inject(this.message.container);
  256. var toNode = this.message.desktop.desktopNode;
  257. MWF.xDesktop.Message.tooltipNode.position({
  258. relativeTo: toNode,
  259. position: "rightTop",
  260. edge: "rightTop"
  261. });
  262. }
  263. this.node.inject(MWF.xDesktop.Message.tooltipNode);
  264. this.node.setStyles(this.css.messageTooltipNode);
  265. //if (MWF.xDesktop.Message.Item.tooltips.length){
  266. // var toNode = MWF.xDesktop.Message.Item.tooltips[MWF.xDesktop.Message.Item.tooltips.length-1].node;
  267. // this.node.position({
  268. // relativeTo: toNode,
  269. // position: "centerBottom",
  270. // edge: "centerTop",
  271. // offset: {"x": 0, "y": 10}
  272. // });
  273. //}else{
  274. // var toNode = this.message.desktop.desktopNode;
  275. // this.node.position({
  276. // relativeTo: toNode,
  277. // position: "rightTop",
  278. // edge: "rightTop",
  279. // offset: {"x": -10, "y": 10}
  280. // });
  281. //}
  282. MWF.xDesktop.Message.Item.tooltips.push(this);
  283. window.setTimeout(function(){
  284. this.close(function(){
  285. MWF.xDesktop.Message.Item.tooltips.erase(this);
  286. }.bind(this));
  287. }.bind(this), 10000);
  288. this.closeNode.addEvents({
  289. "click": function(){
  290. this.close(function(){
  291. MWF.xDesktop.Message.Item.tooltips.erase(this);
  292. }.bind(this));
  293. }.bind(this)
  294. });
  295. },
  296. closeItem: function(callback){
  297. var morph = new Fx.Morph(this.node, {
  298. duration: "200"
  299. // transition: Fx.Transitions.Sine.easeOut
  300. });
  301. var size = this.node.getSize();
  302. this.node.setStyle("height", ""+size.y+"px");
  303. this.message.items.erase(this);
  304. morph.start({
  305. "opacity": 0,
  306. "height": "0px"
  307. }).chain(function(){
  308. //this.message.addUnread(-1);
  309. this.node.destroy();
  310. if (callback) callback();
  311. delete this;
  312. }.bind(this));
  313. }
  314. });