blob: 66835403288d00fb1583db98867cad8cd9cb7a3d [file] [log] [blame]
Rushabh Mehtaaaf86ba2012-02-28 17:40:13 +05301// 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
Rushabh Mehtafdea9662012-02-27 18:03:54 +053017wn.provide('erpnext.messages');
18
19wn.pages.messages.onload = function(wrapper) {
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053020 wn.ui.make_app_page({
21 parent: wrapper,
22 title: "Messages"
Rushabh Mehtafdea9662012-02-27 18:03:54 +053023 });
24
Rushabh Mehtab62bbc62012-10-03 18:32:10 +053025 $('<div><div class="avatar avatar-large">\
26 <img id="avatar-image" src="lib/images/ui/avatar.png"></div>\
27 <h3 style="display: inline-block" id="message-title">Everyone</h3>\
28 </div><hr>\
29 <div id="post-message">\
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053030 <textarea style="width: 100%; height: 24px;"></textarea>\
31 <div><button class="btn btn-small">Post</button></div><hr>\
32 </div>\
33 <div class="all-messages"></div>').appendTo($(wrapper).find('.layout-main-section'));
34
35 erpnext.messages = new erpnext.Messages(wrapper);
Rushabh Mehtaec406862012-11-13 10:59:11 +053036 erpnext.toolbar.set_new_comments(0);
Rushabh Mehtafdea9662012-02-27 18:03:54 +053037}
38
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +053039$(wn.pages.messages).bind('show', function() {
Rushabh Mehta3009c462012-10-03 11:56:38 +053040 // remove alerts
41 $('#alert-container .alert').remove();
42
Rushabh Mehtaec406862012-11-13 10:59:11 +053043 erpnext.toolbar.set_new_comments(0);
Rushabh Mehtafdea9662012-02-27 18:03:54 +053044 erpnext.messages.show();
Rushabh Mehtab62bbc62012-10-03 18:32:10 +053045 setTimeout("erpnext.messages.refresh()", 17000);
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +053046})
Rushabh Mehtafdea9662012-02-27 18:03:54 +053047
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053048erpnext.Messages = Class.extend({
49 init: function(wrapper) {
50 this.wrapper = wrapper;
51 this.show_active_users();
52 this.make_post_message();
53 this.make_list();
54 //this.update_messages('reset'); //Resets notification icons
55 },
56 make_post_message: function() {
57 var me = this;
58 $('#post-message textarea').keydown(function(e) {
59 if(e.which==13) {
60 $('#post-message .btn').click();
61 return false;
62 }
63 });
64
65 $('#post-message .btn').click(function() {
66 var txt = $('#post-message textarea').val();
67 if(txt) {
68 wn.call({
69 module:'utilities',
70 page:'messages',
71 method:'post',
72 args: {
73 txt: txt,
74 contact: me.contact
75 },
76 callback:function(r,rt) {
77 $('#post-message textarea').val('')
78 me.list.run();
79 },
80 btn: this
81 });
82 }
83 });
84 },
Rushabh Mehtafdea9662012-02-27 18:03:54 +053085 show: function() {
Rushabh Mehta3009c462012-10-03 11:56:38 +053086 var contact = this.get_contact() || this.contact || user;
Rushabh Mehtafdea9662012-02-27 18:03:54 +053087
Rushabh Mehtab62bbc62012-10-03 18:32:10 +053088 $('#message-title').html(contact==user ? "Everyone" :
89 wn.user_info(contact).fullname)
90
Rushabh Mehta24a69612012-12-11 15:58:19 +053091 $('#avatar-image').attr("src", wn.utils.get_file_link(wn.user_info(contact).image));
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053092
93 $("#show-everyone").toggle(contact!=user);
94
Rushabh Mehtab62bbc62012-10-03 18:32:10 +053095 $("#post-message button").text(contact==user ? "Post Publicly" : "Post to user")
96
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053097 this.contact = contact;
98 this.list.opts.args.contact = contact;
99 this.list.run();
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530100
101 },
102 // check for updates every 5 seconds if page is active
103 refresh: function() {
Rushabh Mehtab62bbc62012-10-03 18:32:10 +0530104 setTimeout("erpnext.messages.refresh()", 17000);
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +0530105 if(wn.container.page.label != 'Messages') return;
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530106 this.show();
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530107 },
108 get_contact: function() {
109 var route = location.hash;
110 if(route.indexOf('/')!=-1) {
111 var name = decodeURIComponent(route.split('/')[1]);
112 if(name.indexOf('__at__')!=-1) {
113 name = name.replace('__at__', '@');
114 }
115 return name;
116 }
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530117 },
118 make_list: function() {
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530119 this.list = new wn.ui.Listing({
120 parent: $(this.wrapper).find('.all-messages'),
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530121 method: 'utilities.page.messages.messages.get_list',
122 args: {
123 contact: null
124 },
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530125 hide_refresh: true,
126 no_loading: true,
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530127 render_row: function(wrapper, data) {
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +0530128 $(wrapper).removeClass('list-row');
129
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530130 data.creation = dateutil.comment_when(data.creation);
Rushabh Mehta204e77d2012-02-29 19:09:20 +0530131 data.comment_by_fullname = wn.user_info(data.owner).fullname;
Rushabh Mehta24a69612012-12-11 15:58:19 +0530132 data.image = wn.utils.get_file_link(wn.user_info(data.owner).image);
Rushabh Mehtab62bbc62012-10-03 18:32:10 +0530133 data.mark_html = "";
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530134
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530135 data.reply_html = '';
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530136 if(data.owner==user) {
137 data.cls = 'message-self';
138 data.comment_by_fullname = 'You';
139 } else {
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530140 data.cls = 'message-other';
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530141 }
142
Rushabh Mehta294778a2012-09-27 15:43:06 +0200143 // delete
144 data.delete_html = "";
145 if(data.owner==user || data.comment.indexOf("assigned to")!=-1) {
146 data.delete_html = repl('<a class="close" \
147 onclick="erpnext.messages.delete(this)"\
148 data-name="%(name)s">&times;</a>', data);
149 }
Rushabh Mehtab62bbc62012-10-03 18:32:10 +0530150
151 if(data.owner==data.comment_docname) {
152 data.mark_html = "<div class='message-mark' title='Public'\
153 style='background-color: green'></div>"
154 }
Rushabh Mehta294778a2012-09-27 15:43:06 +0200155
Rushabh Mehtab62bbc62012-10-03 18:32:10 +0530156 wrapper.innerHTML = repl('<div class="message %(cls)s">%(mark_html)s\
157 <span class="avatar avatar-small"><img src="%(image)s"></span><b>%(comment)s</b>\
158 %(delete_html)s\
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530159 <div class="help">by %(comment_by_fullname)s, %(creation)s</div>\
Rushabh Mehtab62bbc62012-10-03 18:32:10 +0530160 </div>\
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530161 <div style="clear: both;"></div>', data);
162 }
163 });
164 },
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530165 delete: function(ele) {
166 $(ele).parent().css('opacity', 0.6);
167 wn.call({
168 method:'utilities.page.messages.messages.delete',
169 args: {name : $(ele).attr('data-name')},
170 callback: function() {
171 $(ele).parent().toggle(false);
172 }
173 });
174 },
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530175 show_active_users: function() {
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530176 var me = this;
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530177 wn.call({
178 module:'utilities',
179 page:'messages',
180 method:'get_active_users',
181 callback: function(r,rt) {
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530182 var $body = $(me.wrapper).find('.layout-side-section');
Rushabh Mehtab62bbc62012-10-03 18:32:10 +0530183 $('<h4>Users</h4><hr>\
184 <div id="show-everyone">\
185 <a href="#messages/'+user+'" class="btn btn-small">\
186 Show messages from everyone</a><hr></div>\
187 ').appendTo($body);
188 r.message.sort(function(a, b) { return b.has_session - a.has_session; });
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530189 for(var i in r.message) {
190 var p = r.message[i];
Rushabh Mehta7f9c3692012-09-27 18:48:43 +0530191 if(p.name != user) {
192 p.fullname = wn.user_info(p.name).fullname;
Rushabh Mehta24a69612012-12-11 15:58:19 +0530193 p.image = wn.utils.get_file_link(wn.user_info(p.name).image);
Rushabh Mehta7f9c3692012-09-27 18:48:43 +0530194 p.name = p.name.replace('@', '__at__');
Rushabh Mehtab62bbc62012-10-03 18:32:10 +0530195 p.status_color = p.has_session ? "green" : "#ddd";
Rushabh Mehta7f9c3692012-09-27 18:48:43 +0530196 p.status = p.has_session ? "Online" : "Offline";
Rushabh Mehtab62bbc62012-10-03 18:32:10 +0530197 $(repl('<p>\
198 <span class="avatar avatar-small" \
199 style="border: 3px solid %(status_color)s" \
200 title="%(status)s"><img src="%(image)s"></span>\
Rushabh Mehta7f9c3692012-09-27 18:48:43 +0530201 <a href="#!messages/%(name)s">%(fullname)s</a>\
202 </p>', p))
203 .appendTo($body);
204 }
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530205 }
206 }
207 });
208 }
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530209});
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530210
211