blob: 693779616855f1a966c9d4f250d889775cd74148 [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) {
20 erpnext.messages.show_active_users();
21 erpnext.messages.make_list();
Anand Doshi08c8edb2012-03-01 14:53:05 +053022 update_messages('reset'); //Resets notification icons
Rushabh Mehtafdea9662012-02-27 18:03:54 +053023
24 // post message
25 $('#message-post').click(function() {
26 var txt = $('#message-post-text').val();
27 if(txt) {
28 wn.call({
29 module:'utilities',
30 page:'messages',
31 method:'post',
32 args: {
33 txt: txt,
34 contact: erpnext.messages.contact
35 },
36 callback:function(r,rt) {
37 $('#message-post-text').val('')
38 erpnext.messages.list.run();
39 },
40 btn: this
41 });
42 }
43 });
44
45 // enable, disable button
46 $('#message-post-text').keyup(function() {
47 if($(this).val()) {
48 $('#message-post').attr('disabled', false);
49 } else {
50 $('#message-post').attr('disabled', true);
51 }
52 })
53}
54
55wn.pages.messages.onshow = function(wrapper) {
56 erpnext.messages.show();
57 setTimeout(erpnext.messages.refresh, 5000);
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +053058 $('#message-post-text').focus();
Rushabh Mehtafdea9662012-02-27 18:03:54 +053059}
60
61erpnext.messages = {
62 show: function() {
63 var contact = erpnext.messages.get_contact();
64
65 // can't send message to self
66 $(wn.pages.messages).find('.well').toggle(contact==user ? false : true);
67
68 $(wn.pages.messages).find('h1:first').html('Messages: '
Rushabh Mehta204e77d2012-02-29 19:09:20 +053069 + (user==contact ? 'From everyone' : wn.user_info(contact).fullname));
Rushabh Mehtafdea9662012-02-27 18:03:54 +053070
71 erpnext.messages.contact = contact;
72 erpnext.messages.list.opts.args.contact = contact;
73 erpnext.messages.list.run();
74
75 },
76 // check for updates every 5 seconds if page is active
77 refresh: function() {
78 setTimeout(erpnext.messages.refresh, 10000);
79 if(page_body.cur_page_label != 'messages') return;
80 erpnext.messages.show();
81 },
82 get_contact: function() {
83 var route = location.hash;
84 if(route.indexOf('/')!=-1) {
85 var name = decodeURIComponent(route.split('/')[1]);
86 if(name.indexOf('__at__')!=-1) {
87 name = name.replace('__at__', '@');
88 }
89 return name;
90 }
91 return user;
92 },
93 make_list: function() {
94 erpnext.messages.list = new wn.widgets.Listing({
95 parent: $('#message-list').get(0),
96 method: 'utilities.page.messages.messages.get_list',
97 args: {
98 contact: null
99 },
100 render_row: function(wrapper, data) {
101 data.creation = dateutil.comment_when(data.creation);
Rushabh Mehta204e77d2012-02-29 19:09:20 +0530102 data.comment_by_fullname = wn.user_info(data.owner).fullname;
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530103
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530104 data.reply_html = '';
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530105 if(data.owner==user) {
106 data.cls = 'message-self';
107 data.comment_by_fullname = 'You';
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530108 data.delete_html = repl('<a class="close" \
109 onclick="erpnext.messages.delete(this)"\
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530110 data-name="%(name)s">&times;</a>', data);
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530111 } else {
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530112 data.cls = 'message-other';
113 data.delete_html = '';
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530114 if(erpnext.messages.contact==user) {
115 data.reply_html = repl('<a href="#!messages/%(owner)s">\
116 <i class="icon-share-alt"></i> Reply</a>', data)
117 }
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530118 }
119
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530120 wrapper.innerHTML = repl('<div class="message %(cls)s">%(delete_html)s\
121 <b>%(comment)s</b>\
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530122 <div class="help">by %(comment_by_fullname)s, %(creation)s</div>\
123 %(reply_html)s</div>\
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530124 <div style="clear: both;"></div>', data);
125 }
126 });
127 },
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530128 delete: function(ele) {
129 $(ele).parent().css('opacity', 0.6);
130 wn.call({
131 method:'utilities.page.messages.messages.delete',
132 args: {name : $(ele).attr('data-name')},
133 callback: function() {
134 $(ele).parent().toggle(false);
135 }
136 });
137 },
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530138 show_active_users: function() {
139 wn.call({
140 module:'utilities',
141 page:'messages',
142 method:'get_active_users',
143 callback: function(r,rt) {
144 var $body = $(wn.pages.messages).find('.section-body');
145 for(var i in r.message) {
146 var p = r.message[i];
Rushabh Mehta204e77d2012-02-29 19:09:20 +0530147 p.fullname = wn.user_info(p.name).fullname;
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530148 p.name = p.name.replace('@', '__at__');
149 $body.append(repl('<div class="section-item">\
150 <a href="#!messages/%(name)s">%(fullname)s</a></div>', p))
151 }
152 }
153 });
154 }
155}
156
157