blob: 0409563d4d1c26f01c2bf3234bc98151c58cdb27 [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 Mehta6e04ef72012-09-27 18:41:46 +053025 $('<h3 id="message-title">Everyone</h3>\
26 <div id="show-everyone" style="display: none;">\
27 <a href="#messages" style="font-size: 80%;">\
28 Show messages from everyone</a></div><hr>\
29 <div id="post-message" style="display: none">\
30 <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 Mehtafdea9662012-02-27 18:03:54 +053036}
37
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +053038$(wn.pages.messages).bind('show', function() {
Rushabh Mehtafdea9662012-02-27 18:03:54 +053039 erpnext.messages.show();
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053040 setTimeout("erpnext.messages.refresh()", 7000);
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +053041})
Rushabh Mehtafdea9662012-02-27 18:03:54 +053042
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053043erpnext.Messages = Class.extend({
44 init: function(wrapper) {
45 this.wrapper = wrapper;
46 this.show_active_users();
47 this.make_post_message();
48 this.make_list();
49 //this.update_messages('reset'); //Resets notification icons
50 },
51 make_post_message: function() {
52 var me = this;
53 $('#post-message textarea').keydown(function(e) {
54 if(e.which==13) {
55 $('#post-message .btn').click();
56 return false;
57 }
58 });
59
60 $('#post-message .btn').click(function() {
61 var txt = $('#post-message textarea').val();
62 if(txt) {
63 wn.call({
64 module:'utilities',
65 page:'messages',
66 method:'post',
67 args: {
68 txt: txt,
69 contact: me.contact
70 },
71 callback:function(r,rt) {
72 $('#post-message textarea').val('')
73 me.list.run();
74 },
75 btn: this
76 });
77 }
78 });
79 },
Rushabh Mehtafdea9662012-02-27 18:03:54 +053080 show: function() {
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053081 var contact = this.get_contact();
Rushabh Mehtafdea9662012-02-27 18:03:54 +053082
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053083 $('#message-title').text(contact==user ? "Everyone" :
84 wn.boot.user_info[contact].fullname)
85
86 $("#show-everyone").toggle(contact!=user);
87
Rushabh Mehtafdea9662012-02-27 18:03:54 +053088 // can't send message to self
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053089 $('#post-message').toggle(contact!=user);
Rushabh Mehtafdea9662012-02-27 18:03:54 +053090
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053091 this.contact = contact;
92 this.list.opts.args.contact = contact;
93 this.list.run();
Rushabh Mehtafdea9662012-02-27 18:03:54 +053094
95 },
96 // check for updates every 5 seconds if page is active
97 refresh: function() {
Rushabh Mehta6e04ef72012-09-27 18:41:46 +053098 setTimeout("erpnext.messages.refresh()", 7000);
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +053099 if(wn.container.page.label != 'Messages') return;
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530100 this.show();
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530101 },
102 get_contact: function() {
103 var route = location.hash;
104 if(route.indexOf('/')!=-1) {
105 var name = decodeURIComponent(route.split('/')[1]);
106 if(name.indexOf('__at__')!=-1) {
107 name = name.replace('__at__', '@');
108 }
109 return name;
110 }
111 return user;
112 },
113 make_list: function() {
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530114 this.list = new wn.ui.Listing({
115 parent: $(this.wrapper).find('.all-messages'),
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530116 method: 'utilities.page.messages.messages.get_list',
117 args: {
118 contact: null
119 },
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530120 hide_refresh: true,
121 no_loading: true,
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530122 render_row: function(wrapper, data) {
Rushabh Mehtafa0e7b62012-03-22 13:44:04 +0530123 $(wrapper).removeClass('list-row');
124
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530125 data.creation = dateutil.comment_when(data.creation);
Rushabh Mehta204e77d2012-02-29 19:09:20 +0530126 data.comment_by_fullname = wn.user_info(data.owner).fullname;
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530127
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530128 data.reply_html = '';
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530129 if(data.owner==user) {
130 data.cls = 'message-self';
131 data.comment_by_fullname = 'You';
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530132 data.delete_html = repl('<a class="close" \
133 onclick="erpnext.messages.delete(this)"\
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530134 data-name="%(name)s">&times;</a>', data);
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530135 } else {
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530136 data.cls = 'message-other';
137 data.delete_html = '';
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530138 if(this.contact==user) {
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530139 data.reply_html = repl('<a href="#!messages/%(owner)s">\
140 <i class="icon-share-alt"></i> Reply</a>', data)
141 }
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530142 }
143
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530144 wrapper.innerHTML = repl('<div class="message %(cls)s">%(delete_html)s\
145 <b>%(comment)s</b>\
Rushabh Mehta1a5c9c52012-02-27 18:56:31 +0530146 <div class="help">by %(comment_by_fullname)s, %(creation)s</div>\
147 %(reply_html)s</div>\
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530148 <div style="clear: both;"></div>', data);
149 }
150 });
151 },
Rushabh Mehtaef29e552012-02-27 18:41:11 +0530152 delete: function(ele) {
153 $(ele).parent().css('opacity', 0.6);
154 wn.call({
155 method:'utilities.page.messages.messages.delete',
156 args: {name : $(ele).attr('data-name')},
157 callback: function() {
158 $(ele).parent().toggle(false);
159 }
160 });
161 },
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530162 show_active_users: function() {
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530163 var me = this;
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530164 wn.call({
165 module:'utilities',
166 page:'messages',
167 method:'get_active_users',
168 callback: function(r,rt) {
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530169 var $body = $(me.wrapper).find('.layout-side-section');
170 $("<h4>Users</h4><hr>").appendTo($body);
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530171 for(var i in r.message) {
172 var p = r.message[i];
Rushabh Mehta204e77d2012-02-29 19:09:20 +0530173 p.fullname = wn.user_info(p.name).fullname;
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530174 p.name = p.name.replace('@', '__at__');
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530175 p.label_status = p.has_sessions ? "label-success" : "";
176 p.status = p.has_sessions ? "Online" : "Offline";
177 $(repl('<p><span class="label %(label_status)s">%(status)s</span>\
178 <a href="#!messages/%(name)s">%(fullname)s</a>\
179 </p>', p))
180 .appendTo($body);
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530181 }
182 }
183 });
184 }
Rushabh Mehta6e04ef72012-09-27 18:41:46 +0530185});
Rushabh Mehtafdea9662012-02-27 18:03:54 +0530186
187