blob: 43adce404d8ad5bbf40cf4300b9e2f0b477f4a1b [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();
22
23 // post message
24 $('#message-post').click(function() {
25 var txt = $('#message-post-text').val();
26 if(txt) {
27 wn.call({
28 module:'utilities',
29 page:'messages',
30 method:'post',
31 args: {
32 txt: txt,
33 contact: erpnext.messages.contact
34 },
35 callback:function(r,rt) {
36 $('#message-post-text').val('')
37 erpnext.messages.list.run();
38 },
39 btn: this
40 });
41 }
42 });
43
44 // enable, disable button
45 $('#message-post-text').keyup(function() {
46 if($(this).val()) {
47 $('#message-post').attr('disabled', false);
48 } else {
49 $('#message-post').attr('disabled', true);
50 }
51 })
52}
53
54wn.pages.messages.onshow = function(wrapper) {
55 erpnext.messages.show();
56 setTimeout(erpnext.messages.refresh, 5000);
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +053057 $('#message-post-text').focus();
Rushabh Mehtafdea9662012-02-27 18:03:54 +053058}
59
60erpnext.messages = {
61 show: function() {
62 var contact = erpnext.messages.get_contact();
63
64 // can't send message to self
65 $(wn.pages.messages).find('.well').toggle(contact==user ? false : true);
66
67 $(wn.pages.messages).find('h1:first').html('Messages: '
68 + (user==contact ? 'From everyone' : wn.boot.user_fullnames[contact]))
69
70 erpnext.messages.contact = contact;
71 erpnext.messages.list.opts.args.contact = contact;
72 erpnext.messages.list.run();
73
74 },
75 // check for updates every 5 seconds if page is active
76 refresh: function() {
77 setTimeout(erpnext.messages.refresh, 10000);
78 if(page_body.cur_page_label != 'messages') return;
79 erpnext.messages.show();
80 },
81 get_contact: function() {
82 var route = location.hash;
83 if(route.indexOf('/')!=-1) {
84 var name = decodeURIComponent(route.split('/')[1]);
85 if(name.indexOf('__at__')!=-1) {
86 name = name.replace('__at__', '@');
87 }
88 return name;
89 }
90 return user;
91 },
92 make_list: function() {
93 erpnext.messages.list = new wn.widgets.Listing({
94 parent: $('#message-list').get(0),
95 method: 'utilities.page.messages.messages.get_list',
96 args: {
97 contact: null
98 },
99 render_row: function(wrapper, data) {
100 data.creation = dateutil.comment_when(data.creation);
101 data.comment_by_fullname = wn.boot.user_fullnames[data.owner];
102
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530103 data.reply_html = '';
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530104 if(data.owner==user) {
105 data.cls = 'message-self';
106 data.comment_by_fullname = 'You';
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530107 data.delete_html = repl('<a class="close" \
108 onclick="erpnext.messages.delete(this)"\
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530109 data-name="%(name)s">&times;</a>', data);
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530110 } else {
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530111 data.cls = 'message-other';
112 data.delete_html = '';
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530113 if(erpnext.messages.contact==user) {
114 data.reply_html = repl('<a href="#!messages/%(owner)s">\
115 <i class="icon-share-alt"></i> Reply</a>', data)
116 }
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530117 }
118
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530119 wrapper.innerHTML = repl('<div class="message %(cls)s">%(delete_html)s\
120 <b>%(comment)s</b>\
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530121 <div class="help">by %(comment_by_fullname)s, %(creation)s</div>\
122 %(reply_html)s</div>\
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530123 <div style="clear: both;"></div>', data);
124 }
125 });
126 },
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530127 delete: function(ele) {
128 $(ele).parent().css('opacity', 0.6);
129 wn.call({
130 method:'utilities.page.messages.messages.delete',
131 args: {name : $(ele).attr('data-name')},
132 callback: function() {
133 $(ele).parent().toggle(false);
134 }
135 });
136 },
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530137 show_active_users: function() {
138 wn.call({
139 module:'utilities',
140 page:'messages',
141 method:'get_active_users',
142 callback: function(r,rt) {
143 var $body = $(wn.pages.messages).find('.section-body');
144 for(var i in r.message) {
145 var p = r.message[i];
146 p.fullname = wn.boot.user_fullnames[p.name];
147 p.name = p.name.replace('@', '__at__');
148 $body.append(repl('<div class="section-item">\
149 <a href="#!messages/%(name)s">%(fullname)s</a></div>', p))
150 }
151 }
152 });
153 }
154}
155
156