blob: 9283f9430f443df78b167854553fca147a7b0145 [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();
Rushabh Mehta39489272012-05-03 10:44:44 +053022 erpnext.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
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +053046 $('#message-post-text').keyup(function(e) {
Rushabh Mehtafdea9662012-02-27 18:03:54 +053047 if($(this).val()) {
48 $('#message-post').attr('disabled', false);
49 } else {
50 $('#message-post').attr('disabled', true);
51 }
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +053052
53 if(e.which==13) {
54 $('#message-post').click();
55 }
Rushabh Mehtafdea9662012-02-27 18:03:54 +053056 })
57}
58
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +053059$(wn.pages.messages).bind('show', function() {
Rushabh Mehtafdea9662012-02-27 18:03:54 +053060 erpnext.messages.show();
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +053061 setTimeout(erpnext.messages.refresh, 7000);
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +053062 $('#message-post-text').focus();
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +053063})
Rushabh Mehtafdea9662012-02-27 18:03:54 +053064
65erpnext.messages = {
66 show: function() {
67 var contact = erpnext.messages.get_contact();
68
69 // can't send message to self
70 $(wn.pages.messages).find('.well').toggle(contact==user ? false : true);
71
72 $(wn.pages.messages).find('h1:first').html('Messages: '
Rushabh Mehta204e77d2012-02-29 19:09:20 +053073 + (user==contact ? 'From everyone' : wn.user_info(contact).fullname));
Rushabh Mehtafdea9662012-02-27 18:03:54 +053074
75 erpnext.messages.contact = contact;
76 erpnext.messages.list.opts.args.contact = contact;
77 erpnext.messages.list.run();
78
79 },
80 // check for updates every 5 seconds if page is active
81 refresh: function() {
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +053082 setTimeout(erpnext.messages.refresh, 7000);
83 if(wn.container.page.label != 'Messages') return;
Rushabh Mehtafdea9662012-02-27 18:03:54 +053084 erpnext.messages.show();
85 },
86 get_contact: function() {
87 var route = location.hash;
88 if(route.indexOf('/')!=-1) {
89 var name = decodeURIComponent(route.split('/')[1]);
90 if(name.indexOf('__at__')!=-1) {
91 name = name.replace('__at__', '@');
92 }
93 return name;
94 }
95 return user;
96 },
97 make_list: function() {
Rushabh Mehtaf81a64e2012-03-07 18:19:41 +053098 erpnext.messages.list = new wn.ui.Listing({
Rushabh Mehtafdea9662012-02-27 18:03:54 +053099 parent: $('#message-list').get(0),
100 method: 'utilities.page.messages.messages.get_list',
101 args: {
102 contact: null
103 },
104 render_row: function(wrapper, data) {
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +0530105 $(wrapper).removeClass('list-row');
106
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530107 data.creation = dateutil.comment_when(data.creation);
Rushabh Mehta204e77d2012-02-29 19:09:20 +0530108 data.comment_by_fullname = wn.user_info(data.owner).fullname;
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530109
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530110 data.reply_html = '';
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530111 if(data.owner==user) {
112 data.cls = 'message-self';
113 data.comment_by_fullname = 'You';
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530114 data.delete_html = repl('<a class="close" \
115 onclick="erpnext.messages.delete(this)"\
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530116 data-name="%(name)s">&times;</a>', data);
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530117 } else {
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530118 data.cls = 'message-other';
119 data.delete_html = '';
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530120 if(erpnext.messages.contact==user) {
121 data.reply_html = repl('<a href="#!messages/%(owner)s">\
122 <i class="icon-share-alt"></i> Reply</a>', data)
123 }
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530124 }
125
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530126 wrapper.innerHTML = repl('<div class="message %(cls)s">%(delete_html)s\
127 <b>%(comment)s</b>\
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530128 <div class="help">by %(comment_by_fullname)s, %(creation)s</div>\
129 %(reply_html)s</div>\
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530130 <div style="clear: both;"></div>', data);
131 }
132 });
133 },
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530134 delete: function(ele) {
135 $(ele).parent().css('opacity', 0.6);
136 wn.call({
137 method:'utilities.page.messages.messages.delete',
138 args: {name : $(ele).attr('data-name')},
139 callback: function() {
140 $(ele).parent().toggle(false);
141 }
142 });
143 },
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530144 show_active_users: function() {
145 wn.call({
146 module:'utilities',
147 page:'messages',
148 method:'get_active_users',
149 callback: function(r,rt) {
150 var $body = $(wn.pages.messages).find('.section-body');
151 for(var i in r.message) {
152 var p = r.message[i];
Rushabh Mehta204e77d2012-02-29 19:09:20 +0530153 p.fullname = wn.user_info(p.name).fullname;
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530154 p.name = p.name.replace('@', '__at__');
155 $body.append(repl('<div class="section-item">\
156 <a href="#!messages/%(name)s">%(fullname)s</a></div>', p))
157 }
158 }
159 });
160 }
161}
162
163