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 | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 25 | $('<h3 id="message-title">Everyone</h3>\ |
| 26 | <div id="show-everyone" style="display: none;">\ |
Rushabh Mehta | 3009c46 | 2012-10-03 11:56:38 +0530 | [diff] [blame] | 27 | <a href="#messages/'+user+'" style="font-size: 80%;">\ |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 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 Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 36 | } |
| 37 | |
Rushabh Mehta | fa0e7b6 | 2012-03-22 13:44:04 +0530 | [diff] [blame] | 38 | $(wn.pages.messages).bind('show', function() { |
Rushabh Mehta | 3009c46 | 2012-10-03 11:56:38 +0530 | [diff] [blame] | 39 | // remove alerts |
| 40 | $('#alert-container .alert').remove(); |
| 41 | |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 42 | erpnext.messages.show(); |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 43 | setTimeout("erpnext.messages.refresh()", 7000); |
Rushabh Mehta | fa0e7b6 | 2012-03-22 13:44:04 +0530 | [diff] [blame] | 44 | }) |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 45 | |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 46 | erpnext.Messages = Class.extend({ |
| 47 | init: function(wrapper) { |
| 48 | this.wrapper = wrapper; |
| 49 | this.show_active_users(); |
| 50 | this.make_post_message(); |
| 51 | this.make_list(); |
| 52 | //this.update_messages('reset'); //Resets notification icons |
| 53 | }, |
| 54 | make_post_message: function() { |
| 55 | var me = this; |
| 56 | $('#post-message textarea').keydown(function(e) { |
| 57 | if(e.which==13) { |
| 58 | $('#post-message .btn').click(); |
| 59 | return false; |
| 60 | } |
| 61 | }); |
| 62 | |
| 63 | $('#post-message .btn').click(function() { |
| 64 | var txt = $('#post-message textarea').val(); |
| 65 | if(txt) { |
| 66 | wn.call({ |
| 67 | module:'utilities', |
| 68 | page:'messages', |
| 69 | method:'post', |
| 70 | args: { |
| 71 | txt: txt, |
| 72 | contact: me.contact |
| 73 | }, |
| 74 | callback:function(r,rt) { |
| 75 | $('#post-message textarea').val('') |
| 76 | me.list.run(); |
| 77 | }, |
| 78 | btn: this |
| 79 | }); |
| 80 | } |
| 81 | }); |
| 82 | }, |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 83 | show: function() { |
Rushabh Mehta | 3009c46 | 2012-10-03 11:56:38 +0530 | [diff] [blame] | 84 | var contact = this.get_contact() || this.contact || user; |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 85 | |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 86 | $('#message-title').text(contact==user ? "Everyone" : |
| 87 | wn.boot.user_info[contact].fullname) |
| 88 | |
| 89 | $("#show-everyone").toggle(contact!=user); |
| 90 | |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 91 | // can't send message to self |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 92 | $('#post-message').toggle(contact!=user); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 93 | |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 94 | this.contact = contact; |
| 95 | this.list.opts.args.contact = contact; |
| 96 | this.list.run(); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 97 | |
| 98 | }, |
| 99 | // check for updates every 5 seconds if page is active |
| 100 | refresh: function() { |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 101 | setTimeout("erpnext.messages.refresh()", 7000); |
Rushabh Mehta | fa0e7b6 | 2012-03-22 13:44:04 +0530 | [diff] [blame] | 102 | if(wn.container.page.label != 'Messages') return; |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 103 | this.show(); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 104 | }, |
| 105 | get_contact: function() { |
| 106 | var route = location.hash; |
| 107 | if(route.indexOf('/')!=-1) { |
| 108 | var name = decodeURIComponent(route.split('/')[1]); |
| 109 | if(name.indexOf('__at__')!=-1) { |
| 110 | name = name.replace('__at__', '@'); |
| 111 | } |
| 112 | return name; |
| 113 | } |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 114 | }, |
| 115 | make_list: function() { |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 116 | this.list = new wn.ui.Listing({ |
| 117 | parent: $(this.wrapper).find('.all-messages'), |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 118 | method: 'utilities.page.messages.messages.get_list', |
| 119 | args: { |
| 120 | contact: null |
| 121 | }, |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 122 | hide_refresh: true, |
| 123 | no_loading: true, |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 124 | render_row: function(wrapper, data) { |
Rushabh Mehta | fa0e7b6 | 2012-03-22 13:44:04 +0530 | [diff] [blame] | 125 | $(wrapper).removeClass('list-row'); |
| 126 | |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 127 | data.creation = dateutil.comment_when(data.creation); |
Rushabh Mehta | 204e77d | 2012-02-29 19:09:20 +0530 | [diff] [blame] | 128 | data.comment_by_fullname = wn.user_info(data.owner).fullname; |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 129 | |
Rushabh Mehta | 1a5c9c5 | 2012-02-27 18:56:31 +0530 | [diff] [blame] | 130 | data.reply_html = ''; |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 131 | if(data.owner==user) { |
| 132 | data.cls = 'message-self'; |
| 133 | data.comment_by_fullname = 'You'; |
| 134 | } else { |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 135 | data.cls = 'message-other'; |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 136 | if(this.contact==user) { |
Rushabh Mehta | 1a5c9c5 | 2012-02-27 18:56:31 +0530 | [diff] [blame] | 137 | data.reply_html = repl('<a href="#!messages/%(owner)s">\ |
| 138 | <i class="icon-share-alt"></i> Reply</a>', data) |
| 139 | } |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 140 | } |
| 141 | |
Rushabh Mehta | 294778a | 2012-09-27 15:43:06 +0200 | [diff] [blame] | 142 | // delete |
| 143 | data.delete_html = ""; |
| 144 | if(data.owner==user || data.comment.indexOf("assigned to")!=-1) { |
| 145 | data.delete_html = repl('<a class="close" \ |
| 146 | onclick="erpnext.messages.delete(this)"\ |
| 147 | data-name="%(name)s">×</a>', data); |
| 148 | } |
| 149 | |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 150 | wrapper.innerHTML = repl('<div class="message %(cls)s">%(delete_html)s\ |
| 151 | <b>%(comment)s</b>\ |
Rushabh Mehta | 1a5c9c5 | 2012-02-27 18:56:31 +0530 | [diff] [blame] | 152 | <div class="help">by %(comment_by_fullname)s, %(creation)s</div>\ |
| 153 | %(reply_html)s</div>\ |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 154 | <div style="clear: both;"></div>', data); |
| 155 | } |
| 156 | }); |
| 157 | }, |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 158 | delete: function(ele) { |
| 159 | $(ele).parent().css('opacity', 0.6); |
| 160 | wn.call({ |
| 161 | method:'utilities.page.messages.messages.delete', |
| 162 | args: {name : $(ele).attr('data-name')}, |
| 163 | callback: function() { |
| 164 | $(ele).parent().toggle(false); |
| 165 | } |
| 166 | }); |
| 167 | }, |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 168 | show_active_users: function() { |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 169 | var me = this; |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 170 | wn.call({ |
| 171 | module:'utilities', |
| 172 | page:'messages', |
| 173 | method:'get_active_users', |
| 174 | callback: function(r,rt) { |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 175 | var $body = $(me.wrapper).find('.layout-side-section'); |
| 176 | $("<h4>Users</h4><hr>").appendTo($body); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 177 | for(var i in r.message) { |
| 178 | var p = r.message[i]; |
Rushabh Mehta | 7f9c369 | 2012-09-27 18:48:43 +0530 | [diff] [blame] | 179 | if(p.name != user) { |
| 180 | p.fullname = wn.user_info(p.name).fullname; |
| 181 | p.name = p.name.replace('@', '__at__'); |
| 182 | p.label_status = p.has_session ? "label-success" : ""; |
| 183 | p.status = p.has_session ? "Online" : "Offline"; |
| 184 | $(repl('<p><span class="label %(label_status)s">%(status)s</span>\ |
| 185 | <a href="#!messages/%(name)s">%(fullname)s</a>\ |
| 186 | </p>', p)) |
| 187 | .appendTo($body); |
| 188 | } |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 189 | } |
| 190 | } |
| 191 | }); |
| 192 | } |
Rushabh Mehta | 6e04ef7 | 2012-09-27 18:41:46 +0530 | [diff] [blame] | 193 | }); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 194 | |
| 195 | |