You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

messages.js 6.2 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. // ERPNext - web based ERP (http://erpnext.com)
  2. // Copyright (C) 2012 Web Notes Technologies Pvt Ltd
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. wn.provide('erpnext.messages');
  17. wn.pages.messages.onload = function(wrapper) {
  18. wn.ui.make_app_page({
  19. parent: wrapper,
  20. title: "Messages"
  21. });
  22. $('<div><div class="avatar avatar-large">\
  23. <img id="avatar-image" src="lib/images/ui/avatar.png"></div>\
  24. <h3 style="display: inline-block" id="message-title">Everyone</h3>\
  25. </div><hr>\
  26. <div id="post-message">\
  27. <textarea style="width: 100%; height: 64px; margin-bottom: 7px;"></textarea>\
  28. <div><button class="btn btn-default">Post</button></div><hr>\
  29. </div>\
  30. <div class="all-messages"></div>').appendTo($(wrapper).find('.layout-main-section'));
  31. wrapper.appframe.add_module_icon("Messages");
  32. erpnext.messages = new erpnext.Messages(wrapper);
  33. erpnext.toolbar.set_new_comments(0);
  34. }
  35. $(wn.pages.messages).bind('show', function() {
  36. // remove alerts
  37. $('#alert-container .alert').remove();
  38. erpnext.toolbar.set_new_comments(0);
  39. erpnext.messages.show();
  40. setTimeout("erpnext.messages.refresh()", 5000);
  41. })
  42. erpnext.Messages = Class.extend({
  43. init: function(wrapper) {
  44. this.wrapper = wrapper;
  45. this.show_active_users();
  46. this.make_post_message();
  47. this.make_list();
  48. //this.update_messages('reset'); //Resets notification icons
  49. },
  50. make_post_message: function() {
  51. var me = this;
  52. $('#post-message .btn').click(function() {
  53. var txt = $('#post-message textarea').val();
  54. if(txt) {
  55. wn.call({
  56. module:'core',
  57. page:'messages',
  58. method:'post',
  59. args: {
  60. txt: txt,
  61. contact: me.contact
  62. },
  63. callback:function(r,rt) {
  64. $('#post-message textarea').val('')
  65. me.list.run();
  66. },
  67. btn: this
  68. });
  69. }
  70. });
  71. },
  72. show: function() {
  73. var contact = this.get_contact() || this.contact || user;
  74. $('#message-title').html(contact===user ? "Everyone" :
  75. wn.user_info(contact).fullname)
  76. $('#avatar-image').attr("src", wn.utils.get_file_link(wn.user_info(contact).image));
  77. $("#show-everyone").toggle(contact!==user);
  78. $("#post-message button").text(contact==user ? "Post Publicly" : "Post to user")
  79. this.contact = contact;
  80. this.list.opts.args.contact = contact;
  81. this.list.run();
  82. },
  83. // check for updates every 5 seconds if page is active
  84. refresh: function() {
  85. setTimeout("erpnext.messages.refresh()", 5000);
  86. if(wn.container.page.label != 'Messages')
  87. return;
  88. if(!wn.session_alive)
  89. return;
  90. this.show();
  91. },
  92. get_contact: function() {
  93. var route = location.hash;
  94. if(route.indexOf('/')!=-1) {
  95. var name = decodeURIComponent(route.split('/')[1]);
  96. if(name.indexOf('__at__')!=-1) {
  97. name = name.replace('__at__', '@');
  98. }
  99. return name;
  100. }
  101. },
  102. make_list: function() {
  103. this.list = new wn.ui.Listing({
  104. parent: $(this.wrapper).find('.all-messages'),
  105. method: 'core.page.messages.messages.get_list',
  106. args: {
  107. contact: null
  108. },
  109. hide_refresh: true,
  110. no_loading: true,
  111. render_row: function(wrapper, data) {
  112. $(wrapper).removeClass('list-row');
  113. data.creation = dateutil.comment_when(data.creation);
  114. data.comment_by_fullname = wn.user_info(data.owner).fullname;
  115. data.image = wn.utils.get_file_link(wn.user_info(data.owner).image);
  116. data.mark_html = "";
  117. data.reply_html = '';
  118. if(data.owner==user) {
  119. data.cls = 'message-self';
  120. data.comment_by_fullname = 'You';
  121. } else {
  122. data.cls = 'message-other';
  123. }
  124. // delete
  125. data.delete_html = "";
  126. if(data.owner==user || data.comment.indexOf("assigned to")!=-1) {
  127. data.delete_html = repl('<a class="close" \
  128. onclick="erpnext.messages.delete(this)"\
  129. data-name="%(name)s">&times;</a>', data);
  130. }
  131. if(data.owner==data.comment_docname && data.parenttype!="Assignment") {
  132. data.mark_html = "<div class='message-mark' title='Public'\
  133. style='background-color: green'></div>"
  134. }
  135. wrapper.innerHTML = repl('<div class="message %(cls)s">%(mark_html)s\
  136. <span class="avatar avatar-small"><img src="%(image)s"></span><b>%(comment)s</b>\
  137. %(delete_html)s\
  138. <div class="help">by %(comment_by_fullname)s, %(creation)s</div>\
  139. </div>\
  140. <div style="clear: both;"></div>', data);
  141. }
  142. });
  143. },
  144. delete: function(ele) {
  145. $(ele).parent().css('opacity', 0.6);
  146. wn.call({
  147. method:'core.page.messages.messages.delete',
  148. args: {name : $(ele).attr('data-name')},
  149. callback: function() {
  150. $(ele).parent().toggle(false);
  151. }
  152. });
  153. },
  154. show_active_users: function() {
  155. var me = this;
  156. wn.call({
  157. module:'core',
  158. page:'messages',
  159. method:'get_active_users',
  160. callback: function(r,rt) {
  161. var $body = $(me.wrapper).find('.layout-side-section');
  162. $('<h4>Users</h4><hr>\
  163. <div id="show-everyone">\
  164. <a href="#messages/'+user+'" class="btn btn-default">\
  165. Messages from everyone</a><hr></div>\
  166. ').appendTo($body);
  167. $("#show-everyone").toggle(me.contact!==user);
  168. r.message.sort(function(a, b) { return b.has_session - a.has_session; });
  169. for(var i in r.message) {
  170. var p = r.message[i];
  171. if(p.name != user) {
  172. p.fullname = wn.user_info(p.name).fullname;
  173. p.image = wn.utils.get_file_link(wn.user_info(p.name).image);
  174. p.name = p.name.replace('@', '__at__');
  175. p.status_color = p.has_session ? "green" : "#ddd";
  176. p.status = p.has_session ? "Online" : "Offline";
  177. $(repl('<p>\
  178. <span class="avatar avatar-small" \
  179. style="border: 3px solid %(status_color)s" \
  180. title="%(status)s"><img src="%(image)s"></span>\
  181. <a href="#!messages/%(name)s">%(fullname)s</a>\
  182. </p>', p))
  183. .appendTo($body);
  184. }
  185. }
  186. }
  187. });
  188. }
  189. });