Rushabh Mehta | aaf86ba | 2012-02-28 17:40:13 +0530 | [diff] [blame] | 1 | // 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 Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 17 | wn.provide('erpnext.messages'); |
| 18 | |
| 19 | wn.pages.messages.onload = function(wrapper) { |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 20 | wn.ui.make_app_page({ |
| 21 | parent: wrapper, |
| 22 | title: "Messages" |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 23 | }); |
| 24 | |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 25 | $('<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 Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 30 | <textarea style="width: 100%; height: 24px;"></textarea>\ |
Rushabh Mehta | ee472b0 | 2012-12-18 11:47:13 +0530 | [diff] [blame] | 31 | <div><button class="btn">Post</button></div><hr>\ |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 32 | </div>\ |
| 33 | <div class="all-messages"></div>').appendTo($(wrapper).find('.layout-main-section')); |
Rushabh Mehta | d3c9fb6 | 2012-12-21 18:27:40 +0530 | [diff] [blame] | 34 | |
| 35 | wrapper.appframe.add_home_breadcrumb(); |
| 36 | wrapper.appframe.add_breadcrumb(wn.modules["Messages"].icon); |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 37 | |
| 38 | erpnext.messages = new erpnext.Messages(wrapper); |
Rushabh Mehta | ec40686 | 2012-11-13 10:59:11 +0530 | [diff] [blame] | 39 | erpnext.toolbar.set_new_comments(0); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 40 | } |
| 41 | |
Rushabh Mehta | fa0e7b6 | 2012-03-22 13:44:04 +0530 | [diff] [blame] | 42 | $(wn.pages.messages).bind('show', function() { |
Rushabh Mehta | 3009c46 | 2012-10-03 11:56:38 +0530 | [diff] [blame] | 43 | // remove alerts |
| 44 | $('#alert-container .alert').remove(); |
| 45 | |
Rushabh Mehta | ec40686 | 2012-11-13 10:59:11 +0530 | [diff] [blame] | 46 | erpnext.toolbar.set_new_comments(0); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 47 | erpnext.messages.show(); |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 48 | setTimeout("erpnext.messages.refresh()", 17000); |
Rushabh Mehta | fa0e7b6 | 2012-03-22 13:44:04 +0530 | [diff] [blame] | 49 | }) |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 50 | |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 51 | erpnext.Messages = Class.extend({ |
| 52 | init: function(wrapper) { |
| 53 | this.wrapper = wrapper; |
| 54 | this.show_active_users(); |
| 55 | this.make_post_message(); |
| 56 | this.make_list(); |
| 57 | //this.update_messages('reset'); //Resets notification icons |
| 58 | }, |
| 59 | make_post_message: function() { |
| 60 | var me = this; |
| 61 | $('#post-message textarea').keydown(function(e) { |
| 62 | if(e.which==13) { |
| 63 | $('#post-message .btn').click(); |
| 64 | return false; |
| 65 | } |
| 66 | }); |
| 67 | |
| 68 | $('#post-message .btn').click(function() { |
| 69 | var txt = $('#post-message textarea').val(); |
| 70 | if(txt) { |
| 71 | wn.call({ |
| 72 | module:'utilities', |
| 73 | page:'messages', |
| 74 | method:'post', |
| 75 | args: { |
| 76 | txt: txt, |
| 77 | contact: me.contact |
| 78 | }, |
| 79 | callback:function(r,rt) { |
| 80 | $('#post-message textarea').val('') |
| 81 | me.list.run(); |
| 82 | }, |
| 83 | btn: this |
| 84 | }); |
| 85 | } |
| 86 | }); |
| 87 | }, |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 88 | show: function() { |
Rushabh Mehta | 3009c46 | 2012-10-03 11:56:38 +0530 | [diff] [blame] | 89 | var contact = this.get_contact() || this.contact || user; |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 90 | |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 91 | $('#message-title').html(contact==user ? "Everyone" : |
| 92 | wn.user_info(contact).fullname) |
| 93 | |
Rushabh Mehta | 24a6961 | 2012-12-11 15:58:19 +0530 | [diff] [blame] | 94 | $('#avatar-image').attr("src", wn.utils.get_file_link(wn.user_info(contact).image)); |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 95 | |
| 96 | $("#show-everyone").toggle(contact!=user); |
| 97 | |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 98 | $("#post-message button").text(contact==user ? "Post Publicly" : "Post to user") |
| 99 | |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 100 | this.contact = contact; |
| 101 | this.list.opts.args.contact = contact; |
| 102 | this.list.run(); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 103 | |
| 104 | }, |
| 105 | // check for updates every 5 seconds if page is active |
| 106 | refresh: function() { |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 107 | setTimeout("erpnext.messages.refresh()", 17000); |
Rushabh Mehta | fa0e7b6 | 2012-03-22 13:44:04 +0530 | [diff] [blame] | 108 | if(wn.container.page.label != 'Messages') return; |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 109 | this.show(); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 110 | }, |
| 111 | get_contact: function() { |
| 112 | var route = location.hash; |
| 113 | if(route.indexOf('/')!=-1) { |
| 114 | var name = decodeURIComponent(route.split('/')[1]); |
| 115 | if(name.indexOf('__at__')!=-1) { |
| 116 | name = name.replace('__at__', '@'); |
| 117 | } |
| 118 | return name; |
| 119 | } |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 120 | }, |
| 121 | make_list: function() { |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 122 | this.list = new wn.ui.Listing({ |
| 123 | parent: $(this.wrapper).find('.all-messages'), |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 124 | method: 'utilities.page.messages.messages.get_list', |
| 125 | args: { |
| 126 | contact: null |
| 127 | }, |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 128 | hide_refresh: true, |
| 129 | no_loading: true, |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 130 | render_row: function(wrapper, data) { |
Rushabh Mehta | fa0e7b6 | 2012-03-22 13:44:04 +0530 | [diff] [blame] | 131 | $(wrapper).removeClass('list-row'); |
| 132 | |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 133 | data.creation = dateutil.comment_when(data.creation); |
Rushabh Mehta | 204e77d | 2012-02-29 19:09:20 +0530 | [diff] [blame] | 134 | data.comment_by_fullname = wn.user_info(data.owner).fullname; |
Rushabh Mehta | 24a6961 | 2012-12-11 15:58:19 +0530 | [diff] [blame] | 135 | data.image = wn.utils.get_file_link(wn.user_info(data.owner).image); |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 136 | data.mark_html = ""; |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 137 | |
Rushabh Mehta | 1a5c9c5 | 2012-02-27 18:56:31 +0530 | [diff] [blame] | 138 | data.reply_html = ''; |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 139 | if(data.owner==user) { |
| 140 | data.cls = 'message-self'; |
| 141 | data.comment_by_fullname = 'You'; |
| 142 | } else { |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 143 | data.cls = 'message-other'; |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 144 | } |
| 145 | |
Rushabh Mehta | 294778a | 2012-09-27 15:43:06 +0200 | [diff] [blame] | 146 | // delete |
| 147 | data.delete_html = ""; |
| 148 | if(data.owner==user || data.comment.indexOf("assigned to")!=-1) { |
| 149 | data.delete_html = repl('<a class="close" \ |
| 150 | onclick="erpnext.messages.delete(this)"\ |
| 151 | data-name="%(name)s">×</a>', data); |
| 152 | } |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 153 | |
Rushabh Mehta | 39b5b06 | 2012-12-11 16:28:33 +0530 | [diff] [blame] | 154 | if(data.owner==data.comment_docname && data.parenttype!="Assignment") { |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 155 | data.mark_html = "<div class='message-mark' title='Public'\ |
| 156 | style='background-color: green'></div>" |
| 157 | } |
Rushabh Mehta | 294778a | 2012-09-27 15:43:06 +0200 | [diff] [blame] | 158 | |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 159 | wrapper.innerHTML = repl('<div class="message %(cls)s">%(mark_html)s\ |
| 160 | <span class="avatar avatar-small"><img src="%(image)s"></span><b>%(comment)s</b>\ |
| 161 | %(delete_html)s\ |
Rushabh Mehta | 1a5c9c5 | 2012-02-27 18:56:31 +0530 | [diff] [blame] | 162 | <div class="help">by %(comment_by_fullname)s, %(creation)s</div>\ |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 163 | </div>\ |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 164 | <div style="clear: both;"></div>', data); |
| 165 | } |
| 166 | }); |
| 167 | }, |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 168 | delete: function(ele) { |
| 169 | $(ele).parent().css('opacity', 0.6); |
| 170 | wn.call({ |
| 171 | method:'utilities.page.messages.messages.delete', |
| 172 | args: {name : $(ele).attr('data-name')}, |
| 173 | callback: function() { |
| 174 | $(ele).parent().toggle(false); |
| 175 | } |
| 176 | }); |
| 177 | }, |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 178 | show_active_users: function() { |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 179 | var me = this; |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 180 | wn.call({ |
| 181 | module:'utilities', |
| 182 | page:'messages', |
| 183 | method:'get_active_users', |
| 184 | callback: function(r,rt) { |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 185 | var $body = $(me.wrapper).find('.layout-side-section'); |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 186 | $('<h4>Users</h4><hr>\ |
| 187 | <div id="show-everyone">\ |
Rushabh Mehta | ee472b0 | 2012-12-18 11:47:13 +0530 | [diff] [blame] | 188 | <a href="#messages/'+user+'" class="btn">\ |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 189 | Show messages from everyone</a><hr></div>\ |
| 190 | ').appendTo($body); |
| 191 | r.message.sort(function(a, b) { return b.has_session - a.has_session; }); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 192 | for(var i in r.message) { |
| 193 | var p = r.message[i]; |
Rushabh Mehta | 7f9c369 | 2012-09-27 18:48:43 +0530 | [diff] [blame] | 194 | if(p.name != user) { |
| 195 | p.fullname = wn.user_info(p.name).fullname; |
Rushabh Mehta | 24a6961 | 2012-12-11 15:58:19 +0530 | [diff] [blame] | 196 | p.image = wn.utils.get_file_link(wn.user_info(p.name).image); |
Rushabh Mehta | 7f9c369 | 2012-09-27 18:48:43 +0530 | [diff] [blame] | 197 | p.name = p.name.replace('@', '__at__'); |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 198 | p.status_color = p.has_session ? "green" : "#ddd"; |
Rushabh Mehta | 7f9c369 | 2012-09-27 18:48:43 +0530 | [diff] [blame] | 199 | p.status = p.has_session ? "Online" : "Offline"; |
Rushabh Mehta | b62bbc6 | 2012-10-03 18:32:10 +0530 | [diff] [blame] | 200 | $(repl('<p>\ |
| 201 | <span class="avatar avatar-small" \ |
| 202 | style="border: 3px solid %(status_color)s" \ |
| 203 | title="%(status)s"><img src="%(image)s"></span>\ |
Rushabh Mehta | 7f9c369 | 2012-09-27 18:48:43 +0530 | [diff] [blame] | 204 | <a href="#!messages/%(name)s">%(fullname)s</a>\ |
| 205 | </p>', p)) |
| 206 | .appendTo($body); |
| 207 | } |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 208 | } |
| 209 | } |
| 210 | }); |
| 211 | } |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 212 | }); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 213 | |
| 214 | |