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) { |
| 20 | erpnext.messages.show_active_users(); |
| 21 | erpnext.messages.make_list(); |
Anand Doshi | 08c8edb | 2012-03-01 14:53:05 +0530 | [diff] [blame] | 22 | update_messages('reset'); //Resets notification icons |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 23 | |
| 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 |
| 46 | $('#message-post-text').keyup(function() { |
| 47 | if($(this).val()) { |
| 48 | $('#message-post').attr('disabled', false); |
| 49 | } else { |
| 50 | $('#message-post').attr('disabled', true); |
| 51 | } |
| 52 | }) |
| 53 | } |
| 54 | |
| 55 | wn.pages.messages.onshow = function(wrapper) { |
| 56 | erpnext.messages.show(); |
| 57 | setTimeout(erpnext.messages.refresh, 5000); |
Rushabh Mehta | 1a5c9c5 | 2012-02-27 18:56:31 +0530 | [diff] [blame] | 58 | $('#message-post-text').focus(); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | erpnext.messages = { |
| 62 | show: function() { |
| 63 | var contact = erpnext.messages.get_contact(); |
| 64 | |
| 65 | // can't send message to self |
| 66 | $(wn.pages.messages).find('.well').toggle(contact==user ? false : true); |
| 67 | |
| 68 | $(wn.pages.messages).find('h1:first').html('Messages: ' |
Rushabh Mehta | 204e77d | 2012-02-29 19:09:20 +0530 | [diff] [blame] | 69 | + (user==contact ? 'From everyone' : wn.user_info(contact).fullname)); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 70 | |
| 71 | erpnext.messages.contact = contact; |
| 72 | erpnext.messages.list.opts.args.contact = contact; |
| 73 | erpnext.messages.list.run(); |
| 74 | |
| 75 | }, |
| 76 | // check for updates every 5 seconds if page is active |
| 77 | refresh: function() { |
| 78 | setTimeout(erpnext.messages.refresh, 10000); |
| 79 | if(page_body.cur_page_label != 'messages') return; |
| 80 | erpnext.messages.show(); |
| 81 | }, |
| 82 | get_contact: function() { |
| 83 | var route = location.hash; |
| 84 | if(route.indexOf('/')!=-1) { |
| 85 | var name = decodeURIComponent(route.split('/')[1]); |
| 86 | if(name.indexOf('__at__')!=-1) { |
| 87 | name = name.replace('__at__', '@'); |
| 88 | } |
| 89 | return name; |
| 90 | } |
| 91 | return user; |
| 92 | }, |
| 93 | make_list: function() { |
| 94 | erpnext.messages.list = new wn.widgets.Listing({ |
| 95 | parent: $('#message-list').get(0), |
| 96 | method: 'utilities.page.messages.messages.get_list', |
| 97 | args: { |
| 98 | contact: null |
| 99 | }, |
| 100 | render_row: function(wrapper, data) { |
| 101 | data.creation = dateutil.comment_when(data.creation); |
Rushabh Mehta | 204e77d | 2012-02-29 19:09:20 +0530 | [diff] [blame] | 102 | data.comment_by_fullname = wn.user_info(data.owner).fullname; |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 103 | |
Rushabh Mehta | 1a5c9c5 | 2012-02-27 18:56:31 +0530 | [diff] [blame] | 104 | data.reply_html = ''; |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 105 | if(data.owner==user) { |
| 106 | data.cls = 'message-self'; |
| 107 | data.comment_by_fullname = 'You'; |
Rushabh Mehta | 1a5c9c5 | 2012-02-27 18:56:31 +0530 | [diff] [blame] | 108 | data.delete_html = repl('<a class="close" \ |
| 109 | onclick="erpnext.messages.delete(this)"\ |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 110 | data-name="%(name)s">×</a>', data); |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 111 | } else { |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 112 | data.cls = 'message-other'; |
| 113 | data.delete_html = ''; |
Rushabh Mehta | 1a5c9c5 | 2012-02-27 18:56:31 +0530 | [diff] [blame] | 114 | if(erpnext.messages.contact==user) { |
| 115 | data.reply_html = repl('<a href="#!messages/%(owner)s">\ |
| 116 | <i class="icon-share-alt"></i> Reply</a>', data) |
| 117 | } |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 118 | } |
| 119 | |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 120 | wrapper.innerHTML = repl('<div class="message %(cls)s">%(delete_html)s\ |
| 121 | <b>%(comment)s</b>\ |
Rushabh Mehta | 1a5c9c5 | 2012-02-27 18:56:31 +0530 | [diff] [blame] | 122 | <div class="help">by %(comment_by_fullname)s, %(creation)s</div>\ |
| 123 | %(reply_html)s</div>\ |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 124 | <div style="clear: both;"></div>', data); |
| 125 | } |
| 126 | }); |
| 127 | }, |
Rushabh Mehta | ef29e55 | 2012-02-27 18:41:11 +0530 | [diff] [blame] | 128 | delete: function(ele) { |
| 129 | $(ele).parent().css('opacity', 0.6); |
| 130 | wn.call({ |
| 131 | method:'utilities.page.messages.messages.delete', |
| 132 | args: {name : $(ele).attr('data-name')}, |
| 133 | callback: function() { |
| 134 | $(ele).parent().toggle(false); |
| 135 | } |
| 136 | }); |
| 137 | }, |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 138 | show_active_users: function() { |
| 139 | wn.call({ |
| 140 | module:'utilities', |
| 141 | page:'messages', |
| 142 | method:'get_active_users', |
| 143 | callback: function(r,rt) { |
| 144 | var $body = $(wn.pages.messages).find('.section-body'); |
| 145 | for(var i in r.message) { |
| 146 | var p = r.message[i]; |
Rushabh Mehta | 204e77d | 2012-02-29 19:09:20 +0530 | [diff] [blame] | 147 | p.fullname = wn.user_info(p.name).fullname; |
Rushabh Mehta | fdea966 | 2012-02-27 18:03:54 +0530 | [diff] [blame] | 148 | p.name = p.name.replace('@', '__at__'); |
| 149 | $body.append(repl('<div class="section-item">\ |
| 150 | <a href="#!messages/%(name)s">%(fullname)s</a></div>', p)) |
| 151 | } |
| 152 | } |
| 153 | }); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | |